File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.440: download - view: text, annotated - select for diffs
Wed May 1 15:18:58 2024 UTC (2 weeks, 4 days ago) by raeburn
Branches: MAIN
CVS tags: version_2_12_X, HEAD
- Bug 6990: Ability to download tarball of Authoring Space's files/directories.
  Add option to domain's Authoring Space defaults, and capability to override
  for individual author(s).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.440 2024/05/01 15:18:58 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, placement, 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 LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: use Crypt::CBC;
  181: 
  182: my $registered_cleanup;
  183: my $modified_urls;
  184: 
  185: sub handler {
  186:     my $r=shift;
  187:     if ($r->header_only) {
  188:         &Apache::loncommon::content_type($r,'text/html');
  189:         $r->send_http_header;
  190:         return OK;
  191:     }
  192: 
  193:     my $context = 'domain';
  194:     my $dom = $env{'request.role.domain'};
  195:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  196:     if (&Apache::lonnet::allowed('mau',$dom)) {
  197:         &Apache::loncommon::content_type($r,'text/html');
  198:         $r->send_http_header;
  199:     } else {
  200:         $env{'user.error.msg'}=
  201:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  202:         return HTTP_NOT_ACCEPTABLE;
  203:     }
  204: 
  205:     $registered_cleanup=0;
  206:     @{$modified_urls}=();
  207: 
  208:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                             ['phase','actions']);
  211:     my $phase = 'pickactions';
  212:     if ( exists($env{'form.phase'}) ) {
  213:         $phase = $env{'form.phase'};
  214:     }
  215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  216:     my %domconfig =
  217:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  218:                 'quotas','autoenroll','autoupdate','autocreate',
  219:                 'directorysrch','usercreation','usermodification',
  220:                 'contacts','defaults','scantron','coursecategories',
  221:                 'serverstatuses','requestcourses','helpsettings',
  222:                 'coursedefaults','usersessions','loadbalancing',
  223:                 'requestauthor','selfenrollment','inststatus',
  224:                 'ltitools','toolsec','ssl','trust','lti','ltisec',
  225:                 'privacy','passwords','proctoring','wafproxy',
  226:                 'ipaccess','authordefaults'],$dom);
  227:     my %encconfig =
  228:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  229:     my ($checked_is_home,$is_home);
  230:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  231:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  232:             my $home = &Apache::lonnet::domain($dom,'primary');
  233:             unless (($home eq 'no_host') || ($home eq '')) {
  234:                 my @ids=&Apache::lonnet::current_machine_ids();
  235:                 if (grep(/^\Q$home\E$/,@ids)) {
  236:                     $is_home = 1;
  237:                 }
  238:             }
  239:             $checked_is_home = 1;
  240:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  241:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  243:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  244:                     if (($is_home) && ($phase eq 'process')) {
  245:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'lti'}) eq 'HASH') {
  252:         if (ref($encconfig{'lti'}) eq 'HASH') {
  253:             unless ($checked_is_home) {
  254:                 my $home = &Apache::lonnet::domain($dom,'primary');
  255:                 unless (($home eq 'no_host') || ($home eq '')) {
  256:                     my @ids=&Apache::lonnet::current_machine_ids();
  257:                     if (grep(/^\Q$home\E$/,@ids)) {
  258:                         $is_home = 1;
  259:                     }
  260:                 }
  261:                 $checked_is_home = 1;
  262:             }
  263:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  264:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  265:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  266:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  267:                     if (($is_home) && ($phase eq 'process')) {
  268:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  269:                     }
  270:                 }
  271:             }
  272:         }
  273:     }
  274:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  275:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  276:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  277:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  278:                     unless ($id =~ /^\d+$/) {
  279:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  280:                     }
  281:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  282:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  283:                         foreach my $item ('key','secret') {
  284:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  285:                         }
  286:                     }
  287:                 }
  288:             }
  289:         }
  290:     }
  291:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  292:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  293:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  294:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  295:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  296:                     foreach my $item ('key','secret') {
  297:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  298:                     }
  299:                 }
  300:             }
  301:         }
  302:     }
  303:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  304:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  305:                        'contacts','privacy','usercreation','selfcreation',
  306:                        'usermodification','scantron','requestcourses','requestauthor',
  307:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  308:                        'authordefaults','ltitools','proctoring','selfenrollment',
  309:                        'usersessions','ssl','trust','lti');
  310:     my %existing;
  311:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  312:         %existing = %{$domconfig{'loadbalancing'}};
  313:     }
  314:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  315:         push(@prefs_order,'loadbalancing');
  316:     }
  317:     my %prefs = (
  318:         'rolecolors' =>
  319:                    { text => 'Default color schemes',
  320:                      help => 'Domain_Configuration_Color_Schemes',
  321:                      header => [{col1 => 'Student Settings',
  322:                                  col2 => '',},
  323:                                 {col1 => 'Coordinator Settings',
  324:                                  col2 => '',},
  325:                                 {col1 => 'Author Settings',
  326:                                  col2 => '',},
  327:                                 {col1 => 'Administrator Settings',
  328:                                  col2 => '',}],
  329:                       print => \&print_rolecolors,
  330:                       modify => \&modify_rolecolors,
  331:                     },
  332:         'login' =>
  333:                     { text => 'Log-in page options',
  334:                       help => 'Domain_Configuration_Login_Page',
  335:                       header => [{col1 => 'Log-in Page Items',
  336:                                   col2 => '',},
  337:                                  {col1 => 'Log-in Help',
  338:                                   col2 => 'Value'},
  339:                                  {col1 => 'Custom HTML in document head',
  340:                                   col2 => 'Value'},
  341:                                  {col1 => 'SSO',
  342:                                   col2 => 'Dual login: SSO and non-SSO options'},
  343:                                 ],
  344:                       print => \&print_login,
  345:                       modify => \&modify_login,
  346:                     },
  347:         'defaults' => 
  348:                     { text => 'Default authentication/language/timezone/portal/types',
  349:                       help => 'Domain_Configuration_LangTZAuth',
  350:                       header => [{col1 => 'Setting',
  351:                                   col2 => 'Value'},
  352:                                  {col1 => 'Institutional user types',
  353:                                   col2 => 'Name displayed'},
  354:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  355:                                   col2 => 'Rules in use'}],
  356:                       print => \&print_defaults,
  357:                       modify => \&modify_defaults,
  358:                     },
  359:         'wafproxy' =>
  360:                     { text => 'Web Application Firewall/Reverse Proxy',
  361:                       help => 'Domain_Configuration_WAF_Proxy',
  362:                       header => [{col1 => 'Domain(s)',
  363:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  364:                                  },
  365:                                  {col1 => 'Domain(s)',
  366:                                   col2 => 'WAF Configuration',}],
  367:                       print => \&print_wafproxy,
  368:                       modify => \&modify_wafproxy,
  369:                     },
  370:         'passwords' =>
  371:                     { text => 'Passwords (Internal authentication)',
  372:                       help => 'Domain_Configuration_Passwords',
  373:                       header => [{col1 => 'Resetting Forgotten Password',
  374:                                   col2 => 'Settings'},
  375:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  376:                                   col2 => 'Settings'},
  377:                                  {col1 => 'Rules for LON-CAPA Passwords',
  378:                                   col2 => 'Settings'},
  379:                                  {col1 => 'Course Owner Changing Student Passwords',
  380:                                   col2 => 'Settings'}],
  381:                       print => \&print_passwords,
  382:                       modify => \&modify_passwords,
  383:                     },
  384:         'quotas' => 
  385:                     { text => 'Blogs, personal pages/timezones, portfolio/quotas',
  386:                       help => 'Domain_Configuration_Quotas',
  387:                       header => [{col1 => 'User affiliation',
  388:                                   col2 => 'Available tools',
  389:                                   col3 => 'Portfolio quota (MB)',}],
  390:                       print => \&print_quotas,
  391:                       modify => \&modify_quotas,
  392:                     },
  393:         'autoenroll' =>
  394:                    { text => 'Auto-enrollment settings',
  395:                      help => 'Domain_Configuration_Auto_Enrollment',
  396:                      header => [{col1 => 'Configuration setting',
  397:                                  col2 => 'Value(s)'}],
  398:                      print => \&print_autoenroll,
  399:                      modify => \&modify_autoenroll,
  400:                    },
  401:         'autoupdate' => 
  402:                    { text => 'Auto-update settings',
  403:                      help => 'Domain_Configuration_Auto_Updates',
  404:                      header => [{col1 => 'Setting',
  405:                                  col2 => 'Value',},
  406:                                 {col1 => 'Setting',
  407:                                  col2 => 'Affiliation'},
  408:                                 {col1 => 'User population',
  409:                                  col2 => 'Updatable user data'}],
  410:                      print => \&print_autoupdate,
  411:                      modify => \&modify_autoupdate,
  412:                   },
  413:         'autocreate' => 
  414:                   { text => 'Auto-course creation settings',
  415:                      help => 'Domain_Configuration_Auto_Creation',
  416:                      header => [{col1 => 'Configuration Setting',
  417:                                  col2 => 'Value',}],
  418:                      print => \&print_autocreate,
  419:                      modify => \&modify_autocreate,
  420:                   },
  421:         'directorysrch' => 
  422:                   { text => 'Directory searches',
  423:                     help => 'Domain_Configuration_InstDirectory_Search',
  424:                     header => [{col1 => 'Institutional Directory Setting',
  425:                                 col2 => 'Value',},
  426:                                {col1 => 'LON-CAPA Directory Setting',
  427:                                 col2 => 'Value',}],
  428:                     print => \&print_directorysrch,
  429:                     modify => \&modify_directorysrch,
  430:                   },
  431:         'contacts' =>
  432:                   { text => 'E-mail addresses and helpform',
  433:                     help => 'Domain_Configuration_Contact_Info',
  434:                     header => [{col1 => 'Default e-mail addresses',
  435:                                 col2 => 'Value',},
  436:                                {col1 => 'Recipient(s) for notifications',
  437:                                 col2 => 'Value',},
  438:                                {col1 => 'Nightly status check e-mail',
  439:                                 col2 => 'Settings',},
  440:                                {col1 => 'Ask helpdesk form settings',
  441:                                 col2 => 'Value',},],
  442:                     print => \&print_contacts,
  443:                     modify => \&modify_contacts,
  444:                   },
  445:         'usercreation' => 
  446:                   { text => 'User creation',
  447:                     help => 'Domain_Configuration_User_Creation',
  448:                     header => [{col1 => 'Format rule type',
  449:                                 col2 => 'Format rules in force'},
  450:                                {col1 => 'User account creation',
  451:                                 col2 => 'Usernames which may be created',},
  452:                                {col1 => 'Context',
  453:                                 col2 => 'Assignable authentication types'}],
  454:                     print => \&print_usercreation,
  455:                     modify => \&modify_usercreation,
  456:                   },
  457:         'selfcreation' => 
  458:                   { text => 'Users self-creating accounts',
  459:                     help => 'Domain_Configuration_Self_Creation', 
  460:                     header => [{col1 => 'Self-creation with institutional username',
  461:                                 col2 => 'Enabled?'},
  462:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  463:                                 col2 => 'Information user can enter'},
  464:                                {col1 => 'Self-creation with e-mail verification',
  465:                                 col2 => 'Settings'}],
  466:                     print => \&print_selfcreation,
  467:                     modify => \&modify_selfcreation,
  468:                   },
  469:         'usermodification' =>
  470:                   { text => 'User modification',
  471:                     help => 'Domain_Configuration_User_Modification',
  472:                     header => [{col1 => 'Target user has role',
  473:                                 col2 => 'User information updatable in author context'},
  474:                                {col1 => 'Target user has role',
  475:                                 col2 => 'User information updatable in course context'}],
  476:                     print => \&print_usermodification,
  477:                     modify => \&modify_usermodification,
  478:                   },
  479:         'scantron' =>
  480:                   { text => 'Bubblesheet format',
  481:                     help => 'Domain_Configuration_Scantron_Format',
  482:                     header => [ {col1 => 'Bubblesheet format file',
  483:                                  col2 => ''},
  484:                                 {col1 => 'Bubblesheet data upload formats',
  485:                                  col2 => 'Settings'}],
  486:                     print => \&print_scantron,
  487:                     modify => \&modify_scantron,
  488:                   },
  489:         'requestcourses' => 
  490:                  {text => 'Request creation of courses',
  491:                   help => 'Domain_Configuration_Request_Courses',
  492:                   header => [{col1 => 'User affiliation',
  493:                               col2 => 'Availability/Processing of requests',},
  494:                              {col1 => 'Setting',
  495:                               col2 => 'Value'},
  496:                              {col1 => 'Available textbooks',
  497:                               col2 => ''},
  498:                              {col1 => 'Available templates',
  499:                               col2 => ''},
  500:                              {col1 => 'Validation (not official courses)',
  501:                               col2 => 'Value'},],
  502:                   print => \&print_quotas,
  503:                   modify => \&modify_quotas,
  504:                  },
  505:         'requestauthor' =>
  506:                  {text => 'Request Authoring Space',
  507:                   help => 'Domain_Configuration_Request_Author',
  508:                   header => [{col1 => 'User affiliation',
  509:                               col2 => 'Availability/Processing of requests',},
  510:                              {col1 => 'Setting',
  511:                               col2 => 'Value'}],
  512:                   print => \&print_quotas,
  513:                   modify => \&modify_quotas,
  514:                  },
  515:         'coursecategories' =>
  516:                   { text => 'Cataloging of courses/communities',
  517:                     help => 'Domain_Configuration_Cataloging_Courses',
  518:                     header => [{col1 => 'Catalog type/availability',
  519:                                 col2 => '',},
  520:                                {col1 => 'Category settings for standard catalog',
  521:                                 col2 => '',},
  522:                                {col1 => 'Categories',
  523:                                 col2 => '',
  524:                                }],
  525:                     print => \&print_coursecategories,
  526:                     modify => \&modify_coursecategories,
  527:                   },
  528:         'serverstatuses' =>
  529:                  {text   => 'Access to server status pages',
  530:                   help   => 'Domain_Configuration_Server_Status',
  531:                   header => [{col1 => 'Status Page',
  532:                               col2 => 'Other named users',
  533:                               col3 => 'Specific IPs',
  534:                             }],
  535:                   print => \&print_serverstatuses,
  536:                   modify => \&modify_serverstatuses,
  537:                  },
  538:         'helpsettings' =>
  539:                  {text   => 'Support settings',
  540:                   help   => 'Domain_Configuration_Help_Settings',
  541:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  542:                               col2 => 'Value'},
  543:                              {col1 => 'Helpdesk Roles',
  544:                               col2 => 'Settings'},],
  545:                   print  => \&print_helpsettings,
  546:                   modify => \&modify_helpsettings,
  547:                  },
  548:         'coursedefaults' => 
  549:                  {text => 'Course/Community defaults',
  550:                   help => 'Domain_Configuration_Course_Defaults',
  551:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  552:                               col2 => 'Value',},
  553:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  554:                               col2 => 'Value',},],
  555:                   print => \&print_coursedefaults,
  556:                   modify => \&modify_coursedefaults,
  557:                  },
  558:         'selfenrollment' => 
  559:                  {text   => 'Self-enrollment in Course/Community',
  560:                   help   => 'Domain_Configuration_Selfenrollment',
  561:                   header => [{col1 => 'Configuration Rights',
  562:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  563:                              {col1 => 'Defaults',
  564:                               col2 => 'Value'},
  565:                              {col1 => 'Self-enrollment validation (optional)',
  566:                               col2 => 'Value'},],
  567:                   print => \&print_selfenrollment,
  568:                   modify => \&modify_selfenrollment,
  569:                  },
  570:         'privacy' => 
  571:                  {text   => 'Role assignments and user privacy',
  572:                   help   => 'Domain_Configuration_User_Privacy',
  573:                   header => [{col1 => 'Role assigned in different domain',
  574:                               col2 => 'Approval options'},
  575:                              {col1 => 'Role assigned in different domain to user of type',
  576:                               col2 => 'User information available in that domain'},
  577:                              {col1 => "Role assigned in user's domain",
  578:                               col2 => 'Information viewable by privileged user'},
  579:                              {col1 => "Role assigned in user's domain",
  580:                               col2 => 'Information viewable by unprivileged user'}],
  581:                   print => \&print_privacy,
  582:                   modify => \&modify_privacy,
  583:                  },
  584:         'usersessions' =>
  585:                  {text  => 'User session hosting/offloading',
  586:                   help  => 'Domain_Configuration_User_Sessions',
  587:                   header => [{col1 => 'Domain server',
  588:                               col2 => 'Servers to offload sessions to when busy'},
  589:                              {col1 => 'Hosting of users from other domains',
  590:                               col2 => 'Rules'},
  591:                              {col1 => "Hosting domain's own users elsewhere",
  592:                               col2 => 'Rules'}],
  593:                   print => \&print_usersessions,
  594:                   modify => \&modify_usersessions,
  595:                  },
  596:         'loadbalancing' =>
  597:                  {text  => 'Dedicated Load Balancer(s)',
  598:                   help  => 'Domain_Configuration_Load_Balancing',
  599:                   header => [{col1 => 'Balancers',
  600:                               col2 => 'Default destinations',
  601:                               col3 => 'User affiliation',
  602:                               col4 => 'Overrides'},
  603:                             ],
  604:                   print => \&print_loadbalancing,
  605:                   modify => \&modify_loadbalancing,
  606:                  },
  607:         'ltitools' =>
  608:                  {text => 'External Tools (LTI)',
  609:                   help => 'Domain_Configuration_LTI_Tools',
  610:                   header => [{col1 => 'Encryption of shared secrets',
  611:                               col2 => 'Settings'},
  612:                              {col1 => 'Rules for shared secrets',
  613:                               col2 => 'Settings'},
  614:                              {col1 => 'Providers',
  615:                               col2 => 'Settings',}],
  616:                   print => \&print_ltitools,
  617:                   modify => \&modify_ltitools,
  618:                  },
  619:         'proctoring' =>
  620:                  {text => 'Remote Proctoring Integration',
  621:                   help => 'Domain_Configuration_Proctoring',
  622:                   header => [{col1 => 'Name',
  623:                               col2 => 'Configuration'}],
  624:                   print => \&print_proctoring,
  625:                   modify => \&modify_proctoring,
  626:                  },
  627:         'ssl' =>
  628:                  {text  => 'LON-CAPA Network (SSL)',
  629:                   help  => 'Domain_Configuration_Network_SSL',
  630:                   header => [{col1 => 'Server',
  631:                               col2 => 'Certificate Status'},
  632:                              {col1 => 'Connections to other servers',
  633:                               col2 => 'Rules'},
  634:                              {col1 => 'Connections from other servers',
  635:                               col2 => 'Rules'},
  636:                              {col1 => "Replicating domain's published content",
  637:                               col2 => 'Rules'}],
  638:                   print => \&print_ssl,
  639:                   modify => \&modify_ssl,
  640:                  },
  641:         'trust' =>
  642:                  {text   => 'Trust Settings',
  643:                   help   => 'Domain_Configuration_Trust',
  644:                   header => [{col1 => "Access to this domain's content by others",
  645:                               col2 => 'Rules'},
  646:                              {col1 => "Access to other domain's content by this domain",
  647:                               col2 => 'Rules'},
  648:                              {col1 => "Enrollment in this domain's courses by others",
  649:                               col2 => 'Rules',},
  650:                              {col1 => "Co-author roles in this domain for others",
  651:                               col2 => 'Rules',},
  652:                              {col1 => "Co-author roles for this domain's users elsewhere",
  653:                               col2 => 'Rules',},
  654:                              {col1 => "Domain roles in this domain assignable to others",
  655:                               col2 => 'Rules'},
  656:                              {col1 => "Course catalog for this domain displayed elsewhere",
  657:                               col2 => 'Rules'},
  658:                              {col1 => "Requests for creation of courses in this domain by others",
  659:                               col2 => 'Rules'},
  660:                              {col1 => "Users in other domains can send messages to this domain",
  661:                               col2 => 'Rules'},],
  662:                   print => \&print_trust,
  663:                   modify => \&modify_trust,
  664:                  },
  665:         'lti' =>
  666:                  {text => 'LTI Link Protection and LTI Consumers',
  667:                   help => 'Domain_Configuration_LTI_Provider',
  668:                   header => [{col1 => 'Encryption of shared secrets',
  669:                               col2 => 'Settings'},
  670:                              {col1 => 'Rules for shared secrets',
  671:                               col2 => 'Settings'},
  672:                              {col1 => 'Link Protectors in Courses',
  673:                               col2 => 'Values'},
  674:                              {col1 => 'Link Protectors',
  675:                               col2 => 'Settings'},
  676:                              {col1 => 'Consumers',
  677:                               col2 => 'Settings'},],
  678:                   print => \&print_lti,
  679:                   modify => \&modify_lti,
  680:                  },
  681:         'ipaccess' =>
  682:                        {text => 'IP-based access control',
  683:                         help => 'Domain_Configuration_IP_Access',
  684:                         header => [{col1 => 'Setting',
  685:                                     col2 => 'Value'},],
  686:                         print  => \&print_ipaccess,
  687:                         modify => \&modify_ipaccess,
  688:                        },
  689:         'authordefaults' =>
  690:                             {text => 'Authoring Space defaults',
  691:                              help => 'Domain_Configuration_Author_Defaults',
  692:                              header => [{col1 => 'Defaults which can be overridden by Author',
  693:                                          col2 => 'Settings',},
  694:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  695:                                          col2 => 'Settings',},],
  696:                              print => \&print_authordefaults,
  697:                              modify => \&modify_authordefaults,
  698:                             },
  699:     );
  700:     if (keys(%servers) > 1) {
  701:         $prefs{'login'}  = { text   => 'Log-in page options',
  702:                              help   => 'Domain_Configuration_Login_Page',
  703:                             header => [{col1 => 'Log-in Service',
  704:                                         col2 => 'Server Setting',},
  705:                                        {col1 => 'Log-in Page Items',
  706:                                         col2 => 'Settings'},
  707:                                        {col1 => 'Log-in Help',
  708:                                         col2 => 'Value'},
  709:                                        {col1 => 'Custom HTML in document head',
  710:                                         col2 => 'Value'},
  711:                                        {col1 => 'SSO',
  712:                                         col2 => 'Dual login: SSO and non-SSO options'},
  713:                                       ],
  714:                             print => \&print_login,
  715:                             modify => \&modify_login,
  716:                            };
  717:     }
  718: 
  719:     my @roles = ('student','coordinator','author','admin');
  720:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  721:     &Apache::lonhtmlcommon::add_breadcrumb
  722:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  723:       text=>"Settings to display/modify"});
  724:     my $confname = $dom.'-domainconfig';
  725: 
  726:     if ($phase eq 'process') {
  727:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  728:                                                               \%prefs,\%domconfig,$confname,\@roles);
  729:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  730:             $r->rflush();
  731:             &devalidate_remote_domconfs($dom,$result);
  732:         }
  733:     } elsif ($phase eq 'display') {
  734:         my $js = &recaptcha_js().
  735:                  &toggle_display_js();
  736:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  737:             my ($othertitle,$usertypes,$types) =
  738:                 &Apache::loncommon::sorted_inst_types($dom);
  739:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  740:                                           $domconfig{'loadbalancing'}).
  741:                    &new_spares_js().
  742:                    &common_domprefs_js().
  743:                    &Apache::loncommon::javascript_array_indexof();
  744:         }
  745:         if (grep(/^requestcourses$/,@actions)) {
  746:             my $javascript_validations;
  747:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  748:             $js .= <<END;
  749: <script type="text/javascript">
  750: $javascript_validations
  751: </script>
  752: $coursebrowserjs
  753: END
  754:         } elsif (grep(/^ipaccess$/,@actions)) {
  755:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  756:         }
  757:         if (grep(/^selfcreation$/,@actions)) {
  758:             $js .= &selfcreate_javascript();
  759:         }
  760:         if (grep(/^contacts$/,@actions)) {
  761:             $js .= &contacts_javascript();
  762:         }
  763:         if (grep(/^scantron$/,@actions)) {
  764:             $js .= &scantron_javascript();
  765:         }
  766:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  767:     } else {
  768: # check if domconfig user exists for the domain.
  769:         my $servadm = $r->dir_config('lonAdmEMail');
  770:         my ($configuserok,$author_ok,$switchserver) =
  771:             &config_check($dom,$confname,$servadm);
  772:         unless ($configuserok eq 'ok') {
  773:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  774:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  775:                           $confname).
  776:                       '<br />'
  777:             );
  778:             if ($switchserver) {
  779:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  780:                           '<br />'.
  781:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  782:                           '<br />'.
  783:                           &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).
  784:                           '<br />'.
  785:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  786:                 );
  787:             } else {
  788:                 $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.').
  789:                           '<br />'.
  790:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  791:                 );
  792:             }
  793:             $r->print(&Apache::loncommon::end_page());
  794:             return OK;
  795:         }
  796:         if (keys(%domconfig) == 0) {
  797:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  798:             my @ids=&Apache::lonnet::current_machine_ids();
  799:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  800:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  801:                 my @loginimages = ('img','logo','domlogo','login');
  802:                 my $custom_img_count = 0;
  803:                 foreach my $img (@loginimages) {
  804:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  805:                         $custom_img_count ++;
  806:                     }
  807:                 }
  808:                 foreach my $role (@roles) {
  809:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  810:                         $custom_img_count ++;
  811:                     }
  812:                 }
  813:                 if ($custom_img_count > 0) {
  814:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  815:                     my $switch_server = &check_switchserver($dom,$confname);
  816:                     $r->print(
  817:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  818:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  819:     &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 />'.
  820:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  821:                     if ($switch_server) {
  822:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  823:                     }
  824:                     $r->print(&Apache::loncommon::end_page());
  825:                     return OK;
  826:                 }
  827:             }
  828:         }
  829:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  830:     }
  831:     return OK;
  832: }
  833: 
  834: sub process_changes {
  835:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  836:     my %domconfig;
  837:     if (ref($values) eq 'HASH') {
  838:         %domconfig = %{$values};
  839:     }
  840:     my $output;
  841:     if ($action eq 'login') {
  842:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  843:     } elsif ($action eq 'rolecolors') {
  844:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  845:                                      $lastactref,%domconfig);
  846:     } elsif ($action eq 'quotas') {
  847:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  848:     } elsif ($action eq 'autoenroll') {
  849:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  850:     } elsif ($action eq 'autoupdate') {
  851:         $output = &modify_autoupdate($dom,%domconfig);
  852:     } elsif ($action eq 'autocreate') {
  853:         $output = &modify_autocreate($dom,%domconfig);
  854:     } elsif ($action eq 'directorysrch') {
  855:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  856:     } elsif ($action eq 'usercreation') {
  857:         $output = &modify_usercreation($dom,%domconfig);
  858:     } elsif ($action eq 'selfcreation') {
  859:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  860:     } elsif ($action eq 'usermodification') {
  861:         $output = &modify_usermodification($dom,%domconfig);
  862:     } elsif ($action eq 'contacts') {
  863:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  864:     } elsif ($action eq 'defaults') {
  865:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  866:     } elsif ($action eq 'scantron') {
  867:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  868:     } elsif ($action eq 'coursecategories') {
  869:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  870:     } elsif ($action eq 'serverstatuses') {
  871:         $output = &modify_serverstatuses($dom,%domconfig);
  872:     } elsif ($action eq 'requestcourses') {
  873:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  874:     } elsif ($action eq 'requestauthor') {
  875:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  876:     } elsif ($action eq 'helpsettings') {
  877:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  878:     } elsif ($action eq 'coursedefaults') {
  879:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  880:     } elsif ($action eq 'selfenrollment') {
  881:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  882:     } elsif ($action eq 'usersessions') {
  883:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  884:     } elsif ($action eq 'loadbalancing') {
  885:         $output = &modify_loadbalancing($dom,%domconfig);
  886:     } elsif ($action eq 'ltitools') {
  887:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  888:     } elsif ($action eq 'proctoring') {
  889:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  890:     } elsif ($action eq 'ssl') {
  891:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  892:     } elsif ($action eq 'trust') {
  893:         $output = &modify_trust($dom,$lastactref,%domconfig);
  894:     } elsif ($action eq 'lti') {
  895:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  896:     } elsif ($action eq 'privacy') {
  897:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  898:     } elsif ($action eq 'passwords') {
  899:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  900:     } elsif ($action eq 'wafproxy') {
  901:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  902:     } elsif ($action eq 'ipaccess') {
  903:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  904:     } elsif ($action eq 'authordefaults') {
  905:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  906:     }
  907:     return $output;
  908: }
  909: 
  910: sub print_config_box {
  911:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  912:     my $rowtotal = 0;
  913:     my $output;
  914:     if ($action eq 'coursecategories') {
  915:         $output = &coursecategories_javascript($settings);
  916:     } elsif ($action eq 'defaults') {
  917:         $output = &defaults_javascript($settings); 
  918:     } elsif ($action eq 'passwords') {
  919:         $output = &passwords_javascript($action);
  920:     } elsif ($action eq 'helpsettings') {
  921:         my (%privs,%levelscurrent);
  922:         my %full=();
  923:         my %levels=(
  924:                      course => {},
  925:                      domain => {},
  926:                      system => {},
  927:                    );
  928:         my $context = 'domain';
  929:         my $crstype = 'Course';
  930:         my $formname = 'display';
  931:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  932:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  933:         $output =
  934:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  935:                                                       \@templateroles);
  936:     } elsif ($action eq 'ltitools') {
  937:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  938:     } elsif ($action eq 'lti') {
  939:         $output .= &passwords_javascript('ltisecrets')."\n".
  940:                    &lti_javascript($dom,$settings);
  941:     } elsif ($action eq 'proctoring') {
  942:         $output .= &proctoring_javascript($settings);
  943:     } elsif ($action eq 'wafproxy') {
  944:         $output .= &wafproxy_javascript($dom);
  945:     } elsif ($action eq 'autoupdate') {
  946:         $output .= &autoupdate_javascript();
  947:     } elsif ($action eq 'autoenroll') {
  948:         $output .= &autoenroll_javascript();
  949:     } elsif ($action eq 'login') {
  950:         $output .= &saml_javascript();
  951:     } elsif ($action eq 'ipaccess') {
  952:         $output .= &ipaccess_javascript($settings);
  953:     } elsif ($action eq 'authordefaults') {
  954:         $output .= &authordefaults_javascript();
  955:     }
  956:     $output .=
  957:          '<table class="LC_nested_outer">
  958:           <tr>
  959:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  960:            &mt($item->{text}).'&nbsp;'.
  961:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  962:           '</tr>';
  963:     $rowtotal ++;
  964:     my $numheaders = 1;
  965:     if (ref($item->{'header'}) eq 'ARRAY') {
  966:         $numheaders = scalar(@{$item->{'header'}});
  967:     }
  968:     if ($numheaders > 1) {
  969:         my $colspan = '';
  970:         my $rightcolspan = '';
  971:         my $leftnobr = '';
  972:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  973:             ($action eq 'directorysrch') ||
  974:             (($action eq 'login') && ($numheaders < 5))) {
  975:             $colspan = ' colspan="2"';
  976:         }
  977:         if ($action eq 'usersessions') {
  978:             $rightcolspan = ' colspan="3"'; 
  979:         }
  980:         if ($action eq 'passwords') {
  981:             $leftnobr = ' LC_nobreak';
  982:         }
  983:         $output .= '
  984:           <tr>
  985:            <td>
  986:             <table class="LC_nested">
  987:              <tr class="LC_info_row">
  988:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  989:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  990:              </tr>';
  991:         $rowtotal ++;
  992:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  993:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  994:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  995:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  996:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  997:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
  998:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  999:         } elsif ($action eq 'passwords') {
 1000:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
 1001:         } elsif ($action eq 'coursecategories') {
 1002:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'scantron') {
 1004:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
 1005:         } elsif ($action eq 'login') {
 1006:             if ($numheaders == 5) {
 1007:                 $colspan = ' colspan="2"';
 1008:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
 1009:             } else {
 1010:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
 1011:             }
 1012:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 1013:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1014:         } elsif ($action eq 'rolecolors') {
 1015:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1016:         }
 1017:         $output .= '
 1018:            </table>
 1019:           </td>
 1020:          </tr>
 1021:          <tr>
 1022:            <td>
 1023:             <table class="LC_nested">
 1024:              <tr class="LC_info_row">
 1025:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1026:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1027:              </tr>';
 1028:             $rowtotal ++;
 1029:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1030:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1031:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1032:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1033:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1034:             ($action eq 'ltitools')) {
 1035:             if ($action eq 'coursecategories') {
 1036:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1037:                 $colspan = ' colspan="2"';
 1038:             } elsif ($action eq 'trust') {
 1039:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1040:             } elsif ($action eq 'passwords') {
 1041:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1042:             } elsif ($action eq 'lti') {
 1043:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
 1044:                            </table>
 1045:                           </td>
 1046:                          </tr>
 1047:                          <tr>
 1048:                           <td>
 1049:                           <table class="LC_nested">
 1050:                            <tr class="LC_info_row">
 1051:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1052:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1053:                            </tr>'."\n".
 1054:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1055:             } else {
 1056:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1057:             }
 1058:             if ($action eq 'trust') {
 1059:                 $output .= '
 1060:             </table>
 1061:           </td>
 1062:          </tr>';
 1063:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1064:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1065:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1066:                     $output .= '
 1067:          <tr>
 1068:            <td>
 1069:             <table class="LC_nested">
 1070:              <tr class="LC_info_row">
 1071:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1072:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1073:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1074:             </table>
 1075:           </td>
 1076:          </tr>';
 1077:                 }
 1078:                 $output .= '
 1079:          <tr>
 1080:            <td>
 1081:             <table class="LC_nested">
 1082:              <tr class="LC_info_row">
 1083:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1084:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1085:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1086:             } else {
 1087:                 my $hdridx = 2;
 1088:                 if ($action eq 'lti') {
 1089:                     $hdridx = 3;
 1090:                 }
 1091:                 $output .= '
 1092:            </table>
 1093:           </td>
 1094:          </tr>
 1095:          <tr>
 1096:            <td>
 1097:             <table class="LC_nested">
 1098:              <tr class="LC_info_row">
 1099:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1100:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
 1101:              </tr>'."\n";
 1102:                 if ($action eq 'coursecategories') {
 1103:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1104:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1105:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1106:                     if ($action eq 'passwords') {
 1107:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1108:                     } else {
 1109:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1110:                     }
 1111:                     $hdridx ++;
 1112:                     $output .= '
 1113:              </tr>
 1114:             </table>
 1115:            </td>
 1116:           </tr>
 1117:           <tr>
 1118:            <td>
 1119:             <table class="LC_nested">
 1120:              <tr class="LC_info_row">
 1121:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1122:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
 1123:                     if ($action eq 'passwords') {
 1124:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1125:                     } else {
 1126:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1127:                     }
 1128:                     $output .= '
 1129:             </table>
 1130:           </td>
 1131:          </tr>
 1132:          <tr>';
 1133:                 } else {
 1134:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1135:                 }
 1136:             }
 1137:             $rowtotal ++;
 1138:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1139:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1140:                  ($action eq 'wafproxy') || ($action eq 'authordefaults')) {
 1141:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1142:         } elsif ($action eq 'scantron') {
 1143:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1144:         } elsif ($action eq 'ssl') {
 1145:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1146:             </table>
 1147:           </td>
 1148:          </tr>
 1149:          <tr>
 1150:            <td>
 1151:             <table class="LC_nested">
 1152:              <tr class="LC_info_row">
 1153:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1154:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1155:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1156:             </table>
 1157:           </td>
 1158:          </tr>
 1159:          <tr>
 1160:            <td>
 1161:             <table class="LC_nested">
 1162:              <tr class="LC_info_row">
 1163:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1164:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1165:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1166:         } elsif ($action eq 'login') {
 1167:             if ($numheaders == 5) {
 1168:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1169:            </table>
 1170:           </td>
 1171:          </tr>
 1172:          <tr>
 1173:            <td>
 1174:             <table class="LC_nested">
 1175:              <tr class="LC_info_row">
 1176:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1177:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1178:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1179:                 $rowtotal ++;
 1180:             } else {
 1181:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1182:             }
 1183:             $output .= '
 1184:            </table>
 1185:           </td>
 1186:          </tr>
 1187:          <tr>
 1188:            <td>
 1189:             <table class="LC_nested">
 1190:              <tr class="LC_info_row">';
 1191:             if ($numheaders == 5) {
 1192:                 $output .= '
 1193:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1194:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1195:              </tr>';
 1196:             } else {
 1197:                 $output .= '
 1198:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1199:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1200:              </tr>';
 1201:             }
 1202:             $rowtotal ++;
 1203:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1204:            </table>
 1205:           </td>
 1206:          </tr>
 1207:          <tr>
 1208:            <td>
 1209:             <table class="LC_nested">
 1210:              <tr class="LC_info_row">';
 1211:             if ($numheaders == 5) {
 1212:                 $output .= '
 1213:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1214:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1215:              </tr>';
 1216:             } else {
 1217:                 $output .= '
 1218:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1219:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1220:              </tr>';
 1221:             }
 1222:             $rowtotal ++;
 1223:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1224:         } elsif ($action eq 'requestcourses') {
 1225:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1226:             $rowtotal ++;
 1227:             $output .= &print_studentcode($settings,\$rowtotal).'
 1228:            </table>
 1229:           </td>
 1230:          </tr>
 1231:          <tr>
 1232:            <td>
 1233:             <table class="LC_nested">
 1234:              <tr class="LC_info_row">
 1235:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1236:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1237:                        &textbookcourses_javascript($settings).
 1238:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1239:             </table>
 1240:            </td>
 1241:           </tr>
 1242:          <tr>
 1243:            <td>
 1244:             <table class="LC_nested">
 1245:              <tr class="LC_info_row">
 1246:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1247:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1248:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1249:             </table>
 1250:            </td>
 1251:           </tr>
 1252:           <tr>
 1253:            <td>
 1254:             <table class="LC_nested">
 1255:              <tr class="LC_info_row">
 1256:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1257:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1258:              </tr>'.
 1259:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1260:         } elsif ($action eq 'requestauthor') {
 1261:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1262:             $rowtotal ++;
 1263:         } elsif ($action eq 'rolecolors') {
 1264:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1265:            </table>
 1266:           </td>
 1267:          </tr>
 1268:          <tr>
 1269:            <td>
 1270:             <table class="LC_nested">
 1271:              <tr class="LC_info_row">
 1272:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1273:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1274:               <td class="LC_right_item" style="vertical-align: top">'.
 1275:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1276:              </tr>'.
 1277:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1278:            </table>
 1279:           </td>
 1280:          </tr>
 1281:          <tr>
 1282:            <td>
 1283:             <table class="LC_nested">
 1284:              <tr class="LC_info_row">
 1285:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1286:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1287:              </tr>'.
 1288:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1289:             $rowtotal += 2;
 1290:         }
 1291:     } else {
 1292:         $output .= '
 1293:           <tr>
 1294:            <td>
 1295:             <table class="LC_nested">
 1296:              <tr class="LC_info_row">';
 1297:         if ($action eq 'login') {
 1298:             $output .= '  
 1299:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1300:         } elsif ($action eq 'serverstatuses') {
 1301:             $output .= '
 1302:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1303:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1304: 
 1305:         } else {
 1306:             $output .= '
 1307:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1308:         }
 1309:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1310:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1311:                        &mt($item->{'header'}->[0]->{'col2'});
 1312:             if ($action eq 'serverstatuses') {
 1313:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1314:             } 
 1315:         } else {
 1316:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1317:                        &mt($item->{'header'}->[0]->{'col2'});
 1318:         }
 1319:         $output .= '</td>';
 1320:         if ($item->{'header'}->[0]->{'col3'}) {
 1321:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1322:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1323:                             &mt($item->{'header'}->[0]->{'col3'});
 1324:             } else {
 1325:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1326:                            &mt($item->{'header'}->[0]->{'col3'});
 1327:             }
 1328:             if ($action eq 'serverstatuses') {
 1329:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1330:             }
 1331:             $output .= '</td>';
 1332:         }
 1333:         if ($item->{'header'}->[0]->{'col4'}) {
 1334:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1335:                        &mt($item->{'header'}->[0]->{'col4'});
 1336:         }
 1337:         $output .= '</tr>';
 1338:         $rowtotal ++;
 1339:         if ($action eq 'quotas') {
 1340:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1341:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1342:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1343:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1344:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1345:         }
 1346:     }
 1347:     $output .= '
 1348:    </table>
 1349:   </td>
 1350:  </tr>
 1351: </table><br />';
 1352:     return ($output,$rowtotal);
 1353: }
 1354: 
 1355: sub print_login {
 1356:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1357:     my ($css_class,$datatable,$switchserver,%lt);
 1358:     my %choices = &login_choices();
 1359:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1360:         %lt = &login_file_options();
 1361:         $switchserver = &check_switchserver($dom,$confname);
 1362:     }
 1363:     if ($caller eq 'service') {
 1364:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1365:         my $choice = $choices{'disallowlogin'};
 1366:         $css_class = ' class="LC_odd_row"';
 1367:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1368:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1369:                       '<th>'.$choices{'server'}.'</th>'.
 1370:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1371:                       '<th>'.$choices{'custompath'}.'</th>'.
 1372:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1373:         my %disallowed;
 1374:         if (ref($settings) eq 'HASH') {
 1375:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1376:                %disallowed = %{$settings->{'loginvia'}};
 1377:             }
 1378:         }
 1379:         foreach my $lonhost (sort(keys(%servers))) {
 1380:             my $direct = 'selected="selected"';
 1381:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1382:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1383:                     $direct = '';
 1384:                 }
 1385:             }
 1386:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1387:                           '<td><select name="'.$lonhost.'_server">'.
 1388:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1389:                           '</option>';
 1390:             foreach my $hostid (sort(keys(%servers))) {
 1391:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1392:                 my $selected = '';
 1393:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1394:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1395:                         $selected = 'selected="selected"';
 1396:                     }
 1397:                 }
 1398:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1399:                               $servers{$hostid}.'</option>';
 1400:             }
 1401:             $datatable .= '</select></td>'.
 1402:                           '<td><select name="'.$lonhost.'_serverpath">';
 1403:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1404:                 my $pathname = $path;
 1405:                 if ($path eq 'custom') {
 1406:                     $pathname = &mt('Custom Path').' ->';
 1407:                 }
 1408:                 my $selected = '';
 1409:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1410:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1411:                         $selected = 'selected="selected"';
 1412:                     }
 1413:                 } elsif ($path eq '') {
 1414:                     $selected = 'selected="selected"';
 1415:                 }
 1416:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1417:             }
 1418:             $datatable .= '</select></td>';
 1419:             my ($custom,$exempt);
 1420:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1421:                 $custom = $disallowed{$lonhost}{'custompath'};
 1422:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1423:             }
 1424:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1425:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1426:                           '</tr>';
 1427:         }
 1428:         $datatable .= '</table></td></tr>';
 1429:         return $datatable;
 1430:     } elsif ($caller eq 'page') {
 1431:         my %defaultchecked = ( 
 1432:                                'coursecatalog' => 'on',
 1433:                                'helpdesk'      => 'on',
 1434:                                'adminmail'     => 'off',
 1435:                                'newuser'       => 'off',
 1436:                              );
 1437:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1438:         my (%checkedon,%checkedoff);
 1439:         foreach my $item (@toggles) {
 1440:             if ($defaultchecked{$item} eq 'on') { 
 1441:                 $checkedon{$item} = ' checked="checked" ';
 1442:                 $checkedoff{$item} = ' ';
 1443:             } elsif ($defaultchecked{$item} eq 'off') {
 1444:                 $checkedoff{$item} = ' checked="checked" ';
 1445:                 $checkedon{$item} = ' ';
 1446:             }
 1447:         }
 1448:         my @images = ('img','logo','domlogo','login');
 1449:         my @alttext = ('img','logo','domlogo');
 1450:         my @logintext = ('textcol','bgcol');
 1451:         my @bgs = ('pgbg','mainbg','sidebg');
 1452:         my @links = ('link','alink','vlink');
 1453:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1454:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1455:         my (%is_custom,%designs);
 1456:         my %defaults = (
 1457:                        font => $defaultdesign{'login.font'},
 1458:                        );
 1459:         foreach my $item (@images) {
 1460:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1461:             $defaults{'showlogo'}{$item} = 1;
 1462:         }
 1463:         foreach my $item (@bgs) {
 1464:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1465:         }
 1466:         foreach my $item (@logintext) {
 1467:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1468:         }
 1469:         foreach my $item (@links) {
 1470:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1471:         }
 1472:         if (ref($settings) eq 'HASH') {
 1473:             foreach my $item (@toggles) {
 1474:                 if ($settings->{$item} eq '1') {
 1475:                     $checkedon{$item} =  ' checked="checked" ';
 1476:                     $checkedoff{$item} = ' ';
 1477:                 } elsif ($settings->{$item} eq '0') {
 1478:                     $checkedoff{$item} =  ' checked="checked" ';
 1479:                     $checkedon{$item} = ' ';
 1480:                 }
 1481:             }
 1482:             foreach my $item (@images) {
 1483:                 if (defined($settings->{$item})) {
 1484:                     $designs{$item} = $settings->{$item};
 1485:                     $is_custom{$item} = 1;
 1486:                 }
 1487:                 if (defined($settings->{'showlogo'}{$item})) {
 1488:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1489:                 }
 1490:             }
 1491:             foreach my $item (@alttext) {
 1492:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1493:                     if ($settings->{'alttext'}->{$item} ne '') {
 1494:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1495:                     }
 1496:                 }
 1497:             }
 1498:             foreach my $item (@logintext) {
 1499:                 if ($settings->{$item} ne '') {
 1500:                     $designs{'logintext'}{$item} = $settings->{$item};
 1501:                     $is_custom{$item} = 1;
 1502:                 }
 1503:             }
 1504:             if ($settings->{'font'} ne '') {
 1505:                 $designs{'font'} = $settings->{'font'};
 1506:                 $is_custom{'font'} = 1;
 1507:             }
 1508:             foreach my $item (@bgs) {
 1509:                 if ($settings->{$item} ne '') {
 1510:                     $designs{'bgs'}{$item} = $settings->{$item};
 1511:                     $is_custom{$item} = 1;
 1512:                 }
 1513:             }
 1514:             foreach my $item (@links) {
 1515:                 if ($settings->{$item} ne '') {
 1516:                     $designs{'links'}{$item} = $settings->{$item};
 1517:                     $is_custom{$item} = 1;
 1518:                 }
 1519:             }
 1520:         } else {
 1521:             if ($designhash{$dom.'.login.font'} ne '') {
 1522:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1523:                 $is_custom{'font'} = 1;
 1524:             }
 1525:             foreach my $item (@images) {
 1526:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1527:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1528:                     $is_custom{$item} = 1;
 1529:                 }
 1530:             }
 1531:             foreach my $item (@bgs) {
 1532:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1533:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1534:                     $is_custom{$item} = 1;
 1535:                 }
 1536:             }
 1537:             foreach my $item (@links) {
 1538:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1539:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1540:                     $is_custom{$item} = 1;
 1541:                 }
 1542:             }
 1543:         }
 1544:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1545:                                                       logo => 'Institution Logo',
 1546:                                                       domlogo => 'Domain Logo',
 1547:                                                       login => 'Login box');
 1548:         my $itemcount = 1;
 1549:         foreach my $item (@toggles) {
 1550:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1551:             $datatable .=  
 1552:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1553:                 '</td><td>'.
 1554:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1555:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1556:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1557:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1558:                 '</tr>';
 1559:             $itemcount ++;
 1560:         }
 1561:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1562:         $datatable .= '</tr></table></td></tr>';
 1563:     } elsif ($caller eq 'help') {
 1564:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1565:         my $itemcount = 1;
 1566:         $defaulturl = '/adm/loginproblems.html';
 1567:         $defaulttype = 'default';
 1568:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1569:         my @currlangs;
 1570:         if (ref($settings) eq 'HASH') {
 1571:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1572:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1573:                     next if ($settings->{'helpurl'}{$key} eq '');
 1574:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1575:                     $type{$key} = 'custom';
 1576:                     unless ($key eq 'nolang') {
 1577:                         push(@currlangs,$key);
 1578:                     }
 1579:                 }
 1580:             } elsif ($settings->{'helpurl'} ne '') {
 1581:                 $type{'nolang'} = 'custom';
 1582:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1583:             }
 1584:         }
 1585:         foreach my $lang ('nolang',sort(@currlangs)) {
 1586:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1587:             $datatable .= '<tr'.$css_class.'>';
 1588:             if ($url{$lang} eq '') {
 1589:                 $url{$lang} = $defaulturl;
 1590:             }
 1591:             if ($type{$lang} eq '') {
 1592:                 $type{$lang} = $defaulttype;
 1593:             }
 1594:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1595:             if ($lang eq 'nolang') {
 1596:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1597:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1598:             } else {
 1599:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1600:                                   $langchoices{$lang},
 1601:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1602:             }
 1603:             $datatable .= '</span></td>'."\n".
 1604:                           '<td class="LC_left_item">';
 1605:             if ($type{$lang} eq 'custom') {
 1606:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1607:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1608:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1609:             } else {
 1610:                 $datatable .= $lt{'upl'};
 1611:             }
 1612:             $datatable .='<br />';
 1613:             if ($switchserver) {
 1614:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1615:             } else {
 1616:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1617:             }
 1618:             $datatable .= '</td></tr>';
 1619:             $itemcount ++;
 1620:         }
 1621:         my @addlangs;
 1622:         foreach my $lang (sort(keys(%langchoices))) {
 1623:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1624:             push(@addlangs,$lang);
 1625:         }
 1626:         if (@addlangs > 0) {
 1627:             my %toadd;
 1628:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1629:             $toadd{''} = &mt('Select');
 1630:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1631:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1632:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1633:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1634:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1635:             if ($switchserver) {
 1636:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1637:             } else {
 1638:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1639:             }
 1640:             $datatable .= '</td></tr>';
 1641:             $itemcount ++;
 1642:         }
 1643:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1644:     } elsif ($caller eq 'headtag') {
 1645:         my %domservers = &Apache::lonnet::get_servers($dom);
 1646:         my $choice = $choices{'headtag'};
 1647:         $css_class = ' class="LC_odd_row"';
 1648:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1649:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1650:                       '<th>'.$choices{'current'}.'</th>'.
 1651:                       '<th>'.$choices{'action'}.'</th>'.
 1652:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1653:         my (%currurls,%currexempt);
 1654:         if (ref($settings) eq 'HASH') {
 1655:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1656:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1657:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1658:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1659:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1660:                     }
 1661:                 }
 1662:             }
 1663:         }
 1664:         foreach my $lonhost (sort(keys(%domservers))) {
 1665:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1666:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1667:             if ($currurls{$lonhost}) {
 1668:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1669:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1670:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1671:                               '">'.$lt{'curr'}.'</a></td>'.
 1672:                               '<td><span class="LC_nobreak"><label>'.
 1673:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1674:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1675:             } else {
 1676:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1677:             }
 1678:             $datatable .='<br />';
 1679:             if ($switchserver) {
 1680:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1681:             } else {
 1682:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1683:             }
 1684:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1685:         }
 1686:         $datatable .= '</table></td></tr>';
 1687:     } elsif ($caller eq 'saml') {
 1688:         my %domservers = &Apache::lonnet::get_servers($dom);
 1689:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1690:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1691:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1692:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1693:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1694:         foreach my $lonhost (keys(%domservers)) {
 1695:             $samlurl{$lonhost} = '/adm/sso';
 1696:             $styleon{$lonhost} = 'display:none';
 1697:             $styleoff{$lonhost} = '';
 1698:         }
 1699:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1700:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1701:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1702:                     $saml{$lonhost} = 1;
 1703:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1704:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1705:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1706:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1707:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1708:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1709:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1710:                     $styleon{$lonhost} = '';
 1711:                     $styleoff{$lonhost} = 'display:none';
 1712:                 } else {
 1713:                     $styleon{$lonhost} = 'display:none';
 1714:                     $styleoff{$lonhost} = '';
 1715:                 }
 1716:             }
 1717:         }
 1718:         my $itemcount = 1;
 1719:         foreach my $lonhost (sort(keys(%domservers))) {
 1720:             my $samlon = ' ';
 1721:             my $samloff = ' checked="checked" ';
 1722:             if ($saml{$lonhost}) {
 1723:                 $samlon = $samloff;
 1724:                 $samloff = ' ';
 1725:             }
 1726:             my $samlwinon = '';
 1727:             my $samlwinoff = ' checked="checked"';
 1728:             if ($samlwindow{$lonhost}) {
 1729:                 $samlwinon = $samlwinoff;
 1730:                 $samlwinoff = '';
 1731:             }
 1732:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1733:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1734:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1735:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1736:                           &mt('No').'</label>'.('&nbsp;'x2).
 1737:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1738:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1739:                           &mt('Yes').'</label></span></td>'.
 1740:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1741:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1742:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1743:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1744:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1745:                           $samltext{$lonhost}.'" /></td><td>';
 1746:             if ($samlimg{$lonhost}) {
 1747:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1748:                               '<span class="LC_nobreak"><label>'.
 1749:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1750:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1751:             } else {
 1752:                 $datatable .= $lt{'upl'};
 1753:             }
 1754:             $datatable .='<br />';
 1755:             if ($switchserver) {
 1756:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1757:             } else {
 1758:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1759:             }
 1760:             $datatable .= '</td>'.
 1761:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1762:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1763:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1764:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1765:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1766:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1767:                           '<tr'.$css_class.'>'.
 1768:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1769:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1770:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1771:                           $samltitle{$lonhost}.'</textarea></td>'.
 1772:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1773:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1774:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1775:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1776:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1777:                           '</table></td>'.
 1778:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1779:            $itemcount ++;
 1780:         }
 1781:         $datatable .= '</table></td></tr>';
 1782:     }
 1783:     return $datatable;
 1784: }
 1785: 
 1786: sub login_choices {
 1787:     my %choices =
 1788:         &Apache::lonlocal::texthash (
 1789:             coursecatalog => 'Display Course/Community Catalog link?',
 1790:             adminmail     => "Display Administrator's E-mail Address?",
 1791:             helpdesk      => 'Display "Contact Helpdesk" link',
 1792:             disallowlogin => "Login page requests redirected",
 1793:             hostid        => "Server",
 1794:             server        => "Redirect to:",
 1795:             serverpath    => "Path",
 1796:             custompath    => "Custom", 
 1797:             exempt        => "Exempt IP(s)",
 1798:             directlogin   => "No redirect",
 1799:             newuser       => "Link to create a user account",
 1800:             img           => "Header",
 1801:             logo          => "Main Logo",
 1802:             domlogo       => "Domain Logo",
 1803:             login         => "Log-in Header", 
 1804:             textcol       => "Text color",
 1805:             bgcol         => "Box color",
 1806:             bgs           => "Background colors",
 1807:             links         => "Link colors",
 1808:             font          => "Font color",
 1809:             pgbg          => "Header",
 1810:             mainbg        => "Page",
 1811:             sidebg        => "Login box",
 1812:             link          => "Link",
 1813:             alink         => "Active link",
 1814:             vlink         => "Visited link",
 1815:             headtag       => "Custom markup",
 1816:             action        => "Action",
 1817:             current       => "Current",
 1818:             samllanding   => "Dual login?",
 1819:             samloptions   => "Options",
 1820:             alttext       => "Alt text",
 1821:         );
 1822:     return %choices;
 1823: }
 1824: 
 1825: sub login_file_options {
 1826:       return &Apache::lonlocal::texthash(
 1827:                                            del     => 'Delete?',
 1828:                                            rep     => 'Replace:',
 1829:                                            upl     => 'Upload:',
 1830:                                            curr    => 'View contents',
 1831:                                            default => 'Default',
 1832:                                            custom  => 'Custom',
 1833:                                            none    => 'None',
 1834:       );
 1835: }
 1836: 
 1837: sub print_ipaccess {
 1838:     my ($dom,$settings,$rowtotal) = @_;
 1839:     my $css_class;
 1840:     my $itemcount = 0;
 1841:     my $datatable;
 1842:     my %ordered;
 1843:     if (ref($settings) eq 'HASH') {
 1844:         foreach my $item (keys(%{$settings})) {
 1845:             if (ref($settings->{$item}) eq 'HASH') {
 1846:                 my $num = $settings->{$item}{'order'};
 1847:                 if ($num eq '') {
 1848:                     $num = scalar(keys(%{$settings}));
 1849:                 }
 1850:                 $ordered{$num} = $item;
 1851:             }
 1852:         }
 1853:     }
 1854:     my $maxnum = scalar(keys(%ordered));
 1855:     if (keys(%ordered)) {
 1856:         my @items = sort { $a <=> $b } keys(%ordered);
 1857:         for (my $i=0; $i<@items; $i++) {
 1858:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1859:             my $item = $ordered{$items[$i]};
 1860:             my ($name,$ipranges,%commblocks,%courses);
 1861:             if (ref($settings->{$item}) eq 'HASH') {
 1862:                 $name = $settings->{$item}->{'name'};
 1863:                 $ipranges = $settings->{$item}->{'ip'};
 1864:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1865:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1866:                 }
 1867:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1868:                     %courses = %{$settings->{$item}->{'courses'}};
 1869:                 }
 1870:             }
 1871:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1872:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1873:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1874:             for (my $k=0; $k<=$maxnum; $k++) {
 1875:                 my $vpos = $k+1;
 1876:                 my $selstr;
 1877:                 if ($k == $i) {
 1878:                     $selstr = ' selected="selected" ';
 1879:                 }
 1880:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1881:             }
 1882:             $datatable .= '</select>'.('&nbsp;'x2).
 1883:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1884:                 &mt('Delete?').'</label></span></td>'.
 1885:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1886:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1887:                 '</td></tr>';
 1888:             $itemcount ++;
 1889:         }
 1890:     }
 1891:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1892:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1893:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1894:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1895:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1896:     for (my $k=0; $k<$maxnum+1; $k++) {
 1897:         my $vpos = $k+1;
 1898:         my $selstr;
 1899:         if ($k == $maxnum) {
 1900:             $selstr = ' selected="selected" ';
 1901:         }
 1902:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1903:     }
 1904:     $datatable .= '</select>&nbsp;'."\n".
 1905:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1906:                   '<td colspan="2">'.
 1907:                   &ipaccess_options('add',$itemcount,$dom).
 1908:                   '</td>'."\n".
 1909:                   '</tr>'."\n";
 1910:     $$rowtotal ++;
 1911:     return $datatable;
 1912: }
 1913: 
 1914: sub ipaccess_options {
 1915:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1916:     my (%currblocks,%currcourses,$output);
 1917:     if (ref($blocksref) eq 'HASH') {
 1918:         %currblocks = %{$blocksref};
 1919:     }
 1920:     if (ref($coursesref) eq 'HASH') {
 1921:         %currcourses = %{$coursesref};
 1922:     }
 1923:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1924:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1925:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1926:               '</span></fieldset>'.
 1927:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1928:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1929:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1930:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1931:               $ipranges.'</textarea></fieldset>'.
 1932:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1933:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1934:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1935:               '<table>';
 1936:     foreach my $cid (sort(keys(%currcourses))) {
 1937:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1938:         $output .= '<tr><td><span class="LC_nobreak">'.
 1939:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1940:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1941:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1942:     }
 1943:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1944:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1945:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1946:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1947:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1948:                '</span></td></tr></table>'."\n".
 1949:                '</fieldset>';
 1950:     return $output;
 1951: }
 1952: 
 1953: sub blocker_checkboxes {
 1954:     my ($num,$blocks) = @_;
 1955:     my ($typeorder,$types) = &commblocktype_text();
 1956:     my $numinrow = 6;
 1957:     my $output = '<table>';
 1958:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1959:         my $block = $typeorder->[$i];
 1960:         my $blockstatus;
 1961:         if (ref($blocks) eq 'HASH') {
 1962:             if ($blocks->{$block} eq 'on') {
 1963:                 $blockstatus = 'checked="checked"';
 1964:             }
 1965:         }
 1966:         my $rem = $i%($numinrow);
 1967:         if ($rem == 0) {
 1968:             if ($i > 0) {
 1969:                 $output .= '</tr>';
 1970:             }
 1971:             $output .= '<tr>';
 1972:         }
 1973:         if ($i == scalar(@{$typeorder})-1) {
 1974:             my $colsleft = $numinrow-$rem;
 1975:             if ($colsleft > 1) {
 1976:                 $output .= '<td colspan="'.$colsleft.'">';
 1977:             } else {
 1978:                 $output .= '<td>';
 1979:             }
 1980:         } else {
 1981:             $output .= '<td>';
 1982:         }
 1983:         my $item = 'ipaccess_block_'.$num;
 1984:         if ($blockstatus) {
 1985:             $blockstatus = ' '.$blockstatus;
 1986:         }
 1987:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1988:                    '<input type="checkbox" name="'.$item.'"'.
 1989:                    $blockstatus.' value="'.$block.'"'.' />'.
 1990:                    $types->{$block}.'</label></span>'."\n".
 1991:                    '<br /></td>';
 1992:     }
 1993:     $output .= '</tr></table>';
 1994:     return $output;
 1995: }
 1996: 
 1997: sub commblocktype_text {
 1998:     my %types = &Apache::lonlocal::texthash(
 1999:         'com' => 'Messaging',
 2000:         'chat' => 'Chat Room',
 2001:         'boards' => 'Discussion',
 2002:         'port' => 'Portfolio',
 2003:         'groups' => 'Groups',
 2004:         'blogs' => 'Blogs',
 2005:         'about' => 'User Information',
 2006:         'printout' => 'Printouts',
 2007:         'passwd' => 'Change Password',
 2008:         'grades' => 'Gradebook',
 2009:         'search' => 'Course search',
 2010:         'wishlist' => 'Stored links',
 2011:         'annotate' => 'Annotations',
 2012:     );
 2013:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 2014:     return ($typeorder,\%types);
 2015: }
 2016: 
 2017: sub print_rolecolors {
 2018:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 2019:     my %choices = &color_font_choices();
 2020:     my @bgs = ('pgbg','tabbg','sidebg');
 2021:     my @links = ('link','alink','vlink');
 2022:     my @images = ('img');
 2023:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 2024:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 2025:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2026:     my (%is_custom,%designs);
 2027:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 2028:     if (ref($settings) eq 'HASH') {
 2029:         if (ref($settings->{$role}) eq 'HASH') {
 2030:             if ($settings->{$role}->{'img'} ne '') {
 2031:                 $designs{'img'} = $settings->{$role}->{'img'};
 2032:                 $is_custom{'img'} = 1;
 2033:             }
 2034:             if ($settings->{$role}->{'font'} ne '') {
 2035:                 $designs{'font'} = $settings->{$role}->{'font'};
 2036:                 $is_custom{'font'} = 1;
 2037:             }
 2038:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2039:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2040:                 $is_custom{'fontmenu'} = 1;
 2041:             }
 2042:             foreach my $item (@bgs) {
 2043:                 if ($settings->{$role}->{$item} ne '') {
 2044:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2045:                     $is_custom{$item} = 1;
 2046:                 }
 2047:             }
 2048:             foreach my $item (@links) {
 2049:                 if ($settings->{$role}->{$item} ne '') {
 2050:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2051:                     $is_custom{$item} = 1;
 2052:                 }
 2053:             }
 2054:         }
 2055:     } else {
 2056:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 2057:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 2058:             $is_custom{'img'} = 1;
 2059:         }
 2060:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2061:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2062:             $is_custom{'fontmenu'} = 1; 
 2063:         }
 2064:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2065:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2066:             $is_custom{'font'} = 1;
 2067:         }
 2068:         foreach my $item (@bgs) {
 2069:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2070:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2071:                 $is_custom{$item} = 1;
 2072:             
 2073:             }
 2074:         }
 2075:         foreach my $item (@links) {
 2076:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2077:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2078:                 $is_custom{$item} = 1;
 2079:             }
 2080:         }
 2081:     }
 2082:     my $itemcount = 1;
 2083:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2084:     $datatable .= '</tr></table></td></tr>';
 2085:     return $datatable;
 2086: }
 2087: 
 2088: sub role_defaults {
 2089:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2090:     my %defaults;
 2091:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2092:         return %defaults;
 2093:     }
 2094:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2095:     if ($role eq 'login') {
 2096:         %defaults = (
 2097:                        font => $defaultdesign{$role.'.font'},
 2098:                     );
 2099:         if (ref($logintext) eq 'ARRAY') {
 2100:             foreach my $item (@{$logintext}) {
 2101:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2102:             }
 2103:         }
 2104:         foreach my $item (@{$images}) {
 2105:             $defaults{'showlogo'}{$item} = 1;
 2106:         }
 2107:     } else {
 2108:         %defaults = (
 2109:                        img => $defaultdesign{$role.'.img'},
 2110:                        font => $defaultdesign{$role.'.font'},
 2111:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2112:                     );
 2113:     }
 2114:     foreach my $item (@{$bgs}) {
 2115:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2116:     }
 2117:     foreach my $item (@{$links}) {
 2118:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2119:     }
 2120:     foreach my $item (@{$images}) {
 2121:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2122:     }
 2123:     return %defaults;
 2124: }
 2125: 
 2126: sub display_color_options {
 2127:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2128:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2129:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2130:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2131:     my $datatable = '<tr'.$css_class.'>'.
 2132:         '<td>'.$choices->{'font'}.'</td>';
 2133:     if (!$is_custom->{'font'}) {
 2134:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2135:     } else {
 2136:         $datatable .= '<td>&nbsp;</td>';
 2137:     }
 2138:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2139: 
 2140:     $datatable .= '<td><span class="LC_nobreak">'.
 2141:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2142:                   ' value="'.$current_color.'" />&nbsp;'.
 2143:                   '&nbsp;</span></td></tr>';
 2144:     unless ($role eq 'login') { 
 2145:         $datatable .= '<tr'.$css_class.'>'.
 2146:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2147:         if (!$is_custom->{'fontmenu'}) {
 2148:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2149:         } else {
 2150:             $datatable .= '<td>&nbsp;</td>';
 2151:         }
 2152: 	$current_color = $designs->{'fontmenu'} ?
 2153: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2154:         $datatable .= '<td><span class="LC_nobreak">'.
 2155:                       '<input class="colorchooser" type="text" size="10" name="'
 2156: 		      .$role.'_fontmenu"'.
 2157:                       ' value="'.$current_color.'" />&nbsp;'.
 2158:                       '&nbsp;</span></td></tr>';
 2159:     }
 2160:     my $switchserver = &check_switchserver($dom,$confname);
 2161:     foreach my $img (@{$images}) {
 2162: 	$itemcount ++;
 2163:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2164:         $datatable .= '<tr'.$css_class.'>'.
 2165:                       '<td>'.$choices->{$img};
 2166:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2167:         if ($role eq 'login') {
 2168:             if ($img eq 'login') {
 2169:                 $login_hdr_pick =
 2170:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2171:                 $logincolors =
 2172:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2173:                                        $designs,$defaults);
 2174:             } else {
 2175:                 if ($img ne 'domlogo') {
 2176:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2177:                 }
 2178:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2179:                     $alttext = $designs->{'alttext'}{$img};
 2180:                 }
 2181:             }
 2182:         }
 2183:         $datatable .= '</td>';
 2184:         if ($designs->{$img} ne '') {
 2185:             $imgfile = $designs->{$img};
 2186: 	    $img_import = ($imgfile =~ m{^/adm/});
 2187:         } else {
 2188:             $imgfile = $defaults->{$img};
 2189:         }
 2190:         if ($imgfile) {
 2191:             my ($showfile,$fullsize);
 2192:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2193:                 my $urldir = $1;
 2194:                 my $filename = $2;
 2195:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2196:                 if (@info) {
 2197:                     my $thumbfile = 'tn-'.$filename;
 2198:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2199:                     if (@thumb) {
 2200:                         $showfile = $urldir.'/'.$thumbfile;
 2201:                     } else {
 2202:                         $showfile = $imgfile;
 2203:                     }
 2204:                 } else {
 2205:                     $showfile = '';
 2206:                 }
 2207:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2208:                 $showfile = $imgfile;
 2209:                 my $imgdir = $1;
 2210:                 my $filename = $2;
 2211:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2212:                     $showfile = "/$imgdir/tn-".$filename;
 2213:                 } else {
 2214:                     my $input = $londocroot.$imgfile;
 2215:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2216:                     if (!-e $output) {
 2217:                         my ($width,$height) = &thumb_dimensions();
 2218:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2219:                         if ($fullwidth ne '' && $fullheight ne '') {
 2220:                             if ($fullwidth > $width && $fullheight > $height) { 
 2221:                                 my $size = $width.'x'.$height;
 2222:                                 my @args = ('convert','-sample',$size,$input,$output);
 2223:                                 system({$args[0]} @args);
 2224:                                 $showfile = "/$imgdir/tn-".$filename;
 2225:                             }
 2226:                         }
 2227:                     }
 2228:                 }
 2229:             }
 2230:             if ($showfile) {
 2231:                 if ($showfile =~ m{^/(adm|res)/}) {
 2232:                     if ($showfile =~ m{^/res/}) {
 2233:                         my $local_showfile =
 2234:                             &Apache::lonnet::filelocation('',$showfile);
 2235:                         &Apache::lonnet::repcopy($local_showfile);
 2236:                     }
 2237:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2238:                 }
 2239:                 if ($imgfile) {
 2240:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2241:                         if ($imgfile =~ m{^/res/}) {
 2242:                             my $local_imgfile =
 2243:                                 &Apache::lonnet::filelocation('',$imgfile);
 2244:                             &Apache::lonnet::repcopy($local_imgfile);
 2245:                         }
 2246:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2247:                     } else {
 2248:                         $fullsize = $imgfile;
 2249:                     }
 2250:                 }
 2251:                 $datatable .= '<td>';
 2252:                 if ($img eq 'login') {
 2253:                     $datatable .= $login_hdr_pick;
 2254:                 } 
 2255:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2256:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2257:             } else {
 2258:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2259:                               &mt('Upload:').'<br />';
 2260:             }
 2261:         } else {
 2262:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2263:                           &mt('Upload:').'<br />';
 2264:         }
 2265:         if ($switchserver) {
 2266:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2267:         } else {
 2268:             if ($img ne 'login') { # suppress file selection for Log-in header
 2269:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2270:             }
 2271:         }
 2272:         if (($role eq 'login') && ($img ne 'login')) {
 2273:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2274:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2275:                           '</label></span>';
 2276:         }
 2277:         $datatable .= '</td></tr>';
 2278:     }
 2279:     $itemcount ++;
 2280:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2281:     $datatable .= '<tr'.$css_class.'>'.
 2282:                   '<td>'.$choices->{'bgs'}.'</td>';
 2283:     my $bgs_def;
 2284:     foreach my $item (@{$bgs}) {
 2285:         if (!$is_custom->{$item}) {
 2286:             $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>';
 2287:         }
 2288:     }
 2289:     if ($bgs_def) {
 2290:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2291:     } else {
 2292:         $datatable .= '<td>&nbsp;</td>';
 2293:     }
 2294:     $datatable .= '<td class="LC_right_item">'.
 2295:                   '<table border="0"><tr>';
 2296: 
 2297:     foreach my $item (@{$bgs}) {
 2298:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2299: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2300:         if ($designs->{'bgs'}{$item}) {
 2301:             $datatable .= '&nbsp;';
 2302:         }
 2303:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2304:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2305:     }
 2306:     $datatable .= '</tr></table></td></tr>';
 2307:     $itemcount ++;
 2308:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2309:     $datatable .= '<tr'.$css_class.'>'.
 2310:                   '<td>'.$choices->{'links'}.'</td>';
 2311:     my $links_def;
 2312:     foreach my $item (@{$links}) {
 2313:         if (!$is_custom->{$item}) {
 2314:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2315:         }
 2316:     }
 2317:     if ($links_def) {
 2318:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2319:     } else {
 2320:         $datatable .= '<td>&nbsp;</td>';
 2321:     }
 2322:     $datatable .= '<td class="LC_right_item">'.
 2323:                   '<table border="0"><tr>';
 2324:     foreach my $item (@{$links}) {
 2325: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2326:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2327:         if ($designs->{'links'}{$item}) {
 2328:             $datatable.='&nbsp;';
 2329:         }
 2330:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2331:                       '" /></td>';
 2332:     }
 2333:     $$rowtotal += $itemcount;
 2334:     return $datatable;
 2335: }
 2336: 
 2337: sub logo_display_options {
 2338:     my ($img,$defaults,$designs) = @_;
 2339:     my $checkedon;
 2340:     if (ref($defaults) eq 'HASH') {
 2341:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2342:             if ($defaults->{'showlogo'}{$img}) {
 2343:                 $checkedon = 'checked="checked" ';     
 2344:             }
 2345:         } 
 2346:     }
 2347:     if (ref($designs) eq 'HASH') {
 2348:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2349:             if (defined($designs->{'showlogo'}{$img})) {
 2350:                 if ($designs->{'showlogo'}{$img} == 0) {
 2351:                     $checkedon = '';
 2352:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2353:                     $checkedon = 'checked="checked" ';
 2354:                 }
 2355:             }
 2356:         }
 2357:     }
 2358:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2359:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2360:            &mt('show').'</label>'."\n";
 2361: }
 2362: 
 2363: sub login_header_options  {
 2364:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2365:     my $output = '';
 2366:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2367:         $output .= &mt('Text default(s):').'<br />';
 2368:         if (!$is_custom->{'textcol'}) {
 2369:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2370:                        '&nbsp;&nbsp;&nbsp;';
 2371:         }
 2372:         if (!$is_custom->{'bgcol'}) {
 2373:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2374:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2375:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2376:         }
 2377:         $output .= '<br />';
 2378:     }
 2379:     $output .='<br />';
 2380:     return $output;
 2381: }
 2382: 
 2383: sub login_text_colors {
 2384:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2385:     my $color_menu = '<table border="0"><tr>';
 2386:     foreach my $item (@{$logintext}) {
 2387:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2388:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2389:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2390:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2391:     }
 2392:     $color_menu .= '</tr></table><br />';
 2393:     return $color_menu;
 2394: }
 2395: 
 2396: sub image_changes {
 2397:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2398:     my $output;
 2399:     if ($img eq 'login') {
 2400:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2401:     } elsif (!$is_custom) {
 2402:         if ($img ne 'domlogo') {
 2403:             $output = &mt('Default image:').'<br />';
 2404:         } else {
 2405:             $output = &mt('Default in use:').'<br />';
 2406:         }
 2407:     }
 2408:     if ($img ne 'login') {
 2409:         if ($img_import) {
 2410:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2411:         }
 2412:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2413:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2414:         if ($is_custom) {
 2415:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2416:                        '<input type="checkbox" name="'.
 2417:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2418:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2419:         } else {
 2420:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2421:         }
 2422:     }
 2423:     return $output;
 2424: }
 2425: 
 2426: sub print_quotas {
 2427:     my ($dom,$settings,$rowtotal,$action) = @_;
 2428:     my $context;
 2429:     if ($action eq 'quotas') {
 2430:         $context = 'tools';
 2431:     } else {
 2432:         $context = $action;
 2433:     }
 2434:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2435:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2436:     my $typecount = 0;
 2437:     my ($css_class,%titles);
 2438:     if ($context eq 'requestcourses') {
 2439:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2440:         @options =('norequest','approval','validate','autolimit');
 2441:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2442:         %titles = &courserequest_titles();
 2443:     } elsif ($context eq 'requestauthor') {
 2444:         @usertools = ('author');
 2445:         @options = ('norequest','approval','automatic');
 2446:         %titles = &authorrequest_titles();
 2447:     } else {
 2448:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
 2449:         %titles = &tool_titles();
 2450:     }
 2451:     if (ref($types) eq 'ARRAY') {
 2452:         foreach my $type (@{$types}) {
 2453:             my $currdefquota;
 2454:             unless (($context eq 'requestcourses') ||
 2455:                     ($context eq 'requestauthor')) {
 2456:                 if (ref($settings) eq 'HASH') {
 2457:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2458:                         $currdefquota = $settings->{defaultquota}->{$type};
 2459:                     } else {
 2460:                         $currdefquota = $settings->{$type};
 2461:                     }
 2462:                 }
 2463:             }
 2464:             if (defined($usertypes->{$type})) {
 2465:                 $typecount ++;
 2466:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2467:                 $datatable .= '<tr'.$css_class.'>'.
 2468:                               '<td>'.$usertypes->{$type}.'</td>'.
 2469:                               '<td class="LC_left_item">';
 2470:                 if ($context eq 'requestcourses') {
 2471:                     $datatable .= '<table><tr>';
 2472:                 }
 2473:                 my %cell;  
 2474:                 foreach my $item (@usertools) {
 2475:                     if ($context eq 'requestcourses') {
 2476:                         my ($curroption,$currlimit);
 2477:                         if (ref($settings) eq 'HASH') {
 2478:                             if (ref($settings->{$item}) eq 'HASH') {
 2479:                                 $curroption = $settings->{$item}->{$type};
 2480:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2481:                                     $currlimit = $1; 
 2482:                                 }
 2483:                             }
 2484:                         }
 2485:                         if (!$curroption) {
 2486:                             $curroption = 'norequest';
 2487:                         }
 2488:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2489:                         foreach my $option (@options) {
 2490:                             my $val = $option;
 2491:                             if ($option eq 'norequest') {
 2492:                                 $val = 0;  
 2493:                             }
 2494:                             if ($option eq 'validate') {
 2495:                                 my $canvalidate = 0;
 2496:                                 if (ref($validations{$item}) eq 'HASH') { 
 2497:                                     if ($validations{$item}{$type}) {
 2498:                                         $canvalidate = 1;
 2499:                                     }
 2500:                                 }
 2501:                                 next if (!$canvalidate);
 2502:                             }
 2503:                             my $checked = '';
 2504:                             if ($option eq $curroption) {
 2505:                                 $checked = ' checked="checked"';
 2506:                             } elsif ($option eq 'autolimit') {
 2507:                                 if ($curroption =~ /^autolimit/) {
 2508:                                     $checked = ' checked="checked"';
 2509:                                 }                       
 2510:                             } 
 2511:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2512:                                   '<input type="radio" name="crsreq_'.$item.
 2513:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2514:                                   $titles{$option}.'</label>';
 2515:                             if ($option eq 'autolimit') {
 2516:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2517:                                                 $item.'_limit_'.$type.'" size="1" '.
 2518:                                                 'value="'.$currlimit.'" />';
 2519:                             }
 2520:                             $cell{$item} .= '</span> ';
 2521:                             if ($option eq 'autolimit') {
 2522:                                 $cell{$item} .= $titles{'unlimited'};
 2523:                             }
 2524:                         }
 2525:                     } elsif ($context eq 'requestauthor') {
 2526:                         my $curroption;
 2527:                         if (ref($settings) eq 'HASH') {
 2528:                             $curroption = $settings->{$type};
 2529:                         }
 2530:                         if (!$curroption) {
 2531:                             $curroption = 'norequest';
 2532:                         }
 2533:                         foreach my $option (@options) {
 2534:                             my $val = $option;
 2535:                             if ($option eq 'norequest') {
 2536:                                 $val = 0;
 2537:                             }
 2538:                             my $checked = '';
 2539:                             if ($option eq $curroption) {
 2540:                                 $checked = ' checked="checked"';
 2541:                             }
 2542:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2543:                                   '<input type="radio" name="authorreq_'.$type.
 2544:                                   '" value="'.$val.'"'.$checked.' />'.
 2545:                                   $titles{$option}.'</label></span>&nbsp; ';
 2546:                         }
 2547:                     } else {
 2548:                         my $checked = 'checked="checked" ';
 2549:                         if ($item eq 'timezone') {
 2550:                             $checked = '';
 2551:                         }
 2552:                         if (ref($settings) eq 'HASH') {
 2553:                             if (ref($settings->{$item}) eq 'HASH') {
 2554:                                 if (!$settings->{$item}->{$type}) {
 2555:                                     $checked = '';
 2556:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2557:                                     $checked =  'checked="checked" ';
 2558:                                 }
 2559:                             }
 2560:                         }
 2561:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2562:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2563:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2564:                                       '</label></span>&nbsp; ';
 2565:                     }
 2566:                 }
 2567:                 if ($context eq 'requestcourses') {
 2568:                     $datatable .= '</tr><tr>';
 2569:                     foreach my $item (@usertools) {
 2570:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2571:                     }
 2572:                     $datatable .= '</tr></table>';
 2573:                 }
 2574:                 $datatable .= '</td>';
 2575:                 unless (($context eq 'requestcourses') ||
 2576:                         ($context eq 'requestauthor')) {
 2577:                     $datatable .= 
 2578:                               '<td class="LC_right_item">'.
 2579:                               '<span class="LC_nobreak">'.
 2580:                               '<input type="text" name="quota_'.$type.
 2581:                               '" value="'.$currdefquota.
 2582:                               '" size="5" /></span></td>';
 2583:                 }
 2584:                 $datatable .= '</tr>';
 2585:             }
 2586:         }
 2587:     }
 2588:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2589:         $defaultquota = '20';
 2590:         if (ref($settings) eq 'HASH') {
 2591:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2592:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2593:             } elsif (defined($settings->{'default'})) {
 2594:                 $defaultquota = $settings->{'default'};
 2595:             }
 2596:         }
 2597:     }
 2598:     $typecount ++;
 2599:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2600:     $datatable .= '<tr'.$css_class.'>'.
 2601:                   '<td>'.$othertitle.'</td>'.
 2602:                   '<td class="LC_left_item">';
 2603:     if ($context eq 'requestcourses') {
 2604:         $datatable .= '<table><tr>';
 2605:     }
 2606:     my %defcell;
 2607:     foreach my $item (@usertools) {
 2608:         if ($context eq 'requestcourses') {
 2609:             my ($curroption,$currlimit);
 2610:             if (ref($settings) eq 'HASH') {
 2611:                 if (ref($settings->{$item}) eq 'HASH') {
 2612:                     $curroption = $settings->{$item}->{'default'};
 2613:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2614:                         $currlimit = $1;
 2615:                     }
 2616:                 }
 2617:             }
 2618:             if (!$curroption) {
 2619:                 $curroption = 'norequest';
 2620:             }
 2621:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2622:             foreach my $option (@options) {
 2623:                 my $val = $option;
 2624:                 if ($option eq 'norequest') {
 2625:                     $val = 0;
 2626:                 }
 2627:                 if ($option eq 'validate') {
 2628:                     my $canvalidate = 0;
 2629:                     if (ref($validations{$item}) eq 'HASH') {
 2630:                         if ($validations{$item}{'default'}) {
 2631:                             $canvalidate = 1;
 2632:                         }
 2633:                     }
 2634:                     next if (!$canvalidate);
 2635:                 }
 2636:                 my $checked = '';
 2637:                 if ($option eq $curroption) {
 2638:                     $checked = ' checked="checked"';
 2639:                 } elsif ($option eq 'autolimit') {
 2640:                     if ($curroption =~ /^autolimit/) {
 2641:                         $checked = ' checked="checked"';
 2642:                     }
 2643:                 }
 2644:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2645:                                   '<input type="radio" name="crsreq_'.$item.
 2646:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2647:                                   $titles{$option}.'</label>';
 2648:                 if ($option eq 'autolimit') {
 2649:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2650:                                        $item.'_limit_default" size="1" '.
 2651:                                        'value="'.$currlimit.'" />';
 2652:                 }
 2653:                 $defcell{$item} .= '</span> ';
 2654:                 if ($option eq 'autolimit') {
 2655:                     $defcell{$item} .= $titles{'unlimited'};
 2656:                 }
 2657:             }
 2658:         } elsif ($context eq 'requestauthor') {
 2659:             my $curroption;
 2660:             if (ref($settings) eq 'HASH') {
 2661:                 $curroption = $settings->{'default'};
 2662:             }
 2663:             if (!$curroption) {
 2664:                 $curroption = 'norequest';
 2665:             }
 2666:             foreach my $option (@options) {
 2667:                 my $val = $option;
 2668:                 if ($option eq 'norequest') {
 2669:                     $val = 0;
 2670:                 }
 2671:                 my $checked = '';
 2672:                 if ($option eq $curroption) {
 2673:                     $checked = ' checked="checked"';
 2674:                 }
 2675:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2676:                               '<input type="radio" name="authorreq_default"'.
 2677:                               ' value="'.$val.'"'.$checked.' />'.
 2678:                               $titles{$option}.'</label></span>&nbsp; ';
 2679:             }
 2680:         } else {
 2681:             my $checked = 'checked="checked" ';
 2682:             if (ref($settings) eq 'HASH') {
 2683:                 if (ref($settings->{$item}) eq 'HASH') {
 2684:                     if ($settings->{$item}->{'default'} == 0) {
 2685:                         $checked = '';
 2686:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2687:                         $checked = 'checked="checked" ';
 2688:                     }
 2689:                 }
 2690:             }
 2691:             $datatable .= '<span class="LC_nobreak"><label>'.
 2692:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2693:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2694:                           '</label></span>&nbsp; ';
 2695:         }
 2696:     }
 2697:     if ($context eq 'requestcourses') {
 2698:         $datatable .= '</tr><tr>';
 2699:         foreach my $item (@usertools) {
 2700:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2701:         }
 2702:         $datatable .= '</tr></table>';
 2703:     }
 2704:     $datatable .= '</td>';
 2705:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2706:         $datatable .= '<td class="LC_right_item">'.
 2707:                       '<span class="LC_nobreak">'.
 2708:                       '<input type="text" name="defaultquota" value="'.
 2709:                       $defaultquota.'" size="5" /></span></td>';
 2710:     }
 2711:     $datatable .= '</tr>';
 2712:     $typecount ++;
 2713:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2714:     $datatable .= '<tr'.$css_class.'>'.
 2715:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2716:     if ($context eq 'requestcourses') {
 2717:         $datatable .= &mt('(overrides affiliation, if set)').
 2718:                       '</td>'.
 2719:                       '<td class="LC_left_item">'.
 2720:                       '<table><tr>';
 2721:     } else {
 2722:         $datatable .= &mt('(overrides affiliation, if checked)').
 2723:                       '</td>'.
 2724:                       '<td class="LC_left_item" colspan="2">'.
 2725:                       '<br />';
 2726:     }
 2727:     my %advcell;
 2728:     foreach my $item (@usertools) {
 2729:         if ($context eq 'requestcourses') {
 2730:             my ($curroption,$currlimit);
 2731:             if (ref($settings) eq 'HASH') {
 2732:                 if (ref($settings->{$item}) eq 'HASH') {
 2733:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2734:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2735:                         $currlimit = $1;
 2736:                     }
 2737:                 }
 2738:             }
 2739:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2740:             my $checked = '';
 2741:             if ($curroption eq '') {
 2742:                 $checked = ' checked="checked"';
 2743:             }
 2744:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2745:                                '<input type="radio" name="crsreq_'.$item.
 2746:                                '__LC_adv" value=""'.$checked.' />'.
 2747:                                &mt('No override set').'</label></span>&nbsp; ';
 2748:             foreach my $option (@options) {
 2749:                 my $val = $option;
 2750:                 if ($option eq 'norequest') {
 2751:                     $val = 0;
 2752:                 }
 2753:                 if ($option eq 'validate') {
 2754:                     my $canvalidate = 0;
 2755:                     if (ref($validations{$item}) eq 'HASH') {
 2756:                         if ($validations{$item}{'_LC_adv'}) {
 2757:                             $canvalidate = 1;
 2758:                         }
 2759:                     }
 2760:                     next if (!$canvalidate);
 2761:                 }
 2762:                 my $checked = '';
 2763:                 if ($val eq $curroption) {
 2764:                     $checked = ' checked="checked"';
 2765:                 } elsif ($option eq 'autolimit') {
 2766:                     if ($curroption =~ /^autolimit/) {
 2767:                         $checked = ' checked="checked"';
 2768:                     }
 2769:                 }
 2770:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2771:                                   '<input type="radio" name="crsreq_'.$item.
 2772:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2773:                                   $titles{$option}.'</label>';
 2774:                 if ($option eq 'autolimit') {
 2775:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2776:                                        $item.'_limit__LC_adv" size="1" '.
 2777:                                        'value="'.$currlimit.'" />';
 2778:                 }
 2779:                 $advcell{$item} .= '</span> ';
 2780:                 if ($option eq 'autolimit') {
 2781:                     $advcell{$item} .= $titles{'unlimited'};
 2782:                 }
 2783:             }
 2784:         } elsif ($context eq 'requestauthor') {
 2785:             my $curroption;
 2786:             if (ref($settings) eq 'HASH') {
 2787:                 $curroption = $settings->{'_LC_adv'};
 2788:             }
 2789:             my $checked = '';
 2790:             if ($curroption eq '') {
 2791:                 $checked = ' checked="checked"';
 2792:             }
 2793:             $datatable .= '<span class="LC_nobreak"><label>'.
 2794:                           '<input type="radio" name="authorreq__LC_adv"'.
 2795:                           ' value=""'.$checked.' />'.
 2796:                           &mt('No override set').'</label></span>&nbsp; ';
 2797:             foreach my $option (@options) {
 2798:                 my $val = $option;
 2799:                 if ($option eq 'norequest') {
 2800:                     $val = 0;
 2801:                 }
 2802:                 my $checked = '';
 2803:                 if ($val eq $curroption) {
 2804:                     $checked = ' checked="checked"';
 2805:                 }
 2806:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2807:                               '<input type="radio" name="authorreq__LC_adv"'.
 2808:                               ' value="'.$val.'"'.$checked.' />'.
 2809:                               $titles{$option}.'</label></span>&nbsp; ';
 2810:             }
 2811:         } else {
 2812:             my $checked = 'checked="checked" ';
 2813:             if (ref($settings) eq 'HASH') {
 2814:                 if (ref($settings->{$item}) eq 'HASH') {
 2815:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2816:                         $checked = '';
 2817:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2818:                         $checked = 'checked="checked" ';
 2819:                     }
 2820:                 }
 2821:             }
 2822:             $datatable .= '<span class="LC_nobreak"><label>'.
 2823:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2824:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2825:                           '</label></span>&nbsp; ';
 2826:         }
 2827:     }
 2828:     if ($context eq 'requestcourses') {
 2829:         $datatable .= '</tr><tr>';
 2830:         foreach my $item (@usertools) {
 2831:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2832:         }
 2833:         $datatable .= '</tr></table>';
 2834:     }
 2835:     $datatable .= '</td></tr>';
 2836:     $$rowtotal += $typecount;
 2837:     return $datatable;
 2838: }
 2839: 
 2840: sub print_requestmail {
 2841:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2842:     my ($now,$datatable,%currapp);
 2843:     $now = time;
 2844:     if (ref($settings) eq 'HASH') {
 2845:         if (ref($settings->{'notify'}) eq 'HASH') {
 2846:             if ($settings->{'notify'}{'approval'} ne '') {
 2847:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2848:             }
 2849:         }
 2850:     }
 2851:     my $numinrow = 2;
 2852:     my $css_class;
 2853:     if ($$rowtotal%2) {
 2854:         $css_class = 'LC_odd_row';
 2855:     }
 2856:     if ($customcss) {
 2857:         $css_class .= " $customcss";
 2858:     }
 2859:     $css_class =~ s/^\s+//;
 2860:     if ($css_class) {
 2861:         $css_class = ' class="'.$css_class.'"';
 2862:     }
 2863:     if ($rowstyle) {
 2864:         $css_class .= ' style="'.$rowstyle.'"';
 2865:     }
 2866:     my $text;
 2867:     if ($action eq 'requestcourses') {
 2868:         $text = &mt('Receive notification of course requests requiring approval');
 2869:     } elsif ($action eq 'requestauthor') {
 2870:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2871:     } else {
 2872:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2873:     }
 2874:     $datatable = '<tr'.$css_class.'>'.
 2875:                  ' <td>'.$text.'</td>'.
 2876:                  ' <td class="LC_left_item">';
 2877:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2878:                                                  $action.'notifyapproval',%currapp);
 2879:     if ($numdc > 0) {
 2880:         $datatable .= $table;
 2881:     } else {
 2882:         $datatable .= &mt('There are no active Domain Coordinators');
 2883:     }
 2884:     $datatable .='</td></tr>';
 2885:     return $datatable;
 2886: }
 2887: 
 2888: sub print_studentcode {
 2889:     my ($settings,$rowtotal) = @_;
 2890:     my $rownum = 0; 
 2891:     my ($output,%current);
 2892:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2893:     if (ref($settings) eq 'HASH') {
 2894:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2895:             foreach my $type (@crstypes) {
 2896:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2897:             }
 2898:         }
 2899:     }
 2900:     $output .= '<tr>'.
 2901:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2902:                '<td class="LC_left_item">';
 2903:     foreach my $type (@crstypes) {
 2904:         my $check = ' ';
 2905:         if ($current{$type}) {
 2906:             $check = ' checked="checked" ';
 2907:         }
 2908:         $output .= '<span class="LC_nobreak"><label>'.
 2909:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2910:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2911:     }
 2912:     $output .= '</td></tr>';
 2913:     $$rowtotal ++;
 2914:     return $output;
 2915: }
 2916: 
 2917: sub print_textbookcourses {
 2918:     my ($dom,$type,$settings,$rowtotal) = @_;
 2919:     my $rownum = 0;
 2920:     my $css_class;
 2921:     my $itemcount = 1;
 2922:     my $maxnum = 0;
 2923:     my $bookshash;
 2924:     if (ref($settings) eq 'HASH') {
 2925:         $bookshash = $settings->{$type};
 2926:     }
 2927:     my %ordered;
 2928:     if (ref($bookshash) eq 'HASH') {
 2929:         foreach my $item (keys(%{$bookshash})) {
 2930:             if (ref($bookshash->{$item}) eq 'HASH') {
 2931:                 my $num = $bookshash->{$item}{'order'};
 2932:                 $ordered{$num} = $item;
 2933:             }
 2934:         }
 2935:     }
 2936:     my $confname = $dom.'-domainconfig';
 2937:     my $switchserver = &check_switchserver($dom,$confname);
 2938:     my $maxnum = scalar(keys(%ordered));
 2939:     my $datatable;
 2940:     if (keys(%ordered)) {
 2941:         my @items = sort { $a <=> $b } keys(%ordered);
 2942:         for (my $i=0; $i<@items; $i++) {
 2943:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2944:             my $key = $ordered{$items[$i]};
 2945:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2946:             my $coursetitle = $coursehash{'description'};
 2947:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2948:             if (ref($bookshash->{$key}) eq 'HASH') {
 2949:                 $subject = $bookshash->{$key}->{'subject'};
 2950:                 $title = $bookshash->{$key}->{'title'};
 2951:                 if ($type eq 'textbooks') {
 2952:                     $publisher = $bookshash->{$key}->{'publisher'};
 2953:                     $author = $bookshash->{$key}->{'author'};
 2954:                     $image = $bookshash->{$key}->{'image'};
 2955:                     if ($image ne '') {
 2956:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2957:                         my $imagethumb = "$path/tn-".$imagefile;
 2958:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2959:                     }
 2960:                 }
 2961:             }
 2962:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2963:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2964:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2965:             for (my $k=0; $k<=$maxnum; $k++) {
 2966:                 my $vpos = $k+1;
 2967:                 my $selstr;
 2968:                 if ($k == $i) {
 2969:                     $selstr = ' selected="selected" ';
 2970:                 }
 2971:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2972:             }
 2973:             $datatable .= '</select>'.('&nbsp;'x2).
 2974:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2975:                 &mt('Delete?').'</label></span></td>'.
 2976:                 '<td colspan="2">'.
 2977:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2978:                 ('&nbsp;'x2).
 2979:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2980:             if ($type eq 'textbooks') {
 2981:                 $datatable .= ('&nbsp;'x2).
 2982:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2983:                               ('&nbsp;'x2).
 2984:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2985:                               ('&nbsp;'x2).
 2986:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2987:                 if ($image) {
 2988:                     $datatable .= $imgsrc.
 2989:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2990:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2991:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2992:                 }
 2993:                 if ($switchserver) {
 2994:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2995:                 } else {
 2996:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2997:                 }
 2998:             }
 2999:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 3000:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3001:                           $coursetitle.'</span></td></tr>'."\n";
 3002:             $itemcount ++;
 3003:         }
 3004:     }
 3005:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3006:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 3007:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3008:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 3009:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 3010:     for (my $k=0; $k<$maxnum+1; $k++) {
 3011:         my $vpos = $k+1;
 3012:         my $selstr;
 3013:         if ($k == $maxnum) {
 3014:             $selstr = ' selected="selected" ';
 3015:         }
 3016:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3017:     }
 3018:     $datatable .= '</select>&nbsp;'."\n".
 3019:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3020:                   '<td colspan="2">'.
 3021:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3022:                   ('&nbsp;'x2).
 3023:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3024:                   ('&nbsp;'x2);
 3025:     if ($type eq 'textbooks') {
 3026:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3027:                       ('&nbsp;'x2).
 3028:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3029:                       ('&nbsp;'x2).
 3030:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3031:         if ($switchserver) {
 3032:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3033:         } else {
 3034:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3035:         }
 3036:         $datatable .= '</span>'."\n";
 3037:     }
 3038:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3039:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3040:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3041:                   &Apache::loncommon::selectcourse_link
 3042:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3043:                   '</span></td>'."\n".
 3044:                   '</tr>'."\n";
 3045:     $itemcount ++;
 3046:     return $datatable;
 3047: }
 3048: 
 3049: sub textbookcourses_javascript {
 3050:     my ($settings) = @_;
 3051:     return unless(ref($settings) eq 'HASH');
 3052:     my (%ordered,%total,%jstext);
 3053:     foreach my $type ('textbooks','templates') {
 3054:         $total{$type} = 0;
 3055:         if (ref($settings->{$type}) eq 'HASH') {
 3056:             foreach my $item (keys(%{$settings->{$type}})) {
 3057:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3058:                     my $num = $settings->{$type}->{$item}{'order'};
 3059:                     $ordered{$type}{$num} = $item;
 3060:                 }
 3061:             }
 3062:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3063:         }
 3064:         my @jsarray = ();
 3065:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3066:             push(@jsarray,$ordered{$type}{$item});
 3067:         }
 3068:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3069:     }
 3070:     return <<"ENDSCRIPT";
 3071: <script type="text/javascript">
 3072: // <![CDATA[
 3073: function reorderBooks(form,item,caller) {
 3074:     var changedVal;
 3075: $jstext{'textbooks'};
 3076: $jstext{'templates'};
 3077:     var newpos;
 3078:     var maxh;
 3079:     if (caller == 'textbooks') {  
 3080:         newpos = 'textbooks_addbook_pos';
 3081:         maxh = 1 + $total{'textbooks'};
 3082:     } else {
 3083:         newpos = 'templates_addbook_pos';
 3084:         maxh = 1 + $total{'templates'};
 3085:     }
 3086:     var current = new Array;
 3087:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3088:     if (item == newpos) {
 3089:         changedVal = newitemVal;
 3090:     } else {
 3091:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3092:         current[newitemVal] = newpos;
 3093:     }
 3094:     if (caller == 'textbooks') {
 3095:         for (var i=0; i<textbooks.length; i++) {
 3096:             var elementName = 'textbooks_'+textbooks[i];
 3097:             if (elementName != item) {
 3098:                 if (form.elements[elementName]) {
 3099:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3100:                     current[currVal] = elementName;
 3101:                 }
 3102:             }
 3103:         }
 3104:     }
 3105:     if (caller == 'templates') {
 3106:         for (var i=0; i<templates.length; i++) {
 3107:             var elementName = 'templates_'+templates[i];
 3108:             if (elementName != item) {
 3109:                 if (form.elements[elementName]) {
 3110:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3111:                     current[currVal] = elementName;
 3112:                 }
 3113:             }
 3114:         }
 3115:     }
 3116:     var oldVal;
 3117:     for (var j=0; j<maxh; j++) {
 3118:         if (current[j] == undefined) {
 3119:             oldVal = j;
 3120:         }
 3121:     }
 3122:     if (oldVal < changedVal) {
 3123:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3124:            var elementName = current[k];
 3125:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3126:         }
 3127:     } else {
 3128:         for (var k=changedVal; k<oldVal; k++) {
 3129:             var elementName = current[k];
 3130:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3131:         }
 3132:     }
 3133:     return;
 3134: }
 3135: 
 3136: // ]]>
 3137: </script>
 3138: 
 3139: ENDSCRIPT
 3140: }
 3141: 
 3142: sub ltitools_javascript {
 3143:     my ($settings) = @_;
 3144:     my $togglejs = &ltitools_toggle_js();
 3145:     unless (ref($settings) eq 'HASH') {
 3146:         return $togglejs;
 3147:     }
 3148:     my (%ordered,$total,%jstext);
 3149:     $total = 0;
 3150:     foreach my $item (keys(%{$settings})) {
 3151:         if (ref($settings->{$item}) eq 'HASH') {
 3152:             my $num = $settings->{$item}{'order'};
 3153:             $ordered{$num} = $item;
 3154:         }
 3155:     }
 3156:     $total = scalar(keys(%{$settings}));
 3157:     my @jsarray = ();
 3158:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3159:         push(@jsarray,$ordered{$item});
 3160:     }
 3161:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3162:     return <<"ENDSCRIPT";
 3163: <script type="text/javascript">
 3164: // <![CDATA[
 3165: function reorderLTITools(form,item) {
 3166:     var changedVal;
 3167: $jstext
 3168:     var newpos = 'ltitools_add_pos';
 3169:     var maxh = 1 + $total;
 3170:     var current = new Array;
 3171:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3172:     if (item == newpos) {
 3173:         changedVal = newitemVal;
 3174:     } else {
 3175:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3176:         current[newitemVal] = newpos;
 3177:     }
 3178:     for (var i=0; i<ltitools.length; i++) {
 3179:         var elementName = 'ltitools_'+ltitools[i];
 3180:         if (elementName != item) {
 3181:             if (form.elements[elementName]) {
 3182:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3183:                 current[currVal] = elementName;
 3184:             }
 3185:         }
 3186:     }
 3187:     var oldVal;
 3188:     for (var j=0; j<maxh; j++) {
 3189:         if (current[j] == undefined) {
 3190:             oldVal = j;
 3191:         }
 3192:     }
 3193:     if (oldVal < changedVal) {
 3194:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3195:            var elementName = current[k];
 3196:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3197:         }
 3198:     } else {
 3199:         for (var k=changedVal; k<oldVal; k++) {
 3200:             var elementName = current[k];
 3201:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3202:         }
 3203:     }
 3204:     return;
 3205: }
 3206: 
 3207: // ]]>
 3208: </script>
 3209: 
 3210: $togglejs
 3211: 
 3212: ENDSCRIPT
 3213: }
 3214: 
 3215: sub ltitools_toggle_js {
 3216:     return <<"ENDSCRIPT";
 3217: <script type="text/javascript">
 3218: // <![CDATA[
 3219: 
 3220: function toggleLTITools(form,setting,item) {
 3221:     var radioname = '';
 3222:     var divid = '';
 3223:     if ((setting == 'passback') || (setting == 'roster')) {
 3224:         radioname = 'ltitools_'+setting+'_'+item;
 3225:         divid = 'ltitools_'+setting+'time_'+item;
 3226:         var num = form.elements[radioname].length;
 3227:         if (num) {
 3228:             var setvis = '';
 3229:             for (var i=0; i<num; i++) {
 3230:                 if (form.elements[radioname][i].checked) {
 3231:                     if (form.elements[radioname][i].value == '1') {
 3232:                         if (document.getElementById(divid)) {
 3233:                             document.getElementById(divid).style.display = 'inline-block';
 3234:                         }
 3235:                         setvis = 1;
 3236:                     }
 3237:                     break;
 3238:                 }
 3239:             }
 3240:         }
 3241:         if (!setvis) {
 3242:             if (document.getElementById(divid)) {
 3243:                 document.getElementById(divid).style.display = 'none';
 3244:             }
 3245:         }
 3246:     }
 3247:     if (setting == 'user') {
 3248:         divid = 'ltitools_'+setting+'_div_'+item;
 3249:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3250:         if (document.getElementById(divid)) {
 3251:             if (document.getElementById(checkid)) {
 3252:                 if (document.getElementById(checkid).checked) {
 3253:                     document.getElementById(divid).style.display = 'inline-block';
 3254:                 } else {
 3255:                     document.getElementById(divid).style.display = 'none';
 3256:                 }
 3257:             }
 3258:         }
 3259:     }
 3260:     return;
 3261: }
 3262: // ]]>
 3263: </script>
 3264: 
 3265: ENDSCRIPT
 3266: }
 3267: 
 3268: sub wafproxy_javascript {
 3269:     my ($dom) = @_;
 3270:     return <<"ENDSCRIPT";
 3271: <script type="text/javascript">
 3272: // <![CDATA[
 3273: function updateWAF() {
 3274:     if (document.getElementById('wafproxy_remoteip')) {
 3275:         var wafremote = 0;
 3276:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3277:             wafremote = 1;
 3278:         }
 3279:         var fields = new Array('header','trust');
 3280:         for (var i=0; i<fields.length; i++) {
 3281:             if (document.getElementById('wafproxy_'+fields[i])) {
 3282:                 if (wafremote == 1) {
 3283:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3284:                 }
 3285:                 else {
 3286:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3287:                 }
 3288:             }
 3289:         }
 3290:         if (document.getElementById('wafproxyranges_$dom')) {
 3291:             if (wafremote == 1) {
 3292:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3293:             } else {
 3294:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3295:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3296:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3297:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3298:                         }
 3299:                     }
 3300:                 }
 3301:             }
 3302:         }
 3303:     }
 3304:     return;
 3305: }
 3306: 
 3307: function checkWAF() {
 3308:     if (document.getElementById('wafproxy_remoteip')) {
 3309:         var wafvpn = 0;
 3310:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3311:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3312:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3313:                     wafvpn = 1;
 3314:                 }
 3315:                 break;
 3316:             }
 3317:         }
 3318:         var vpn = new Array('vpnint','vpnext');
 3319:         for (var i=0; i<vpn.length; i++) {
 3320:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3321:                 if (wafvpn == 1) {
 3322:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3323:                 }
 3324:                 else {
 3325:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3326:                 }
 3327:             }
 3328:         }
 3329:         if (document.getElementById('wafproxyranges_$dom')) {
 3330:             if (wafvpn == 1) {
 3331:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3332:             }
 3333:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3334:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3335:             }
 3336:         }
 3337:     }
 3338:     return;
 3339: }
 3340: 
 3341: function toggleWAF() {
 3342:     if (document.getElementById('wafproxy_table')) {
 3343:         var wafproxy = 0;
 3344:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3345:              if (document.display.wafproxy_${dom}[i].checked) {
 3346:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3347:                      wafproxy = 1;
 3348:                      break;
 3349:                 }
 3350:             }
 3351:         }
 3352:         if (wafproxy == 1) {
 3353:             document.getElementById('wafproxy_table').style.display='inline';
 3354:         }
 3355:         else {
 3356:            document.getElementById('wafproxy_table').style.display='none';
 3357:         }
 3358:         if (document.getElementById('wafproxyrow_${dom}')) {
 3359:             if (wafproxy == 1) {
 3360:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3361:             }
 3362:             else {
 3363:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3364:             }
 3365:         }
 3366:         if (document.getElementById('nowafproxyrow_$dom')) {
 3367:             if (wafproxy == 1) {
 3368:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3369:             }
 3370:             else {
 3371:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3372:             }
 3373:         }
 3374:     }
 3375:     return;
 3376: }
 3377: // ]]>
 3378: </script>
 3379: 
 3380: ENDSCRIPT
 3381: }
 3382: 
 3383: sub proctoring_javascript {
 3384:     my ($settings) = @_;
 3385:     my (%ordered,$total,%jstext);
 3386:     $total = 0;
 3387:     if (ref($settings) eq 'HASH') {
 3388:         foreach my $item (keys(%{$settings})) {
 3389:             if (ref($settings->{$item}) eq 'HASH') {
 3390:                 my $num = $settings->{$item}{'order'};
 3391:                 $ordered{$num} = $item;
 3392:             }
 3393:         }
 3394:         $total = scalar(keys(%{$settings}));
 3395:     } else {
 3396:         %ordered = (
 3397:                        0 => 'proctorio',
 3398:                        1 => 'examity',
 3399:                    );
 3400:         $total = 2; 
 3401:     }
 3402:     my @jsarray = ();
 3403:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3404:         push(@jsarray,$ordered{$item});
 3405:     }
 3406:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3407:     return <<"ENDSCRIPT";
 3408: <script type="text/javascript">
 3409: // <![CDATA[
 3410: function reorderProctoring(form,item) {
 3411:     var changedVal;
 3412: $jstext
 3413:     var maxh = $total;
 3414:     var current = new Array;
 3415:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3416:     for (var i=0; i<proctors.length; i++) {
 3417:         var elementName = 'proctoring_pos_'+proctors[i];
 3418:         if (elementName != item) {
 3419:             if (form.elements[elementName]) {
 3420:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3421:                 current[currVal] = elementName;
 3422:             }
 3423:         }
 3424:     }
 3425:     var oldVal;
 3426:     for (var j=0; j<maxh; j++) {
 3427:         if (current[j] == undefined) {
 3428:             oldVal = j;
 3429:         }
 3430:     }
 3431:     if (oldVal < changedVal) {
 3432:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3433:            var elementName = current[k];
 3434:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3435:         }
 3436:     } else {
 3437:         for (var k=changedVal; k<oldVal; k++) {
 3438:             var elementName = current[k];
 3439:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3440:         }
 3441:     }
 3442:     return;
 3443: }
 3444: 
 3445: function toggleProctoring(form,item) {
 3446:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3447:     if (fieldsets.length) {
 3448:         var radioname = 'proctoring_available_'+item;
 3449:         var num = form.elements[radioname].length;
 3450:         if (num) {
 3451:             var setvis = '';
 3452:             for (var i=0; i<num; i++) {
 3453:                 if (form.elements[radioname][i].checked) {
 3454:                     if (form.elements[radioname][i].value == '1') {
 3455:                        setvis = 1;
 3456:                        break;
 3457:                     }
 3458:                 }
 3459:             }
 3460:             for (var j=0; j<fieldsets.length; j++) {
 3461:                 if (setvis) {
 3462:                     fieldsets[j].style.display = 'block';
 3463:                 } else {
 3464:                     fieldsets[j].style.display = 'none';
 3465:                 }
 3466:             }
 3467:         }
 3468:     }
 3469:     return;
 3470: }
 3471: 
 3472: // ]]>
 3473: </script>
 3474: 
 3475: ENDSCRIPT
 3476: }
 3477: 
 3478: 
 3479: sub lti_javascript {
 3480:     my ($dom,$settings) = @_;
 3481:     my $togglejs = &lti_toggle_js($dom);
 3482:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3483:     unless (ref($settings) eq 'HASH') {
 3484:         return $togglejs.'
 3485: <script type="text/javascript">
 3486: // <![CDATA[
 3487: 
 3488: '.$linkprot_js.'
 3489: 
 3490: // ]]>
 3491: </script>
 3492: ';
 3493:     }
 3494:     my (%ordered,$total,%jstext);
 3495:     $total = scalar(keys(%{$settings}));
 3496:     foreach my $item (keys(%{$settings})) {
 3497:         if (ref($settings->{$item}) eq 'HASH') {
 3498:             my $num = $settings->{$item}{'order'};
 3499:             if ($num eq '') {
 3500:                 $num = $total - 1;
 3501:             }
 3502:             $ordered{$num} = $item;
 3503:         }
 3504:     }
 3505:     my @jsarray = ();
 3506:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3507:         push(@jsarray,$ordered{$item});
 3508:     }
 3509:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3510:     return <<"ENDSCRIPT";
 3511: <script type="text/javascript">
 3512: // <![CDATA[
 3513: function reorderLTI(form,item) {
 3514:     var changedVal;
 3515: $jstext
 3516:     var newpos = 'lti_pos_add';
 3517:     var maxh = 1 + $total;
 3518:     var current = new Array;
 3519:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3520:     if (item == newpos) {
 3521:         changedVal = newitemVal;
 3522:     } else {
 3523:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3524:         current[newitemVal] = newpos;
 3525:     }
 3526:     for (var i=0; i<lti.length; i++) {
 3527:         var elementName = 'lti_pos_'+lti[i];
 3528:         if (elementName != item) {
 3529:             if (form.elements[elementName]) {
 3530:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3531:                 current[currVal] = elementName;
 3532:             }
 3533:         }
 3534:     }
 3535:     var oldVal;
 3536:     for (var j=0; j<maxh; j++) {
 3537:         if (current[j] == undefined) {
 3538:             oldVal = j;
 3539:         }
 3540:     }
 3541:     if (oldVal < changedVal) {
 3542:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3543:            var elementName = current[k];
 3544:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3545:         }
 3546:     } else {
 3547:         for (var k=changedVal; k<oldVal; k++) {
 3548:             var elementName = current[k];
 3549:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3550:         }
 3551:     }
 3552:     return;
 3553: }
 3554: 
 3555: $linkprot_js
 3556: 
 3557: // ]]>
 3558: </script>
 3559: 
 3560: $togglejs
 3561: 
 3562: ENDSCRIPT
 3563: }
 3564: 
 3565: sub lti_toggle_js {
 3566:     my ($dom) = @_;
 3567:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3568:                             localauth => 'Local auth argument',
 3569:                             krb       => 'Kerberos domain',
 3570:                          );
 3571:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3572:     &js_escape(\$crsincalert);
 3573:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3574:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3575:     my $course_servers = "'".join("','",keys(%servers))."'";
 3576:     return <<"ENDSCRIPT";
 3577: <script type="text/javascript">
 3578: // <![CDATA[
 3579: 
 3580: function toggleLTI(form,setting,item) {
 3581:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3582:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3583:         var setvis = '';
 3584:         var radioname = 'lti_requser_'+item;
 3585:         var num = form.elements[radioname].length;
 3586:         if (num) {
 3587:             for (var i=0; i<num; i++) {
 3588:                 if (form.elements[radioname][i].checked) {
 3589:                     if (form.elements[radioname][i].value == '1') {
 3590:                         setvis = 1;
 3591:                         break;
 3592:                     }
 3593:                 }
 3594:             }
 3595:         }
 3596:         if (usrfieldsets.length) {
 3597:             for (var j=0; j<usrfieldsets.length; j++) {
 3598:                 if (setvis) {
 3599:                     usrfieldsets[j].style.display = 'block';
 3600:                 } else {
 3601:                     usrfieldsets[j].style.display = 'none';
 3602:                 }
 3603:             }
 3604:         }
 3605:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3606:         if (crsfieldsets.length) {
 3607:             radioname = 'lti_crsinc_'+item;
 3608:             var num = form.elements[radioname].length;
 3609:             if (num) {
 3610:                 var crsvis = '';
 3611:                 for (var i=0; i<num; i++) {
 3612:                     if (form.elements[radioname][i].checked) {
 3613:                         if (form.elements[radioname][i].value == '1') {
 3614:                             if (setvis == '') {
 3615:                                 if (setting == 'crsinc'){
 3616:                                     alert("$crsincalert");
 3617:                                     form.elements[radioname][0].checked = true;
 3618:                                 }
 3619:                             } else {
 3620:                                 crsvis = 1;
 3621:                             }
 3622:                             break;
 3623:                         }
 3624:                     }
 3625:                 }
 3626:                 setvis = crsvis;
 3627:             }
 3628:             for (var j=0; j<crsfieldsets.length; j++) {
 3629:                 if (setvis) {
 3630:                     crsfieldsets[j].style.display = 'block';
 3631:                 } else {
 3632:                     crsfieldsets[j].style.display = 'none';
 3633:                 }
 3634:             }
 3635:         }
 3636:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3637:         var radioname = '';
 3638:         var divid = '';
 3639:         if (setting == 'user') {
 3640:             radioname = 'lti_mapuser_'+item;
 3641:             divid = 'lti_userfield_'+item;
 3642:         } else if (setting == 'crs') {
 3643:             radioname = 'lti_mapcrs_'+item;
 3644:             divid = 'lti_crsfield_'+item;
 3645:         } else if (setting == 'callback') {
 3646:             radioname = 'lti_callback_'+item;
 3647:             divid = 'lti_callbackfield_'+item;
 3648:         } else {
 3649:             radioname = 'lti_passback_'+item;
 3650:             divid =  'lti_passback_'+item;
 3651:         }
 3652:         var num = form.elements[radioname].length;
 3653:         if (num) {
 3654:             var setvis = '';
 3655:             for (var i=0; i<num; i++) {
 3656:                if (form.elements[radioname][i].checked) {
 3657:                    if ((setting == 'passback') || (setting == 'callback')) {
 3658:                        if (form.elements[radioname][i].value == '1') {
 3659:                            if (document.getElementById(divid)) {
 3660:                                document.getElementById(divid).style.display = 'inline-block';
 3661:                            }
 3662:                            setvis = 1;
 3663:                            break;
 3664:                        }
 3665:                    } else {
 3666:                        if (form.elements[radioname][i].value == 'other') {
 3667:                            if (document.getElementById(divid)) {
 3668:                                document.getElementById(divid).style.display = 'inline-block';
 3669:                            }
 3670:                            setvis = 1;
 3671:                            break;
 3672:                        }
 3673:                    }
 3674:                }
 3675:             }
 3676:             if (!setvis) {
 3677:                 if (document.getElementById(divid)) {
 3678:                     document.getElementById(divid).style.display = 'none';
 3679:                 }
 3680:             }
 3681:         }
 3682:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3683:         var numsec = form.elements['lti_crssec_'+item].length;
 3684:         if (numsec) {
 3685:             var setvis = '';
 3686:             for (var i=0; i<numsec; i++) {
 3687:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3688:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3689:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3690:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3691:                             setvis = 1;
 3692:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3693:                             if (numsrcsec) {
 3694:                                 var setsrcvis = '';
 3695:                                 for (var j=0; j<numsrcsec; j++) {
 3696:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3697:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3698:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3699:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3700:                                                 setsrcvis = 1;
 3701:                                             }
 3702:                                         }
 3703:                                     }
 3704:                                 }
 3705:                                 if (!setsrcvis) {
 3706:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3707:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3708:                                     }
 3709:                                 }
 3710:                             }
 3711:                         }
 3712:                     }
 3713:                 }
 3714:             }
 3715:             if (!setvis) {
 3716:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3717:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3718:                 }
 3719:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3720:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3721:                 }
 3722:             }
 3723:         }
 3724:     } else if (setting == 'lcauth') {
 3725:         var numauth = form.elements['lti_lcauth_'+item].length;
 3726:         if (numauth) {
 3727:             for (var i=0; i<numauth; i++) {
 3728:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3729:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3730:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3731:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3732:                         } else {
 3733:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3734:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3735:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3736:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3737:                                 } else {
 3738:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3739:                                 }
 3740:                             }
 3741:                         }
 3742:                     }
 3743:                 }
 3744:             }
 3745:         }
 3746:     } else if (setting == 'lcmenu') {
 3747:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3748:         var divid = 'lti_menufield_'+item;
 3749:         var setvis = '';
 3750:         for (var i=0; i<menus.length; i++) {
 3751:             var radioname = menus[i];
 3752:             var num = form.elements[radioname].length;
 3753:             if (num) {
 3754:                 for (var j=0; j<num; j++) {
 3755:                     if (form.elements[radioname][j].checked) {
 3756:                         if (form.elements[radioname][j].value == '1') {
 3757:                             if (document.getElementById(divid)) {
 3758:                                 document.getElementById(divid).style.display = 'inline-block';
 3759:                             }
 3760:                             setvis = 1;
 3761:                             break;
 3762:                         }
 3763:                     }
 3764:                 }
 3765:             }
 3766:             if (setvis == 1) {
 3767:                 break;
 3768:             }
 3769:         }
 3770:         if (!setvis) {
 3771:             if (document.getElementById(divid)) {
 3772:                 document.getElementById(divid).style.display = 'none';
 3773:             }
 3774:         }
 3775:     }
 3776:     return;
 3777: }
 3778: 
 3779: // ]]>
 3780: </script>
 3781: 
 3782: ENDSCRIPT
 3783: }
 3784: 
 3785: sub autoupdate_javascript {
 3786:     return <<"ENDSCRIPT";
 3787: <script type="text/javascript">
 3788: // <![CDATA[
 3789: function toggleLastActiveDays(form) {
 3790:     var radioname = 'lastactive';
 3791:     var divid = 'lastactive_div';
 3792:     var num = form.elements[radioname].length;
 3793:     if (num) {
 3794:         var setvis = '';
 3795:         for (var i=0; i<num; i++) {
 3796:             if (form.elements[radioname][i].checked) {
 3797:                 if (form.elements[radioname][i].value == '1') {
 3798:                     if (document.getElementById(divid)) {
 3799:                         document.getElementById(divid).style.display = 'inline-block';
 3800:                     }
 3801:                     setvis = 1;
 3802:                 }
 3803:                 break;
 3804:             }
 3805:         }
 3806:         if (!setvis) {
 3807:             if (document.getElementById(divid)) {
 3808:                 document.getElementById(divid).style.display = 'none';
 3809:             }
 3810:         }
 3811:     }
 3812:     return;
 3813: }
 3814: // ]]>
 3815: </script>
 3816: 
 3817: ENDSCRIPT
 3818: }
 3819: 
 3820: sub autoenroll_javascript {
 3821:     return <<"ENDSCRIPT";
 3822: <script type="text/javascript">
 3823: // <![CDATA[
 3824: function toggleFailsafe(form) {
 3825:     var radioname = 'autoenroll_failsafe';
 3826:     var divid = 'autoenroll_failsafe_div';
 3827:     var num = form.elements[radioname].length;
 3828:     if (num) {
 3829:         var setvis = '';
 3830:         for (var i=0; i<num; i++) {
 3831:             if (form.elements[radioname][i].checked) {
 3832:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3833:                     if (document.getElementById(divid)) {
 3834:                         document.getElementById(divid).style.display = 'inline-block';
 3835:                     }
 3836:                     setvis = 1;
 3837:                 }
 3838:                 break;
 3839:             }
 3840:         }
 3841:         if (!setvis) {
 3842:             if (document.getElementById(divid)) {
 3843:                 document.getElementById(divid).style.display = 'none';
 3844:             }
 3845:         }
 3846:     }
 3847:     return;
 3848: }
 3849: // ]]>
 3850: </script>
 3851: 
 3852: ENDSCRIPT
 3853: }
 3854: 
 3855: sub saml_javascript {
 3856:     return <<"ENDSCRIPT";
 3857: <script type="text/javascript">
 3858: // <![CDATA[
 3859: function toggleSamlOptions(form,hostid) {
 3860:     var radioname = 'saml_'+hostid;
 3861:     var tablecellon = 'samloptionson_'+hostid;
 3862:     var tablecelloff = 'samloptionsoff_'+hostid;
 3863:     var num = form.elements[radioname].length;
 3864:     if (num) {
 3865:         var setvis = '';
 3866:         for (var i=0; i<num; i++) {
 3867:             if (form.elements[radioname][i].checked) {
 3868:                 if (form.elements[radioname][i].value == '1') {
 3869:                     if (document.getElementById(tablecellon)) {
 3870:                         document.getElementById(tablecellon).style.display='';
 3871:                     }
 3872:                     if (document.getElementById(tablecelloff)) {
 3873:                         document.getElementById(tablecelloff).style.display='none';
 3874:                     }
 3875:                     setvis = 1;
 3876:                 }
 3877:                 break;
 3878:             }
 3879:         }
 3880:         if (!setvis) {
 3881:             if (document.getElementById(tablecellon)) {
 3882:                 document.getElementById(tablecellon).style.display='none';
 3883:             }
 3884:             if (document.getElementById(tablecelloff)) {
 3885:                 document.getElementById(tablecelloff).style.display='';
 3886:             }
 3887:         }
 3888:     }
 3889:     return;
 3890: }
 3891: // ]]>
 3892: </script>
 3893: 
 3894: ENDSCRIPT
 3895: }
 3896: 
 3897: sub ipaccess_javascript {
 3898:     my ($settings) = @_;
 3899:     my (%ordered,$total,%jstext);
 3900:     $total = 0;
 3901:     if (ref($settings) eq 'HASH') {
 3902:         foreach my $item (keys(%{$settings})) {
 3903:             if (ref($settings->{$item}) eq 'HASH') {
 3904:                 my $num = $settings->{$item}{'order'};
 3905:                 $ordered{$num} = $item;
 3906:             }
 3907:         }
 3908:         $total = scalar(keys(%{$settings}));
 3909:     }
 3910:     my @jsarray = ();
 3911:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3912:         push(@jsarray,$ordered{$item});
 3913:     }
 3914:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3915:     return <<"ENDSCRIPT";
 3916: <script type="text/javascript">
 3917: // <![CDATA[
 3918: function reorderIPaccess(form,item) {
 3919:     var changedVal;
 3920: $jstext
 3921:     var newpos = 'ipaccess_pos_add';
 3922:     var maxh = 1 + $total;
 3923:     var current = new Array;
 3924:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3925:     if (item == newpos) {
 3926:         changedVal = newitemVal;
 3927:     } else {
 3928:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3929:         current[newitemVal] = newpos;
 3930:     }
 3931:     for (var i=0; i<ipaccess.length; i++) {
 3932:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3933:         if (elementName != item) {
 3934:             if (form.elements[elementName]) {
 3935:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3936:                 current[currVal] = elementName;
 3937:             }
 3938:         }
 3939:     }
 3940:     var oldVal;
 3941:     for (var j=0; j<maxh; j++) {
 3942:         if (current[j] == undefined) {
 3943:             oldVal = j;
 3944:         }
 3945:     }
 3946:     if (oldVal < changedVal) {
 3947:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3948:            var elementName = current[k];
 3949:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3950:         }
 3951:     } else {
 3952:         for (var k=changedVal; k<oldVal; k++) {
 3953:             var elementName = current[k];
 3954:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3955:         }
 3956:     }
 3957:     return;
 3958: }
 3959: // ]]>
 3960: </script>
 3961: 
 3962: ENDSCRIPT
 3963: }
 3964: 
 3965: sub authordefaults_javascript {
 3966:     my %alert = &Apache::lonlocal::texthash (
 3967:                     reqd => 'Warning: at least one editor needs to be available.',
 3968:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3969:     );
 3970:     &js_escape(\%alert);
 3971:     return <<"ENDSCRIPT";
 3972: <script type="text/javascript">
 3973: // <![CDATA[
 3974: 
 3975: function checkEditors(form,checkbox,current) {
 3976:     if (form.elements[checkbox].length != undefined) {
 3977:         var count = 0;
 3978:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3979:             if (form.elements[checkbox][i].checked) {
 3980:                 count ++;
 3981:             }
 3982:         }
 3983:         if (count == 0) {
 3984:             if (current.type =='radio') {
 3985:                 current.checked = true;
 3986:                 alert('$alert{reqd}\\n$alert{rest}');
 3987:             }
 3988:         }
 3989:     }
 3990:     return;
 3991: }
 3992: // ]]>
 3993: </script>
 3994: 
 3995: ENDSCRIPT
 3996: }
 3997: 
 3998: sub print_autoenroll {
 3999:     my ($dom,$settings,$rowtotal) = @_;
 4000:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 4001:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 4002:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 4003:     $failsafesty = 'none';
 4004:     %failsafechecked = (
 4005:         off => ' checked="checked"',
 4006:     );
 4007:     if (ref($settings) eq 'HASH') {
 4008:         if (exists($settings->{'run'})) {
 4009:             if ($settings->{'run'} eq '0') {
 4010:                 $runoff = ' checked="checked" ';
 4011:                 $runon = ' ';
 4012:             } else {
 4013:                 $runon = ' checked="checked" ';
 4014:                 $runoff = ' ';
 4015:             }
 4016:         } else {
 4017:             if ($autorun) {
 4018:                 $runon = ' checked="checked" ';
 4019:                 $runoff = ' ';
 4020:             } else {
 4021:                 $runoff = ' checked="checked" ';
 4022:                 $runon = ' ';
 4023:             }
 4024:         }
 4025:         if (exists($settings->{'co-owners'})) {
 4026:             if ($settings->{'co-owners'} eq '0') {
 4027:                 $coownersoff = ' checked="checked" ';
 4028:                 $coownerson = ' ';
 4029:             } else {
 4030:                 $coownerson = ' checked="checked" ';
 4031:                 $coownersoff = ' ';
 4032:             }
 4033:         } else {
 4034:             $coownersoff = ' checked="checked" ';
 4035:             $coownerson = ' ';
 4036:         }
 4037:         if (exists($settings->{'sender_domain'})) {
 4038:             $defdom = $settings->{'sender_domain'};
 4039:         }
 4040:         if (exists($settings->{'failsafe'})) {
 4041:             $failsafe = $settings->{'failsafe'};
 4042:             if ($failsafe eq 'zero') {
 4043:                 $failsafechecked{'zero'} = ' checked="checked"';
 4044:                 $failsafechecked{'off'} = '';
 4045:                 $failsafesty = 'inline-block';
 4046:             } elsif ($failsafe eq 'any') {
 4047:                 $failsafechecked{'any'} = ' checked="checked"';
 4048:                 $failsafechecked{'off'} = '';
 4049:             }
 4050:             $autofailsafe = $settings->{'autofailsafe'};
 4051:         } elsif (exists($settings->{'autofailsafe'})) {
 4052:             $autofailsafe = $settings->{'autofailsafe'};
 4053:             if ($autofailsafe ne '') {
 4054:                 $failsafechecked{'zero'} = ' checked="checked"';
 4055:                 $failsafe = 'zero';
 4056:                 $failsafechecked{'off'} = '';
 4057:             }
 4058:         }
 4059:     } else {
 4060:         if ($autorun) {
 4061:             $runon = ' checked="checked" ';
 4062:             $runoff = ' ';
 4063:         } else {
 4064:             $runoff = ' checked="checked" ';
 4065:             $runon = ' ';
 4066:         }
 4067:     }
 4068:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4069:     my $notif_sender;
 4070:     if (ref($settings) eq 'HASH') {
 4071:         $notif_sender = $settings->{'sender_uname'};
 4072:     }
 4073:     my $datatable='<tr class="LC_odd_row">'.
 4074:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4075:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4076:                   '<input type="radio" name="autoenroll_run"'.
 4077:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4078:                   '<label><input type="radio" name="autoenroll_run"'.
 4079:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4080:                   '</tr><tr>'.
 4081:                   '<td>'.&mt('Notification messages - sender').
 4082:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4083:                   &mt('username').':&nbsp;'.
 4084:                   '<input type="text" name="sender_uname" value="'.
 4085:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4086:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4087:                   '<tr class="LC_odd_row">'.
 4088:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4089:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4090:                   '<input type="radio" name="autoassign_coowners"'.
 4091:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4092:                   '<label><input type="radio" name="autoassign_coowners"'.
 4093:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4094:                   '</tr><tr>'.
 4095:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4096:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4097:                   '<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; '.
 4098:                   '<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 />'.
 4099:                   '<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>'.
 4100:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4101:                   '<span class="LC_nobreak">'.
 4102:                   &mt('Threshold for number of students in section to drop: [_1]',
 4103:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4104:                   '</span></div></td></tr>';
 4105:     $$rowtotal += 4;
 4106:     return $datatable;
 4107: }
 4108: 
 4109: sub print_autoupdate {
 4110:     my ($position,$dom,$settings,$rowtotal) = @_;
 4111:     my ($enable,$datatable);
 4112:     if ($position eq 'top') {
 4113:         my %choices = &Apache::lonlocal::texthash (
 4114:                           run        => 'Auto-update active?',
 4115:                           classlists => 'Update information in classlists?',
 4116:                           unexpired  => 'Skip updates for users without active or future roles?',
 4117:                           lastactive => 'Skip updates for inactive users?',
 4118:         );
 4119:         my $itemcount = 0;
 4120:         my $updateon = ' ';
 4121:         my $updateoff = ' checked="checked" ';
 4122:         if (ref($settings) eq 'HASH') {
 4123:             if ($settings->{'run'} eq '1') {
 4124:                 $updateon = $updateoff;
 4125:                 $updateoff = ' ';
 4126:             }
 4127:         }
 4128:         $enable = '<tr class="LC_odd_row">'.
 4129:                   '<td>'.$choices{'run'}.'</td>'.
 4130:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4131:                   '<input type="radio" name="autoupdate_run"'.
 4132:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4133:                   '<label><input type="radio" name="autoupdate_run"'.
 4134:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4135:                   '</tr>';
 4136:         my @toggles = ('classlists','unexpired');
 4137:         my %defaultchecked = ('classlists' => 'off',
 4138:                               'unexpired'  => 'off'
 4139:                               );
 4140:         $$rowtotal ++;
 4141:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4142:                                                      \%choices,$itemcount,'','','left','no');
 4143:         $datatable = $enable.$datatable;
 4144:         $$rowtotal += $itemcount;
 4145:         my $lastactiveon = ' ';
 4146:         my $lastactiveoff = ' checked="checked" ';
 4147:         my $lastactivestyle = 'none';
 4148:         my $lastactivedays;
 4149:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4150:         if (ref($settings) eq 'HASH') {
 4151:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4152:                 $lastactiveon = $lastactiveoff;
 4153:                 $lastactiveoff = ' ';
 4154:                 $lastactivestyle = 'inline-block';
 4155:                 $lastactivedays = $settings->{'lastactive'};
 4156:             }
 4157:         }
 4158:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4159:         $datatable .= '<tr'.$css_class.'>'.
 4160:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4161:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4162:                       '<input type="radio" name="lastactive"'.
 4163:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4164:                       '&nbsp;<label>'.
 4165:                       '<input type="radio" name="lastactive"'.
 4166:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4167:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4168:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4169:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4170:                           $lastactivedays.'" />').
 4171:                       '</span></td>'.
 4172:                       '</tr>';
 4173:         $$rowtotal ++;
 4174:     } elsif ($position eq 'middle') {
 4175:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4176:         my $numinrow = 3;
 4177:         my $locknamesettings;
 4178:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4179:                                      $dom,$numinrow,$othertitle,
 4180:                                     'lockablenames',$rowtotal);
 4181:         $$rowtotal ++;
 4182:     } else {
 4183:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4184:         my @fields = ('lastname','firstname','middlename','generation',
 4185:                       'permanentemail','id');
 4186:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4187:         my $numrows = 0;
 4188:         if (ref($types) eq 'ARRAY') {
 4189:             if (@{$types} > 0) {
 4190:                 $datatable = 
 4191:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4192:                                          \@fields,$types,\$numrows);
 4193:                     $$rowtotal += @{$types}; 
 4194:             }
 4195:         }
 4196:         $datatable .= 
 4197:             &usertype_update_row($settings,{'default' => $othertitle},
 4198:                                  \%fieldtitles,\@fields,['default'],
 4199:                                  \$numrows);
 4200:         $$rowtotal ++;     
 4201:     }
 4202:     return $datatable;
 4203: }
 4204: 
 4205: sub print_autocreate {
 4206:     my ($dom,$settings,$rowtotal) = @_;
 4207:     my (%createon,%createoff,%currhash);
 4208:     my @types = ('xml','req');
 4209:     if (ref($settings) eq 'HASH') {
 4210:         foreach my $item (@types) {
 4211:             $createoff{$item} = ' checked="checked" ';
 4212:             $createon{$item} = ' ';
 4213:             if (exists($settings->{$item})) {
 4214:                 if ($settings->{$item}) {
 4215:                     $createon{$item} = ' checked="checked" ';
 4216:                     $createoff{$item} = ' ';
 4217:                 }
 4218:             }
 4219:         }
 4220:         if ($settings->{'xmldc'} ne '') {
 4221:             $currhash{$settings->{'xmldc'}} = 1;
 4222:         }
 4223:     } else {
 4224:         foreach my $item (@types) {
 4225:             $createoff{$item} = ' checked="checked" ';
 4226:             $createon{$item} = ' ';
 4227:         }
 4228:     }
 4229:     $$rowtotal += 2;
 4230:     my $numinrow = 2;
 4231:     my $datatable='<tr class="LC_odd_row">'.
 4232:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4233:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4234:                   '<input type="radio" name="autocreate_xml"'.
 4235:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4236:                   '<label><input type="radio" name="autocreate_xml"'.
 4237:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4238:                   '</td></tr><tr>'.
 4239:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4240:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4241:                   '<input type="radio" name="autocreate_req"'.
 4242:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4243:                   '<label><input type="radio" name="autocreate_req"'.
 4244:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4245:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4246:                                                    'autocreate_xmldc',%currhash);
 4247:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4248:     if ($numdc > 1) {
 4249:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4250:                       '</td><td class="LC_left_item">';
 4251:     } else {
 4252:         $datatable .= &mt('Course creation processed as:').
 4253:                       '</td><td class="LC_right_item">';
 4254:     }
 4255:     $datatable .= $dctable.'</td></tr>';
 4256:     $$rowtotal += $rows;
 4257:     return $datatable;
 4258: }
 4259: 
 4260: sub print_directorysrch {
 4261:     my ($position,$dom,$settings,$rowtotal) = @_;
 4262:     my $datatable;
 4263:     if ($position eq 'top') {
 4264:         my $instsrchon = ' ';
 4265:         my $instsrchoff = ' checked="checked" ';
 4266:         my ($exacton,$containson,$beginson);
 4267:         my $instlocalon = ' ';
 4268:         my $instlocaloff = ' checked="checked" ';
 4269:         if (ref($settings) eq 'HASH') {
 4270:             if ($settings->{'available'} eq '1') {
 4271:                 $instsrchon = $instsrchoff;
 4272:                 $instsrchoff = ' ';
 4273:             }
 4274:             if ($settings->{'localonly'} eq '1') {
 4275:                 $instlocalon = $instlocaloff;
 4276:                 $instlocaloff = ' ';
 4277:             }
 4278:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4279:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4280:                     if ($type eq 'exact') {
 4281:                         $exacton = ' checked="checked" ';
 4282:                     } elsif ($type eq 'contains') {
 4283:                         $containson = ' checked="checked" ';
 4284:                     } elsif ($type eq 'begins') {
 4285:                         $beginson = ' checked="checked" ';
 4286:                     }
 4287:                 }
 4288:             } else {
 4289:                 if ($settings->{'searchtypes'} eq 'exact') {
 4290:                     $exacton = ' checked="checked" ';
 4291:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4292:                     $containson = ' checked="checked" ';
 4293:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4294:                     $exacton = ' checked="checked" ';
 4295:                     $containson = ' checked="checked" ';
 4296:                 }
 4297:             }
 4298:         }
 4299:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4300:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4301: 
 4302:         my $numinrow = 4;
 4303:         my $cansrchrow = 0;
 4304:         $datatable='<tr class="LC_odd_row">'.
 4305:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4306:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4307:                    '<input type="radio" name="dirsrch_available"'.
 4308:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4309:                    '<label><input type="radio" name="dirsrch_available"'.
 4310:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4311:                    '</tr><tr>'.
 4312:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4313:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4314:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4315:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4316:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4317:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4318:                    '</tr>';
 4319:         $$rowtotal += 2;
 4320:         if (ref($usertypes) eq 'HASH') {
 4321:             if (keys(%{$usertypes}) > 0) {
 4322:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4323:                                              $numinrow,$othertitle,'cansearch',
 4324:                                              $rowtotal);
 4325:                 $cansrchrow = 1;
 4326:             }
 4327:         }
 4328:         if ($cansrchrow) {
 4329:             $$rowtotal ++;
 4330:             $datatable .= '<tr>';
 4331:         } else {
 4332:             $datatable .= '<tr class="LC_odd_row">';
 4333:         }
 4334:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4335:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4336:         foreach my $title (@{$titleorder}) {
 4337:             if (defined($searchtitles->{$title})) {
 4338:                 my $check = ' ';
 4339:                 if (ref($settings) eq 'HASH') {
 4340:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4341:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4342:                             $check = ' checked="checked" ';
 4343:                         }
 4344:                     }
 4345:                 }
 4346:                 $datatable .= '<td class="LC_left_item">'.
 4347:                               '<span class="LC_nobreak"><label>'.
 4348:                               '<input type="checkbox" name="searchby" '.
 4349:                               'value="'.$title.'"'.$check.'/>'.
 4350:                               $searchtitles->{$title}.'</label></span></td>';
 4351:             }
 4352:         }
 4353:         $datatable .= '</tr></table></td></tr>';
 4354:         $$rowtotal ++;
 4355:         if ($cansrchrow) {
 4356:             $datatable .= '<tr class="LC_odd_row">';
 4357:         } else {
 4358:             $datatable .= '<tr>';
 4359:         }
 4360:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4361:                       '<td class="LC_left_item" colspan="2">'.
 4362:                       '<span class="LC_nobreak"><label>'.
 4363:                       '<input type="checkbox" name="searchtypes" '.
 4364:                       $exacton.' value="exact" />'.&mt('Exact match').
 4365:                       '</label>&nbsp;'.
 4366:                       '<label><input type="checkbox" name="searchtypes" '.
 4367:                       $beginson.' value="begins" />'.&mt('Begins with').
 4368:                       '</label>&nbsp;'.
 4369:                       '<label><input type="checkbox" name="searchtypes" '.
 4370:                       $containson.' value="contains" />'.&mt('Contains').
 4371:                       '</label></span></td></tr>';
 4372:         $$rowtotal ++;
 4373:     } else {
 4374:         my $domsrchon = ' checked="checked" ';
 4375:         my $domsrchoff = ' ';
 4376:         my $domlocalon = ' ';
 4377:         my $domlocaloff = ' checked="checked" ';
 4378:         if (ref($settings) eq 'HASH') {
 4379:             if ($settings->{'lclocalonly'} eq '1') {
 4380:                 $domlocalon = $domlocaloff;
 4381:                 $domlocaloff = ' ';
 4382:             }
 4383:             if ($settings->{'lcavailable'} eq '0') {
 4384:                 $domsrchoff = $domsrchon;
 4385:                 $domsrchon = ' ';
 4386:             }
 4387:         }
 4388:         $datatable='<tr class="LC_odd_row">'.
 4389:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4390:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4391:                       '<input type="radio" name="dirsrch_domavailable"'.
 4392:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4393:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4394:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4395:                       '</tr><tr>'.
 4396:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4397:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4398:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4399:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4400:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4401:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4402:                       '</tr>';
 4403:         $$rowtotal += 2;
 4404:     }
 4405:     return $datatable;
 4406: }
 4407: 
 4408: sub print_contacts {
 4409:     my ($position,$dom,$settings,$rowtotal) = @_;
 4410:     my $datatable;
 4411:     my @contacts = ('adminemail','supportemail');
 4412:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4413:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4414:     if ($position eq 'top') {
 4415:         if (ref($settings) eq 'HASH') {
 4416:             foreach my $item (@contacts) {
 4417:                 if (exists($settings->{$item})) {
 4418:                     $to{$item} = $settings->{$item};
 4419:                 }
 4420:             }
 4421:         }
 4422:     } elsif ($position eq 'middle') {
 4423:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4424:                      'updatesmail','idconflictsmail','hostipmail');
 4425:         foreach my $type (@mailings) {
 4426:             $otheremails{$type} = '';
 4427:         }
 4428:     } elsif ($position eq 'lower') {
 4429:         if (ref($settings) eq 'HASH') {
 4430:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4431:                 %lonstatus = %{$settings->{'lonstatus'}};
 4432:             }
 4433:         }
 4434:     } else {
 4435:         @mailings = ('helpdeskmail','otherdomsmail');
 4436:         foreach my $type (@mailings) {
 4437:             $otheremails{$type} = '';
 4438:         }
 4439:         $bccemails{'helpdeskmail'} = '';
 4440:         $bccemails{'otherdomsmail'} = '';
 4441:         $includestr{'helpdeskmail'} = '';
 4442:         $includestr{'otherdomsmail'} = '';
 4443:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4444:     }
 4445:     if (ref($settings) eq 'HASH') {
 4446:         unless (($position eq 'top') || ($position eq 'lower')) {
 4447:             foreach my $type (@mailings) {
 4448:                 if (exists($settings->{$type})) {
 4449:                     if (ref($settings->{$type}) eq 'HASH') {
 4450:                         foreach my $item (@contacts) {
 4451:                             if ($settings->{$type}{$item}) {
 4452:                                 $checked{$type}{$item} = ' checked="checked" ';
 4453:                             }
 4454:                         }
 4455:                         $otheremails{$type} = $settings->{$type}{'others'};
 4456:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4457:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4458:                             if ($settings->{$type}{'include'} ne '') {
 4459:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4460:                                 $includestr{$type} = &unescape($includestr{$type});
 4461:                             }
 4462:                         }
 4463:                     }
 4464:                 } elsif ($type eq 'lonstatusmail') {
 4465:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4466:                 }
 4467:             }
 4468:         }
 4469:         if ($position eq 'bottom') {
 4470:             foreach my $type (@mailings) {
 4471:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4472:                 if ($settings->{$type}{'include'} ne '') {
 4473:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4474:                     $includestr{$type} = &unescape($includestr{$type});
 4475:                 }
 4476:             }
 4477:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4478:                 if (ref($fields) eq 'ARRAY') {
 4479:                     foreach my $field (@{$fields}) {
 4480:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4481:                     }
 4482:                 }
 4483:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4484:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4485:                 } else {
 4486:                     $maxsize = '1.0';
 4487:                 }
 4488:             } else {
 4489:                 if (ref($fields) eq 'ARRAY') {
 4490:                     foreach my $field (@{$fields}) {
 4491:                         $currfield{$field} = 'yes';
 4492:                     }
 4493:                 }
 4494:                 $maxsize = '1.0';
 4495:             }
 4496:         }
 4497:     } else {
 4498:         if ($position eq 'top') {
 4499:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4500:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4501:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4502:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4503:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4504:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4505:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4506:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4507:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4508:         } elsif ($position eq 'bottom') {
 4509:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4510:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4511:             if (ref($fields) eq 'ARRAY') {
 4512:                 foreach my $field (@{$fields}) {
 4513:                     $currfield{$field} = 'yes';
 4514:                 }
 4515:             }
 4516:             $maxsize = '1.0';
 4517:         }
 4518:     }
 4519:     my ($titles,$short_titles) = &contact_titles();
 4520:     my $rownum = 0;
 4521:     my $css_class;
 4522:     if ($position eq 'top') {
 4523:         foreach my $item (@contacts) {
 4524:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4525:             $datatable .= '<tr'.$css_class.'>'. 
 4526:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4527:                           '</span></td><td class="LC_right_item">'.
 4528:                           '<input type="text" name="'.$item.'" value="'.
 4529:                           $to{$item}.'" /></td></tr>';
 4530:             $rownum ++;
 4531:         }
 4532:     } elsif ($position eq 'bottom') {
 4533:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4534:         $datatable .= '<tr'.$css_class.'>'.
 4535:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4536:                       &mt('(e-mail, subject, and description always shown)').
 4537:                       '</td><td class="LC_left_item">';
 4538:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4539:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4540:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4541:             foreach my $field (@{$fields}) {
 4542:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4543:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4544:                     $datatable .= ' '.&mt('(logged-in users)');
 4545:                 }
 4546:                 $datatable .='</td><td>';
 4547:                 my $clickaction;
 4548:                 if ($field eq 'screenshot') {
 4549:                     $clickaction = ' onclick="screenshotSize(this);"';
 4550:                 }
 4551:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4552:                     foreach my $option (@{$possoptions->{$field}}) {
 4553:                         my $checked;
 4554:                         if ($currfield{$field} eq $option) {
 4555:                             $checked = ' checked="checked"';
 4556:                         }
 4557:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4558:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4559:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4560:                                       '</label></span>'.('&nbsp;'x2);
 4561:                     }
 4562:                 }
 4563:                 if ($field eq 'screenshot') {
 4564:                     my $display;
 4565:                     if ($currfield{$field} eq 'no') {
 4566:                         $display = ' style="display:none"';
 4567:                     }
 4568:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4569:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4570:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4571:                 }
 4572:                 $datatable .= '</td></tr>';
 4573:             }
 4574:             $datatable .= '</table>';
 4575:         }
 4576:         $datatable .= '</td></tr>'."\n";
 4577:         $rownum ++;
 4578:     }
 4579:     unless (($position eq 'top') || ($position eq 'lower')) {
 4580:         foreach my $type (@mailings) {
 4581:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4582:             $datatable .= '<tr'.$css_class.'>'.
 4583:                           '<td><span class="LC_nobreak">'.
 4584:                           $titles->{$type}.': </span></td>'.
 4585:                           '<td class="LC_left_item">';
 4586:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4587:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4588:             }
 4589:             $datatable .= '<span class="LC_nobreak">';
 4590:             foreach my $item (@contacts) {
 4591:                 $datatable .= '<label>'.
 4592:                               '<input type="checkbox" name="'.$type.'"'.
 4593:                               $checked{$type}{$item}.
 4594:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4595:                               '</label>&nbsp;';
 4596:             }
 4597:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4598:                           '<input type="text" name="'.$type.'_others" '.
 4599:                           'value="'.$otheremails{$type}.'"  />';
 4600:             my %locchecked;
 4601:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4602:                 foreach my $loc ('s','b') {
 4603:                     if ($includeloc{$type} eq $loc) {
 4604:                         $locchecked{$loc} = ' checked="checked"';
 4605:                         last;
 4606:                     }
 4607:                 }
 4608:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4609:                               '<input type="text" name="'.$type.'_bcc" '.
 4610:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4611:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4612:                               &mt('Text automatically added to e-mail:').' '.
 4613:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4614:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4615:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4616:                               ('&nbsp;'x2).
 4617:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4618:                               '</span></fieldset>';
 4619:             }
 4620:             $datatable .= '</td></tr>'."\n";
 4621:             $rownum ++;
 4622:         }
 4623:     }
 4624:     if ($position eq 'middle') {
 4625:         my %choices;
 4626:         my $corelink = &core_link_msu();
 4627:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4628:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4629:                                         $corelink);
 4630:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4631:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4632:         my %defaultchecked = ('reporterrors'  => 'on',
 4633:                               'reportupdates' => 'on',
 4634:                               'reportstatus'  => 'on');
 4635:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4636:                                                    \%choices,$rownum);
 4637:         $datatable .= $reports;
 4638:     } elsif ($position eq 'lower') {
 4639:         my (%current,%excluded,%weights);
 4640:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4641:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4642:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4643:         } else {
 4644:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4645:         }
 4646:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4647:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4648:         } else {
 4649:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4650:         }
 4651:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4652:             foreach my $type ('E','W','N','U') {
 4653:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4654:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4655:                 } else {
 4656:                     $weights{$type} = $defaults->{$type};
 4657:                 }
 4658:             }
 4659:         } else {
 4660:             foreach my $type ('E','W','N','U') {
 4661:                 $weights{$type} = $defaults->{$type};
 4662:             }
 4663:         }
 4664:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4665:             if (@{$lonstatus{'excluded'}} > 0) {
 4666:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4667:             }
 4668:         }
 4669:         foreach my $item ('errorthreshold','errorsysmail') {
 4670:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4671:             $datatable .= '<tr'.$css_class.'>'.
 4672:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4673:                           $titles->{$item}.
 4674:                           '</span></td><td class="LC_left_item">'.
 4675:                           '<input type="text" name="'.$item.'" value="'.
 4676:                           $current{$item}.'" size="5" /></td></tr>';
 4677:             $rownum ++;
 4678:         }
 4679:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4680:         $datatable .= '<tr'.$css_class.'>'.
 4681:                       '<td class="LC_left_item">'.
 4682:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4683:                       '</span></td><td class="LC_left_item"><table><tr>';
 4684:         foreach my $type ('E','W','N','U') {
 4685:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4686:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4687:                           $weights{$type}.'" size="5" /></td>';
 4688:         }
 4689:         $datatable .= '</tr></table></tr>';
 4690:         $rownum ++;
 4691:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4692:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4693:                       $titles->{'errorexcluded'}.'</td>'.
 4694:                       '<td class="LC_left_item"><table>';
 4695:         my $numinrow = 4;
 4696:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4697:         for (my $i=0; $i<@ids; $i++) {
 4698:             my $rem = $i%($numinrow);
 4699:             if ($rem == 0) {
 4700:                 if ($i > 0) {
 4701:                     $datatable .= '</tr>';
 4702:                 }
 4703:                 $datatable .= '<tr>';
 4704:             }
 4705:             my $check;
 4706:             if ($excluded{$ids[$i]}) {
 4707:                 $check = ' checked="checked" ';
 4708:             }
 4709:             $datatable .= '<td class="LC_left_item">'.
 4710:                           '<span class="LC_nobreak"><label>'.
 4711:                           '<input type="checkbox" name="errorexcluded" '.
 4712:                           'value="'.$ids[$i].'"'.$check.' />'.
 4713:                           $ids[$i].'</label></span></td>';
 4714:         }
 4715:         my $colsleft = $numinrow - @ids%($numinrow);
 4716:         if ($colsleft > 1 ) {
 4717:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4718:                           '&nbsp;</td>';
 4719:         } elsif ($colsleft == 1) {
 4720:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4721:         }
 4722:         $datatable .= '</tr></table></td></tr>';
 4723:         $rownum ++;
 4724:     } elsif ($position eq 'bottom') {
 4725:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4726:         my (@posstypes,%usertypeshash);
 4727:         if (ref($types) eq 'ARRAY') {
 4728:             @posstypes = @{$types};
 4729:         }
 4730:         if (@posstypes) {
 4731:             if (ref($usertypes) eq 'HASH') {
 4732:                 %usertypeshash = %{$usertypes};
 4733:             }
 4734:             my @overridden;
 4735:             my $numinrow = 4;
 4736:             if (ref($settings) eq 'HASH') {
 4737:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4738:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4739:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4740:                             push(@overridden,$key);
 4741:                             foreach my $item (@contacts) {
 4742:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4743:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4744:                                 }
 4745:                             }
 4746:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4747:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4748:                             $includeloc{'override_'.$key} = '';
 4749:                             $includestr{'override_'.$key} = '';
 4750:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4751:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4752:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4753:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4754:                             }
 4755:                         }
 4756:                     }
 4757:                 }
 4758:             }
 4759:             my $customclass = 'LC_helpdesk_override';
 4760:             my $optionsprefix = 'LC_options_helpdesk_';
 4761: 
 4762:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4763:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4764:                                          $numinrow,$othertitle,'overrides',
 4765:                                          \$rownum,$onclicktypes,$customclass);
 4766:             $rownum ++;
 4767:             $usertypeshash{'default'} = $othertitle;
 4768:             foreach my $status (@posstypes) {
 4769:                 my $css_class;
 4770:                 if ($rownum%2) {
 4771:                     $css_class = 'LC_odd_row ';
 4772:                 }
 4773:                 $css_class .= $customclass;
 4774:                 my $rowid = $optionsprefix.$status;
 4775:                 my $hidden = 1;
 4776:                 my $currstyle = 'display:none';
 4777:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4778:                     $currstyle = 'display:table-row';
 4779:                     $hidden = 0;
 4780:                 }
 4781:                 my $key = 'override_'.$status;
 4782:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4783:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4784:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4785:                                                   \@contacts,$short_titles);
 4786:                 unless ($hidden) {
 4787:                     $rownum ++;
 4788:                 }
 4789:             }
 4790:         }
 4791:     }
 4792:     $$rowtotal += $rownum;
 4793:     return $datatable;
 4794: }
 4795: 
 4796: sub core_link_msu {
 4797:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4798:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4799: }
 4800: 
 4801: sub overridden_helpdesk {
 4802:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4803:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4804:     my $class = 'LC_left_item';
 4805:     if ($css_class) {
 4806:         $css_class = ' class="'.$css_class.'"';
 4807:     }
 4808:     if ($rowid) {
 4809:         $rowid = ' id="'.$rowid.'"';
 4810:     }
 4811:     if ($rowstyle) {
 4812:         $rowstyle = ' style="'.$rowstyle.'"';
 4813:     }
 4814:     my ($output,$description);
 4815:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4816:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4817:               "<td>$description</td>\n".
 4818:               '<td class="'.$class.'" colspan="2">'.
 4819:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4820:               '<span class="LC_nobreak">';
 4821:     if (ref($contacts) eq 'ARRAY') {
 4822:         foreach my $item (@{$contacts}) {
 4823:             my $check;
 4824:             if (ref($checked) eq 'HASH') {
 4825:                $check = $checked->{$item};
 4826:             }
 4827:             my $title;
 4828:             if (ref($short_titles) eq 'HASH') {
 4829:                 $title = $short_titles->{$item};
 4830:             }
 4831:             $output .= '<label>'.
 4832:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4833:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4834:         }
 4835:     }
 4836:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4837:                '<input type="text" name="override_'.$type.'_others" '.
 4838:                'value="'.$otheremails.'"  />';
 4839:     my %locchecked;
 4840:     foreach my $loc ('s','b') {
 4841:         if ($includeloc eq $loc) {
 4842:             $locchecked{$loc} = ' checked="checked"';
 4843:             last;
 4844:         }
 4845:     }
 4846:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4847:                '<input type="text" name="override_'.$type.'_bcc" '.
 4848:                'value="'.$bccemails.'"  /></fieldset>'.
 4849:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4850:                &mt('Text automatically added to e-mail:').' '.
 4851:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4852:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4853:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4854:                ('&nbsp;'x2).
 4855:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4856:                '</span></fieldset>'.
 4857:                '</td></tr>'."\n";
 4858:     return $output;
 4859: }
 4860: 
 4861: sub contacts_javascript {
 4862:     return <<"ENDSCRIPT";
 4863: 
 4864: <script type="text/javascript">
 4865: // <![CDATA[
 4866: 
 4867: function screenshotSize(field) {
 4868:     if (document.getElementById('help_screenshotsize')) {
 4869:         if (field.value == 'no') {
 4870:             document.getElementById('help_screenshotsize').style.display="none";
 4871:         } else {
 4872:             document.getElementById('help_screenshotsize').style.display="";
 4873:         }
 4874:     }
 4875:     return;
 4876: }
 4877: 
 4878: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4879:     if (form.elements[checkbox].length != undefined) {
 4880:         var count = 0;
 4881:         if (docount) {
 4882:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4883:                 if (form.elements[checkbox][i].checked) {
 4884:                     count ++;
 4885:                 }
 4886:             }
 4887:         }
 4888:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4889:             var type = form.elements[checkbox][i].value;
 4890:             if (document.getElementById(prefix+type)) {
 4891:                 if (form.elements[checkbox][i].checked) {
 4892:                     document.getElementById(prefix+type).style.display = 'table-row';
 4893:                     if (count % 2 == 1) {
 4894:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4895:                     } else {
 4896:                         document.getElementById(prefix+type).className = target;
 4897:                     }
 4898:                     count ++;
 4899:                 } else {
 4900:                     document.getElementById(prefix+type).style.display = 'none';
 4901:                 }
 4902:             }
 4903:         }
 4904:     }
 4905:     return;
 4906: }
 4907: 
 4908: // ]]>
 4909: </script>
 4910: 
 4911: ENDSCRIPT
 4912: }
 4913: 
 4914: sub print_helpsettings {
 4915:     my ($position,$dom,$settings,$rowtotal) = @_;
 4916:     my $confname = $dom.'-domainconfig';
 4917:     my $formname = 'display';
 4918:     my ($datatable,$itemcount);
 4919:     if ($position eq 'top') {
 4920:         $itemcount = 1;
 4921:         my (%choices,%defaultchecked,@toggles);
 4922:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4923:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4924:                                      &mt('LON-CAPA bug tracker'),600,500));
 4925:         %defaultchecked = ('submitbugs' => 'on');
 4926:         @toggles = ('submitbugs');
 4927:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4928:                                                      \%choices,$itemcount);
 4929:         $$rowtotal ++;
 4930:     } else {
 4931:         my $css_class;
 4932:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4933:         my (%customroles,%ordered,%current);
 4934:         if (ref($settings) eq 'HASH') {
 4935:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4936:                 %current = %{$settings->{'adhoc'}};
 4937:             }
 4938:         }
 4939:         my $count = 0;
 4940:         foreach my $key (sort(keys(%existing))) {
 4941:             if ($key=~/^rolesdef\_(\w+)$/) {
 4942:                 my $rolename = $1;
 4943:                 my (%privs,$order);
 4944:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4945:                 $customroles{$rolename} = \%privs;
 4946:                 if (ref($current{$rolename}) eq 'HASH') {
 4947:                     $order = $current{$rolename}{'order'};
 4948:                 }
 4949:                 if ($order eq '') {
 4950:                     $order = $count;
 4951:                 }
 4952:                 $ordered{$order} = $rolename;
 4953:                 $count++;
 4954:             }
 4955:         }
 4956:         my $maxnum = scalar(keys(%ordered));
 4957:         my @roles_by_num = ();
 4958:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4959:             push(@roles_by_num,$item);
 4960:         }
 4961:         my $context = 'domprefs';
 4962:         my $crstype = 'Course';
 4963:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4964:         my @accesstypes = ('all','dh','da','none');
 4965:         my ($numstatustypes,@jsarray);
 4966:         if (ref($types) eq 'ARRAY') {
 4967:             if (@{$types} > 0) {
 4968:                 $numstatustypes = scalar(@{$types});
 4969:                 push(@accesstypes,'status');
 4970:                 @jsarray = ('bystatus');
 4971:             }
 4972:         }
 4973:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4974:         if (keys(%domhelpdesk)) {
 4975:             push(@accesstypes,('inc','exc'));
 4976:             push(@jsarray,('notinc','notexc'));
 4977:         }
 4978:         my $hiddenstr = join("','",@jsarray);
 4979:         my $context = 'domprefs';
 4980:         my $crstype = 'Course';
 4981:         my $prefix = 'helproles_';
 4982:         my $add_class = 'LC_hidden';
 4983:         foreach my $num (@roles_by_num) {
 4984:             my $role = $ordered{$num};
 4985:             my ($desc,$access,@statuses);
 4986:             if (ref($current{$role}) eq 'HASH') {
 4987:                 $desc = $current{$role}{'desc'};
 4988:                 $access = $current{$role}{'access'};
 4989:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4990:                     @statuses = @{$current{$role}{'insttypes'}};
 4991:                 }
 4992:             }
 4993:             if ($desc eq '') {
 4994:                 $desc = $role;
 4995:             }
 4996:             my $identifier = 'custhelp'.$num;
 4997:             my %full=();
 4998:             my %levels= (
 4999:                          course => {},
 5000:                          domain => {},
 5001:                          system => {},
 5002:                         );
 5003:             my %levelscurrent=(
 5004:                                course => {},
 5005:                                domain => {},
 5006:                                system => {},
 5007:                               );
 5008:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 5009:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5010:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5011:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 5012:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 5013:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 5014:             for (my $k=0; $k<=$maxnum; $k++) {
 5015:                 my $vpos = $k+1;
 5016:                 my $selstr;
 5017:                 if ($k == $num) {
 5018:                     $selstr = ' selected="selected" ';
 5019:                 }
 5020:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5021:             }
 5022:             $datatable .= '</select>'.('&nbsp;'x2).
 5023:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 5024:                           '</td>'.
 5025:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5026:                           &mt('Name shown to users:').
 5027:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 5028:                           '</fieldset>'.
 5029:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 5030:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 5031:                           '<fieldset>'.
 5032:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 5033:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 5034:                                                                    \%levelscurrent,$identifier,
 5035:                                                                    'LC_hidden',$prefix.$num.'_privs').
 5036:                           '</fieldset></td>';
 5037:             $itemcount ++;
 5038:         }
 5039:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5040:         my $newcust = 'custhelp'.$count;
 5041:         my (%privs,%levelscurrent);
 5042:         my %full=();
 5043:         my %levels= (
 5044:                      course => {},
 5045:                      domain => {},
 5046:                      system => {},
 5047:                     );
 5048:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 5049:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5050:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 5051:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 5052:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5053:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5054:         for (my $k=0; $k<$maxnum+1; $k++) {
 5055:             my $vpos = $k+1;
 5056:             my $selstr;
 5057:             if ($k == $maxnum) {
 5058:                 $selstr = ' selected="selected" ';
 5059:             }
 5060:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5061:         }
 5062:         $datatable .= '</select>&nbsp;'."\n".
 5063:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5064:                       '</label></span></td>'.
 5065:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5066:                       '<span class="LC_nobreak">'.
 5067:                       &mt('Internal name:').
 5068:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5069:                       '</span>'.('&nbsp;'x4).
 5070:                       '<span class="LC_nobreak">'.
 5071:                       &mt('Name shown to users:').
 5072:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5073:                       '</span></fieldset>'.
 5074:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5075:                                              $usertypes,$types,\%domhelpdesk).
 5076:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5077:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5078:                                                                 \@templateroles,$newcust).
 5079:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5080:                                                                \%levelscurrent,$newcust).
 5081:                       '</fieldset>'.
 5082:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5083:                       '</td></tr>';
 5084:         $count ++;
 5085:         $$rowtotal += $count;
 5086:     }
 5087:     return $datatable;
 5088: }
 5089: 
 5090: sub adhocbutton {
 5091:     my ($prefix,$num,$field,$visibility) = @_;
 5092:     my %lt = &Apache::lonlocal::texthash(
 5093:                                           show => 'Show details',
 5094:                                           hide => 'Hide details',
 5095:                                         );
 5096:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5097:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5098:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5099:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5100: }
 5101: 
 5102: sub helpsettings_javascript {
 5103:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5104:     return unless(ref($roles_by_num) eq 'ARRAY');
 5105:     my %html_js_lt = &Apache::lonlocal::texthash(
 5106:                                           show => 'Show details',
 5107:                                           hide => 'Hide details',
 5108:                                         );
 5109:     &html_escape(\%html_js_lt);
 5110:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5111:     return <<"ENDSCRIPT";
 5112: <script type="text/javascript">
 5113: // <![CDATA[
 5114: 
 5115: function reorderHelpRoles(form,item) {
 5116:     var changedVal;
 5117: $jstext
 5118:     var newpos = 'helproles_${total}_pos';
 5119:     var maxh = 1 + $total;
 5120:     var current = new Array();
 5121:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5122:     if (item == newpos) {
 5123:         changedVal = newitemVal;
 5124:     } else {
 5125:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5126:         current[newitemVal] = newpos;
 5127:     }
 5128:     for (var i=0; i<helproles.length; i++) {
 5129:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5130:         if (elementName != item) {
 5131:             if (form.elements[elementName]) {
 5132:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5133:                 current[currVal] = elementName;
 5134:             }
 5135:         }
 5136:     }
 5137:     var oldVal;
 5138:     for (var j=0; j<maxh; j++) {
 5139:         if (current[j] == undefined) {
 5140:             oldVal = j;
 5141:         }
 5142:     }
 5143:     if (oldVal < changedVal) {
 5144:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5145:            var elementName = current[k];
 5146:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5147:         }
 5148:     } else {
 5149:         for (var k=changedVal; k<oldVal; k++) {
 5150:             var elementName = current[k];
 5151:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5152:         }
 5153:     }
 5154:     return;
 5155: }
 5156: 
 5157: function helpdeskAccess(num) {
 5158:     var curraccess = null;
 5159:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5160:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5161:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5162:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5163:             }
 5164:         }
 5165:     }
 5166:     var shown = Array();
 5167:     var hidden = Array();
 5168:     if (curraccess == 'none') {
 5169:         hidden = Array('$hiddenstr');
 5170:     } else {
 5171:         if (curraccess == 'status') {
 5172:             shown = Array('bystatus');
 5173:             hidden = Array('notinc','notexc');
 5174:         } else {
 5175:             if (curraccess == 'exc') {
 5176:                 shown = Array('notexc');
 5177:                 hidden = Array('notinc','bystatus');
 5178:             }
 5179:             if (curraccess == 'inc') {
 5180:                 shown = Array('notinc');
 5181:                 hidden = Array('notexc','bystatus');
 5182:             }
 5183:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5184:                 hidden = Array('notinc','notexc','bystatus');
 5185:             }
 5186:         }
 5187:     }
 5188:     if (hidden.length > 0) {
 5189:         for (var i=0; i<hidden.length; i++) {
 5190:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5191:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5192:             }
 5193:         }
 5194:     }
 5195:     if (shown.length > 0) {
 5196:         for (var i=0; i<shown.length; i++) {
 5197:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5198:                 if (shown[i] == 'privs') {
 5199:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5200:                 } else {
 5201:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5202:                 }
 5203:             }
 5204:         }
 5205:     }
 5206:     return;
 5207: }
 5208: 
 5209: function toggleHelpdeskItem(num,field) {
 5210:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5211:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5212:             document.getElementById('helproles_'+num+'_'+field).className =
 5213:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5214:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5215:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5216:             }
 5217:         } else {
 5218:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5219:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5220:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5221:             }
 5222:         }
 5223:     }
 5224:     return;
 5225: }
 5226: 
 5227: // ]]>
 5228: </script>
 5229: 
 5230: ENDSCRIPT
 5231: }
 5232: 
 5233: sub helpdeskroles_access {
 5234:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5235:         $usertypes,$types,$domhelpdesk) = @_;
 5236:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5237:     my %lt = &Apache::lonlocal::texthash(
 5238:                     'rou'    => 'Role usage',
 5239:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5240:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5241:                     'dh'     => 'All with domain helpdesk role',
 5242:                     'da'     => 'All with domain helpdesk assistant role',
 5243:                     'none'   => 'None',
 5244:                     'status' => 'Determined based on institutional status',
 5245:                     'inc'    => 'Include all, but exclude specific personnel',
 5246:                     'exc'    => 'Exclude all, but include specific personnel',
 5247:                   );
 5248:     my %usecheck = (
 5249:                      all => ' checked="checked"',
 5250:                    );
 5251:     my %displaydiv = (
 5252:                       status => 'none',
 5253:                       inc    => 'none',
 5254:                       exc    => 'none',
 5255:                       priv   => 'block',
 5256:                      );
 5257:     my $output;
 5258:     if (ref($current) eq 'HASH') {
 5259:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5260:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5261:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5262:                 delete($usecheck{'all'});
 5263:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5264:                     my $access = $1;
 5265:                     $displaydiv{$access} = 'inline';
 5266:                 } elsif ($current->{access} eq 'none') {
 5267:                     $displaydiv{'priv'} = 'none';
 5268:                 }
 5269:             }
 5270:         }
 5271:     }
 5272:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5273:               '<p>'.$lt{'whi'}.'</p>';
 5274:     foreach my $access (@{$accesstypes}) {
 5275:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5276:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5277:                    $lt{$access}.'</label>';
 5278:         if ($access eq 'status') {
 5279:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5280:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5281:                                                                  $othertitle,$usertypes,$types).
 5282:                        '</div>';
 5283:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5284:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5285:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5286:                        '</div>';
 5287:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5288:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5289:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5290:                        '</div>';
 5291:         }
 5292:         $output .= '</p>';
 5293:     }
 5294:     $output .= '</fieldset>';
 5295:     return $output;
 5296: }
 5297: 
 5298: sub radiobutton_prefs {
 5299:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5300:         $additional,$align,$firstval) = @_;
 5301:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5302:                    (ref($choices) eq 'HASH'));
 5303: 
 5304:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5305: 
 5306:     foreach my $item (@{$toggles}) {
 5307:         if ($defaultchecked->{$item} eq 'on') {
 5308:             $checkedon{$item} = ' checked="checked" ';
 5309:             $checkedoff{$item} = ' ';
 5310:         } elsif ($defaultchecked->{$item} eq 'off') {
 5311:             $checkedoff{$item} = ' checked="checked" ';
 5312:             $checkedon{$item} = ' ';
 5313:         }
 5314:     }
 5315:     if (ref($settings) eq 'HASH') {
 5316:         foreach my $item (@{$toggles}) {
 5317:             if ($settings->{$item} eq '1') {
 5318:                 $checkedon{$item} =  ' checked="checked" ';
 5319:                 $checkedoff{$item} = ' ';
 5320:             } elsif ($settings->{$item} eq '0') {
 5321:                 $checkedoff{$item} =  ' checked="checked" ';
 5322:                 $checkedon{$item} = ' ';
 5323:             }
 5324:         }
 5325:     }
 5326:     if ($onclick) {
 5327:         $onclick = ' onclick="'.$onclick.'"';
 5328:     }
 5329:     foreach my $item (@{$toggles}) {
 5330:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5331:         $datatable .=
 5332:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5333:             '<span class="LC_nobreak">'.$choices->{$item}.
 5334:             '</span></td>';
 5335:         if ($align eq 'left') {
 5336:             $datatable .= '<td class="LC_left_item">';
 5337:         } else {
 5338:             $datatable .= '<td class="LC_right_item">';
 5339:         }
 5340:         $datatable .= '<span class="LC_nobreak">';
 5341:         if ($firstval eq 'no') {
 5342:             $datatable .=
 5343:                 '<label><input type="radio" name="'.
 5344:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5345:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5346:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5347:         } else {
 5348:             $datatable .=
 5349:                 '<label><input type="radio" name="'.
 5350:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5351:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5352:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5353:         }
 5354:         $datatable .= '</span>'.$additional.'</td></tr>';
 5355:         $itemcount ++;
 5356:     }
 5357:     return ($datatable,$itemcount);
 5358: }
 5359: 
 5360: sub print_ltitools {
 5361:     my ($position,$dom,$settings,$rowtotal) = @_;
 5362:     my (%rules,%encrypt,%privkeys,%linkprot);
 5363:     if (ref($settings) eq 'HASH') {
 5364:         if ($position eq 'top') {
 5365:             if (exists($settings->{'encrypt'})) {
 5366:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5367:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5368:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5369:                     }
 5370:                 }
 5371:             }
 5372:             if (exists($settings->{'private'})) {
 5373:                 if (ref($settings->{'private'}) eq 'HASH') {
 5374:                     if (ref($settings->{'private'}) eq 'HASH') {
 5375:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5376:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5377:                         }
 5378:                     }
 5379:                 }
 5380:             }
 5381:         } elsif ($position eq 'middle') {
 5382:             if (exists($settings->{'rules'})) {
 5383:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5384:                     %rules = %{$settings->{'rules'}};
 5385:                 }
 5386:             }
 5387:         } else {
 5388:             foreach my $key ('encrypt','private','rules') {
 5389:                 if (exists($settings->{$key})) {
 5390:                     delete($settings->{$key});
 5391:                 }
 5392:             }
 5393:         }
 5394:     }
 5395:     my $datatable;
 5396:     my $itemcount = 1;
 5397:     if ($position eq 'top') {
 5398:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5399:     } elsif ($position eq 'middle') {
 5400:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5401:         $$rowtotal += $itemcount;
 5402:     } else {
 5403:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5404:     }
 5405:     return $datatable;
 5406: }
 5407: 
 5408: sub ltitools_names {
 5409:     my %lt = &Apache::lonlocal::texthash(
 5410:                                           'title'          => 'Title',
 5411:                                           'version'        => 'Version',
 5412:                                           'msgtype'        => 'Message Type',
 5413:                                           'sigmethod'      => 'Signature Method',
 5414:                                           'url'            => 'URL',
 5415:                                           'key'            => 'Key',
 5416:                                           'lifetime'       => 'Nonce lifetime (s)',
 5417:                                           'secret'         => 'Secret',
 5418:                                           'icon'           => 'Icon',
 5419:                                           'user'           => 'User',
 5420:                                           'fullname'       => 'Full Name',
 5421:                                           'firstname'      => 'First Name',
 5422:                                           'lastname'       => 'Last Name',
 5423:                                           'email'          => 'E-mail',
 5424:                                           'roles'          => 'Role',
 5425:                                           'window'         => 'Window',
 5426:                                           'tab'            => 'Tab',
 5427:                                           'iframe'         => 'iFrame',
 5428:                                           'height'         => 'Height',
 5429:                                           'width'          => 'Width',
 5430:                                           'linktext'       => 'Default Link Text',
 5431:                                           'explanation'    => 'Default Explanation',
 5432:                                           'passback'       => 'Tool can return grades:',
 5433:                                           'roster'         => 'Tool can retrieve roster:',
 5434:                                           'crstarget'      => 'Display target',
 5435:                                           'crslabel'       => 'Course label',
 5436:                                           'crstitle'       => 'Course title',
 5437:                                           'crslinktext'    => 'Link Text',
 5438:                                           'crsexplanation' => 'Explanation',
 5439:                                           'crsappend'      => 'Provider URL',
 5440:                                         );
 5441:     return %lt;
 5442: }
 5443: 
 5444: sub secrets_form {
 5445:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5446:     my @ids=&Apache::lonnet::current_machine_ids();
 5447:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5448:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5449:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5450:     $itemcount = 0;
 5451:     foreach my $hostid (sort(keys(%servers))) {
 5452:         my ($showextra,$divsty,$switch);
 5453:         if ($hostid eq $primary) {
 5454:             if ($context eq 'ltisec') {
 5455:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5456:                     $showextra = 1;
 5457:                 }
 5458:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5459:                     $showextra = 1;
 5460:                 }
 5461:             } else {
 5462:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5463:                     $showextra = 1;
 5464:                 }
 5465:             }
 5466:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5467:                 $switch = 1;
 5468:             }
 5469:             if ($showextra) {
 5470:                 $numshown ++;
 5471:                 $divsty = 'display:inline-block';
 5472:             } else {
 5473:                 $divsty = 'display:none';
 5474:             }
 5475:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5476:                       '<legend>'.$hostid.'</legend>';
 5477:             if ($switch) {
 5478:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5479:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5480:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5481:                 if (exists($privkeys->{$hostid})) {
 5482:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5483:                               '<span class="LC_nobreak">'.
 5484:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5485:                               '<span class="LC_nobreak">'.&mt('Change?').
 5486:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5487:                               ('&nbsp;'x2).
 5488:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5489:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5490:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5491:                               '</span></div>';
 5492:                 } else {
 5493:                     $extra .= '<span class="LC_nobreak">'.
 5494:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5495:                               '</span>'."\n";
 5496:                 }
 5497:             } elsif (exists($privkeys->{$hostid})) {
 5498:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5499:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5500:                           '<span class="LC_nobreak">'.&mt('Change?').
 5501:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5502:                           ('&nbsp;'x2).
 5503:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5504:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5505:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5506:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5507:                           '<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>'.
 5508:                           '</span></div>';
 5509:             } else {
 5510:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5511:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5512:                           '<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>';
 5513:             }
 5514:             $extra .= '</fieldset>';
 5515:         }
 5516:     }
 5517:     my (%choices,@toggles,%defaultchecked);
 5518:     if ($context eq 'ltisec') {
 5519:         %choices = &Apache::lonlocal::texthash (
 5520:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5521:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5522:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5523:                                                );
 5524:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5525:         %defaultchecked = (
 5526:                            'ltisec_crslinkprot' => 'off',
 5527:                            'ltisec_domlinkprot' => 'off',
 5528:                            'ltisec_consumers'   => 'off',
 5529:                           );
 5530:     } else {
 5531:         %choices = &Apache::lonlocal::texthash (
 5532:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5533:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5534:                                                );
 5535:         @toggles = qw(toolsec_crs toolsec_dom);
 5536:         %defaultchecked = (
 5537:                            'toolsec_crs' => 'off',
 5538:                            'toolsec_dom' => 'off',
 5539:                           );
 5540:     }
 5541:     my ($onclick,$itemcount);
 5542:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5543:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5544:                                               \%choices,$itemcount,$onclick,'','left','no');
 5545: 
 5546:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5547:     my $noprivkeysty = 'display:inline-block';
 5548:     if ($numshown) {
 5549:         $noprivkeysty = 'display:none';
 5550:     }
 5551:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5552:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5553:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5554:                $extra.
 5555:                '</td></tr>';
 5556:     $itemcount ++;
 5557:     $$rowtotal += $itemcount;
 5558:     return $output;
 5559: }
 5560: 
 5561: sub print_proctoring {
 5562:     my ($dom,$settings,$rowtotal) = @_;
 5563:     my $itemcount = 1;
 5564:     my (%ordered,%providernames,%current,%currentdef);
 5565:     my $confname = $dom.'-domainconfig';
 5566:     my $switchserver = &check_switchserver($dom,$confname);
 5567:     if (ref($settings) eq 'HASH') {
 5568:         foreach my $item (keys(%{$settings})) {
 5569:             if (ref($settings->{$item}) eq 'HASH') {
 5570:                 my $num = $settings->{$item}{'order'};
 5571:                 $ordered{$num} = $item;
 5572:             }
 5573:         }
 5574:     } else {
 5575:         %ordered = (
 5576:                      1 => 'proctorio',
 5577:                      2 => 'examity',
 5578:                    );
 5579:     }
 5580:     %providernames = &proctoring_providernames();
 5581:     my $maxnum = scalar(keys(%ordered));
 5582:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5583:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5584:     if (ref($requref) eq 'HASH') {
 5585:         %requserfields = %{$requref};
 5586:     }
 5587:     if (ref($opturef) eq 'HASH') {
 5588:         %optuserfields = %{$opturef};
 5589:     }
 5590:     if (ref($defref) eq 'HASH') {
 5591:         %defaults = %{$defref};
 5592:     }
 5593:     if (ref($extref) eq 'HASH') {
 5594:         %extended = %{$extref};
 5595:     }
 5596:     if (ref($crsref) eq 'HASH') {
 5597:         %crsconf = %{$crsref};
 5598:     }
 5599:     if (ref($rolesref) eq 'ARRAY') {
 5600:         @courseroles = @{$rolesref};
 5601:     }
 5602:     if (ref($ltiref) eq 'ARRAY') {
 5603:         @ltiroles = @{$ltiref};
 5604:     }
 5605:     my $datatable;
 5606:     my $css_class;
 5607:     if (keys(%ordered)) {
 5608:         my @items = sort { $a <=> $b } keys(%ordered);
 5609:         for (my $i=0; $i<@items; $i++) {
 5610:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5611:             my $provider = $ordered{$items[$i]};
 5612:             my $optionsty = 'none';
 5613:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5614:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5615:             if (ref($settings) eq 'HASH') {
 5616:                 if (ref($settings->{$provider}) eq 'HASH') {
 5617:                     %current = %{$settings->{$provider}};
 5618:                     if ($current{'available'}) {
 5619:                         $optionsty = 'block';
 5620:                         $available = 1;
 5621:                     }
 5622:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5623:                         $lifetime = $current{'lifetime'};
 5624:                     }
 5625:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5626:                         $version = $current{'version'};
 5627:                     }
 5628:                     if ($current{'image'} ne '') {
 5629:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5630:                     }
 5631:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5632:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5633:                     }
 5634:                     $userincdom = $current{'incdom'};
 5635:                     if (ref($current{'roles'}) eq 'HASH') {
 5636:                         %rolemaps = %{$current{'roles'}};
 5637:                         $checkedfields{'roles'} = 1;
 5638:                     }
 5639:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5640:                         foreach my $val (@{$current{'defaults'}}) {
 5641:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5642:                                 $inuse{$val} = 1;
 5643:                             } else {
 5644:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5645:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5646:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5647:                                             $inuse{$poss} = $val;
 5648:                                             last;
 5649:                                         }
 5650:                                     }
 5651:                                 }
 5652:                             }
 5653:                         }
 5654:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5655:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5656:                             my $currval = $current{'defaults'}{$key}; 
 5657:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5658:                                 $inuse{$key} = 1;
 5659:                             } else {
 5660:                                 my $match;
 5661:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5662:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5663:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5664:                                             $inuse{$poss} = $key;
 5665:                                             last;
 5666:                                         }
 5667:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5668:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5669:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5670:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5671:                                                     $currentdef{$inner} = $currval;
 5672:                                                     $match = 1;
 5673:                                                     last;
 5674:                                                 }
 5675:                                             } elsif ($inner eq $key) {
 5676:                                                 $currentdef{$key} = $currval;
 5677:                                                 $match = 1;
 5678:                                                 last;
 5679:                                             }
 5680:                                         }
 5681:                                     }
 5682:                                     last if ($match);
 5683:                                 }
 5684:                             }
 5685:                         }
 5686:                     }
 5687:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5688:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5689:                     }
 5690:                 }
 5691:             }
 5692:             my %lt = &proctoring_titles($provider);
 5693:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5694:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5695:             my %checkedavailable = (
 5696:                                    yes => '',
 5697:                                    no  => ' checked="checked"',
 5698:                                  );
 5699:             if ($available) {
 5700:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5701:                 $checkedavailable{'no'} = '';
 5702:             }
 5703:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5704:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5705:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5706:             for (my $k=0; $k<$maxnum; $k++) {
 5707:                 my $vpos = $k+1;
 5708:                 my $selstr;
 5709:                 if ($k == $i) {
 5710:                     $selstr = ' selected="selected" ';
 5711:                 }
 5712:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5713:             }
 5714:             if ($version eq '') {
 5715:                 if ($provider eq 'proctorio') {
 5716:                     $version = '1.0';
 5717:                 } elsif ($provider eq 'examity') {
 5718:                     $version = '1.1';
 5719:                 }
 5720:             }
 5721:             if ($lifetime eq '') {
 5722:                 $lifetime = '300';
 5723:             }
 5724:             $datatable .=
 5725:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5726:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5727:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5728:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5729:                 '</td>'.
 5730:                 '<td colspan="2">'.
 5731:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5732:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5733:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5734:                 ('&nbsp;'x2).
 5735:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5736:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5737:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5738:                 ('&nbsp;'x2).
 5739:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5740:                 '<br />'.
 5741:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5742:                 '<br />'.
 5743:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5744:                 ('&nbsp;'x2).
 5745:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5746:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.proctoring_'.$provider.'_secret.type='."'text'".' } else { this.form.proctoring_'.$provider.'_secret.type='."'password'".' }" />'.$lt{'visible'}.'</label></span><br />'."\n";
 5747:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5748:             if ($imgsrc) {
 5749:                 $datatable .= $imgsrc.
 5750:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5751:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5752:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5753:             }
 5754:             $datatable .= '&nbsp;';
 5755:             if ($switchserver) {
 5756:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5757:             } else {
 5758:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5759:             }
 5760:             unless ($imgsrc) {
 5761:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5762:             }
 5763:             $datatable .= '</fieldset>'."\n";
 5764:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5765:                 if (@{$requserfields{$provider}} > 0) {
 5766:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5767:                     foreach my $field (@{$requserfields{$provider}}) {
 5768:                         $datatable .= '<span class="LC_nobreak">'.
 5769:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5770:                                       $lt{$field}.'</label>';
 5771:                         if ($field eq 'user') {
 5772:                             my $seluserdom = '';
 5773:                             my $unseluserdom = ' selected="selected"';
 5774:                             if ($userincdom) {
 5775:                                 $seluserdom = $unseluserdom;
 5776:                                 $unseluserdom = '';
 5777:                             }
 5778:                             $datatable .= ':&nbsp;'.
 5779:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5780:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5781:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5782:                                 '</select>&nbsp;';
 5783:                         } else {
 5784:                             $datatable .= '&nbsp;';
 5785:                             if ($field eq 'roles') {
 5786:                                 $showroles = 1; 
 5787:                             } 
 5788:                         }
 5789:                         $datatable .= '</span> ';
 5790:                     }
 5791:                 }
 5792:                 $datatable .= '</fieldset>'."\n";
 5793:             }
 5794:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5795:                 if (@{$optuserfields{$provider}} > 0) {
 5796:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5797:                     foreach my $field (@{$optuserfields{$provider}}) {
 5798:                         my $checked;
 5799:                         if ($checkedfields{$field}) {
 5800:                             $checked = ' checked="checked"';
 5801:                         }
 5802:                         $datatable .= '<span class="LC_nobreak">'.
 5803:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5804:                     }
 5805:                     $datatable .= '</fieldset>'."\n";
 5806:                 }
 5807:             }
 5808:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5809:                 if (@{$defaults{$provider}}) {
 5810:                     my (%options,@selectboxes);
 5811:                     if (ref($extended{$provider}) eq 'HASH') {
 5812:                         %options = %{$extended{$provider}};
 5813:                     }
 5814:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5815:                     my ($rem,$numinrow,$dropdowns);
 5816:                     if ($provider eq 'proctorio') { 
 5817:                         $datatable .= '<table>';
 5818:                         $numinrow = 4;
 5819:                     }
 5820:                     my $i = 0;
 5821:                     foreach my $field (@{$defaults{$provider}}) {
 5822:                         my $checked;
 5823:                         if ($inuse{$field}) {
 5824:                             $checked = ' checked="checked"';
 5825:                         }
 5826:                         if ($provider eq 'examity') {
 5827:                             if ($field eq 'display') {
 5828:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5829:                                 foreach my $option ('iframe','tab','window') {
 5830:                                     my $checkdisp;
 5831:                                     if ($currentdef{'target'} eq $option) {
 5832:                                         $checkdisp = ' checked="checked"';
 5833:                                     }
 5834:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5835:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5836:                                 }
 5837:                                 $datatable .= ('&nbsp;'x4);
 5838:                                 foreach my $dimen ('width','height') {
 5839:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5840:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5841:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5842:                                                   ('&nbsp;'x2);
 5843:                                 }
 5844:                                 $datatable .= '</span><br />'.
 5845:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5846:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5847:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5848:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5849:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5850:                                               $currentdef{'explanation'}.
 5851:                                               '</textarea></div><div style=""></div><br />';
 5852:                             }
 5853:                         } else {
 5854:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5855:                                 my ($output,$selnone);
 5856:                                 unless ($checked) {
 5857:                                     $selnone = ' selected="selected"';
 5858:                                 }
 5859:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5860:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5861:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5862:                                 foreach my $option (@{$options{$field}}) {
 5863:                                     my $sel; 
 5864:                                     if ($inuse{$field} eq $option) {
 5865:                                         $sel = ' selected="selected"';
 5866:                                     }
 5867:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5868:                                 }
 5869:                                 $output .= '</select></span>';
 5870:                                 push(@selectboxes,$output);
 5871:                             } else {
 5872:                                 $rem = $i%($numinrow);
 5873:                                 if ($rem == 0) {
 5874:                                     if ($i > 0) {
 5875:                                         $datatable .= '</tr>';
 5876:                                     }
 5877:                                     $datatable .= '<tr>';
 5878:                                 }
 5879:                                 $datatable .= '<td class="LC_left_item">'.
 5880:                                               '<span class="LC_nobreak">'.
 5881:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5882:                                               $fieldtitles{$field}.'</label></span></td>';
 5883:                                 $i++;
 5884:                             }
 5885:                         }
 5886:                     }
 5887:                     if ($provider eq 'proctorio') {
 5888:                         if ($numinrow) {
 5889:                             $rem = $i%$numinrow;
 5890:                         }
 5891:                         my $colsleft = $numinrow - $rem;
 5892:                         if ($colsleft > 1) {
 5893:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5894:                         } else {
 5895:                             $datatable .= '<td class="LC_left_item">';
 5896:                         }
 5897:                         $datatable .= '&nbsp;'.
 5898:                                       '</td></tr></table>';
 5899:                         if (@selectboxes) {
 5900:                             $datatable .= '<hr /><table>';
 5901:                             $numinrow = 2;
 5902:                             for (my $i=0; $i<@selectboxes; $i++) {
 5903:                                 $rem = $i%($numinrow);
 5904:                                 if ($rem == 0) {
 5905:                                     if ($i > 0) {
 5906:                                         $datatable .= '</tr>';
 5907:                                     }
 5908:                                     $datatable .= '<tr>';
 5909:                                 }
 5910:                                 $datatable .= '<td class="LC_left_item">'.
 5911:                                               $selectboxes[$i].'</td>';
 5912:                             }
 5913:                             if ($numinrow) {
 5914:                                 $rem = $i%$numinrow;
 5915:                             }
 5916:                             $colsleft = $numinrow - $rem;
 5917:                             if ($colsleft > 1) {
 5918:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5919:                             } else {
 5920:                                 $datatable .= '<td class="LC_left_item">';
 5921:                             }
 5922:                             $datatable .= '&nbsp;'.
 5923:                                           '</td></tr></table>';
 5924:                         }
 5925:                     }
 5926:                     $datatable .= '</fieldset>';
 5927:                 }
 5928:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5929:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5930:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5931:                     my ($rem,$numinrow);
 5932:                     if ($provider eq 'proctorio') {
 5933:                         $datatable .= '<table>';
 5934:                         $numinrow = 4;
 5935:                     }
 5936:                     my $i = 0;
 5937:                     foreach my $item (@{$crsconf{$provider}}) {
 5938:                         my $name;
 5939:                         if ($provider eq 'examity') {
 5940:                             $name = $lt{'crs'.$item};
 5941:                         } elsif ($provider eq 'proctorio') {
 5942:                             $name = $fieldtitles{$item};
 5943:                             $rem = $i%($numinrow);
 5944:                             if ($rem == 0) {
 5945:                                 if ($i > 0) {
 5946:                                     $datatable .= '</tr>';
 5947:                                 }
 5948:                                 $datatable .= '<tr>';
 5949:                             }
 5950:                             $datatable .= '<td class="LC_left_item>';
 5951:                         }
 5952:                         my $checked;
 5953:                         if ($crsconfig{$item}) {
 5954:                             $checked = ' checked="checked"';
 5955:                         }
 5956:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5957:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5958:                                       $name.'</label></span>';
 5959:                         if ($provider eq 'examity') {
 5960:                             $datatable .= '&nbsp; ';
 5961:                         }
 5962:                         $datatable .= "\n";
 5963:                         $i++;
 5964:                     }
 5965:                     if ($provider eq 'proctorio') {
 5966:                         if ($numinrow) {
 5967:                             $rem = $i%$numinrow;
 5968:                         }
 5969:                         my $colsleft = $numinrow - $rem;
 5970:                         if ($colsleft > 1) {
 5971:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5972:                         } else {
 5973:                             $datatable .= '<td class="LC_left_item">';
 5974:                         }
 5975:                         $datatable .= '&nbsp;'.
 5976:                                       '</td></tr></table>';
 5977:                     }
 5978:                     $datatable .= '</fieldset>';
 5979:                 }
 5980:                 if ($showroles) {
 5981:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5982:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5983:                     foreach my $role (@courseroles) {
 5984:                         my ($selected,$selectnone);
 5985:                         if (!$rolemaps{$role}) {
 5986:                             $selectnone = ' selected="selected"';
 5987:                         }
 5988:                         $datatable .= '<td style="text-align: center">'.
 5989:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5990:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5991:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5992:                         foreach my $ltirole (@ltiroles) {
 5993:                             unless ($selectnone) {
 5994:                                 if ($rolemaps{$role} eq $ltirole) {
 5995:                                     $selected = ' selected="selected"';
 5996:                                 } else {
 5997:                                     $selected = '';
 5998:                                 }
 5999:                             }
 6000:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 6001:                         }
 6002:                         $datatable .= '</select></td>';
 6003:                     }
 6004:                     $datatable .= '</tr></table></fieldset>'.
 6005:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6006:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 6007:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 6008:                                   '<tr><td></td><td>lms</td>'.
 6009:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 6010:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 6011:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 6012:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 6013:                         my %custom = %{$settings->{$provider}->{'custom'}};
 6014:                         if (keys(%custom) > 0) {
 6015:                             foreach my $key (sort(keys(%custom))) {
 6016:                                 next if ($key eq 'lms');
 6017:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 6018:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 6019:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6020:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6021:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6022:                             }
 6023:                         }
 6024:                     }
 6025:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6026:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6027:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6028:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6029:                                   '</table></fieldset></td></tr>'."\n";
 6030:                 }
 6031:                 $datatable .= '</td></tr>';
 6032:             }
 6033:             $itemcount ++;
 6034:         }
 6035:     }
 6036:     return $datatable;
 6037: }
 6038: 
 6039: sub proctoring_data {
 6040:     my $requserfields = {
 6041:                       proctorio => ['user'],
 6042:                       examity   => ['roles','user'],
 6043:                      };
 6044:     my $optuserfields = {
 6045:                         proctorio => ['fullname'],
 6046:                         examity   => ['fullname','firstname','lastname','email'],
 6047:                      };
 6048:     my $defaults = {
 6049:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6050:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6051:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6052:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6053:                                 'reentry','calculator','whiteboard'],
 6054:                   examity   => ['display'],
 6055:                 };
 6056:     my $extended = { 
 6057:                   proctorio => {
 6058:                                  verifyid => ['verifyidauto','verifyidlive'],
 6059:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6060:                                  tabslinks => ['notabs','linksonly'],
 6061:                                  reentry => ['noreentry','agentreentry'],
 6062:                                  calculator => ['calculatorbasic','calculatorsci'],
 6063:                                },
 6064:                   examity => {
 6065:                                display => {
 6066:                                            target      => ['iframe','tab','window'],
 6067:                                            width       => '',
 6068:                                            height      => '',
 6069:                                            linktext    => '',
 6070:                                            explanation => '',
 6071:                                           },
 6072:                              },
 6073:                 };
 6074:     my $crsconf = {
 6075:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6076:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6077:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6078:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6079:                                'reentry','calculator','whiteboard'],
 6080:                  examity => ['label','title','target','linktext','explanation','append'],
 6081:                };
 6082:     my $courseroles = ['cc','in','ta','ep','st'];
 6083:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6084:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6085: }
 6086: 
 6087: sub proctoring_titles {
 6088:     my ($item) = @_;
 6089:     my (%common_lt,%custom_lt);
 6090:     %common_lt = &Apache::lonlocal::texthash (
 6091:                      'avai'      => 'Available?',
 6092:                      'base'      => 'Basic Settings',
 6093:                      'requ'      => 'User data required to be sent on launch',
 6094:                      'optu'      => 'User data optionally sent on launch',
 6095:                      'udsl'      => 'User data sent on launch',
 6096:                      'defa'      => 'Defaults for items configurable in course',
 6097:                      'sigmethod' => 'Signature Method',
 6098:                      'key'       => 'Key',
 6099:                      'lifetime'  => 'Nonce lifetime (s)',
 6100:                      'secret'    => 'Secret',
 6101:                      'icon'      => 'Icon',
 6102:                      'fullname'  => 'Full Name',
 6103:                      'visible'   => 'Visible input',
 6104:                      'username'  => 'username',
 6105:                      'user'      => 'User',
 6106:                  );
 6107:     if ($item eq 'proctorio') {
 6108:         %custom_lt = &Apache::lonlocal::texthash (
 6109:                          'version'        => 'OAuth version',
 6110:                          'url'            => 'API URL',
 6111:                          'uname:dom'      => 'username-domain',
 6112:         );
 6113:     } elsif ($item eq 'examity') {
 6114:         %custom_lt = &Apache::lonlocal::texthash (
 6115:                          'version'        => 'LTI Version',
 6116:                          'url'            => 'URL',
 6117:                          'uname:dom'      => 'username:domain',
 6118:                          'msgtype'        => 'Message Type',
 6119:                          'firstname'      => 'First Name',
 6120:                          'lastname'       => 'Last Name',
 6121:                          'email'          => 'E-mail',
 6122:                          'roles'          => 'Role',
 6123:                          'crstarget'      => 'Display target',
 6124:                          'crslabel'       => 'Course label',
 6125:                          'crstitle'       => 'Course title', 
 6126:                          'crslinktext'    => 'Link Text',
 6127:                          'crsexplanation' => 'Explanation',
 6128:                          'crsappend'      => 'Provider URL',
 6129:         );
 6130:     }
 6131:     my %lt = (%common_lt,%custom_lt);
 6132:     return %lt;
 6133: }
 6134: 
 6135: sub proctoring_fieldtitles {
 6136:     my ($item) = @_;
 6137:     if ($item eq 'proctorio') {
 6138:         return &Apache::lonlocal::texthash (
 6139:                   'recordvideo' => 'Record video',
 6140:                   'recordaudio' => 'Record audio',
 6141:                   'recordscreen' => 'Record screen',
 6142:                   'recordwebtraffic' => 'Record web traffic',
 6143:                   'recordroomstart' => 'Record room scan',
 6144:                   'verifyvideo' => 'Verify webcam',
 6145:                   'verifyaudio' => 'Verify microphone',
 6146:                   'verifydesktop' => 'Verify desktop recording',
 6147:                   'verifyid' => 'Photo ID verification',
 6148:                   'verifysignature' => 'Require signature',
 6149:                   'fullscreen' => 'Fullscreen',
 6150:                   'clipboard' => 'Disable copy/paste',
 6151:                   'tabslinks' => 'New tabs/windows',
 6152:                   'closetabs' => 'Close other tabs',
 6153:                   'onescreen' => 'Limit to single screen',
 6154:                   'print' => 'Disable Printing',
 6155:                   'downloads' => 'Disable Downloads',
 6156:                   'cache' => 'Empty cache after exam',
 6157:                   'rightclick' => 'Disable right click',
 6158:                   'reentry' => 'Re-entry to exam',
 6159:                   'calculator' => 'Onscreen calculator',
 6160:                   'whiteboard' => 'Onscreen whiteboard',
 6161:                   'verifyidauto' => 'Automated verification',
 6162:                   'verifyidlive' => 'Live agent verification',
 6163:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6164:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6165:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6166:                   'notabs' => 'Disaallowed',
 6167:                   'linksonly' => 'Allowed from links in exam',
 6168:                   'noreentry' => 'Disallowed',
 6169:                   'agentreentry' => 'Agent required for re-entry',
 6170:                   'calculatorbasic' => 'Basic',
 6171:                   'calculatorsci' => 'Scientific',
 6172:         );
 6173:     } elsif ($item eq 'examity') {
 6174:         return &Apache::lonlocal::texthash (
 6175:                 'target'         => 'Display target',   
 6176:                 'window'         => 'Window',
 6177:                 'tab'            => 'Tab',
 6178:                 'iframe'         => 'iFrame',
 6179:                 'height'         => 'Height (pixels)',
 6180:                 'width'          => 'Width (pixels)',
 6181:                 'linktext'       => 'Default Link Text',
 6182:                 'explanation'    => 'Default Explanation',
 6183:                 'append'         => 'Provider URL',
 6184:         );
 6185:     }
 6186: }
 6187: 
 6188: sub proctoring_providernames {
 6189:     return (
 6190:              proctorio => 'Proctorio',
 6191:              examity   => 'Examity',
 6192:            );
 6193: }
 6194: 
 6195: sub print_lti {
 6196:     my ($position,$dom,$settings,$rowtotal) = @_;
 6197:     my $itemcount = 1;
 6198:     my ($datatable,$css_class);
 6199:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 6200:     if (ref($settings) eq 'HASH') {
 6201:         if ($position eq 'top') {
 6202:             if (exists($settings->{'encrypt'})) {
 6203:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6204:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6205:                         if ($key eq 'consumers') {
 6206:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6207:                         } else {
 6208:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6209:                         }
 6210:                     }
 6211:                 }
 6212:             }
 6213:             if (exists($settings->{'private'})) {
 6214:                 if (ref($settings->{'private'}) eq 'HASH') {
 6215:                     if (ref($settings->{'private'}) eq 'HASH') {
 6216:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6217:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6218:                         }
 6219:                     }
 6220:                 }
 6221:             }
 6222:         } elsif ($position eq 'upper') {
 6223:             if (exists($settings->{'rules'})) {
 6224:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6225:                     %rules = %{$settings->{'rules'}};
 6226:                 }
 6227:             }
 6228:         } elsif ($position eq 'middle') {
 6229:             if (exists($settings->{'suggested'})) {
 6230:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 6231:                     %suggestions = %{$settings->{'suggested'}};
 6232:                 }
 6233:             }
 6234:         } elsif ($position eq 'lower') {
 6235:             if (exists($settings->{'linkprot'})) {
 6236:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6237:                     %linkprot = %{$settings->{'linkprot'}};
 6238:                     if ($linkprot{'lock'}) {
 6239:                         delete($linkprot{'lock'});
 6240:                     }
 6241:                 }
 6242:             }
 6243:         } else {
 6244:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 6245:                 if (exists($settings->{$key})) {
 6246:                     delete($settings->{$key});
 6247:                 }
 6248:             }
 6249:         }
 6250:     }
 6251:     if ($position eq 'top') {
 6252:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6253:     } elsif ($position eq 'upper') {
 6254:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6255:         $$rowtotal += $itemcount;
 6256:     } elsif ($position eq 'middle') {
 6257:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 6258:         $$rowtotal += $itemcount;
 6259:     } elsif ($position eq 'lower') {
 6260:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6261:     } else {
 6262:         my ($switchserver,$switchmessage);
 6263:         $switchserver = &check_switchserver($dom);
 6264:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6265:         my $maxnum = 0;
 6266:         my %ordered;
 6267:         if (ref($settings) eq 'HASH') {
 6268:             foreach my $item (keys(%{$settings})) {
 6269:                 if (ref($settings->{$item}) eq 'HASH') {
 6270:                     my $num = $settings->{$item}{'order'};
 6271:                     if ($num eq '') {
 6272:                         $num = scalar(keys(%{$settings}));
 6273:                     }
 6274:                     $ordered{$num} = $item;
 6275:                 }
 6276:             }
 6277:         }
 6278:         $maxnum = scalar(keys(%ordered));
 6279:         my %lt = &lti_names();
 6280:         if (keys(%ordered)) {
 6281:             my @items = sort { $a <=> $b } keys(%ordered);
 6282:             for (my $i=0; $i<@items; $i++) {
 6283:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6284:                 my $item = $ordered{$items[$i]};
 6285:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6286:                 if (ref($settings->{$item}) eq 'HASH') {
 6287:                     $key = $settings->{$item}->{'key'};
 6288:                     $usable = $settings->{$item}->{'usable'};
 6289:                     $lifetime = $settings->{$item}->{'lifetime'};
 6290:                     $consumer = $settings->{$item}->{'consumer'};
 6291:                     $requser = $settings->{$item}->{'requser'};
 6292:                     $crsinc = $settings->{$item}->{'crsinc'};
 6293:                     $current = $settings->{$item};
 6294:                 }
 6295:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6296:                 my %checkedrequser = (
 6297:                                        yes => ' checked="checked"',
 6298:                                        no  => '',
 6299:                                      );
 6300:                 if (!$requser) {
 6301:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6302:                     $checkedrequser{'yes'} = '';
 6303:                 }
 6304:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6305:                 my %checkedcrsinc = (
 6306:                                       yes => ' checked="checked"',
 6307:                                       no  => '',
 6308:                                     );
 6309:                 if (!$crsinc) {
 6310:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6311:                     $checkedcrsinc{'yes'} = '';
 6312:                 }
 6313:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6314:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6315:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6316:                 for (my $k=0; $k<=$maxnum; $k++) {
 6317:                     my $vpos = $k+1;
 6318:                     my $selstr;
 6319:                     if ($k == $i) {
 6320:                         $selstr = ' selected="selected" ';
 6321:                     }
 6322:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6323:                 }
 6324:                 $datatable .= '</select>'.('&nbsp;'x2).
 6325:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6326:                     &mt('Delete?').'</label></span></td>'.
 6327:                     '<td colspan="2">'.
 6328:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6329:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6330:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6331:                     ('&nbsp;'x2).
 6332:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6333:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6334:                     ('&nbsp;'x2).
 6335:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6336:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6337:                 if ($key ne '') {
 6338:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6339:                     if ($switchserver) {
 6340:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6341:                     } else {
 6342:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6343:                     }
 6344:                     $datatable .= '</span> '.('&nbsp;'x2);
 6345:                 } elsif (!$switchserver) {
 6346:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6347:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6348:                                   '</span> '.('&nbsp;'x2);
 6349:                 }
 6350:                 if ($switchserver) {
 6351:                     if ($usable ne '') {
 6352:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6353:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6354:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6355:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6356:                                       ('&nbsp;'x2).
 6357:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6358:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6359:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6360:                                       '</div>';
 6361:                     } elsif ($key eq '') {
 6362:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6363:                     } else {
 6364:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6365:                     }
 6366:                 } else {
 6367:                     if ($usable ne '') {
 6368:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6369:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6370:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6371:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6372:                                       ('&nbsp;'x2).
 6373:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6374:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6375:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6376:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6377:                                       '<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>';
 6378:                     } else {
 6379:                         $datatable .=
 6380:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6381:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6382:                             '<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>';
 6383:                     }
 6384:                 }
 6385:                 $datatable .= '<br /><br />'.
 6386:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6387:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6388:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6389:                     '<br /><br />'.
 6390:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6391:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6392:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6393:                     ('&nbsp;'x4).
 6394:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6395:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6396:                 $itemcount ++;
 6397:             }
 6398:         }
 6399:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6400:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6401:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6402:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6403:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6404:         for (my $k=0; $k<$maxnum+1; $k++) {
 6405:             my $vpos = $k+1;
 6406:             my $selstr;
 6407:             if ($k == $maxnum) {
 6408:                 $selstr = ' selected="selected" ';
 6409:             }
 6410:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6411:         }
 6412:         $datatable .= '</select>&nbsp;'."\n".
 6413:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6414:                       '<td colspan="2">'.
 6415:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6416:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6417:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6418:                       ('&nbsp;'x2).
 6419:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6420:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6421:                       ('&nbsp;'x2).
 6422:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6423:         if ($switchserver) {
 6424:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6425:         } else {
 6426:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6427:                           ('&nbsp;'x2).
 6428:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6429:                           '<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";
 6430:         }
 6431:         $datatable .= '<br /><br />'.
 6432:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6433:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6434:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6435:                       '<br /><br />'.
 6436:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6437:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6438:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6439:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6440:                       '</td>'."\n".
 6441:                       '</tr>'."\n";
 6442:         $itemcount ++;
 6443:     }
 6444:     $$rowtotal += $itemcount;
 6445:     return $datatable;
 6446: }
 6447: 
 6448: sub lti_names {
 6449:     my %lt = &Apache::lonlocal::texthash(
 6450:                                           'version'   => 'LTI Version',
 6451:                                           'url'       => 'URL',
 6452:                                           'key'       => 'Key',
 6453:                                           'lifetime'  => 'Nonce lifetime (s)',
 6454:                                           'consumer'  => 'Consumer',
 6455:                                           'secret'    => 'Secret',
 6456:                                           'requser'   => "User's identity sent",
 6457:                                           'crsinc'    => "Course's identity sent",
 6458:                                           'email'     => 'Email address',
 6459:                                           'sourcedid' => 'User ID',
 6460:                                           'other'     => 'Other',
 6461:                                           'passback'  => 'Can return grades to Consumer:',
 6462:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6463:                                           'topmenu'   => 'Display LON-CAPA page header',
 6464:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6465:                                         );
 6466:     return %lt;
 6467: }
 6468: 
 6469: sub lti_options {
 6470:     my ($num,$current,$itemcount,%lt) = @_;
 6471:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6472:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6473:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6474:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6475:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6476:     $checked{'mapcrstype'} = {};
 6477:     $checked{'makeuser'} = {};
 6478:     $checked{'selfenroll'} = {};
 6479:     $checked{'crssec'} = {};
 6480:     $checked{'crssecsrc'} = {};
 6481:     $checked{'lcauth'} = {};
 6482:     $checked{'menuitem'} = {};
 6483:     if ($num eq 'add') {
 6484:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6485:     }
 6486:     my $userfieldsty = 'none';
 6487:     my $crsfieldsty = 'none';
 6488:     my $crssecfieldsty = 'none';
 6489:     my $secsrcfieldsty = 'none';
 6490:     my $callbacksty = 'none';
 6491:     my $passbacksty = 'none';
 6492:     my $optionsty = 'block';
 6493:     my $crssty = 'block';
 6494:     my $lcauthparm;
 6495:     my $lcauthparmstyle = 'display:none';
 6496:     my $lcauthparmtext;
 6497:     my $menusty;
 6498:     my $numinrow = 4;
 6499:     my %menutitles = &ltimenu_titles();
 6500: 
 6501:     if (ref($current) eq 'HASH') {
 6502:         if (!$current->{'requser'}) {
 6503:             $optionsty = 'none';
 6504:             $crssty = 'none';
 6505:         } elsif (!$current->{'crsinc'}) {
 6506:             $crssty = 'none';
 6507:         }
 6508:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6509:             $checked{'mapuser'}{'sourcedid'} = '';
 6510:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6511:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6512:             } else {
 6513:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6514:                 $userfield = $current->{'mapuser'};
 6515:                 $userfieldsty = 'inline-block';
 6516:             }
 6517:         }
 6518:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6519:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6520:             if ($current->{'mapcrs'} eq 'context_id') {
 6521:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6522:             } else {
 6523:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6524:                 $cidfield = $current->{'mapcrs'};
 6525:                 $crsfieldsty = 'inline-block';
 6526:             }
 6527:         }
 6528:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6529:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6530:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6531:             }
 6532:         }
 6533:         if (!$current->{'storecrs'}) {
 6534:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6535:             $checked{'storecrs'}{'Y'} = '';
 6536:         }
 6537:         if ($current->{'makecrs'}) {
 6538:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6539:         }
 6540:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6541:             foreach my $role (@{$current->{'makeuser'}}) {
 6542:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6543:             }
 6544:         }
 6545:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6546:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6547:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6548:                 $lcauthparm = $current->{'lcauthparm'};
 6549:                 $lcauthparmstyle = 'display:table-row';
 6550:                 if ($current->{'lcauth'} eq 'localauth') {
 6551:                     $lcauthparmtext = &mt('Local auth argument');
 6552:                 } else {
 6553:                     $lcauthparmtext = &mt('Kerberos domain');
 6554:                 }
 6555:             }
 6556:         }
 6557:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6558:             foreach my $role (@{$current->{'selfenroll'}}) {
 6559:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6560:             }
 6561:         }
 6562:         if (ref($current->{'maproles'}) eq 'HASH') {
 6563:             %rolemaps = %{$current->{'maproles'}};
 6564:         }
 6565:         if ($current->{'section'} ne '') {
 6566:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6567:             $crssecfieldsty = 'inline-block';
 6568:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6569:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6570:             } else {
 6571:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6572:                 $crssecsrc = $current->{'section'};
 6573:                 $secsrcfieldsty = 'inline-block';
 6574:             }
 6575:         } else {
 6576:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6577:         }
 6578:         if ($current->{'callback'} ne '') {
 6579:             $callback = $current->{'callback'};
 6580:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6581:             $callbacksty = 'inline-block';
 6582:         } else {
 6583:             $checked{'callback'}{'N'} = ' checked="checked"';
 6584:         }
 6585:         if ($current->{'topmenu'}) {
 6586:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6587:         } else {
 6588:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6589:         }
 6590:         if ($current->{'inlinemenu'}) {
 6591:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6592:         } else {
 6593:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6594:         }
 6595:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6596:             $menusty = 'inline-block';
 6597:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6598:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6599:                     if (exists($menutitles{$item})) {
 6600:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6601:                     }
 6602:                 }
 6603:             }
 6604:         } else {
 6605:             $menusty = 'none';
 6606:         }
 6607:     } else {
 6608:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6609:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6610:         $checked{'callback'}{'N'} = ' checked="checked"';
 6611:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6612:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6613:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6614:         $menusty = 'inline-block';
 6615:     }
 6616:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6617:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6618:                                official   => 'Official',
 6619:                                unofficial => 'Unofficial',
 6620:                                community  => 'Community',
 6621:                                textbook   => 'Textbook',
 6622:                                placement  => 'Placement Test',
 6623:                                lti        => 'LTI Provider',
 6624:     );
 6625:     my @authtypes = ('internal','krb4','krb5','localauth');
 6626:     my %shortauth = (
 6627:                      internal => 'int',
 6628:                      krb4 => 'krb4',
 6629:                      krb5 => 'krb5',
 6630:                      localauth  => 'loc'
 6631:                     );
 6632:     my %authnames = &authtype_names();
 6633:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6634:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6635:     my @courseroles = ('cc','in','ta','ep','st');
 6636:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6637:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6638:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6639:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6640:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6641:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6642:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6643:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6644:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6645:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6646:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6647:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6648:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6649:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6650:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6651:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6652:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6653:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6654:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6655:     foreach my $option ('sourcedid','email','other') {
 6656:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6657:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6658:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6659:     }
 6660:     $output .= '</span></div>'.
 6661:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6662:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6663:                'value="'.$userfield.'" /></div></fieldset>'.
 6664:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6665:     foreach my $ltirole (@ltiroles) {
 6666:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6667:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6668:     }
 6669:     $output .= '</fieldset>'.
 6670:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6671:                '<table>'.
 6672:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6673:                '</table>'.
 6674:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6675:                '<td class="LC_left_item">';
 6676:     foreach my $auth ('lti',@authtypes) {
 6677:         my $authtext;
 6678:         if ($auth eq 'lti') {
 6679:             $authtext = &mt('None');
 6680:         } else {
 6681:             $authtext = $authnames{$shortauth{$auth}};
 6682:         }
 6683:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6684:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6685:                    $authtext.'</label></span> &nbsp;';
 6686:     }
 6687:     $output .= '</td></tr>'.
 6688:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6689:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6690:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6691:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6692:                '</table></fieldset>'.
 6693:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6694:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6695:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6696:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6697:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6698:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6699:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6700:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6701:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6702:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6703:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6704:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6705:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6706:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6707:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6708:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6709:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6710:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6711:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6712:                    ('&nbsp;'x2);
 6713:     }
 6714:     $output .= '</span></div></fieldset>'.
 6715:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6716:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6717:                &mt('Unique course identifier').':&nbsp;';
 6718:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6719:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6720:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6721:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6722:     }
 6723:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6724:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6725:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6726:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6727:     foreach my $type (@coursetypes) {
 6728:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6729:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6730:                    ('&nbsp;'x2);
 6731:     }
 6732:     $output .= '</span><br /><br />'.
 6733:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6734:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6735:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6736:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6737:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6738:                '</fieldset>'.
 6739:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6740:     foreach my $ltirole (@lticourseroles) {
 6741:         my ($selected,$selectnone);
 6742:         if ($rolemaps{$ltirole} eq '') {
 6743:             $selectnone = ' selected="selected"';
 6744:         }
 6745:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6746:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6747:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6748:         foreach my $role (@courseroles) {
 6749:             unless ($selectnone) {
 6750:                 if ($rolemaps{$ltirole} eq $role) {
 6751:                     $selected = ' selected="selected"';
 6752:                 } else {
 6753:                     $selected = '';
 6754:                 }
 6755:             }
 6756:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6757:                        &Apache::lonnet::plaintext($role,'Course').
 6758:                        '</option>';
 6759:         }
 6760:         $output .= '</select></td>';
 6761:     }
 6762:     $output .= '</tr></table></fieldset>'.
 6763:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6764:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6765:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6766:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6767:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6768:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6769:                '</fieldset>'.
 6770:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6771:     foreach my $lticrsrole (@lticourseroles) {
 6772:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6773:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6774:     }
 6775:     $output .= '</fieldset>'.
 6776:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6777:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6778:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6779:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6780:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6781:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6782:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6783:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6784:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6785:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6786:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6787:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6788:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6789:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6790:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6791:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6792:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6793:     foreach my $extra ('roster','passback') {
 6794:         my $checkedon = '';
 6795:         my $checkedoff = ' checked="checked"';
 6796:         if ($extra eq 'passback') {
 6797:             $pb1p1chk = ' checked="checked"';
 6798:             $pb1p0chk = '';
 6799:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6800:         } else {
 6801:             $onclickpb = '';
 6802:         }
 6803:         if (ref($current) eq 'HASH') {
 6804:             if (($current->{$extra})) {
 6805:                 $checkedon = $checkedoff;
 6806:                 $checkedoff = '';
 6807:                 if ($extra eq 'passback') {
 6808:                     $passbacksty = 'inline-block';
 6809:                 }
 6810:                 if ($current->{'passbackformat'} eq '1.0') {
 6811:                     $pb1p0chk =  ' checked="checked"';
 6812:                     $pb1p1chk = '';
 6813:                 }
 6814:             }
 6815:         }
 6816:         $output .= $lt{$extra}.'&nbsp;'.
 6817:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6818:                    &mt('No').'</label>'.('&nbsp;'x2).
 6819:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6820:                    &mt('Yes').'</label><br />';
 6821:     }
 6822:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6823:                '<span class="LC_nobreak">'.&mt('Grade format').
 6824:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6825:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6826:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6827:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6828:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6829:     $output .= '</span></div></fieldset>';
 6830: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6831: #
 6832: #    $output .= '</fieldset>'.
 6833: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6834:     return $output;
 6835: }
 6836: 
 6837: sub ltimenu_titles {
 6838:     return &Apache::lonlocal::texthash(
 6839:                                         fullname    => 'Full name',
 6840:                                         coursetitle => 'Course title',
 6841:                                         role        => 'Role',
 6842:                                         logout      => 'Logout',
 6843:                                         grades      => 'Grades',
 6844:     );
 6845: }
 6846: 
 6847: sub linkprot_suggestions {
 6848:     my ($suggested,$itemcount) = @_;
 6849:     my $count = 0;
 6850:     my $next = 1;
 6851:     my %lt = &Apache::lonlocal::texthash(
 6852:                                           'name' => 'Suggested Launcher',
 6853:                                           'info' => 'Recommendations',
 6854:                                         );
 6855:     my ($datatable,$css_class,$dest);
 6856:     if (ref($suggested) eq 'HASH') {
 6857:         my @current = sort { $a <=> $b } keys(%{$suggested});
 6858:         $next += $current[-1];
 6859:         for (my $i=0; $i<@current; $i++) {
 6860:             my $num = $current[$i];
 6861:             my %values;
 6862:             if (ref($suggested->{$num}) eq 'HASH') {
 6863:                 %values = %{$suggested->{$num}};
 6864:             } else {
 6865:                 next;
 6866:             }
 6867:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6868:             $datatable .=
 6869:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6870:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 6871:                 &mt('Delete?').'</label></span></td><td>'."\n".
 6872:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6873:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 6874:                 '</fieldset></div>'.
 6875:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6876:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 6877:                 '</fieldset></div>'.
 6878:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6879:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 6880:             $$itemcount ++;
 6881:         }
 6882:     }
 6883:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6884:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6885:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 6886:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6887:                   '<td>'."\n".
 6888:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6889:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 6890:                   '</fieldset></div>'.
 6891:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6892:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 6893:                   '</fieldset></div>'.
 6894:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6895:                   '</td></tr>'."\n";
 6896:     return $datatable;
 6897: }
 6898: 
 6899: sub print_coursedefaults {
 6900:     my ($position,$dom,$settings,$rowtotal) = @_;
 6901:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6902:     my $itemcount = 1;
 6903:     my %choices =  &Apache::lonlocal::texthash (
 6904:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6905:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6906:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course (MB)',
 6907:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6908:         coursecredits        => 'Credits can be specified for courses',
 6909:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6910:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6911:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6912:         texengine            => 'Default method to display mathematics',
 6913:         postsubmit           => 'Disable submit button/keypress following student submission',
 6914:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6915:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6916:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6917:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6918:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6919:         crsauthor            => 'Standard LON-CAPA problems can be created within a course/community (by type)',
 6920:         crseditors           => 'Available editors for web pages and/or problems created in a course/community',
 6921:     );
 6922:     my %staticdefaults = (
 6923:                            anonsurvey_threshold => 10,
 6924:                            uploadquota          => 500,
 6925:                            coursequota          => 20,
 6926:                            postsubmit           => 60,
 6927:                            mysqltables          => 172800,
 6928:                            domexttool           => 1,
 6929:                            exttool              => 0,
 6930:                            crsauthor            => 1,
 6931:                            crseditors           => ['edit','xml'],  
 6932:                          );
 6933:     if ($position eq 'top') {
 6934:         %defaultchecked = (
 6935:                             'canuse_pdfforms' => 'off',
 6936:                             'uselcmath'       => 'on',
 6937:                             'usejsme'         => 'on',
 6938:                             'inline_chem'     => 'on',
 6939:                             'canclone'        => 'none',
 6940:                           );
 6941:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6942:         my $deftex = $Apache::lonnet::deftex;
 6943:         if (ref($settings) eq 'HASH') {
 6944:             if ($settings->{'texengine'}) {
 6945:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6946:                     $deftex = $settings->{'texengine'};
 6947:                 }
 6948:             }
 6949:         }
 6950:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6951:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6952:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6953:                        '</span></td><td class="LC_right_item">'.
 6954:                        '<select name="texengine">'."\n";
 6955:         my %texoptions = (
 6956:                             MathJax  => 'MathJax',
 6957:                             mimetex  => &mt('Convert to Images'),
 6958:                             tth      => &mt('TeX to HTML'),
 6959:                          );
 6960:         foreach my $renderer ('MathJax','mimetex','tth') {
 6961:             my $selected = '';
 6962:             if ($renderer eq $deftex) {
 6963:                 $selected = ' selected="selected"';
 6964:             }
 6965:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6966:         }
 6967:         $mathdisp .= '</select></td></tr>'."\n";
 6968:         $itemcount ++;
 6969:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6970:                                                      \%choices,$itemcount);
 6971:         $datatable = $mathdisp.$datatable;
 6972:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6973:         $datatable .=
 6974:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6975:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6976:             '</span></td><td class="LC_left_item">';
 6977:         my $currcanclone = 'none';
 6978:         my $onclick;
 6979:         my @cloneoptions = ('none','domain');
 6980:         my %clonetitles = &Apache::lonlocal::texthash (
 6981:                              none     => 'No additional course requesters',
 6982:                              domain   => "Any course requester in course's domain",
 6983:                              instcode => 'Course requests for official courses ...',
 6984:                           );
 6985:         my (%codedefaults,@code_order,@posscodes);
 6986:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6987:                                                     \@code_order) eq 'ok') {
 6988:             if (@code_order > 0) {
 6989:                 push(@cloneoptions,'instcode');
 6990:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6991:             }
 6992:         }
 6993:         if (ref($settings) eq 'HASH') {
 6994:             if ($settings->{'canclone'}) {
 6995:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6996:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6997:                         if (@code_order > 0) {
 6998:                             $currcanclone = 'instcode';
 6999:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 7000:                         }
 7001:                     }
 7002:                 } elsif ($settings->{'canclone'} eq 'domain') {
 7003:                     $currcanclone = $settings->{'canclone'};
 7004:                 }
 7005:             }
 7006:         }
 7007:         foreach my $option (@cloneoptions) {
 7008:             my ($checked,$additional);
 7009:             if ($currcanclone eq $option) {
 7010:                 $checked = ' checked="checked"';
 7011:             }
 7012:             if ($option eq 'instcode') {
 7013:                 if (@code_order) {
 7014:                     my $show = 'none';
 7015:                     if ($checked) {
 7016:                         $show = 'block';
 7017:                     }
 7018:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 7019:                                   &mt('Institutional codes for new and cloned course have identical:').
 7020:                                   '<br />';
 7021:                     foreach my $item (@code_order) {
 7022:                         my $codechk;
 7023:                         if ($checked) {
 7024:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 7025:                                 $codechk = ' checked="checked"';
 7026:                             }
 7027:                         }
 7028:                         $additional .= '<label>'.
 7029:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 7030:                                        $item.'</label>';
 7031:                     }
 7032:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 7033:                 }
 7034:             }
 7035:             $datatable .=
 7036:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 7037:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 7038:                 '</label>&nbsp;'.$additional.'</span><br />';
 7039:         }
 7040:         $datatable .= '</td>'.
 7041:                       '</tr>';
 7042:         $itemcount ++;
 7043:     } else {
 7044:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7045:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 7046:             %deftimeout,%currmysql);
 7047:         my $currusecredits = 0;
 7048:         my $postsubmitclient = 1;
 7049:         my $ltiauth = 0;
 7050:         my %domexttool;
 7051:         my %exttool;
 7052:         my %crsauthor;
 7053:         my %crseditors;
 7054:         my @types = ('official','unofficial','community','textbook','placement');
 7055:         if (ref($settings) eq 'HASH') {
 7056:             if ($settings->{'ltiauth'}) {
 7057:                 $ltiauth = 1;
 7058:             }
 7059:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 7060:                 foreach my $type (@types) {
 7061:                     if ($settings->{'domexttool'}->{$type}) {
 7062:                         $domexttool{$type} = ' checked="checked"';
 7063:                     }
 7064:                 }
 7065:             } else {
 7066:                 foreach my $type (@types) {
 7067:                     if ($staticdefaults{'domexttool'}) {
 7068:                         $domexttool{$type} = ' checked="checked"';
 7069:                     }
 7070:                 }
 7071:             }
 7072:             if (ref($settings->{'exttool'}) eq 'HASH') {
 7073:                 foreach my $type (@types) {
 7074:                     if ($settings->{'exttool'}->{$type}) {
 7075:                         $exttool{$type} = ' checked="checked"';
 7076:                     }
 7077:                 }
 7078:             }
 7079:             if (ref($settings->{'crsauthor'}) eq 'HASH') {
 7080:                 foreach my $type (@types) {
 7081:                     if ($settings->{'crsauthor'}->{$type}) {
 7082:                         $crsauthor{$type} = ' checked="checked"';
 7083:                     }
 7084:                 }
 7085:             } else {
 7086:                 foreach my $type (@types) {
 7087:                     if ($staticdefaults{'crsauthor'}) {
 7088:                         $crsauthor{$type} = ' checked="checked"';
 7089:                     }
 7090:                 }
 7091:             }
 7092:             if (ref($settings->{'crseditors'}) eq 'ARRAY') {
 7093:                 foreach my $editor (@{$settings->{'crseditors'}}) {
 7094:                     $crseditors{$editor} = ' checked="checked"';
 7095:                 }
 7096:             } else {
 7097:                 foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7098:                     $crseditors{$editor} = ' checked="checked"';
 7099:                 }
 7100:             }
 7101:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 7102:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 7103:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 7104:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 7105:                 }
 7106:             }
 7107:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 7108:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 7109:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 7110:                 }
 7111:             }
 7112:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 7113:                 foreach my $type (@types) {
 7114:                     next if ($type eq 'community');
 7115:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 7116:                     if ($defcredits{$type} ne '') {
 7117:                         $currusecredits = 1;
 7118:                     }
 7119:                 }
 7120:             }
 7121:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 7122:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 7123:                     $postsubmitclient = 0;
 7124:                     foreach my $type (@types) {
 7125:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7126:                     }
 7127:                 } else {
 7128:                     foreach my $type (@types) {
 7129:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 7130:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 7131:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 7132:                             } else {
 7133:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7134:                             }
 7135:                         } else {
 7136:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7137:                         }
 7138:                     }
 7139:                 }
 7140:             } else {
 7141:                 foreach my $type (@types) {
 7142:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7143:                 }
 7144:             }
 7145:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 7146:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 7147:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 7148:                 }
 7149:             } else {
 7150:                 foreach my $type (@types) {
 7151:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7152:                 }
 7153:             }
 7154:         } else {
 7155:             foreach my $type (@types) {
 7156:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7157:                 if ($staticdefaults{'domexttool'}) {
 7158:                     $domexttool{$type} = ' checked="checked"';
 7159:                 }
 7160:                 if ($staticdefaults{'crsauthor'}) {
 7161:                     $crsauthor{$type} = ' checked="checked"';
 7162:                 }
 7163:             }
 7164:             foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7165:                 $crseditors{$editor} = ' checked="checked"';
 7166:             }
 7167:         }
 7168:         if (!$currdefresponder) {
 7169:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7170:         } elsif ($currdefresponder < 1) {
 7171:             $currdefresponder = 1;
 7172:         }
 7173:         foreach my $type (@types) {
 7174:             if ($curruploadquota{$type} eq '') {
 7175:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7176:             }
 7177:             if ($currcoursequota{$type} eq '') {
 7178:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 7179:             }
 7180:         }
 7181:         $datatable .=
 7182:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7183:                 $choices{'anonsurvey_threshold'}.
 7184:                 '</span></td>'.
 7185:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7186:                 '<input type="text" name="anonsurvey_threshold"'.
 7187:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7188:                 '</td></tr>'."\n";
 7189:         $itemcount ++;
 7190:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7191:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7192:                       $choices{'uploadquota'}.
 7193:                       '</span></td>'.
 7194:                       '<td style="text-align: right" class="LC_right_item">'.
 7195:                       '<table><tr>';
 7196:         foreach my $type (@types) {
 7197:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7198:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7199:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7200:         }
 7201:         $datatable .= '</tr></table></td></tr>'."\n";
 7202:         $itemcount ++;
 7203:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7204:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7205:                       $choices{'coursequota'}.
 7206:                       '</span></td>'.
 7207:                       '<td style="text-align: right" class="LC_right_item">'.
 7208:                       '<table><tr>';
 7209:         foreach my $type (@types) {
 7210:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7211:                            '<input type="text" name="coursequota_'.$type.'"'.
 7212:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 7213:         }
 7214:         $datatable .= '</tr></table></td></tr>'."\n";
 7215:         $itemcount ++;
 7216:         my $onclick = "toggleDisplay(this.form,'credits');";
 7217:         my $display = 'none';
 7218:         if ($currusecredits) {
 7219:             $display = 'block';
 7220:         }
 7221:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7222:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7223:         foreach my $type (@types) {
 7224:             next if ($type eq 'community');
 7225:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7226:                            '<input type="text" name="'.$type.'_credits"'.
 7227:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7228:         }
 7229:         $additional .= '</tr></table></div>'."\n";
 7230:         %defaultchecked = ('coursecredits' => 'off');
 7231:         @toggles = ('coursecredits');
 7232:         my $current = {
 7233:                         'coursecredits' => $currusecredits,
 7234:                       };
 7235:         (my $table,$itemcount) =
 7236:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7237:                                \%choices,$itemcount,$onclick,$additional,'left');
 7238:         $datatable .= $table;
 7239:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7240:         my $display = 'none';
 7241:         if ($postsubmitclient) {
 7242:             $display = 'block';
 7243:         }
 7244:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7245:                       &mt('Number of seconds submit is disabled').'<br />'.
 7246:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7247:                       '<table><tr>';
 7248:         foreach my $type (@types) {
 7249:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7250:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7251:                            $deftimeout{$type}.'" size="5" /></td>';
 7252:         }
 7253:         $additional .= '</tr></table></div>'."\n";
 7254:         %defaultchecked = ('postsubmit' => 'on');
 7255:         @toggles = ('postsubmit');
 7256:         $current = {
 7257:                        'postsubmit' => $postsubmitclient,
 7258:                    };
 7259:         ($table,$itemcount) =
 7260:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7261:                                \%choices,$itemcount,$onclick,$additional,'left');
 7262:         $datatable .= $table;
 7263:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7264:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7265:                       $choices{'mysqltables'}.
 7266:                       '</span></td>'.
 7267:                       '<td style="text-align: right" class="LC_right_item">'.
 7268:                       '<table><tr>';
 7269:         foreach my $type (@types) {
 7270:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7271:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7272:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7273:         }
 7274:         $datatable .= '</tr></table></td></tr>'."\n";
 7275:         $itemcount ++;
 7276:         %defaultchecked = ('ltiauth' => 'off');
 7277:         @toggles = ('ltiauth');
 7278:         $current = {
 7279:                        'ltiauth' => $ltiauth,
 7280:                    };
 7281:         ($table,$itemcount) =
 7282:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7283:                                \%choices,$itemcount,undef,undef,'left');
 7284:         $datatable .= $table;
 7285:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7286:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7287:                       $choices{'domexttool'}.
 7288:                       '</span></td>'.
 7289:                       '<td style="text-align: right" class="LC_right_item">'.
 7290:                       '<table><tr>';
 7291:         foreach my $type (@types) {
 7292:             $datatable .= '<td style="text-align: left">'.
 7293:                           '<span class="LC_nobreak">'.
 7294:                           '<label><input type="checkbox" name="domexttool"'.
 7295:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7296:                           &mt($type).'</label></span></td>'."\n";
 7297:         }
 7298:         $datatable .= '</tr></table></td></tr>'."\n";
 7299:         $itemcount ++;
 7300:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7301:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7302:                       $choices{'exttool'}.
 7303:                       '</span></td>'.
 7304:                       '<td style="text-align: right" class="LC_right_item">'.
 7305:                       '<table><tr>';
 7306:         foreach my $type (@types) {
 7307:             $datatable .= '<td style="text-align: left">'.
 7308:                           '<span class="LC_nobreak">'.
 7309:                           '<label><input type="checkbox" name="exttool"'.
 7310:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7311:                           &mt($type).'</label></span></td>'."\n";
 7312:         }
 7313:         $datatable .= '</tr></table></td></tr>'."\n";
 7314:         $itemcount ++;
 7315:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7316:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7317:                       $choices{'crsauthor'}.
 7318:                       '</span></td>'.
 7319:                       '<td style="text-align: right" class="LC_right_item">'.
 7320:                       '<table><tr>';
 7321:         foreach my $type (@types) {
 7322:             $datatable .= '<td style="text-align: left">'.
 7323:                           '<span class="LC_nobreak">'.
 7324:                           '<label><input type="checkbox" name="crsauthor"'.
 7325:                           ' value="'.$type.'"'.$crsauthor{$type}.' />'.
 7326:                           &mt($type).'</label></span></td>'."\n";
 7327:         }
 7328:         $datatable .= '</tr></table></td></tr>'."\n";
 7329:         $itemcount ++;
 7330:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7331:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7332:                       $choices{'crseditors'}.
 7333:                       '</span></td>'.
 7334:                       '<td style="text-align: right" class="LC_right_item">'.
 7335:                       '<table><tr>';
 7336:         my @editors = ('edit','xml','daxe');
 7337:         my %editornames = &crseditor_titles();
 7338:         foreach my $editor (@editors) {
 7339:             $datatable .= '<td style="text-align: left">'.
 7340:                           '<span class="LC_nobreak">'.
 7341:                           '<label><input type="checkbox" name="crseditors"'.
 7342:                           ' value="'.$editor.'"'.$crseditors{$editor}.' />'.
 7343:                           $editornames{$editor}.'</label></span></td>'."\n";
 7344:         }
 7345:         $datatable .= '</tr></table></td></tr>'."\n";
 7346:     }
 7347:     $$rowtotal += $itemcount;
 7348:     return $datatable;
 7349: }
 7350: 
 7351: sub crseditor_titles {
 7352:     return &Apache::lonlocal::texthash(
 7353:                edit  => 'Standard editor (Edit)',
 7354:                xml   => 'Text editor (EditXML)',
 7355:                daxe  => 'Daxe editor (Daxe)',
 7356:            );
 7357: }
 7358: 
 7359: sub print_authordefaults {
 7360:     my ($position,$dom,$settings,$rowtotal) = @_;
 7361:     my ($css_class,$datatable,%checkedon,%checkedoff);
 7362:     my $itemcount = 1;
 7363:     my %titles = &authordefaults_titles();
 7364:     if ($position eq 'top') {
 7365:         my %defaultchecked = (
 7366:                             'nocodemirror' => 'off',
 7367:                             'daxecollapse' => 'off',
 7368:                             'domcoordacc'  => 'on',
 7369:                           );
 7370:         my @toggles = ('nocodemirror','daxecollapse','domcoordacc');
 7371:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 7372:                                                      \%titles,$itemcount);
 7373:         my %staticdefaults = (
 7374:                                 'copyright'    => 'default',
 7375:                                 'sourceavail'  => 'closed',
 7376:                              );
 7377:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7378:         my %currrights;
 7379:         foreach my $item ('copyright','sourceavail') {
 7380:             $currrights{$item} = $staticdefaults{$item};
 7381:             if (ref($settings) eq 'HASH') {
 7382:                 if (exists($settings->{$item})) {
 7383:                     $currrights{$item} = $settings->{$item};
 7384:                 }
 7385:             }
 7386:         }
 7387:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7388:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 7389:                       '</span></td><td class="LC_right_item">'.
 7390:                       &selectbox('copyright',$currrights{'copyright'},'',
 7391:                                  \&Apache::loncommon::copyrightdescription,
 7392:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 7393:                       '</td></tr>'."\n";
 7394:         $itemcount ++;
 7395:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7396:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7397:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 7398:                       '</span></td><td class="LC_right_item">'.
 7399:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 7400:                                  \&Apache::loncommon::source_copyrightdescription,
 7401:                                  (&Apache::loncommon::source_copyrightids)).
 7402:                       '</td></tr>'."\n";        
 7403:     } else {
 7404:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7405:         my $curreditors;
 7406:         my %staticdefaults = (
 7407:                                 editors => ['edit','xml'],
 7408:                                 authorquota => 500,
 7409:                                 webdav => 0,
 7410:                              );
 7411:         my $curreditors = $staticdefaults{'editors'};
 7412:         if ((ref($settings) eq 'HASH') &&
 7413:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 7414:             $curreditors = $settings->{'editors'};
 7415:         } else {
 7416:             $curreditors = $staticdefaults{'editors'};
 7417:         }
 7418:         my @editors = ('edit','xml','daxe');
 7419:         $datatable = '<tr'.$css_class.'>'."\n".
 7420:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 7421:                      '<td class="LC_left_item">'."\n".
 7422:                      '<span class="LC_nobreak">';
 7423:         foreach my $editor (@editors) {
 7424:             my $checked;
 7425:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 7426:                 $checked = ' checked="checked"';
 7427:             }
 7428:             $datatable .= '<label>'.
 7429:                           '<input type="checkbox" name="author_editors" '.
 7430:                           $checked.' value="'.$editor.'" '.
 7431:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 7432:                           $titles{$editor}.'</label>&nbsp;';
 7433:         }
 7434:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 7435:         $itemcount ++;
 7436:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7437:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7438:         my @insttypes;
 7439:         if (ref($types) eq 'ARRAY') {
 7440:             @insttypes = @{$types};
 7441:         }
 7442:         my $typecount = 0;
 7443:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 7444:         my @items = ('webdav','authorquota');
 7445:         my %quotas;
 7446:         if (ref($domconf{'quotas'}) eq 'HASH') {
 7447:             %quotas = %{$domconf{'quotas'}};
 7448:             foreach my $item (@items) {
 7449:                 if (ref($quotas{$item}) eq 'HASH') {
 7450:                     foreach my $type (@insttypes,'default') {
 7451:                         if ($item eq 'authorquota') {
 7452:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 7453:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7454:                             }
 7455:                         } elsif ($item eq 'webdav') {
 7456:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 7457:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7458:                             }
 7459:                         }
 7460:                     }
 7461:                 } else {
 7462:                     foreach my $type (@insttypes,'default') {
 7463:                         $quotas{$item}{$type} = $staticdefaults{$item};
 7464:                     }
 7465:                 }
 7466:             }
 7467:         } else {
 7468:             foreach my $item (@items) {
 7469:                 foreach my $type (@insttypes,'default') {
 7470:                     $quotas{$item}{$type} = $staticdefaults{$item};
 7471:                 }
 7472:             }
 7473:         }
 7474:         if (ref($usertypes) eq 'HASH') {
 7475:             my $numinrow = 4;
 7476:             my $onclick = '';
 7477:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7478:                                          $numinrow,$othertitle,'authorquota',
 7479:                                          \$itemcount,$onclick);
 7480:             $itemcount ++;
 7481:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7482:                                          $numinrow,$othertitle,'webdav',
 7483:                                          \$itemcount);
 7484:             $itemcount ++;
 7485:         }
 7486:         my $checkedno = ' checked="checked"';
 7487:         my ($checkedon,$checkedoff);
 7488:         if (ref($quotas{'webdav'}) eq 'HASH') {
 7489:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) {
 7490:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 7491:                     $checkedon = $checkedno;
 7492:                 } else {
 7493:                     $checkedoff = $checkedno;
 7494:                 }
 7495:                 undef($checkedno);
 7496:             }
 7497:         }
 7498:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7499:         $datatable .= '<tr'.$css_class.'>'.
 7500:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 7501:                              $titles{'webdav_LC_adv_over'}.
 7502:                       '</td>'.
 7503:                       '<td class="LC_left_item">';
 7504:         foreach my $option ('none','off','on') {
 7505:             my ($text,$val,$checked);
 7506:             if ($option eq 'none') {
 7507:                 $text = $titles{'none'};
 7508:                 $val = '';
 7509:                 $checked = $checkedno;
 7510:             } elsif ($option eq 'off') {
 7511:                 $text = $titles{'overoff'};
 7512:                 $val = 0;
 7513:                 $checked = $checkedoff;
 7514:             } elsif ($option eq 'on') {
 7515:                 $text = $titles{'overon'};
 7516:                 $val = 1;
 7517:                 $checked = $checkedon;
 7518:             }
 7519:             $datatable .= '<span class="LC_nobreak"><label>'.
 7520:                           '<input type="radio" name="webdav_LC_adv"'.
 7521:                           ' value="'.$val.'"'.$checked.' />'.
 7522:                           $text.'</label></span>&nbsp; ';
 7523:         }
 7524:         $datatable .= '</td></tr>';
 7525:         $itemcount ++;
 7526:         my %defchecked = (
 7527:                                 'archive' => 'off',
 7528:                              );
 7529:         my @toggles = ('archive');
 7530:         (my $archive,$itemcount) = &radiobutton_prefs($settings,['archive'],
 7531:                                                       {'archive' => 'off'},
 7532:                                                       \%titles,$itemcount);
 7533:         $datatable .= $archive."\n";
 7534:         $itemcount ++;
 7535:     }
 7536:     $$rowtotal += $itemcount;
 7537:     return $datatable;
 7538: }
 7539: 
 7540: sub authordefaults_titles {
 7541:     return &Apache::lonlocal::texthash(
 7542:                copyright => 'Copyright/Distribution',
 7543:                sourceavail => ' Source Available',
 7544:                editors => 'Available Editors',
 7545:                webdav => 'WebDAV',
 7546:                authorquota => 'Authoring Space quotas (MB)',
 7547:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 7548:                daxecollapse => 'Daxe editor: LON-CAPA standard menus start collapsed',
 7549:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 7550:                edit  => 'Standard editor (Edit)',
 7551:                xml   => 'Text editor (EditXML)',
 7552:                daxe  => 'Daxe editor (Daxe)',
 7553:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 7554:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 7555:                none => 'No override set',
 7556:                overon => 'Override -- webDAV on',
 7557:                overoff => 'Override -- webDAV off',
 7558:                archive => 'Authors can download tar.gz file of Authoring Space',
 7559:     );
 7560: }
 7561: 
 7562: sub selectbox {
 7563:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 7564:     my $selout = '<select name="'.$name.'">';
 7565:     foreach my $id (@idlist) {
 7566:         $selout.='<option value="'.$id.'"';
 7567:         if ($id eq $value) {
 7568:             $selout.=' selected="selected"';
 7569:         }
 7570:         if ($readonly) {
 7571:             $selout .= ' disabled="disabled"';
 7572:         }
 7573:         $selout.='>'.&{$functionref}($id).'</option>';
 7574:     }
 7575:     $selout.='</select>';
 7576:     return $selout;
 7577: }
 7578: 
 7579: sub print_selfenrollment {
 7580:     my ($position,$dom,$settings,$rowtotal) = @_;
 7581:     my ($css_class,$datatable);
 7582:     my $itemcount = 1;
 7583:     my @types = ('official','unofficial','community','textbook','placement');
 7584:     if (($position eq 'top') || ($position eq 'middle')) {
 7585:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7586:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7587:         my @rows;
 7588:         my $key;
 7589:         if ($position eq 'top') {
 7590:             $key = 'admin'; 
 7591:             if (ref($rowsref) eq 'ARRAY') {
 7592:                 @rows = @{$rowsref};
 7593:             }
 7594:         } elsif ($position eq 'middle') {
 7595:             $key = 'default';
 7596:             @rows = ('types','registered','approval','limit');
 7597:         }
 7598:         foreach my $row (@rows) {
 7599:             if (defined($titlesref->{$row})) {
 7600:                 $itemcount ++;
 7601:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7602:                 $datatable .= '<tr'.$css_class.'>'.
 7603:                               '<td>'.$titlesref->{$row}.'</td>'.
 7604:                               '<td class="LC_left_item">'.
 7605:                               '<table><tr>';
 7606:                 my (%current,%currentcap);
 7607:                 if (ref($settings) eq 'HASH') {
 7608:                     if (ref($settings->{$key}) eq 'HASH') {
 7609:                         foreach my $type (@types) {
 7610:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7611:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7612:                             }
 7613:                             if (($row eq 'limit') && ($key eq 'default')) {
 7614:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7615:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7616:                                 }
 7617:                             }
 7618:                         }
 7619:                     }
 7620:                 }
 7621:                 my %roles = (
 7622:                              '0' => &Apache::lonnet::plaintext('dc'),
 7623:                             ); 
 7624:             
 7625:                 foreach my $type (@types) {
 7626:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7627:                         $datatable .= '<th>'.&mt($type).'</th>';
 7628:                     }
 7629:                 }
 7630:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7631:                     $datatable .= '</tr><tr>';
 7632:                 }
 7633:                 foreach my $type (@types) {
 7634:                     if ($type eq 'community') {
 7635:                         $roles{'1'} = &mt('Community personnel');
 7636:                     } else {
 7637:                         $roles{'1'} = &mt('Course personnel');
 7638:                     }
 7639:                     $datatable .= '<td style="vertical-align: top">';
 7640:                     if ($position eq 'top') {
 7641:                         my %checked;
 7642:                         if ($current{$type} eq '0') {
 7643:                             $checked{'0'} = ' checked="checked"';
 7644:                         } else {
 7645:                             $checked{'1'} = ' checked="checked"';
 7646:                         }
 7647:                         foreach my $role ('1','0') {
 7648:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7649:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7650:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7651:                                           $roles{$role}.'</label></span> ';
 7652:                         }
 7653:                     } else {
 7654:                         if ($row eq 'types') {
 7655:                             my %checked;
 7656:                             if ($current{$type} =~ /^(all|dom)$/) {
 7657:                                 $checked{$1} = ' checked="checked"';
 7658:                             } else {
 7659:                                 $checked{''} = ' checked="checked"';
 7660:                             }
 7661:                             foreach my $val ('','dom','all') {
 7662:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7663:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7664:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7665:                             }
 7666:                         } elsif ($row eq 'registered') {
 7667:                             my %checked;
 7668:                             if ($current{$type} eq '1') {
 7669:                                 $checked{'1'} = ' checked="checked"';
 7670:                             } else {
 7671:                                 $checked{'0'} = ' checked="checked"';
 7672:                             }
 7673:                             foreach my $val ('0','1') {
 7674:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7675:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7676:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7677:                             }
 7678:                         } elsif ($row eq 'approval') {
 7679:                             my %checked;
 7680:                             if ($current{$type} =~ /^([12])$/) {
 7681:                                 $checked{$1} = ' checked="checked"';
 7682:                             } else {
 7683:                                 $checked{'0'} = ' checked="checked"';
 7684:                             }
 7685:                             for my $val (0..2) {
 7686:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7687:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7688:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7689:                             }
 7690:                         } elsif ($row eq 'limit') {
 7691:                             my %checked;
 7692:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7693:                                 $checked{$1} = ' checked="checked"';
 7694:                             } else {
 7695:                                 $checked{'none'} = ' checked="checked"';
 7696:                             }
 7697:                             my $cap;
 7698:                             if ($currentcap{$type} =~ /^\d+$/) {
 7699:                                 $cap = $currentcap{$type};
 7700:                             }
 7701:                             foreach my $val ('none','allstudents','selfenrolled') {
 7702:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7703:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7704:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7705:                             }
 7706:                             $datatable .= '<br />'.
 7707:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7708:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7709:                                           '</span>'; 
 7710:                         }
 7711:                     }
 7712:                     $datatable .= '</td>';
 7713:                 }
 7714:                 $datatable .= '</tr>';
 7715:             }
 7716:             $datatable .= '</table></td></tr>';
 7717:         }
 7718:     } elsif ($position eq 'bottom') {
 7719:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7720:     }
 7721:     $$rowtotal += $itemcount;
 7722:     return $datatable;
 7723: }
 7724: 
 7725: sub print_validation_rows {
 7726:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7727:     my ($itemsref,$namesref,$fieldsref);
 7728:     if ($caller eq 'selfenroll') { 
 7729:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7730:     } elsif ($caller eq 'requestcourses') {
 7731:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7732:     }
 7733:     my %currvalidation;
 7734:     if (ref($settings) eq 'HASH') {
 7735:         if (ref($settings->{'validation'}) eq 'HASH') {
 7736:             %currvalidation = %{$settings->{'validation'}};
 7737:         }
 7738:     }
 7739:     my $datatable;
 7740:     my $itemcount = 0;
 7741:     foreach my $item (@{$itemsref}) {
 7742:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7743:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7744:                       $namesref->{$item}.
 7745:                       '</span></td>'.
 7746:                       '<td class="LC_left_item">';
 7747:         if (($item eq 'url') || ($item eq 'button')) {
 7748:             $datatable .= '<span class="LC_nobreak">'.
 7749:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7750:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7751:         } elsif ($item eq 'fields') {
 7752:             my @currfields;
 7753:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7754:                 @currfields = @{$currvalidation{$item}};
 7755:             }
 7756:             foreach my $field (@{$fieldsref}) {
 7757:                 my $check = '';
 7758:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7759:                     $check = ' checked="checked"';
 7760:                 }
 7761:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7762:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7763:                               ' value="'.$field.'"'.$check.' />'.$field.
 7764:                               '</label></span> ';
 7765:             }
 7766:         } elsif ($item eq 'markup') {
 7767:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7768:                            $currvalidation{$item}.
 7769:                               '</textarea>';
 7770:         }
 7771:         $datatable .= '</td></tr>'."\n";
 7772:         if (ref($rowtotal)) {
 7773:             $itemcount ++;
 7774:         }
 7775:     }
 7776:     if ($caller eq 'requestcourses') {
 7777:         my %currhash;
 7778:         if (ref($settings) eq 'HASH') {
 7779:             if (ref($settings->{'validation'}) eq 'HASH') {
 7780:                 if ($settings->{'validation'}{'dc'} ne '') {
 7781:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7782:                 }
 7783:             }
 7784:         }
 7785:         my $numinrow = 2;
 7786:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7787:                                                        'validationdc',%currhash);
 7788:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7789:         $datatable .= '<tr'.$css_class.'><td>';
 7790:         if ($numdc > 1) {
 7791:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7792:         } else {
 7793:             $datatable .=  &mt('Course creation processed as: ');
 7794:         }
 7795:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7796:         $itemcount ++;
 7797:     }
 7798:     if (ref($rowtotal)) {
 7799:         $$rowtotal += $itemcount;
 7800:     }
 7801:     return $datatable;
 7802: }
 7803: 
 7804: sub print_privacy {
 7805:     my ($position,$dom,$settings,$rowtotal) = @_;
 7806:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7807:     my $itemcount = 0;
 7808:     if ($position eq 'top') {
 7809:         $numinrow = 2;
 7810:     } else {
 7811:         @items = ('domain','author','course','community');
 7812:         %names = &Apache::lonlocal::texthash (
 7813:                      domain => 'Assigned domain role(s)',
 7814:                      author => 'Assigned co-author role(s)',
 7815:                      course => 'Assigned course role(s)',
 7816:                      community => 'Assigned community role(s)',
 7817:                  );
 7818:         $numinrow = 4;
 7819:         ($othertitle,$usertypes,$types) =
 7820:             &Apache::loncommon::sorted_inst_types($dom);
 7821:     }
 7822:     if (($position eq 'top') || ($position eq 'middle')) {
 7823:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7824:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7825:         if ($position eq 'top') {
 7826:             my %curr;
 7827:             my @options = ('none','user','domain','auto');
 7828:             my %titles = &Apache::lonlocal::texthash (
 7829:                 none   => 'Not allowed',
 7830:                 user   => 'User authorizes',
 7831:                 domain => 'DC authorizes',
 7832:                 auto   => 'Unrestricted',
 7833:                 instdom => 'Other domain shares institution/provider',
 7834:                 extdom => 'Other domain has different institution/provider',
 7835:                 notify => 'Notify when role needs authorization',
 7836:             );
 7837:             my %names = &Apache::lonlocal::texthash (
 7838:                 domain => 'Domain role',
 7839:                 author => 'Co-author role',
 7840:                 course => 'Course role',
 7841:                 community => 'Community role',
 7842:             );
 7843:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7844:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7845:             foreach my $domtype ('instdom','extdom') {
 7846:                 my (%checked,$skip);
 7847:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7848:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7849:                               '<td class="LC_left_item">';
 7850:                 if ($domtype eq 'instdom') {
 7851:                     unless (@instdoms > 1) {
 7852:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7853:                         $skip = 1;
 7854:                     }
 7855:                 } elsif ($domtype eq 'extdom') {
 7856:                     if (keys(%by_location) == 0) {
 7857:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7858:                         $skip = 1;
 7859:                     }
 7860:                 }
 7861:                 unless ($skip) {
 7862:                     foreach my $roletype ('domain','author','course','community') {
 7863:                         $checked{'auto'} = ' checked="checked"';
 7864:                         if (ref($settings) eq 'HASH') {
 7865:                             if (ref($settings->{approval}) eq 'HASH') {
 7866:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7867:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7868:                                         $checked{$1} = ' checked="checked"';
 7869:                                         $checked{'auto'} = '';
 7870:                                     }
 7871:                                 }
 7872:                             }
 7873:                         }
 7874:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7875:                         foreach my $option (@options) {
 7876:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7877:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7878:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7879:                                           '</label></span>&nbsp; ';
 7880:                         }
 7881:                         $datatable .= '</fieldset>';
 7882:                     }
 7883:                 }
 7884:                 $datatable .= '</td></tr>';
 7885:                 $itemcount ++;
 7886:             }
 7887:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7888:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7889:                           '<td class="LC_left_item">';
 7890:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7891:                 my %curr;
 7892:                 if (ref($settings) eq 'HASH') {
 7893:                     if ($settings->{'notify'} ne '') {
 7894:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7895:                     }
 7896:                 }
 7897:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7898:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7899:                                                              'privacy_notify',%curr);
 7900:                 if ($numdc > 0) {
 7901:                     $datatable .= $table;
 7902:                 } else {
 7903:                     $datatable .= &mt('There are no active Domain Coordinators');
 7904:                 }
 7905:             } else {
 7906:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7907:             }
 7908:             $datatable .='</td></tr>';
 7909:         } elsif ($position eq 'middle') {
 7910:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7911:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7912:                     foreach my $item (@{$types}) {
 7913:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7914:                                                                $numinrow,$itemcount,'','','','','',
 7915:                                                                '',$usertypes->{$item});
 7916:                         $itemcount ++;
 7917:                     }
 7918:                 }
 7919:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7920:                                                        $numinrow,$itemcount,'','','','','',
 7921:                                                        '',$othertitle);
 7922:                 $itemcount ++;
 7923:             } else {
 7924:                 my (@insttypes,%insttitles);
 7925:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7926:                     @insttypes = @{$types};
 7927:                     %insttitles = %{$usertypes};
 7928:                 }
 7929:                 foreach my $item (@insttypes,'default') {
 7930:                     my $title;
 7931:                     if ($item eq 'default') {
 7932:                         $title = $othertitle;
 7933:                     } else {
 7934:                         $title = $insttitles{$item};
 7935:                     }
 7936:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7937:                     $datatable .= '<tr'.$css_class.'>'.
 7938:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7939:                                   '<td class="LC_left_item">'.
 7940:                                   &mt('Nothing to set here, as there are no other domains').
 7941:                                   '</td></tr>';
 7942:                     $itemcount ++;
 7943:                 }
 7944:             }
 7945:         }
 7946:     } else {
 7947:         my $prefix;
 7948:         if ($position eq 'lower') {
 7949:             $prefix = 'priv';
 7950:         } else {
 7951:             $prefix = 'unpriv';
 7952:         }
 7953:         foreach my $item (@items) {
 7954:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7955:                                                    $numinrow,$itemcount,'','','','','',
 7956:                                                    '',$names{$item});
 7957:             $itemcount ++;
 7958:         }
 7959:     }
 7960:     if (ref($rowtotal)) {
 7961:         $$rowtotal += $itemcount;
 7962:     }
 7963:     return $datatable;
 7964: }
 7965: 
 7966: sub print_passwords {
 7967:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7968:     my ($datatable,$css_class);
 7969:     my $itemcount = 0;
 7970:     my %titles = &Apache::lonlocal::texthash (
 7971:         captcha        => '"Forgot Password" CAPTCHA validation',
 7972:         link           => 'Reset link expiration (hours)',
 7973:         case           => 'Case-sensitive usernames/e-mail',
 7974:         prelink        => 'Information required (form 1)',
 7975:         postlink       => 'Information required (form 2)',
 7976:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7977:         customtext     => 'Domain specific text (HTML)',
 7978:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7979:         intauth_check  => 'Check bcrypt cost if authenticated',
 7980:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7981:         permanent      => 'Permanent e-mail address',
 7982:         critical       => 'Critical notification address',
 7983:         notify         => 'Notification address',
 7984:         min            => 'Minimum password length',
 7985:         max            => 'Maximum password length',
 7986:         chars          => 'Required characters',
 7987:         expire         => 'Password expiration (days)',
 7988:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7989:     );
 7990:     if ($position eq 'top') {
 7991:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7992:         my $shownlinklife = 2;
 7993:         my $prelink = 'both';
 7994:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7995:         if (ref($settings) eq 'HASH') {
 7996:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7997:                 $shownlinklife = $settings->{resetlink};
 7998:             }
 7999:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 8000:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 8001:             }
 8002:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 8003:                 $prelink = $settings->{resetprelink};
 8004:             }
 8005:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 8006:                 %postlink = %{$settings->{resetpostlink}};
 8007:             }
 8008:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 8009:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 8010:             }
 8011:             if ($settings->{resetremove}) {
 8012:                 $nostdtext = 1;
 8013:             }
 8014:             if ($settings->{resetcustom}) {
 8015:                 $customurl = $settings->{resetcustom};
 8016:             }
 8017:         } else {
 8018:             if (ref($types) eq 'ARRAY') {
 8019:                 foreach my $item (@{$types}) {
 8020:                     $casesens{$item} = 1;
 8021:                     $postlink{$item} = ['username','email'];
 8022:                 }
 8023:             }
 8024:             $casesens{'default'} = 1;
 8025:             $postlink{'default'} = ['username','email'];
 8026:             $prelink = 'both';
 8027:             %emailsrc = (
 8028:                           permanent => 1,
 8029:                           critical  => 1,
 8030:                           notify    => 1,
 8031:             );
 8032:         }
 8033:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 8034:         $itemcount ++;
 8035:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8036:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 8037:                       '<td class="LC_left_item">'.
 8038:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 8039:                       'name="passwords_link" size="3" /></td></tr>';
 8040:         $itemcount ++;
 8041:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8042:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 8043:                       '<td class="LC_left_item">';
 8044:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8045:             foreach my $item (@{$types}) {
 8046:                 my $checkedcase;
 8047:                 if ($casesens{$item}) {
 8048:                     $checkedcase = ' checked="checked"';
 8049:                 }
 8050:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8051:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 8052:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 8053:                               '</span>&nbsp;&nbsp; ';
 8054:             }
 8055:         }
 8056:         my $checkedcase;
 8057:         if ($casesens{'default'}) {
 8058:             $checkedcase = ' checked="checked"';
 8059:         }
 8060:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8061:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 8062:                       $othertitle.'</label></span></td>';
 8063:         $itemcount ++;
 8064:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8065:         my %checkedpre = (
 8066:                              both => ' checked="checked"',
 8067:                              either => '',
 8068:                          );
 8069:         if ($prelink eq 'either') {
 8070:             $checkedpre{either} = ' checked="checked"';
 8071:             $checkedpre{both} = '';
 8072:         }
 8073:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 8074:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8075:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 8076:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 8077:                       '<span class="LC_nobreak"><label>'.
 8078:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 8079:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 8080:         $itemcount ++;
 8081:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8082:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 8083:                       '<td class="LC_left_item">';
 8084:         my %postlinked;
 8085:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8086:             foreach my $item (@{$types}) {
 8087:                 undef(%postlinked);
 8088:                 $datatable .= '<fieldset style="display: inline-block;">'.
 8089:                               '<legend>'.$usertypes->{$item}.'</legend>';
 8090:                 if (ref($postlink{$item}) eq 'ARRAY') {
 8091:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 8092:                 }
 8093:                 foreach my $field ('email','username') {
 8094:                     my $checked;
 8095:                     if ($postlinked{$field}) {
 8096:                         $checked = ' checked="checked"';
 8097:                     }
 8098:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8099:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 8100:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 8101:                                   '<span>&nbsp;&nbsp; ';
 8102:                 }
 8103:                 $datatable .= '</fieldset>';
 8104:             }
 8105:         }
 8106:         if (ref($postlink{'default'}) eq 'ARRAY') {
 8107:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 8108:         }
 8109:         $datatable .= '<fieldset style="display: inline-block;">'.
 8110:                       '<legend>'.$othertitle.'</legend>';
 8111:         foreach my $field ('email','username') {
 8112:             my $checked;
 8113:             if ($postlinked{$field}) {
 8114:                 $checked = ' checked="checked"';
 8115:             }
 8116:             $datatable .= '<span class="LC_nobreak"><label>'.
 8117:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 8118:                           $field.'"'.$checked.' />'.$field.'</label>'.
 8119:                           '<span>&nbsp;&nbsp; ';
 8120:         }
 8121:         $datatable .= '</fieldset></td></tr>';
 8122:         $itemcount ++;
 8123:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8124:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 8125:                       '<td class="LC_left_item">';
 8126:         foreach my $type ('permanent','critical','notify') {
 8127:             my $checkedemail;
 8128:             if ($emailsrc{$type}) {
 8129:                 $checkedemail = ' checked="checked"';
 8130:             }
 8131:             $datatable .= '<span class="LC_nobreak"><label>'.
 8132:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 8133:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 8134:                           '<span>&nbsp;&nbsp; ';
 8135:         }
 8136:         $datatable .= '</td></tr>';
 8137:         $itemcount ++;
 8138:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8139:         my $switchserver = &check_switchserver($dom,$confname);
 8140:         my ($showstd,$noshowstd);
 8141:         if ($nostdtext) {
 8142:             $noshowstd = ' checked="checked"';
 8143:         } else {
 8144:             $showstd = ' checked="checked"';
 8145:         }
 8146:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 8147:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8148:                       &mt('Retain standard text:').
 8149:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 8150:                       &mt('Yes').'</label>'.'&nbsp;'.
 8151:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 8152:                       &mt('No').'</label></span><br />'.
 8153:                       '<span class="LC_fontsize_small">'.
 8154:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 8155:                       &mt('Include custom text:');
 8156:         if ($customurl) {
 8157:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 8158:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8159:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 8160:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 8161:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 8162:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 8163:         }
 8164:         if ($switchserver) {
 8165:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 8166:         } else {
 8167:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8168:                          '<input type="file" name="passwords_customfile" /></span>';
 8169:         }
 8170:         $datatable .= '</td></tr>';
 8171:     } elsif ($position eq 'middle') {
 8172:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 8173:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 8174:         my %defaults;
 8175:         if (ref($domconf{'defaults'}) eq 'HASH') {
 8176:             %defaults = %{$domconf{'defaults'}};
 8177:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 8178:                 $defaults{'intauth_cost'} = 10;
 8179:             }
 8180:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 8181:                 $defaults{'intauth_check'} = 0;
 8182:             }
 8183:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 8184:                 $defaults{'intauth_switch'} = 0;
 8185:             }
 8186:         } else {
 8187:             %defaults = (
 8188:                           'intauth_cost'   => 10,
 8189:                           'intauth_check'  => 0,
 8190:                           'intauth_switch' => 0,
 8191:                         );
 8192:         }
 8193:         foreach my $item (@items) {
 8194:             if ($itemcount%2) {
 8195:                 $css_class = '';
 8196:             } else {
 8197:                 $css_class = ' class="LC_odd_row" ';
 8198:             }
 8199:             $datatable .= '<tr'.$css_class.'>'.
 8200:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 8201:                           '</span></td><td class="LC_left_item" colspan="3">';
 8202:             if ($item eq 'intauth_switch') {
 8203:                 my @options = (0,1,2);
 8204:                 my %optiondesc = &Apache::lonlocal::texthash (
 8205:                                    0 => 'No',
 8206:                                    1 => 'Yes',
 8207:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 8208:                                  );
 8209:                 $datatable .= '<table width="100%">';
 8210:                 foreach my $option (@options) {
 8211:                     my $checked = ' ';
 8212:                     if ($defaults{$item} eq $option) {
 8213:                         $checked = ' checked="checked"';
 8214:                     }
 8215:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8216:                                   '<label><input type="radio" name="'.$item.
 8217:                                   '" value="'.$option.'"'.$checked.' />'.
 8218:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8219:                 }
 8220:                 $datatable .= '</table>';
 8221:             } elsif ($item eq 'intauth_check') {
 8222:                 my @options = (0,1,2);
 8223:                 my %optiondesc = &Apache::lonlocal::texthash (
 8224:                                    0 => 'No',
 8225:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 8226:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 8227:                                  );
 8228:                 $datatable .= '<table width="100%">';
 8229:                 foreach my $option (@options) {
 8230:                     my $checked = ' ';
 8231:                     my $onclick;
 8232:                     if ($defaults{$item} eq $option) {
 8233:                         $checked = ' checked="checked"';
 8234:                     }
 8235:                     if ($option == 2) {
 8236:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 8237:                     }
 8238:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8239:                                   '<label><input type="radio" name="'.$item.
 8240:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 8241:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8242:                 }
 8243:                 $datatable .= '</table>';
 8244:             } else {
 8245:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8246:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 8247:             }
 8248:             $datatable .= '</td></tr>';
 8249:             $itemcount ++;
 8250:         }
 8251:     } elsif ($position eq 'lower') {
 8252:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 8253:     } else {
 8254:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8255:         my %ownerchg = (
 8256:                           by  => {},
 8257:                           for => {},
 8258:                        );
 8259:         my %ownertitles = &Apache::lonlocal::texthash (
 8260:                             by  => 'Course owner status(es) allowed',
 8261:                             for => 'Student status(es) allowed',
 8262:                           );
 8263:         if (ref($settings) eq 'HASH') {
 8264:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 8265:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 8266:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 8267:                 }
 8268:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 8269:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 8270:                 }
 8271:             }
 8272:         }
 8273:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8274:         $datatable .= '<tr '.$css_class.'>'.
 8275:                       '<td>'.
 8276:                       &mt('Requirements').'<ul>'.
 8277:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 8278:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 8279:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 8280:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 8281:                       '</ul>'.
 8282:                       '</td>'.
 8283:                       '<td class="LC_left_item">';
 8284:         foreach my $item ('by','for') {
 8285:             $datatable .= '<fieldset style="display: inline-block;">'.
 8286:                           '<legend>'.$ownertitles{$item}.'</legend>';
 8287:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8288:                 foreach my $type (@{$types}) {
 8289:                     my $checked;
 8290:                     if ($ownerchg{$item}{$type}) {
 8291:                         $checked = ' checked="checked"';
 8292:                     }
 8293:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8294:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 8295:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 8296:                                   '</span>&nbsp;&nbsp; ';
 8297:                 }
 8298:             }
 8299:             my $checked;
 8300:             if ($ownerchg{$item}{'default'}) {
 8301:                 $checked = ' checked="checked"';
 8302:             }
 8303:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8304:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 8305:                           $othertitle.'</label></span></fieldset>';
 8306:         }
 8307:         $datatable .= '</td></tr>';
 8308:     }
 8309:     return $datatable;
 8310: }
 8311: 
 8312: sub password_rules {
 8313:     my ($prefix,$itemcountref,$settings) = @_;
 8314:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 8315:     my %titles;
 8316:     if ($prefix eq 'passwords') {
 8317:         %titles = &Apache::lonlocal::texthash (
 8318:             min            => 'Minimum password length',
 8319:             max            => 'Maximum password length',
 8320:             chars          => 'Required characters',
 8321:         );
 8322:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 8323:         %titles = &Apache::lonlocal::texthash (
 8324:             min            => 'Minimum secret length',
 8325:             max            => 'Maximum secret length',
 8326:             chars          => 'Required characters',
 8327:         );
 8328:     }
 8329:     $min = $Apache::lonnet::passwdmin;
 8330:     my $datatable;
 8331:     my $itemcount;
 8332:     if (ref($itemcountref)) {
 8333:         $itemcount = $$itemcountref;
 8334:     }
 8335:     if (ref($settings) eq 'HASH') {
 8336:         if ($settings->{min}) {
 8337:             $min = $settings->{min};
 8338:         }
 8339:         if ($settings->{max}) {
 8340:             $max = $settings->{max};
 8341:         }
 8342:         if (ref($settings->{chars}) eq 'ARRAY') {
 8343:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 8344:         }
 8345:         if ($prefix eq 'passwords') {
 8346:             if ($settings->{expire}) {
 8347:                 $expire = $settings->{expire};
 8348:             }
 8349:             if ($settings->{numsaved}) {
 8350:                 $numsaved = $settings->{numsaved};
 8351:             }
 8352:         }
 8353:     }
 8354:     my %rulenames = &Apache::lonlocal::texthash(
 8355:                                                  uc => 'At least one upper case letter',
 8356:                                                  lc => 'At least one lower case letter',
 8357:                                                  num => 'At least one number',
 8358:                                                  spec => 'At least one non-alphanumeric',
 8359:                                                );
 8360:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8361:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 8362:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8363:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 8364:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8365:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 8366:                   '</span></td></tr>';
 8367:     $itemcount ++;
 8368:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8369:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 8370:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8371:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 8372:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8373:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 8374:                   '</span></td></tr>';
 8375:     $itemcount ++;
 8376:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8377:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 8378:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 8379:                   '</span></td>';
 8380:     my $numinrow = 2;
 8381:     my @possrules = ('uc','lc','num','spec');
 8382:     $datatable .= '<td class="LC_left_item"><table>';
 8383:     for (my $i=0; $i<@possrules; $i++) {
 8384:         my ($rem,$checked);
 8385:         if ($chars{$possrules[$i]}) {
 8386:             $checked = ' checked="checked"';
 8387:         }
 8388:         $rem = $i%($numinrow);
 8389:         if ($rem == 0) {
 8390:             if ($i > 0) {
 8391:                 $datatable .= '</tr>';
 8392:             }
 8393:             $datatable .= '<tr>';
 8394:         }
 8395:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 8396:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 8397:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 8398:     }
 8399:     my $rem = @possrules%($numinrow);
 8400:     my $colsleft = $numinrow - $rem;
 8401:     if ($colsleft > 1 ) {
 8402:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8403:                       '&nbsp;</td>';
 8404:     } elsif ($colsleft == 1) {
 8405:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8406:     }
 8407:     $datatable .='</table></td></tr>';
 8408:     $itemcount ++;
 8409:     if ($prefix eq 'passwords') {
 8410:         $titles{'expire'} = &mt('Password expiration (days)');
 8411:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 8412:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8413:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 8414:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8415:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 8416:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8417:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 8418:                       '</span></td></tr>';
 8419:         $itemcount ++;
 8420:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8421:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 8422:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8423:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 8424:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8425:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 8426:                       '</span></td></tr>';
 8427:         $itemcount ++;
 8428:     }
 8429:     if (ref($itemcountref)) {
 8430:         $$itemcountref += $itemcount;
 8431:     }
 8432:     return $datatable;
 8433: }
 8434: 
 8435: sub print_wafproxy {
 8436:     my ($position,$dom,$settings,$rowtotal) = @_;
 8437:     my $css_class;
 8438:     my $itemcount = 0;
 8439:     my $datatable;
 8440:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8441:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8442:     my %lt = &wafproxy_titles();
 8443:     foreach my $server (sort(keys(%servers))) {
 8444:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8445:         next if ($serverhome eq '');
 8446:         my $serverdom;
 8447:         if ($serverhome ne $server) {
 8448:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8449:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8450:                 $othercontrol{$server} = $serverdom;
 8451:             }
 8452:         } else {
 8453:             $serverdom = &Apache::lonnet::host_domain($server);
 8454:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8455:             if ($serverdom ne $dom) {
 8456:                 $othercontrol{$server} = $serverdom;
 8457:             } else {
 8458:                 $setdom = 1;
 8459:                 if (ref($settings) eq 'HASH') {
 8460:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8461:                         $aliases{$dom} = $settings->{'alias'};
 8462:                         if ($aliases{$dom} ne '') {
 8463:                             $showdom = 1;
 8464:                         }
 8465:                     }
 8466:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8467:                         $saml{$dom} = $settings->{'saml'};
 8468:                     }
 8469:                 }
 8470:             }
 8471:         }
 8472:     }
 8473:     if ($setdom) {
 8474:         %{$values{$dom}} = ();
 8475:         if (ref($settings) eq 'HASH') {
 8476:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8477:                 $values{$dom}{$item} = $settings->{$item};
 8478:             }
 8479:         }
 8480:     }
 8481:     if (keys(%othercontrol)) {
 8482:         %otherdoms = reverse(%othercontrol);
 8483:         foreach my $domain (keys(%otherdoms)) {
 8484:             %{$values{$domain}} = ();
 8485:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8486:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8487:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8488:                 if (exists($config{'wafproxy'}{'saml'})) {
 8489:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8490:                 }
 8491:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8492:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8493:                 }
 8494:             }
 8495:         }
 8496:     }
 8497:     if ($position eq 'top') {
 8498:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8499:         my %aliasinfo;
 8500:         foreach my $server (sort(keys(%servers))) {
 8501:             $itemcount ++;
 8502:             my $dom_in_effect;
 8503:             my $aliasrows = '<tr>'.
 8504:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8505:                             &mt('Hostname').': '.
 8506:                             '<span class="LC_nobreak LC_cusr_emph">'.
 8507:                             &Apache::lonnet::hostname($server).
 8508:                             '</span></td><td>&nbsp;</td>';
 8509:             if ($othercontrol{$server}) {
 8510:                 $dom_in_effect = $othercontrol{$server};
 8511:                 my ($current,$forsaml);
 8512:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8513:                     $current = $aliases{$dom_in_effect}{$server};
 8514:                 }
 8515:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8516:                     if ($saml{$dom_in_effect}{$server}) {
 8517:                         $forsaml = 1;
 8518:                     }
 8519:                 }
 8520:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8521:                               &mt('Alias').':&nbsp';
 8522:                 if ($current) {
 8523:                     $aliasrows .= $current;
 8524:                     if ($forsaml) {
 8525:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8526:                     }
 8527:                 } else {
 8528:                     $aliasrows .= &mt('None');
 8529:                 }
 8530:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8531:                               &mt('controlled by domain: [_1]',
 8532:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8533:             } else {
 8534:                 $dom_in_effect = $dom;
 8535:                 my ($current,$samlon,$samloff);
 8536:                 $samloff = ' checked="checked"';
 8537:                 if (ref($aliases{$dom}) eq 'HASH') {
 8538:                     if ($aliases{$dom}{$server}) {
 8539:                         $current = $aliases{$dom}{$server};
 8540:                     }
 8541:                 }
 8542:                 if (ref($saml{$dom}) eq 'HASH') {
 8543:                     if ($saml{$dom}{$server}) {
 8544:                         $samlon = $samloff;
 8545:                         undef($samloff);
 8546:                     }
 8547:                 }
 8548:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8549:                               &mt('Alias').':&nbsp;'.
 8550:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8551:                               'value="'.$current.'" size="30" />'.
 8552:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8553:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8554:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8555:                               &mt('No').'</label>&nbsp;<label>'.
 8556:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8557:                               &mt('Yes').'</label></span>'.
 8558:                               '</td>';
 8559:             }
 8560:             $aliasrows .= '</tr>';
 8561:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8562:         }
 8563:         if ($aliasinfo{$dom}) {
 8564:             my ($onclick,$wafon,$wafoff,$showtable);
 8565:             $onclick = ' onclick="javascript:toggleWAF();"';
 8566:             $wafoff = ' checked="checked"';
 8567:             $showtable = ' style="display:none";';
 8568:             if ($showdom) {
 8569:                 $wafon = $wafoff;
 8570:                 $wafoff = '';
 8571:                 $showtable = ' style="display:inline;"';
 8572:             }
 8573:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8574:             $datatable = '<tr'.$css_class.'>'.
 8575:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8576:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8577:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8578:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8579:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8580:                          &mt('No').'</label></span></td>'.
 8581:                          '<td class="LC_left_item">'.
 8582:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8583:                          '</table></td></tr>';
 8584:             $itemcount++;
 8585:         }
 8586:         if (keys(%otherdoms)) {
 8587:             foreach my $key (sort(keys(%otherdoms))) {
 8588:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8589:                 $datatable .= '<tr'.$css_class.'>'.
 8590:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8591:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8592:                               '</table></td></tr>';
 8593:                 $itemcount++;
 8594:             }
 8595:         }
 8596:     } else {
 8597:         my %ip_methods = &remoteip_methods();
 8598:         if ($setdom) {
 8599:             $itemcount ++;
 8600:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8601:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8602:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8603:             $wafstyle = ' style="display:none;"';
 8604:             $nowafstyle = ' style="display:table-row;"';
 8605:             $currwafdisplay = ' style="display: none"';
 8606:             $wafrangestyle = ' style="display: none"';
 8607:             $curr_remotip = 'n';
 8608:             $ssltossl = ' checked="checked"';
 8609:             if ($showdom) {
 8610:                 $wafstyle = ' style="display:table-row;"';
 8611:                 $nowafstyle =  ' style="display:none;"';
 8612:                 if (keys(%{$values{$dom}})) {
 8613:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8614:                         $curr_remotip = $values{$dom}{remoteip};
 8615:                     }
 8616:                     if ($curr_remotip eq 'h') {
 8617:                         $currwafdisplay = ' style="display:table-row"';
 8618:                         $wafrangestyle = ' style="display:inline-block;"';
 8619:                     }
 8620:                     if ($values{$dom}{'sslopt'}) {
 8621:                         $alltossl = ' checked="checked"';
 8622:                         $ssltossl = '';
 8623:                     }
 8624:                 }
 8625:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8626:                     $vpndircheck = ' checked="checked"';
 8627:                     $currwafvpn = ' style="display:table-row;"';
 8628:                     $wafrangestyle = ' style="display:inline-block;"';
 8629:                 } else {
 8630:                     $vpnaliascheck = ' checked="checked"';
 8631:                     $currwafvpn = ' style="display:none;"';
 8632:                 }
 8633:             }
 8634:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8635:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8636:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8637:                           '</tr>'.
 8638:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8639:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8640:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8641:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8642:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8643:                           '<td class="LC_left_item"><table>'.
 8644:                           '<tr>'.
 8645:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8646:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8647:             foreach my $option ('m','h','n') {
 8648:                 my $sel;
 8649:                 if ($option eq $curr_remotip) {
 8650:                    $sel = ' selected="selected"';
 8651:                 }
 8652:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8653:                               $ip_methods{$option}.'</option>';
 8654:             }
 8655:             $datatable .= '</select></td></tr>'."\n".
 8656:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8657:                           $lt{'ipheader'}.':&nbsp;'.
 8658:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8659:                           'name="wafproxy_ipheader" />'.
 8660:                           '</td></tr>'."\n".
 8661:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8662:                           $lt{'trusted'}.':<br />'.
 8663:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8664:                           $values{$dom}{'trusted'}.'</textarea>'.
 8665:                           '</td></tr>'."\n".
 8666:                           '<tr><td><hr /></td></tr>'."\n".
 8667:                           '<tr>'.
 8668:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8669:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8670:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8671:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8672:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8673:             foreach my $item ('vpnint','vpnext') {
 8674:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8675:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8676:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8677:                               $values{$dom}{$item}.'</textarea>'.
 8678:                               '</td></tr>'."\n";
 8679:             }
 8680:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8681:                           '<tr>'.
 8682:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8683:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8684:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8685:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8686:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8687:                           '</table></td></tr>';
 8688:         }
 8689:         if (keys(%otherdoms)) {
 8690:             foreach my $domain (sort(keys(%otherdoms))) {
 8691:                 $itemcount ++;
 8692:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8693:                 $datatable .= '<tr'.$css_class.'>'.
 8694:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8695:                               '<td class="LC_left_item"><table>';
 8696:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8697:                     my $showval = &mt('None');
 8698:                     if ($item eq 'ssl') {
 8699:                         $showval = $lt{'ssltossl'};
 8700:                     }
 8701:                     if ($values{$domain}{$item}) {
 8702:                         $showval = $values{$domain}{$item};
 8703:                         if ($item eq 'ssl') {
 8704:                             $showval = $lt{'alltossl'};
 8705:                         } elsif ($item eq 'remoteip') {
 8706:                             $showval = $ip_methods{$values{$domain}{$item}};
 8707:                         }
 8708:                     }
 8709:                     $datatable .= '<tr>'.
 8710:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8711:                 }
 8712:                 $datatable .= '</table></td></tr>';
 8713:             }
 8714:         }
 8715:     }
 8716:     $$rowtotal += $itemcount;
 8717:     return $datatable;
 8718: }
 8719: 
 8720: sub wafproxy_titles {
 8721:     return &Apache::lonlocal::texthash(
 8722:                remoteip   => "Method for determining user's IP",
 8723:                ipheader   => 'Request header containing remote IP',
 8724:                trusted    => 'Trusted IP range(s)',
 8725:                vpnaccess  => 'Access from institutional VPN',
 8726:                vpndirect  => 'via regular hostname (no WAF)',
 8727:                vpnaliased => 'via aliased hostname (WAF)',
 8728:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8729:                vpnext     => 'IP Range(s) for backend WAF connections',
 8730:                sslopt     => 'Forwarding http/https',
 8731:                alltossl   => 'WAF forwards both http and https requests to https',
 8732:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8733:            );
 8734: }
 8735: 
 8736: sub remoteip_methods {
 8737:     return &Apache::lonlocal::texthash(
 8738:               m => 'Use Apache mod_remoteip',
 8739:               h => 'Use headers parsed by LON-CAPA',
 8740:               n => 'Not in use',
 8741:            );
 8742: }
 8743: 
 8744: sub print_usersessions {
 8745:     my ($position,$dom,$settings,$rowtotal) = @_;
 8746:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8747:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8748:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8749: 
 8750:     my @alldoms = &Apache::lonnet::all_domains();
 8751:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8752:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8753:     my %altids = &id_for_thisdom(%servers);
 8754:     if ($position eq 'top') {
 8755:         if (keys(%serverhomes) > 1) {
 8756:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8757:             my ($curroffloadnow,$curroffloadoth);
 8758:             if (ref($settings) eq 'HASH') {
 8759:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8760:                     $curroffloadnow = $settings->{'offloadnow'};
 8761:                 }
 8762:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8763:                     $curroffloadoth = $settings->{'offloadoth'};
 8764:                 }
 8765:             }
 8766:             my $other_insts = scalar(keys(%by_location));
 8767:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8768:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8769:         } else {
 8770:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8771:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8772:                           '</td></tr>';
 8773:         }
 8774:     } else {
 8775:         my %titles = &usersession_titles();
 8776:         my ($prefix,@types);
 8777:         if ($position eq 'bottom') {
 8778:             $prefix = 'remote';
 8779:             @types = ('version','excludedomain','includedomain');
 8780:         } else {
 8781:             $prefix = 'hosted';
 8782:             @types = ('excludedomain','includedomain');
 8783:         }
 8784:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8785:     }
 8786:     $$rowtotal += $itemcount;
 8787:     return $datatable;
 8788: }
 8789: 
 8790: sub rules_by_location {
 8791:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8792:     my ($datatable,$itemcount,$css_class);
 8793:     if (keys(%{$by_location}) == 0) {
 8794:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8795:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8796:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8797:                      '</td></tr>';
 8798:         $itemcount = 1;
 8799:     } else {
 8800:         $itemcount = 0;
 8801:         my $numinrow = 5;
 8802:         my (%current,%checkedon,%checkedoff);
 8803:         my @locations = sort(keys(%{$by_location}));
 8804:         foreach my $type (@{$types}) {
 8805:             $checkedon{$type} = '';
 8806:             $checkedoff{$type} = ' checked="checked"';
 8807:         }
 8808:         if (ref($settings) eq 'HASH') {
 8809:             if (ref($settings->{$prefix}) eq 'HASH') {
 8810:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8811:                     $current{$key} = $settings->{$prefix}{$key};
 8812:                     if ($key eq 'version') {
 8813:                         if ($current{$key} ne '') {
 8814:                             $checkedon{$key} = ' checked="checked"';
 8815:                             $checkedoff{$key} = '';
 8816:                         }
 8817:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8818:                         $checkedon{$key} = ' checked="checked"';
 8819:                         $checkedoff{$key} = '';
 8820:                     }
 8821:                 }
 8822:             }
 8823:         }
 8824:         foreach my $type (@{$types}) {
 8825:             next if ($type ne 'version' && !@locations);
 8826:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8827:             $datatable .= '<tr'.$css_class.'>
 8828:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8829:                            <span class="LC_nobreak">&nbsp;
 8830:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8831:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8832:             if ($type eq 'version') {
 8833:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8834:                 my $selector = '<select name="'.$prefix.'_version">';
 8835:                 foreach my $version (@lcversions) {
 8836:                     my $selected = '';
 8837:                     if ($current{'version'} eq $version) {
 8838:                         $selected = ' selected="selected"';
 8839:                     }
 8840:                     $selector .= ' <option value="'.$version.'"'.
 8841:                                  $selected.'>'.$version.'</option>';
 8842:                 }
 8843:                 $selector .= '</select> ';
 8844:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8845:             } else {
 8846:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8847:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8848:                              ' />'.('&nbsp;'x2).
 8849:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8850:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8851:                              "\n".
 8852:                              '</div><div><table>';
 8853:                 my $rem;
 8854:                 for (my $i=0; $i<@locations; $i++) {
 8855:                     my ($showloc,$value,$checkedtype);
 8856:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8857:                         my $ip = $by_location->{$locations[$i]}->[0];
 8858:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8859:                             $value = join(':',@{$by_ip->{$ip}});
 8860:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8861:                             if (ref($current{$type}) eq 'ARRAY') {
 8862:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8863:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8864:                                         $checkedtype = ' checked="checked"';
 8865:                                         last;
 8866:                                     }
 8867:                                 }
 8868:                             }
 8869:                         }
 8870:                     }
 8871:                     $rem = $i%($numinrow);
 8872:                     if ($rem == 0) {
 8873:                         if ($i > 0) {
 8874:                             $datatable .= '</tr>';
 8875:                         }
 8876:                         $datatable .= '<tr>';
 8877:                     }
 8878:                     $datatable .= '<td class="LC_left_item">'.
 8879:                                   '<span class="LC_nobreak"><label>'.
 8880:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8881:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8882:                                   '</label></span></td>';
 8883:                 }
 8884:                 $rem = @locations%($numinrow);
 8885:                 my $colsleft = $numinrow - $rem;
 8886:                 if ($colsleft > 1 ) {
 8887:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8888:                                   '&nbsp;</td>';
 8889:                 } elsif ($colsleft == 1) {
 8890:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8891:                 }
 8892:                 $datatable .= '</tr></table>';
 8893:             }
 8894:             $datatable .= '</td></tr>';
 8895:             $itemcount ++;
 8896:         }
 8897:     }
 8898:     return ($datatable,$itemcount);
 8899: }
 8900: 
 8901: sub print_ssl {
 8902:     my ($position,$dom,$settings,$rowtotal) = @_;
 8903:     my ($css_class,$datatable);
 8904:     my $itemcount = 1;
 8905:     if ($position eq 'top') {
 8906:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8907:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8908:         my $same_institution;
 8909:         if ($intdom ne '') {
 8910:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8911:             if (ref($internet_names) eq 'ARRAY') {
 8912:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8913:                     $same_institution = 1;
 8914:                 }
 8915:             }
 8916:         }
 8917:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8918:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8919:         if ($same_institution) {
 8920:             my %domservers = &Apache::lonnet::get_servers($dom);
 8921:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8922:         } else {
 8923:             $datatable .= &mt("You need to be logged into one of your own domain's servers to display information about the status of LON-CAPA SSL certificates.");
 8924:         }
 8925:         $datatable .= '</td></tr>';
 8926:         $itemcount ++;
 8927:     } else {
 8928:         my %titles = &ssl_titles();
 8929:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8930:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8931:         my @alldoms = &Apache::lonnet::all_domains();
 8932:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8933:         my @domservers = &Apache::lonnet::get_servers($dom);
 8934:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8935:         my %altids = &id_for_thisdom(%servers);
 8936:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8937:             my $legacy;
 8938:             unless (ref($settings) eq 'HASH') {
 8939:                 my $name;
 8940:                 if ($position eq 'connto') {
 8941:                     $name = 'loncAllowInsecure';
 8942:                 } else {
 8943:                     $name = 'londAllowInsecure';
 8944:                 }
 8945:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8946:                 my @ids=&Apache::lonnet::current_machine_ids();
 8947:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8948:                     my %what = (
 8949:                                    $name => 1,
 8950:                                );
 8951:                     my ($result,$returnhash) =
 8952:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8953:                     if ($result eq 'ok') {
 8954:                         if (ref($returnhash) eq 'HASH') {
 8955:                             $legacy = $returnhash->{$name};
 8956:                         }
 8957:                     }
 8958:                 } else {
 8959:                     $legacy = $Apache::lonnet::perlvar{$name};
 8960:                 }
 8961:             }
 8962:             foreach my $type ('dom','intdom','other') {
 8963:                 my %checked;
 8964:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8965:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8966:                               '<td class="LC_right_item">';
 8967:                 my $skip; 
 8968:                 if ($type eq 'dom') {
 8969:                     unless (keys(%servers) > 1) {
 8970:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8971:                         $skip = 1;
 8972:                     }
 8973:                 }
 8974:                 if ($type eq 'intdom') {
 8975:                     unless (@instdoms > 1) {
 8976:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8977:                         $skip = 1;
 8978:                     } 
 8979:                 } elsif ($type eq 'other') {
 8980:                     if (keys(%by_location) == 0) {
 8981:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8982:                         $skip = 1;
 8983:                     }
 8984:                 }
 8985:                 unless ($skip) {
 8986:                     $checked{'yes'} = ' checked="checked"'; 
 8987:                     if (ref($settings) eq 'HASH') {
 8988:                         if (ref($settings->{$position}) eq 'HASH') {
 8989:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8990:                                 $checked{$1} = $checked{'yes'};
 8991:                                 delete($checked{'yes'}); 
 8992:                             }
 8993:                         }
 8994:                     } else {
 8995:                         if ($legacy == 0) {
 8996:                             $checked{'req'} = $checked{'yes'};
 8997:                             delete($checked{'yes'});    
 8998:                         }
 8999:                     }
 9000:                     foreach my $option ('no','yes','req') {
 9001:                         $datatable .= '<span class="LC_nobreak"><label>'.
 9002:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 9003:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 9004:                                       '</label></span>'.('&nbsp;'x2);
 9005:                     }
 9006:                 }
 9007:                 $datatable .= '</td></tr>';
 9008:                 $itemcount ++; 
 9009:             }
 9010:         } else {
 9011:             my $prefix = 'replication';
 9012:             my @types = ('certreq','nocertreq');
 9013:             if (keys(%by_location) == 0) {
 9014:                 $datatable .= '<tr'.$css_class.'><td>'.
 9015:                               &mt('Nothing to set here, as there are no other institutions').
 9016:                               '</td></tr>';
 9017:                 $itemcount ++;
 9018:             } else {
 9019:                 ($datatable,$itemcount) = 
 9020:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9021:             }
 9022:         }
 9023:     }
 9024:     $$rowtotal += $itemcount;
 9025:     return $datatable;
 9026: }
 9027: 
 9028: sub ssl_titles {
 9029:     return &Apache::lonlocal::texthash (
 9030:                dom           => 'LON-CAPA servers/VMs from same domain',
 9031:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 9032:                other         => 'External LON-CAPA servers/VMs',
 9033:                connto        => 'Connections to other servers',
 9034:                connfrom      => 'Connections from other servers',
 9035:                replication   => 'Replicating content to other institutions',
 9036:                certreq       => 'Client certificate required, but specific domains exempt',
 9037:                nocertreq     => 'No client certificate required, except for specific domains',
 9038:                no            => 'SSL not used',
 9039:                yes           => 'SSL Optional (used if available)',
 9040:                req           => 'SSL Required',
 9041:     );
 9042: }
 9043: 
 9044: sub print_trust {
 9045:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 9046:     my ($css_class,$datatable,%checked,%choices);
 9047:     my (%by_ip,%by_location,@intdoms,@instdoms);
 9048:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 9049:     my $itemcount = 1;
 9050:     my %titles = &trust_titles();
 9051:     my @types = ('exc','inc');
 9052:     if ($prefix eq 'top') {
 9053:         $prefix = 'content';
 9054:     } elsif ($prefix eq 'bottom') {
 9055:         $prefix = 'msg';
 9056:     }
 9057:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9058:     $$rowtotal += $itemcount;
 9059:     return $datatable;
 9060: }
 9061: 
 9062: sub trust_titles {
 9063:     return &Apache::lonlocal::texthash(
 9064:                content  => "Access to this domain's content by others",
 9065:                shared   => "Access to other domain's content by this domain",
 9066:                enroll   => "Enrollment in this domain's courses by others", 
 9067:                othcoau  => "Co-author roles in this domain for others",
 9068:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 9069:                domroles => "Domain roles in this domain assignable to others",
 9070:                catalog  => "Course Catalog for this domain displayed elsewhere",
 9071:                reqcrs   => "Requests for creation of courses in this domain by others",
 9072:                msg      => "Users in other domains can send messages to this domain",
 9073:                exc      => "Allow all, but exclude specific domains",
 9074:                inc      => "Deny all, but include specific domains",
 9075:            );
 9076: } 
 9077: 
 9078: sub build_location_hashes {
 9079:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 9080:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 9081:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 9082:     my %iphost = &Apache::lonnet::get_iphost();
 9083:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 9084:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 9085:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 9086:         foreach my $id (@{$iphost{$primary_ip}}) {
 9087:             my $intdom = &Apache::lonnet::internet_dom($id);
 9088:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 9089:                 push(@{$intdoms},$intdom);
 9090:             }
 9091:         }
 9092:     }
 9093:     foreach my $ip (keys(%iphost)) {
 9094:         if (ref($iphost{$ip}) eq 'ARRAY') {
 9095:             foreach my $id (@{$iphost{$ip}}) {
 9096:                 my $location = &Apache::lonnet::internet_dom($id);
 9097:                 if ($location) {
 9098:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 9099:                         my $dom = &Apache::lonnet::host_domain($id);
 9100:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 9101:                             push(@{$instdoms},$dom);
 9102:                         }
 9103:                         next;
 9104:                     }
 9105:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9106:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 9107:                             push(@{$by_ip->{$ip}},$location);
 9108:                         }
 9109:                     } else {
 9110:                         $by_ip->{$ip} = [$location];
 9111:                     }
 9112:                 }
 9113:             }
 9114:         }
 9115:     }
 9116:     foreach my $ip (sort(keys(%{$by_ip}))) {
 9117:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9118:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 9119:             my $first = $by_ip->{$ip}->[0];
 9120:             if (ref($by_location->{$first}) eq 'ARRAY') {
 9121:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 9122:                     push(@{$by_location->{$first}},$ip);
 9123:                 }
 9124:             } else {
 9125:                 $by_location->{$first} = [$ip];
 9126:             }
 9127:         }
 9128:     }
 9129:     return;
 9130: }
 9131: 
 9132: sub current_offloads_to {
 9133:     my ($dom,$settings,$servers) = @_;
 9134:     my (%spareid,%otherdomconfigs);
 9135:     if (ref($servers) eq 'HASH') {
 9136:         foreach my $lonhost (sort(keys(%{$servers}))) {
 9137:             my $gotspares;
 9138:             if (ref($settings) eq 'HASH') {
 9139:                 if (ref($settings->{'spares'}) eq 'HASH') {
 9140:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 9141:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 9142:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 9143:                         $gotspares = 1;
 9144:                     }
 9145:                 }
 9146:             }
 9147:             unless ($gotspares) {
 9148:                 my $gotspares;
 9149:                 my $serverhomeID =
 9150:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 9151:                 my $serverhomedom =
 9152:                     &Apache::lonnet::host_domain($serverhomeID);
 9153:                 if ($serverhomedom ne $dom) {
 9154:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 9155:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9156:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9157:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9158:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9159:                                 $gotspares = 1;
 9160:                             }
 9161:                         }
 9162:                     } else {
 9163:                         $otherdomconfigs{$serverhomedom} =
 9164:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 9165:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 9166:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9167:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9168:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 9169:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9170:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9171:                                         $gotspares = 1;
 9172:                                     }
 9173:                                 }
 9174:                             }
 9175:                         }
 9176:                     }
 9177:                 }
 9178:             }
 9179:             unless ($gotspares) {
 9180:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9181:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9182:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9183:                } else {
 9184:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 9185:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 9186:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9187:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9188:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9189:                     } else {
 9190:                         my %what = (
 9191:                              spareid => 1,
 9192:                         );
 9193:                         my ($result,$returnhash) = 
 9194:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 9195:                         if ($result eq 'ok') { 
 9196:                             if (ref($returnhash) eq 'HASH') {
 9197:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 9198:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 9199:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 9200:                                 }
 9201:                             }
 9202:                         }
 9203:                     }
 9204:                 }
 9205:             }
 9206:         }
 9207:     }
 9208:     return %spareid;
 9209: }
 9210: 
 9211: sub spares_row {
 9212:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 9213:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 9214:     my $css_class;
 9215:     my $numinrow = 4;
 9216:     my $itemcount = 1;
 9217:     my $datatable;
 9218:     my %typetitles = &sparestype_titles();
 9219:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9220:         foreach my $server (sort(keys(%{$servers}))) {
 9221:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 9222:             my ($othercontrol,$serverdom);
 9223:             if ($serverhome ne $server) {
 9224:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 9225:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9226:             } else {
 9227:                 $serverdom = &Apache::lonnet::host_domain($server);
 9228:                 if ($serverdom ne $dom) {
 9229:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9230:                 }
 9231:             }
 9232:             next unless (ref($spareid->{$server}) eq 'HASH');
 9233:             my ($checkednow,$checkedoth);
 9234:             if (ref($curroffloadnow) eq 'HASH') {
 9235:                 if ($curroffloadnow->{$server}) {
 9236:                     $checkednow = ' checked="checked"';
 9237:                 }
 9238:             }
 9239:             if (ref($curroffloadoth) eq 'HASH') {
 9240:                 if ($curroffloadoth->{$server}) {
 9241:                     $checkedoth = ' checked="checked"';
 9242:                 }
 9243:             }
 9244:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 9245:             $datatable .= '<tr'.$css_class.'>
 9246:                            <td rowspan="2">
 9247:                             <span class="LC_nobreak">'.
 9248:                           &mt('[_1] when busy, offloads to:'
 9249:                               ,'<b>'.$server.'</b>').'</span><br />'.
 9250:                           '<span class="LC_nobreak">'."\n".
 9251:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 9252:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 9253:                           "\n";
 9254:             if ($other_insts) {
 9255:                 $datatable .= '<br />'.
 9256:                               '<span class="LC_nobreak">'."\n".
 9257:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 9258:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 9259:                           "\n";
 9260:             }
 9261:             my (%current,%canselect);
 9262:             my @choices = 
 9263:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 9264:             foreach my $type ('primary','default') {
 9265:                 if (ref($spareid->{$server}) eq 'HASH') {
 9266:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 9267:                         my @spares = @{$spareid->{$server}{$type}};
 9268:                         if (@spares > 0) {
 9269:                             if ($othercontrol) {
 9270:                                 $current{$type} = join(', ',@spares);
 9271:                             } else {
 9272:                                 $current{$type} .= '<table>';
 9273:                                 my $numspares = scalar(@spares);
 9274:                                 for (my $i=0;  $i<@spares; $i++) {
 9275:                                     my $rem = $i%($numinrow);
 9276:                                     if ($rem == 0) {
 9277:                                         if ($i > 0) {
 9278:                                             $current{$type} .= '</tr>';
 9279:                                         }
 9280:                                         $current{$type} .= '<tr>';
 9281:                                     }
 9282:                                     $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;'.
 9283:                                                        $spareid->{$server}{$type}[$i].
 9284:                                                        '</label></td>'."\n";
 9285:                                 }
 9286:                                 my $rem = @spares%($numinrow);
 9287:                                 my $colsleft = $numinrow - $rem;
 9288:                                 if ($colsleft > 1 ) {
 9289:                                     $current{$type} .= '<td colspan="'.$colsleft.
 9290:                                                        '" class="LC_left_item">'.
 9291:                                                        '&nbsp;</td>';
 9292:                                 } elsif ($colsleft == 1) {
 9293:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 9294:                                 }
 9295:                                 $current{$type} .= '</tr></table>';
 9296:                             }
 9297:                         }
 9298:                     }
 9299:                     if ($current{$type} eq '') {
 9300:                         $current{$type} = &mt('None specified');
 9301:                     }
 9302:                     if ($othercontrol) {
 9303:                         if ($type eq 'primary') {
 9304:                             $canselect{$type} = $othercontrol;
 9305:                         }
 9306:                     } else {
 9307:                         $canselect{$type} = 
 9308:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 9309:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 9310:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 9311:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 9312:                         if (@choices > 0) {
 9313:                             foreach my $lonhost (@choices) {
 9314:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 9315:                             }
 9316:                         }
 9317:                         $canselect{$type} .= '</select>'."\n";
 9318:                     }
 9319:                 } else {
 9320:                     $current{$type} = &mt('Could not be determined');
 9321:                     if ($type eq 'primary') {
 9322:                         $canselect{$type} =  $othercontrol;
 9323:                     }
 9324:                 }
 9325:                 if ($type eq 'default') {
 9326:                     $datatable .= '<tr'.$css_class.'>';
 9327:                 }
 9328:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 9329:                               '<td>'.$current{$type}.'</td>'."\n".
 9330:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 9331:             }
 9332:             $itemcount ++;
 9333:         }
 9334:     }
 9335:     $$rowtotal += $itemcount;
 9336:     return $datatable;
 9337: }
 9338: 
 9339: sub possible_newspares {
 9340:     my ($server,$currspares,$serverhomes,$altids) = @_;
 9341:     my $serverhostname = &Apache::lonnet::hostname($server);
 9342:     my %excluded;
 9343:     if ($serverhostname ne '') {
 9344:         %excluded = (
 9345:                        $serverhostname => 1,
 9346:                     );
 9347:     }
 9348:     if (ref($currspares) eq 'HASH') {
 9349:         foreach my $type (keys(%{$currspares})) {
 9350:             if (ref($currspares->{$type}) eq 'ARRAY') {
 9351:                 if (@{$currspares->{$type}} > 0) {
 9352:                     foreach my $curr (@{$currspares->{$type}}) {
 9353:                         my $hostname = &Apache::lonnet::hostname($curr);
 9354:                         $excluded{$hostname} = 1;
 9355:                     }
 9356:                 }
 9357:             }
 9358:         }
 9359:     }
 9360:     my @choices;
 9361:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9362:         if (keys(%{$serverhomes}) > 1) {
 9363:             foreach my $name (sort(keys(%{$serverhomes}))) {
 9364:                 unless ($excluded{$name}) {
 9365:                     if (exists($altids->{$serverhomes->{$name}})) {
 9366:                         push(@choices,$altids->{$serverhomes->{$name}});
 9367:                     } else {
 9368:                         push(@choices,$serverhomes->{$name});
 9369:                     }
 9370:                 }
 9371:             }
 9372:         }
 9373:     }
 9374:     return sort(@choices);
 9375: }
 9376: 
 9377: sub print_loadbalancing {
 9378:     my ($dom,$settings,$rowtotal) = @_;
 9379:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9380:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9381:     my $numinrow = 1;
 9382:     my $datatable;
 9383:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9384:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 9385:     if (ref($settings) eq 'HASH') {
 9386:         %existing = %{$settings};
 9387:     }
 9388:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 9389:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9390:                                   \%currtargets,\%currrules,\%currcookies);
 9391:     } else {
 9392:         return;
 9393:     }
 9394:     my ($othertitle,$usertypes,$types) =
 9395:         &Apache::loncommon::sorted_inst_types($dom);
 9396:     my $rownum = 8;
 9397:     if (ref($types) eq 'ARRAY') {
 9398:         $rownum += scalar(@{$types});
 9399:     }
 9400:     my @css_class = ('LC_odd_row','LC_even_row');
 9401:     my $balnum = 0;
 9402:     my $islast;
 9403:     my (@toshow,$disabledtext);
 9404:     if (keys(%currbalancer) > 0) {
 9405:         @toshow = sort(keys(%currbalancer));
 9406:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 9407:             push(@toshow,'');
 9408:         }
 9409:     } else {
 9410:         @toshow = ('');
 9411:         $disabledtext = &mt('No existing load balancer');
 9412:     }
 9413:     foreach my $lonhost (@toshow) {
 9414:         if ($balnum == scalar(@toshow)-1) {
 9415:             $islast = 1;
 9416:         } else {
 9417:             $islast = 0;
 9418:         }
 9419:         my $cssidx = $balnum%2;
 9420:         my $targets_div_style = 'display: none';
 9421:         my $disabled_div_style = 'display: block';
 9422:         my $homedom_div_style = 'display: none';
 9423:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 9424:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9425:                       '<p>';
 9426:         if ($lonhost eq '') {
 9427:             $datatable .= '<span class="LC_nobreak">';
 9428:             if (keys(%currbalancer) > 0) {
 9429:                 $datatable .= &mt('Add balancer:');
 9430:             } else {
 9431:                 $datatable .= &mt('Enable balancer:');
 9432:             }
 9433:             $datatable .= '&nbsp;'.
 9434:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9435:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9436:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9437:                           '<option value="" selected="selected">'.&mt('None').
 9438:                           '</option>'."\n";
 9439:             foreach my $server (sort(keys(%servers))) {
 9440:                 next if ($currbalancer{$server});
 9441:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9442:             }
 9443:             $datatable .=
 9444:                 '</select>'."\n".
 9445:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9446:         } else {
 9447:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9448:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9449:                            &mt('Stop balancing').'</label>'.
 9450:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9451:             $targets_div_style = 'display: block';
 9452:             $disabled_div_style = 'display: none';
 9453:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9454:                 $homedom_div_style = 'display: block';
 9455:             }
 9456:         }
 9457:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9458:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9459:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9460:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9461:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9462:         my @sparestypes = ('primary','default');
 9463:         my %typetitles = &sparestype_titles();
 9464:         my %hostherechecked = (
 9465:                                   no => ' checked="checked"',
 9466:                               );
 9467:         my %balcookiechecked = (
 9468:                                   no => ' checked="checked"',
 9469:                                );
 9470:         foreach my $sparetype (@sparestypes) {
 9471:             my $targettable;
 9472:             for (my $i=0; $i<$numspares; $i++) {
 9473:                 my $checked;
 9474:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9475:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9476:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9477:                             $checked = ' checked="checked"';
 9478:                         }
 9479:                     }
 9480:                 }
 9481:                 my ($chkboxval,$disabled);
 9482:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9483:                     $chkboxval = $spares[$i];
 9484:                 }
 9485:                 if (exists($currbalancer{$spares[$i]})) {
 9486:                     $disabled = ' disabled="disabled"';
 9487:                 }
 9488:                 $targettable .=
 9489:                     '<td><span class="LC_nobreak"><label>'.
 9490:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9491:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9492:                     '</span></label></span></td>';
 9493:                 my $rem = $i%($numinrow);
 9494:                 if ($rem == 0) {
 9495:                     if (($i > 0) && ($i < $numspares-1)) {
 9496:                         $targettable .= '</tr>';
 9497:                     }
 9498:                     if ($i < $numspares-1) {
 9499:                         $targettable .= '<tr>';
 9500:                     }
 9501:                 }
 9502:             }
 9503:             if ($targettable ne '') {
 9504:                 my $rem = $numspares%($numinrow);
 9505:                 my $colsleft = $numinrow - $rem;
 9506:                 if ($colsleft > 1 ) {
 9507:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9508:                                     '&nbsp;</td>';
 9509:                 } elsif ($colsleft == 1) {
 9510:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9511:                 }
 9512:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9513:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9514:             }
 9515:             $hostherechecked{$sparetype} = '';
 9516:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9517:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9518:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9519:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9520:                         $hostherechecked{'no'} = '';
 9521:                     }
 9522:                 }
 9523:             }
 9524:         }
 9525:         if ($currcookies{$lonhost}) {
 9526:             %balcookiechecked = (
 9527:                                     yes => ' checked="checked"',
 9528:                                 );
 9529:         }
 9530:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9531:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9532:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9533:         foreach my $sparetype (@sparestypes) {
 9534:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9535:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9536:                           '</i></label><br />';
 9537:         }
 9538:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9539:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9540:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9541:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9542:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9543:                       '</div></td></tr>'.
 9544:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9545:                                            $othertitle,$usertypes,$types,\%servers,
 9546:                                            \%currbalancer,$lonhost,
 9547:                                            $targets_div_style,$homedom_div_style,
 9548:                                            $css_class[$cssidx],$balnum,$islast);
 9549:         $$rowtotal += $rownum;
 9550:         $balnum ++;
 9551:     }
 9552:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9553:     return $datatable;
 9554: }
 9555: 
 9556: sub get_loadbalancers_config {
 9557:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9558:     return unless ((ref($servers) eq 'HASH') &&
 9559:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9560:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9561:                    (ref($currcookies) eq 'HASH'));
 9562:     if (keys(%{$existing}) > 0) {
 9563:         my $oldlonhost;
 9564:         foreach my $key (sort(keys(%{$existing}))) {
 9565:             if ($key eq 'lonhost') {
 9566:                 $oldlonhost = $existing->{'lonhost'};
 9567:                 $currbalancer->{$oldlonhost} = 1;
 9568:             } elsif ($key eq 'targets') {
 9569:                 if ($oldlonhost) {
 9570:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9571:                 }
 9572:             } elsif ($key eq 'rules') {
 9573:                 if ($oldlonhost) {
 9574:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9575:                 }
 9576:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9577:                 $currbalancer->{$key} = 1;
 9578:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9579:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9580:                 if ($existing->{$key}{'cookie'}) {
 9581:                     $currcookies->{$key} = 1;
 9582:                 }
 9583:             }
 9584:         }
 9585:     } else {
 9586:         my ($balancerref,$targetsref) =
 9587:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9588:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9589:             foreach my $server (sort(keys(%{$balancerref}))) {
 9590:                 $currbalancer->{$server} = 1;
 9591:                 $currtargets->{$server} = $targetsref->{$server};
 9592:             }
 9593:         }
 9594:     }
 9595:     return;
 9596: }
 9597: 
 9598: sub loadbalancing_rules {
 9599:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9600:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9601:         $css_class,$balnum,$islast) = @_;
 9602:     my $output;
 9603:     my $num = 0;
 9604:     my ($alltypes,$othertypes,$titles) =
 9605:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9606:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9607:         foreach my $type (@{$alltypes}) {
 9608:             $num ++;
 9609:             my $current;
 9610:             if (ref($currrules) eq 'HASH') {
 9611:                 $current = $currrules->{$type};
 9612:             }
 9613:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9614:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9615:                     $current = '';
 9616:                 }
 9617:             }
 9618:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9619:                                              $servers,$currbalancer,$lonhost,$dom,
 9620:                                              $targets_div_style,$homedom_div_style,
 9621:                                              $css_class,$balnum,$num,$islast);
 9622:         }
 9623:     }
 9624:     return $output;
 9625: }
 9626: 
 9627: sub loadbalancing_titles {
 9628:     my ($dom,$intdom,$usertypes,$types) = @_;
 9629:     my %othertypes = (
 9630:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9631:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9632:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9633:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9634:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9635:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9636:                      );
 9637:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9638:     my @available;
 9639:     if (ref($types) eq 'ARRAY') {
 9640:         @available = @{$types};
 9641:     }
 9642:     unless (grep(/^default$/,@available)) {
 9643:         push(@available,'default');
 9644:     }
 9645:     unshift(@alltypes,@available);
 9646:     my %titles;
 9647:     foreach my $type (@alltypes) {
 9648:         if ($type =~ /^_LC_/) {
 9649:             $titles{$type} = $othertypes{$type};
 9650:         } elsif ($type eq 'default') {
 9651:             $titles{$type} = &mt('All users from [_1]',$dom);
 9652:             if (ref($types) eq 'ARRAY') {
 9653:                 if (@{$types} > 0) {
 9654:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9655:                 }
 9656:             }
 9657:         } elsif (ref($usertypes) eq 'HASH') {
 9658:             $titles{$type} = $usertypes->{$type};
 9659:         }
 9660:     }
 9661:     return (\@alltypes,\%othertypes,\%titles);
 9662: }
 9663: 
 9664: sub loadbalance_rule_row {
 9665:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9666:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9667:     my @rulenames;
 9668:     my %ruletitles = &offloadtype_text();
 9669:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9670:         @rulenames = ('balancer','offloadedto','specific');
 9671:     } else {
 9672:         @rulenames = ('default','homeserver');
 9673:         if ($type eq '_LC_external') {
 9674:             push(@rulenames,'externalbalancer');
 9675:         } else {
 9676:             push(@rulenames,'specific');
 9677:         }
 9678:         push(@rulenames,'none');
 9679:     }
 9680:     my $style = $targets_div_style;
 9681:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9682:         $style = $homedom_div_style;
 9683:     }
 9684:     my $space;
 9685:     if ($islast && $num == 1) {
 9686:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9687:     }
 9688:     my $output =
 9689:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9690:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9691:         '<td valaign="top">'.$space.
 9692:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9693:     for (my $i=0; $i<@rulenames; $i++) {
 9694:         my $rule = $rulenames[$i];
 9695:         my ($checked,$extra);
 9696:         if ($rulenames[$i] eq 'default') {
 9697:             $rule = '';
 9698:         }
 9699:         if ($rulenames[$i] eq 'specific') {
 9700:             if (ref($servers) eq 'HASH') {
 9701:                 my $default;
 9702:                 if (($current ne '') && (exists($servers->{$current}))) {
 9703:                     $checked = ' checked="checked"';
 9704:                 }
 9705:                 unless ($checked) {
 9706:                     $default = ' selected="selected"';
 9707:                 }
 9708:                 $extra =
 9709:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9710:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9711:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9712:                     '<option value=""'.$default.'></option>'."\n";
 9713:                 foreach my $server (sort(keys(%{$servers}))) {
 9714:                     if (ref($currbalancer) eq 'HASH') {
 9715:                         next if (exists($currbalancer->{$server}));
 9716:                     }
 9717:                     my $selected;
 9718:                     if ($server eq $current) {
 9719:                         $selected = ' selected="selected"';
 9720:                     }
 9721:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9722:                 }
 9723:                 $extra .= '</select>';
 9724:             }
 9725:         } elsif ($rule eq $current) {
 9726:             $checked = ' checked="checked"';
 9727:         }
 9728:         $output .= '<span class="LC_nobreak"><label>'.
 9729:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9730:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9731:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9732:                    ')"'.$checked.' />&nbsp;';
 9733:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9734:             $output .= $ruletitles{'particular'};
 9735:         } else {
 9736:             $output .= $ruletitles{$rulenames[$i]};
 9737:         }
 9738:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9739:     }
 9740:     $output .= '</div></td></tr>'."\n";
 9741:     return $output;
 9742: }
 9743: 
 9744: sub offloadtype_text {
 9745:     my %ruletitles = &Apache::lonlocal::texthash (
 9746:            'default'          => 'Offloads to default destinations',
 9747:            'homeserver'       => "Offloads to user's home server",
 9748:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9749:            'specific'         => 'Offloads to specific server',
 9750:            'none'             => 'No offload',
 9751:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9752:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9753:            'particular'       => 'Session hosted (after re-auth) on server:',
 9754:     );
 9755:     return %ruletitles;
 9756: }
 9757: 
 9758: sub sparestype_titles {
 9759:     my %typestitles = &Apache::lonlocal::texthash (
 9760:                           'primary' => 'primary',
 9761:                           'default' => 'default',
 9762:                       );
 9763:     return %typestitles;
 9764: }
 9765: 
 9766: sub contact_titles {
 9767:     my %titles = &Apache::lonlocal::texthash (
 9768:                    'supportemail'    => 'Support E-mail address',
 9769:                    'adminemail'      => 'Default Server Admin E-mail address',
 9770:                    'errormail'       => 'Error reports to be e-mailed to',
 9771:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9772:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9773:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9774:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9775:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9776:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9777:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9778:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9779:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9780:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9781:                    'errorweights'    => 'Weights used to compute error count',
 9782:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9783:                  );
 9784:     my %short_titles = &Apache::lonlocal::texthash (
 9785:                            adminemail   => 'Admin E-mail address',
 9786:                            supportemail => 'Support E-mail',
 9787:                        );   
 9788:     return (\%titles,\%short_titles);
 9789: }
 9790: 
 9791: sub helpform_fields {
 9792:     my %titles =  &Apache::lonlocal::texthash (
 9793:                        'username'   => 'Name',
 9794:                        'user'       => 'Username/domain',
 9795:                        'phone'      => 'Phone',
 9796:                        'cc'         => 'Cc e-mail',
 9797:                        'course'     => 'Course Details',
 9798:                        'section'    => 'Sections',
 9799:                        'screenshot' => 'File upload',
 9800:     );
 9801:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9802:     my %possoptions = (
 9803:                         username     => ['yes','no','req'],
 9804:                         phone        => ['yes','no','req'],
 9805:                         user         => ['yes','no'],
 9806:                         cc           => ['yes','no'],
 9807:                         course       => ['yes','no'],
 9808:                         section      => ['yes','no'],
 9809:                         screenshot   => ['yes','no'],
 9810:                       );
 9811:     my %fieldoptions = &Apache::lonlocal::texthash (
 9812:                          'yes'  => 'Optional',
 9813:                          'req'  => 'Required',
 9814:                          'no'   => "Not shown",
 9815:     );
 9816:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9817: }
 9818: 
 9819: sub tool_titles {
 9820:     my %titles = &Apache::lonlocal::texthash (
 9821:                      aboutme    => 'Personal web page',
 9822:                      blog       => 'Blog',
 9823:                      portfolio  => 'Portfolio',
 9824:                      portaccess => 'Share portfolio files',
 9825:                      timezone   => 'Can set time zone',
 9826:                      official   => 'Official courses (with institutional codes)',
 9827:                      unofficial => 'Unofficial courses',
 9828:                      community  => 'Communities',
 9829:                      textbook   => 'Textbook courses',
 9830:                      placement  => 'Placement tests',
 9831:                  );
 9832:     return %titles;
 9833: }
 9834: 
 9835: sub courserequest_titles {
 9836:     my %titles = &Apache::lonlocal::texthash (
 9837:                                    official   => 'Official',
 9838:                                    unofficial => 'Unofficial',
 9839:                                    community  => 'Communities',
 9840:                                    textbook   => 'Textbook',
 9841:                                    placement  => 'Placement tests',
 9842:                                    lti        => 'LTI Provider',
 9843:                                    norequest  => 'Not allowed',
 9844:                                    approval   => 'Approval by DC',
 9845:                                    validate   => 'With validation',
 9846:                                    autolimit  => 'Numerical limit',
 9847:                                    unlimited  => '(blank for unlimited)',
 9848:                  );
 9849:     return %titles;
 9850: }
 9851: 
 9852: sub authorrequest_titles {
 9853:     my %titles = &Apache::lonlocal::texthash (
 9854:                                    norequest  => 'Not allowed',
 9855:                                    approval   => 'Approval by Dom. Coord.',
 9856:                                    automatic  => 'Automatic approval',
 9857:                  );
 9858:     return %titles;
 9859: }
 9860: 
 9861: sub courserequest_conditions {
 9862:     my %conditions = &Apache::lonlocal::texthash (
 9863:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9864:        validate   => '(Processing of request subject to institutional validation).',
 9865:                  );
 9866:     return %conditions;
 9867: }
 9868: 
 9869: 
 9870: sub print_usercreation {
 9871:     my ($position,$dom,$settings,$rowtotal) = @_;
 9872:     my $numinrow = 4;
 9873:     my $datatable;
 9874:     if ($position eq 'top') {
 9875:         $$rowtotal ++;
 9876:         my $rowcount = 0;
 9877:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9878:         if (ref($rules) eq 'HASH') {
 9879:             if (keys(%{$rules}) > 0) {
 9880:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9881:                                                 $ruleorder,$numinrow,$rowcount);
 9882:                 $$rowtotal ++;
 9883:                 $rowcount ++;
 9884:             }
 9885:         }
 9886:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9887:         if (ref($idrules) eq 'HASH') {
 9888:             if (keys(%{$idrules}) > 0) {
 9889:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9890:                                                 $idruleorder,$numinrow,$rowcount);
 9891:                 $$rowtotal ++;
 9892:                 $rowcount ++;
 9893:             }
 9894:         }
 9895:         if ($rowcount == 0) {
 9896:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9897:             $$rowtotal ++;
 9898:             $rowcount ++;
 9899:         }
 9900:     } elsif ($position eq 'middle') {
 9901:         my @creators = ('author','course','requestcrs');
 9902:         my ($rules,$ruleorder) =
 9903:             &Apache::lonnet::inst_userrules($dom,'username');
 9904:         my %lt = &usercreation_types();
 9905:         my %checked;
 9906:         if (ref($settings) eq 'HASH') {
 9907:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9908:                 foreach my $item (@creators) {
 9909:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9910:                 }
 9911:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9912:                 foreach my $item (@creators) {
 9913:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9914:                         $checked{$item} = 'none';
 9915:                     }
 9916:                 }
 9917:             }
 9918:         }
 9919:         my $rownum = 0;
 9920:         foreach my $item (@creators) {
 9921:             $rownum ++;
 9922:             if ($checked{$item} eq '') {
 9923:                 $checked{$item} = 'any';
 9924:             }
 9925:             my $css_class;
 9926:             if ($rownum%2) {
 9927:                 $css_class = '';
 9928:             } else {
 9929:                 $css_class = ' class="LC_odd_row" ';
 9930:             }
 9931:             $datatable .= '<tr'.$css_class.'>'.
 9932:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9933:                          '</span></td><td style="text-align: right">';
 9934:             my @options = ('any');
 9935:             if (ref($rules) eq 'HASH') {
 9936:                 if (keys(%{$rules}) > 0) {
 9937:                     push(@options,('official','unofficial'));
 9938:                 }
 9939:             }
 9940:             push(@options,'none');
 9941:             foreach my $option (@options) {
 9942:                 my $type = 'radio';
 9943:                 my $check = ' ';
 9944:                 if ($checked{$item} eq $option) {
 9945:                     $check = ' checked="checked" ';
 9946:                 } 
 9947:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9948:                               '<input type="'.$type.'" name="can_createuser_'.
 9949:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9950:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9951:             }
 9952:             $datatable .= '</td></tr>';
 9953:         }
 9954:     } else {
 9955:         my @contexts = ('author','course','domain');
 9956:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9957:         my %checked;
 9958:         if (ref($settings) eq 'HASH') {
 9959:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9960:                 foreach my $item (@contexts) {
 9961:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9962:                         foreach my $auth (@authtypes) {
 9963:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9964:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9965:                             }
 9966:                         }
 9967:                     }
 9968:                 }
 9969:             }
 9970:         } else {
 9971:             foreach my $item (@contexts) {
 9972:                 foreach my $auth (@authtypes) {
 9973:                     $checked{$item}{$auth} = ' checked="checked" ';
 9974:                 }
 9975:             }
 9976:         }
 9977:         my %title = &context_names();
 9978:         my %authname = &authtype_names();
 9979:         my $rownum = 0;
 9980:         my $css_class; 
 9981:         foreach my $item (@contexts) {
 9982:             if ($rownum%2) {
 9983:                 $css_class = '';
 9984:             } else {
 9985:                 $css_class = ' class="LC_odd_row" ';
 9986:             }
 9987:             $datatable .=   '<tr'.$css_class.'>'.
 9988:                             '<td>'.$title{$item}.
 9989:                             '</td><td class="LC_left_item">'.
 9990:                             '<span class="LC_nobreak">';
 9991:             foreach my $auth (@authtypes) {
 9992:                 $datatable .= '<label>'. 
 9993:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9994:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9995:                               $authname{$auth}.'</label>&nbsp;';
 9996:             }
 9997:             $datatable .= '</span></td></tr>';
 9998:             $rownum ++;
 9999:         }
10000:         $$rowtotal += $rownum;
10001:     }
10002:     return $datatable;
10003: }
10004: 
10005: sub print_selfcreation {
10006:     my ($position,$dom,$settings,$rowtotal) = @_;
10007:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
10008:         $emaildomain,$datatable);
10009:     if (ref($settings) eq 'HASH') {
10010:         if (ref($settings->{'cancreate'}) eq 'HASH') {
10011:             $createsettings = $settings->{'cancreate'};
10012:             if (ref($createsettings) eq 'HASH') {
10013:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
10014:                     @selfcreate = @{$createsettings->{'selfcreate'}};
10015:                 } elsif ($createsettings->{'selfcreate'} ne '') {
10016:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
10017:                         @selfcreate = ('email','login','sso');
10018:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
10019:                         @selfcreate = ($createsettings->{'selfcreate'});
10020:                     }
10021:                 }
10022:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
10023:                     $processing = $createsettings->{'selfcreateprocessing'};
10024:                 }
10025:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
10026:                     $emailoptions = $createsettings->{'emailoptions'};
10027:                 }
10028:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
10029:                     $emailverified = $createsettings->{'emailverified'};
10030:                 }
10031:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
10032:                     $emaildomain = $createsettings->{'emaildomain'};
10033:                 }
10034:             }
10035:         }
10036:     }
10037:     my %radiohash;
10038:     my $numinrow = 4;
10039:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
10040:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10041:     if ($position eq 'top') {
10042:         my %choices = &Apache::lonlocal::texthash (
10043:                                                       cancreate_login      => 'Institutional Login',
10044:                                                       cancreate_sso        => 'Institutional Single Sign On',
10045:                                                   );
10046:         my @toggles = sort(keys(%choices));
10047:         my %defaultchecked = (
10048:                                'cancreate_login' => 'off',
10049:                                'cancreate_sso'   => 'off',
10050:                              );
10051:         my ($onclick,$itemcount);
10052:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10053:                                                      \%choices,$itemcount,$onclick);
10054:         $$rowtotal += $itemcount;
10055: 
10056:         if (ref($usertypes) eq 'HASH') {
10057:             if (keys(%{$usertypes}) > 0) {
10058:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
10059:                                              $dom,$numinrow,$othertitle,
10060:                                              'statustocreate',$rowtotal);
10061:                 $$rowtotal ++;
10062:             }
10063:         }
10064:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
10065:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10066:         $fieldtitles{'inststatus'} = &mt('Institutional status');
10067:         my $rem;
10068:         my $numperrow = 2;
10069:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
10070:         $datatable .= '<tr'.$css_class.'>'.
10071:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
10072:                      '<td class="LC_left_item">'."\n".
10073:                      '<table>'."\n";
10074:         for (my $i=0; $i<@fields; $i++) {
10075:             $rem = $i%($numperrow);
10076:             if ($rem == 0) {
10077:                 if ($i > 0) {
10078:                     $datatable .= '</tr>';
10079:                 }
10080:                 $datatable .= '<tr>';
10081:             }
10082:             my $currval;
10083:             if (ref($createsettings) eq 'HASH') {
10084:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
10085:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
10086:                 }
10087:             }
10088:             $datatable .= '<td class="LC_left_item">'.
10089:                           '<span class="LC_nobreak">'.
10090:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
10091:                           'value="'.$currval.'" size="10" />&nbsp;'.
10092:                           $fieldtitles{$fields[$i]}.'</span></td>';
10093:         }
10094:         my $colsleft = $numperrow - $rem;
10095:         if ($colsleft > 1 ) {
10096:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10097:                          '&nbsp;</td>';
10098:         } elsif ($colsleft == 1) {
10099:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
10100:         }
10101:         $datatable .= '</tr></table></td></tr>';
10102:         $$rowtotal ++;
10103:     } elsif ($position eq 'middle') {
10104:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
10105:         my @posstypes;
10106:         if (ref($types) eq 'ARRAY') {
10107:             @posstypes = @{$types};
10108:         }
10109:         unless (grep(/^default$/,@posstypes)) {
10110:             push(@posstypes,'default');
10111:         }
10112:         my %usertypeshash;
10113:         if (ref($usertypes) eq 'HASH') {
10114:             %usertypeshash = %{$usertypes};
10115:         }
10116:         $usertypeshash{'default'} = $othertitle;
10117:         foreach my $status (@posstypes) {
10118:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
10119:                                                    $numinrow,$$rowtotal,\%usertypeshash);
10120:             $$rowtotal ++;
10121:         }
10122:     } else {
10123:         my %choices = &Apache::lonlocal::texthash (
10124:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
10125:                                                   );
10126:         my @toggles = sort(keys(%choices));
10127:         my %defaultchecked = (
10128:                                'cancreate_email' => 'off',
10129:                              );
10130:         my $customclass = 'LC_selfcreate_email';
10131:         my $classprefix = 'LC_canmodify_emailusername_';
10132:         my $optionsprefix = 'LC_options_emailusername_';
10133:         my $display = 'none';
10134:         my $rowstyle = 'display:none';
10135:         if (grep(/^\Qemail\E$/,@selfcreate)) {
10136:             $display = 'block';
10137:             $rowstyle = 'display:table-row';
10138:         }
10139:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
10140:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10141:                                                      \%choices,$$rowtotal,$onclick);
10142:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
10143:                                          $rowstyle);
10144:         $$rowtotal ++;
10145:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
10146:                                       $rowstyle);
10147:         $$rowtotal ++;
10148:         my (@ordered,@posstypes,%usertypeshash);
10149:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10150:         my ($emailrules,$emailruleorder) =
10151:             &Apache::lonnet::inst_userrules($dom,'email');
10152:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
10153:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
10154:         if (ref($types) eq 'ARRAY') {
10155:             @posstypes = @{$types};
10156:         }
10157:         if (@posstypes) {
10158:             unless (grep(/^default$/,@posstypes)) {
10159:                 push(@posstypes,'default');
10160:             }
10161:             if (ref($usertypes) eq 'HASH') {
10162:                 %usertypeshash = %{$usertypes};
10163:             }
10164:             my $currassign;
10165:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
10166:                 $currassign = {
10167:                                   selfassign => $domdefaults{'inststatusguest'},
10168:                               };
10169:                 @ordered = @{$domdefaults{'inststatusguest'}};
10170:             } else {
10171:                 $currassign = { selfassign => [] };
10172:             }
10173:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
10174:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
10175:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
10176:                                          $numinrow,$othertitle,'selfassign',
10177:                                          $rowtotal,$onclicktypes,$customclass,
10178:                                          $rowstyle);
10179:             $$rowtotal ++;
10180:             $usertypeshash{'default'} = $othertitle;
10181:             foreach my $status (@posstypes) {
10182:                 my $css_class;
10183:                 if ($$rowtotal%2) {
10184:                     $css_class = 'LC_odd_row ';
10185:                 }
10186:                 $css_class .= $customclass;
10187:                 my $rowid = $optionsprefix.$status;
10188:                 my $hidden = 1;
10189:                 my $currstyle = 'display:none';
10190:                 if (grep(/^\Q$status\E$/,@ordered)) {
10191:                     $currstyle = $rowstyle;
10192:                     $hidden = 0;
10193:                 }
10194:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10195:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
10196:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
10197:                 unless ($hidden) {
10198:                     $$rowtotal ++;
10199:                 }
10200:             }
10201:         } else {
10202:             my $css_class;
10203:             if ($$rowtotal%2) {
10204:                 $css_class = 'LC_odd_row ';
10205:             }
10206:             $css_class .= $customclass;
10207:             $usertypeshash{'default'} = $othertitle;
10208:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10209:                                          $emailrules,$emailruleorder,$settings,'default','',
10210:                                          $othertitle,$css_class,$rowstyle,$intdom);
10211:             $$rowtotal ++;
10212:         }
10213:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10214:         $numinrow = 1;
10215:         if (@posstypes) {
10216:             foreach my $status (@posstypes) {
10217:                 my $rowid = $classprefix.$status;
10218:                 my $datarowstyle = 'display:none';
10219:                 if (grep(/^\Q$status\E$/,@ordered)) {
10220:                     $datarowstyle = $rowstyle;
10221:                 }
10222:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
10223:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10224:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
10225:                 unless ($datarowstyle eq 'display:none') {
10226:                     $$rowtotal ++;
10227:                 }
10228:             }
10229:         } else {
10230:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
10231:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10232:                                                    $infotitles,'',$customclass,$rowstyle);
10233:         }
10234:     }
10235:     return $datatable;
10236: }
10237: 
10238: sub selfcreate_javascript {
10239:     return <<"ENDSCRIPT";
10240: 
10241: <script type="text/javascript">
10242: // <![CDATA[
10243: 
10244: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
10245:     var x = document.getElementsByClassName(target);
10246:     var insttypes = 0;
10247:     var insttypeRegExp = new RegExp(prefix);
10248:     if ((x.length != undefined) && (x.length > 0)) {
10249:         if (form.elements[radio].length != undefined) {
10250:             for (var i=0; i<form.elements[radio].length; i++) {
10251:                 if (form.elements[radio][i].checked) {
10252:                     if (form.elements[radio][i].value == 1) {
10253:                         for (var j=0; j<x.length; j++) {
10254:                             if (x[j].id == 'undefined') {
10255:                                 x[j].style.display = 'table-row';
10256:                             } else if (insttypeRegExp.test(x[j].id)) {
10257:                                 insttypes ++;
10258:                             } else {
10259:                                 x[j].style.display = 'table-row';
10260:                             }
10261:                         }
10262:                     } else {
10263:                         for (var j=0; j<x.length; j++) {
10264:                             x[j].style.display = 'none';
10265:                         }
10266:                     }
10267:                     break;
10268:                 }
10269:             }
10270:             if (insttypes > 0) {
10271:                 toggleDataRow(form,checkbox,target,altprefix);
10272:                 toggleDataRow(form,checkbox,target,prefix,1);
10273:             }
10274:         }
10275:     }
10276:     return;
10277: }
10278: 
10279: function toggleDataRow(form,checkbox,target,prefix,docount) {
10280:     if (form.elements[checkbox].length != undefined) {
10281:         var count = 0;
10282:         if (docount) {
10283:             for (var i=0; i<form.elements[checkbox].length; i++) {
10284:                 if (form.elements[checkbox][i].checked) {
10285:                     count ++;
10286:                 }
10287:             }
10288:         }
10289:         for (var i=0; i<form.elements[checkbox].length; i++) {
10290:             var type = form.elements[checkbox][i].value;
10291:             if (document.getElementById(prefix+type)) {
10292:                 if (form.elements[checkbox][i].checked) {
10293:                     document.getElementById(prefix+type).style.display = 'table-row';
10294:                     if (count % 2 == 1) {
10295:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
10296:                     } else {
10297:                         document.getElementById(prefix+type).className = target;
10298:                     }
10299:                     count ++;
10300:                 } else {
10301:                     document.getElementById(prefix+type).style.display = 'none';
10302:                 }
10303:             }
10304:         }
10305:     }
10306:     return;
10307: }
10308: 
10309: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
10310:     var caller = radio+'_'+status;
10311:     if (form.elements[caller].length != undefined) {
10312:         for (var i=0; i<form.elements[caller].length; i++) {
10313:             if (form.elements[caller][i].checked) {
10314:                 if (document.getElementById(altprefix+'_inst_'+status)) {
10315:                     var curr = form.elements[caller][i].value;
10316:                     if (prefix) {
10317:                         document.getElementById(prefix+'_'+status).style.display = 'none';
10318:                     }
10319:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
10320:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
10321:                     if (curr == 'custom') {
10322:                         if (prefix) {
10323:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
10324:                         }
10325:                     } else if (curr == 'inst') {
10326:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
10327:                     } else if (curr == 'noninst') {
10328:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
10329:                     }
10330:                     break;
10331:                 }
10332:             }
10333:         }
10334:     }
10335: }
10336: 
10337: // ]]>
10338: </script>
10339: 
10340: ENDSCRIPT
10341: }
10342: 
10343: sub noninst_users {
10344:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
10345:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
10346:     my $class = 'LC_left_item';
10347:     if ($css_class) {
10348:         $css_class = ' class="'.$css_class.'"';
10349:     }
10350:     if ($rowid) {
10351:         $rowid = ' id="'.$rowid.'"';
10352:     }
10353:     if ($rowstyle) {
10354:         $rowstyle = ' style="'.$rowstyle.'"';
10355:     }
10356:     my ($output,$description);
10357:     if ($type eq 'default') {
10358:         $description = &mt('Requests for: [_1]',$typetitle);
10359:     } else {
10360:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
10361:     }
10362:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
10363:               "<td>$description</td>\n".
10364:               '<td class="'.$class.'" colspan="2">'.
10365:               '<table><tr>';
10366:     my %headers = &Apache::lonlocal::texthash(
10367:               approve  => 'Processing',
10368:               email    => 'E-mail',
10369:               username => 'Username',
10370:     );
10371:     foreach my $item ('approve','email','username') {
10372:         $output .= '<th>'.$headers{$item}.'</th>';
10373:     }
10374:     $output .= '</tr><tr>';
10375:     foreach my $item ('approve','email','username') {
10376:         $output .= '<td style="vertical-align: top">';
10377:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
10378:         if ($item eq 'approve') {
10379:             %choices = &Apache::lonlocal::texthash (
10380:                                                      automatic => 'Automatically approved',
10381:                                                      approval  => 'Queued for approval',
10382:                                                    );
10383:             @options = ('automatic','approval');
10384:             $hashref = $processing;
10385:             $defoption = 'automatic';
10386:             $name = 'cancreate_emailprocess_'.$type;
10387:         } elsif ($item eq 'email') {
10388:             %choices = &Apache::lonlocal::texthash (
10389:                                                      any     => 'Any e-mail',
10390:                                                      inst    => 'Institutional only',
10391:                                                      noninst => 'Non-institutional only',
10392:                                                      custom  => 'Custom restrictions',
10393:                                                    );
10394:             @options = ('any','inst','noninst');
10395:             my $showcustom;
10396:             if (ref($emailrules) eq 'HASH') {
10397:                 if (keys(%{$emailrules}) > 0) {
10398:                     push(@options,'custom');
10399:                     $showcustom = 'cancreate_emailrule';
10400:                     if (ref($settings) eq 'HASH') {
10401:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
10402:                             foreach my $rule (@{$settings->{'email_rule'}}) {
10403:                                 if (exists($emailrules->{$rule})) {
10404:                                     $hascustom ++;
10405:                                 }
10406:                             }
10407:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
10408:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
10409:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
10410:                                     if (exists($emailrules->{$rule})) {
10411:                                         $hascustom ++;
10412:                                     }
10413:                                 }
10414:                             }
10415:                         }
10416:                     }
10417:                 }
10418:             }
10419:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
10420:                                                      "'cancreate_emaildomain','$type'".');"';
10421:             $hashref = $emailoptions;
10422:             $defoption = 'any';
10423:             $name = 'cancreate_emailoptions_'.$type;
10424:         } elsif ($item eq 'username') {
10425:             %choices = &Apache::lonlocal::texthash (
10426:                                                      all    => 'Same as e-mail',
10427:                                                      first  => 'Omit @domain',
10428:                                                      free   => 'Free to choose',
10429:                                                    );
10430:             @options = ('all','first','free');
10431:             $hashref = $emailverified;
10432:             $defoption = 'all';
10433:             $name = 'cancreate_usernameoptions_'.$type;
10434:         }
10435:         foreach my $option (@options) {
10436:             my $checked;
10437:             if (ref($hashref) eq 'HASH') {
10438:                 if ($type eq '') {
10439:                     if (!exists($hashref->{'default'})) {
10440:                         if ($option eq $defoption) {
10441:                             $checked = ' checked="checked"';
10442:                         }
10443:                     } else {
10444:                         if ($hashref->{'default'} eq $option) {
10445:                             $checked = ' checked="checked"';
10446:                         }
10447:                     }
10448:                 } else {
10449:                     if (!exists($hashref->{$type})) {
10450:                         if ($option eq $defoption) {
10451:                             $checked = ' checked="checked"';
10452:                         }
10453:                     } else {
10454:                         if ($hashref->{$type} eq $option) {
10455:                             $checked = ' checked="checked"';
10456:                         }
10457:                     }
10458:                 }
10459:             } elsif (($item eq 'email') && ($hascustom)) {
10460:                 if ($option eq 'custom') {
10461:                     $checked = ' checked="checked"';
10462:                 }
10463:             } elsif ($option eq $defoption) {
10464:                 $checked = ' checked="checked"';
10465:             }
10466:             $output .= '<span class="LC_nobreak"><label>'.
10467:                        '<input type="radio" name="'.$name.'"'.
10468:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10469:                        $choices{$option}.'</label></span><br />';
10470:             if ($item eq 'email') {
10471:                 if ($option eq 'custom') {
10472:                     my $id = 'cancreate_emailrule_'.$type;
10473:                     my $display = 'none';
10474:                     if ($checked) {
10475:                         $display = 'inline';
10476:                     }
10477:                     my $numinrow = 2;
10478:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10479:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10480:                                &user_formats_row('email',$settings,$emailrules,
10481:                                                  $emailruleorder,$numinrow,'',$type);
10482:                               '</table></fieldset>';
10483:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10484:                     my %text = &Apache::lonlocal::texthash (
10485:                                                              inst    => 'must end:',
10486:                                                              noninst => 'cannot end:',
10487:                                                            );
10488:                     my $value;
10489:                     if (ref($emaildomain) eq 'HASH') {
10490:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10491:                             $value = $emaildomain->{$type}->{$option};
10492:                         }
10493:                     }
10494:                     if ($value eq '') {
10495:                         $value = '@'.$intdom;
10496:                     }
10497:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10498:                     my $display = 'none';
10499:                     if ($checked) {
10500:                         $display = 'inline';
10501:                     }
10502:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10503:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10504:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10505:                                '</div>';
10506:                 }
10507:             }
10508:         }
10509:         $output .= '</td>'."\n";
10510:     }
10511:     $output .= "</tr></table></td></tr>\n";
10512:     return $output;
10513: }
10514: 
10515: sub captcha_choice {
10516:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10517:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10518:         $vertext,$currver);
10519:     my %lt = &captcha_phrases();
10520:     $keyentry = 'hidden';
10521:     my $colspan=2;
10522:     if ($context eq 'cancreate') {
10523:         $rowname = &mt('CAPTCHA validation');
10524:     } elsif ($context eq 'login') {
10525:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10526:     } elsif ($context eq 'passwords') {
10527:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10528:         $colspan=1;
10529:     }
10530:     if (ref($settings) eq 'HASH') {
10531:         if ($settings->{'captcha'}) {
10532:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10533:         } else {
10534:             $checked{'original'} = ' checked="checked"';
10535:         }
10536:         if ($settings->{'captcha'} eq 'recaptcha') {
10537:             $pubtext = $lt{'pub'};
10538:             $privtext = $lt{'priv'};
10539:             $keyentry = 'text';
10540:             $vertext = $lt{'ver'};
10541:             $currver = $settings->{'recaptchaversion'};
10542:             if ($currver ne '2') {
10543:                 $currver = 1;
10544:             }
10545:         }
10546:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10547:             $currpub = $settings->{'recaptchakeys'}{'public'};
10548:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10549:         }
10550:     } else {
10551:         $checked{'original'} = ' checked="checked"';
10552:     }
10553:     my $css_class;
10554:     if ($itemcount%2) {
10555:         $css_class = 'LC_odd_row';
10556:     }
10557:     if ($customcss) {
10558:         $css_class .= " $customcss";
10559:     }
10560:     $css_class =~ s/^\s+//;
10561:     if ($css_class) {
10562:         $css_class = ' class="'.$css_class.'"';
10563:     }
10564:     if ($rowstyle) {
10565:         $css_class .= ' style="'.$rowstyle.'"';
10566:     }
10567:     my $output = '<tr'.$css_class.'>'.
10568:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10569:                  '<table><tr><td>'."\n";
10570:     foreach my $option ('original','recaptcha','notused') {
10571:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10572:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10573:                    $lt{$option}.'</label></span>';
10574:         unless ($option eq 'notused') {
10575:             $output .= ('&nbsp;'x2)."\n";
10576:         }
10577:     }
10578: #
10579: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10580: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10581: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10582: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10583: #
10584:     $output .= '</td></tr>'."\n".
10585:                '<tr><td class="LC_zero_height">'."\n".
10586:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10587:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10588:                $currpub.'" size="40" /></span><br />'."\n".
10589:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10590:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10591:                $currpriv.'" size="40" /></span><br />'.
10592:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10593:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10594:                $currver.'" size="3" /></span><br />'.
10595:                '</td></tr></table>'."\n".
10596:                '</td></tr>';
10597:     return $output;
10598: }
10599: 
10600: sub user_formats_row {
10601:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10602:     my $output;
10603:     my %text = (
10604:                    'username' => 'new usernames',
10605:                    'id'       => 'IDs',
10606:                );
10607:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10608:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10609:         $output = '<tr '.$css_class.'>'.
10610:                   '<td><span class="LC_nobreak">'.
10611:                   &mt("Format rules to check for $text{$type}: ").
10612:                   '</td><td class="LC_left_item" colspan="2"><table>';
10613:     }
10614:     my $rem;
10615:     if (ref($ruleorder) eq 'ARRAY') {
10616:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10617:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10618:                 my $rem = $i%($numinrow);
10619:                 if ($rem == 0) {
10620:                     if ($i > 0) {
10621:                         $output .= '</tr>';
10622:                     }
10623:                     $output .= '<tr>';
10624:                 }
10625:                 my $check = ' ';
10626:                 if (ref($settings) eq 'HASH') {
10627:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10628:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10629:                             $check = ' checked="checked" ';
10630:                         }
10631:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10632:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10633:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10634:                                 $check = ' checked="checked" ';
10635:                             }
10636:                         }
10637:                     }
10638:                 }
10639:                 my $name = $type.'_rule';
10640:                 if ($type eq 'email') {
10641:                     $name .= '_'.$status;
10642:                 }
10643:                 $output .= '<td class="LC_left_item">'.
10644:                            '<span class="LC_nobreak"><label>'.
10645:                            '<input type="checkbox" name="'.$name.'" '.
10646:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10647:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10648:             }
10649:         }
10650:         $rem = @{$ruleorder}%($numinrow);
10651:     }
10652:     my $colsleft;
10653:     if ($rem) {
10654:         $colsleft = $numinrow - $rem;
10655:     }
10656:     if ($colsleft > 1 ) {
10657:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10658:                    '&nbsp;</td>';
10659:     } elsif ($colsleft == 1) {
10660:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10661:     }
10662:     $output .= '</tr>';
10663:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10664:         $output .= '</table></td></tr>';
10665:     }
10666:     return $output;
10667: }
10668: 
10669: sub usercreation_types {
10670:     my %lt = &Apache::lonlocal::texthash (
10671:                     author     => 'When adding a co-author',
10672:                     course     => 'When adding a user to a course',
10673:                     requestcrs => 'When requesting a course',
10674:                     any        => 'Any',
10675:                     official   => 'Institutional only ',
10676:                     unofficial => 'Non-institutional only',
10677:                     none       => 'None',
10678:     );
10679:     return %lt;
10680: }
10681: 
10682: sub selfcreation_types {
10683:     my %lt = &Apache::lonlocal::texthash (
10684:                     selfcreate => 'User creates own account',
10685:                     any        => 'Any',
10686:                     official   => 'Institutional only ',
10687:                     unofficial => 'Non-institutional only',
10688:                     email      => 'E-mail address',
10689:                     login      => 'Institutional Login',
10690:                     sso        => 'SSO',
10691:              );
10692: }
10693: 
10694: sub authtype_names {
10695:     my %lt = &Apache::lonlocal::texthash(
10696:                       int    => 'Internal',
10697:                       krb4   => 'Kerberos 4',
10698:                       krb5   => 'Kerberos 5',
10699:                       loc    => 'Local',
10700:                       lti    => 'LTI',
10701:                   );
10702:     return %lt;
10703: }
10704: 
10705: sub context_names {
10706:     my %context_title = &Apache::lonlocal::texthash(
10707:        author => 'Creating users when an Author',
10708:        course => 'Creating users when in a course',
10709:        domain => 'Creating users when a Domain Coordinator',
10710:     );
10711:     return %context_title;
10712: }
10713: 
10714: sub print_usermodification {
10715:     my ($position,$dom,$settings,$rowtotal) = @_;
10716:     my $numinrow = 4;
10717:     my ($context,$datatable,$rowcount);
10718:     if ($position eq 'top') {
10719:         $rowcount = 0;
10720:         $context = 'author'; 
10721:         foreach my $role ('ca','aa') {
10722:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10723:                                                    $numinrow,$rowcount);
10724:             $$rowtotal ++;
10725:             $rowcount ++;
10726:         }
10727:     } elsif ($position eq 'bottom') {
10728:         $context = 'course';
10729:         $rowcount = 0;
10730:         foreach my $role ('st','ep','ta','in','cr') {
10731:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10732:                                                    $numinrow,$rowcount);
10733:             $$rowtotal ++;
10734:             $rowcount ++;
10735:         }
10736:     }
10737:     return $datatable;
10738: }
10739: 
10740: sub print_defaults {
10741:     my ($position,$dom,$settings,$rowtotal) = @_;
10742:     my $rownum = 0;
10743:     my ($datatable,$css_class,$titles);
10744:     unless ($position eq 'bottom') {
10745:         $titles = &defaults_titles($dom);
10746:     }
10747:     if ($position eq 'top') {
10748:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10749:                      'datelocale_def','portal_def');
10750:         my %defaults;
10751:         if (ref($settings) eq 'HASH') {
10752:             %defaults = %{$settings};
10753:         } else {
10754:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10755:             foreach my $item (@items) {
10756:                 $defaults{$item} = $domdefaults{$item};
10757:             }
10758:         }
10759:         foreach my $item (@items) {
10760:             if ($rownum%2) {
10761:                 $css_class = '';
10762:             } else {
10763:                 $css_class = ' class="LC_odd_row" ';
10764:             }
10765:             $datatable .= '<tr'.$css_class.'>'.
10766:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10767:                           '</span></td><td class="LC_right_item" colspan="3">';
10768:             if ($item eq 'auth_def') {
10769:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10770:                 my %shortauth = (
10771:                                  internal => 'int',
10772:                                  krb4 => 'krb4',
10773:                                  krb5 => 'krb5',
10774:                                  localauth  => 'loc',
10775:                                  lti => 'lti',
10776:                                 );
10777:                 my %authnames = &authtype_names();
10778:                 foreach my $auth (@authtypes) {
10779:                     my $checked = ' ';
10780:                     if ($defaults{$item} eq $auth) {
10781:                         $checked = ' checked="checked" ';
10782:                     }
10783:                     $datatable .= '<label><input type="radio" name="'.$item.
10784:                                   '" value="'.$auth.'"'.$checked.'/>'.
10785:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10786:                 }
10787:             } elsif ($item eq 'timezone_def') {
10788:                 my $includeempty = 1;
10789:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10790:             } elsif ($item eq 'datelocale_def') {
10791:                 my $includeempty = 1;
10792:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10793:             } elsif ($item eq 'lang_def') {
10794:                 my $includeempty = 1;
10795:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10796:             } elsif ($item eq 'portal_def') {
10797:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10798:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10799:                 my $portalsty = 'none';
10800:                 if ($defaults{$item}) {
10801:                     $portalsty = 'block';
10802:                 }
10803:                 foreach my $field ('email','web') {
10804:                     my $checkedoff = ' checked="checked"';
10805:                     my $checkedon;
10806:                     if ($defaults{$item.'_'.$field}) {
10807:                         $checkedon = $checkedoff;
10808:                         $checkedoff = '';
10809:                     }
10810:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10811:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10812:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10813:                               ('&nbsp;'x2).
10814:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10815:                               '</div>';
10816:                 }
10817:             } else {
10818:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10819:             }
10820:             $datatable .= '</td></tr>';
10821:             $rownum ++;
10822:         }
10823:     } elsif ($position eq 'middle') {
10824:         my %defaults;
10825:         if (ref($settings) eq 'HASH') {
10826:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10827:                 my $maxnum = @{$settings->{'inststatusorder'}};
10828:                 for (my $i=0; $i<$maxnum; $i++) {
10829:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10830:                     my $item = $settings->{'inststatusorder'}->[$i];
10831:                     my $title = $settings->{'inststatustypes'}->{$item};
10832:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10833:                     $datatable .= '<tr'.$css_class.'>'.
10834:                                   '<td><span class="LC_nobreak">'.
10835:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10836:                     for (my $k=0; $k<=$maxnum; $k++) {
10837:                         my $vpos = $k+1;
10838:                         my $selstr;
10839:                         if ($k == $i) {
10840:                             $selstr = ' selected="selected" ';
10841:                         }
10842:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10843:                     }
10844:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10845:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10846:                                   &mt('delete').'</span></td>'.
10847:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10848:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10849:                                   '</span></td></tr>';
10850:                 }
10851:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10852:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10853:                 $datatable .= '<tr '.$css_class.'>'.
10854:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10855:                 for (my $k=0; $k<=$maxnum; $k++) {
10856:                     my $vpos = $k+1;
10857:                     my $selstr;
10858:                     if ($k == $maxnum) {
10859:                         $selstr = ' selected="selected" ';
10860:                     }
10861:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10862:                 }
10863:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10864:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10865:                               '&nbsp;'.&mt('(new)').
10866:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10867:                               &mt('Name displayed').':'.
10868:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10869:                               '</tr>'."\n";
10870:                 $rownum ++;
10871:             }
10872:         }
10873:     } else {
10874:         my ($unamemaprules,$ruleorder) =
10875:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10876:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10877:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10878:             my $numinrow = 2;
10879:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10880:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10881:                                             $ruleorder,$numinrow).
10882:                           '</table></td></tr>';
10883:         }
10884:         if ($datatable eq '') {
10885:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10886:                           &mt('No rules set for domain in customized localenroll.pm').
10887:                           '</td></tr>';
10888:         }
10889:     }
10890:     $$rowtotal += $rownum;
10891:     return $datatable;
10892: }
10893: 
10894: sub get_languages_hash {
10895:     my %langchoices;
10896:     foreach my $id (&Apache::loncommon::languageids()) {
10897:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10898:         if ($code ne '') {
10899:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10900:         }
10901:     }
10902:     return %langchoices;
10903: }
10904: 
10905: sub defaults_titles {
10906:     my ($dom) = @_;
10907:     my %titles = &Apache::lonlocal::texthash (
10908:                    'auth_def'      => 'Default authentication type',
10909:                    'auth_arg_def'  => 'Default authentication argument',
10910:                    'lang_def'      => 'Default language',
10911:                    'timezone_def'  => 'Default timezone',
10912:                    'datelocale_def' => 'Default locale for dates',
10913:                    'portal_def'     => 'Portal/Default URL',
10914:                    'email'          => 'Email links use portal URL',
10915:                    'web'            => 'Public web links use portal URL',
10916:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10917:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10918:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10919:                  );
10920:     if ($dom) {
10921:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10922:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10923:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10924:         $protocol = 'http' if ($protocol ne 'https');
10925:         if ($uint_dom) {
10926:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10927:                                          $uint_dom);
10928:         }
10929:     }
10930:     return (\%titles);
10931: }
10932: 
10933: sub print_scantron {
10934:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10935:     if ($position eq 'top') {
10936:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10937:     } else {
10938:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10939:     }
10940: }
10941: 
10942: sub scantron_javascript {
10943:     return <<"ENDSCRIPT";
10944: 
10945: <script type="text/javascript">
10946: // <![CDATA[
10947: 
10948: function toggleScantron(form) {
10949:     var csvfieldset = new Array();
10950:     if (document.getElementById('scantroncsv_cols')) {
10951:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10952:     }
10953:     if (document.getElementById('scantroncsv_options')) {
10954:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10955:     }
10956:     if (csvfieldset.length) {
10957:         if (document.getElementById('scantronconfcsv')) {
10958:             var scantroncsv = document.getElementById('scantronconfcsv');
10959:             if (scantroncsv.checked) {
10960:                 for (var i=0; i<csvfieldset.length; i++) {
10961:                     csvfieldset[i].style.display = 'block';
10962:                 }
10963:             } else {
10964:                 for (var i=0; i<csvfieldset.length; i++) {
10965:                     csvfieldset[i].style.display = 'none';
10966:                 }
10967:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10968:                 if (csvselects.length) {
10969:                     for (var j=0; j<csvselects.length; j++) {
10970:                         csvselects[j].selectedIndex = 0;
10971:                     }
10972:                 }
10973:             }
10974:         }
10975:     }
10976:     return;
10977: }
10978: // ]]>
10979: </script>
10980: 
10981: ENDSCRIPT
10982: 
10983: }
10984: 
10985: sub print_scantronformat {
10986:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10987:     my $itemcount = 1;
10988:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10989:         %confhash);
10990:     my $switchserver = &check_switchserver($dom,$confname);
10991:     my %lt = &Apache::lonlocal::texthash (
10992:                 default => 'Default bubblesheet format file error',
10993:                 custom  => 'Custom bubblesheet format file error',
10994:              );
10995:     my %scantronfiles = (
10996:         default => 'default.tab',
10997:         custom => 'custom.tab',
10998:     );
10999:     foreach my $key (keys(%scantronfiles)) {
11000:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
11001:                               .$scantronfiles{$key};
11002:     }
11003:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
11004:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
11005:         if (!$switchserver) {
11006:             my $servadm = $r->dir_config('lonAdmEMail');
11007:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
11008:             if ($configuserok eq 'ok') {
11009:                 if ($author_ok eq 'ok') {
11010:                     my %legacyfile = (
11011:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
11012:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
11013:                     );
11014:                     my %md5chk;
11015:                     foreach my $type (keys(%legacyfile)) {
11016:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
11017:                         chomp($md5chk{$type});
11018:                     }
11019:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
11020:                         foreach my $type (keys(%legacyfile)) {
11021:                             ($scantronurls{$type},my $error) =
11022:                                 &legacy_scantronformat($r,$dom,$confname,
11023:                                                  $type,$legacyfile{$type},
11024:                                                  $scantronurls{$type},
11025:                                                  $scantronfiles{$type});
11026:                             if ($error ne '') {
11027:                                 $error{$type} = $error;
11028:                             }
11029:                         }
11030:                         if (keys(%error) == 0) {
11031:                             $is_custom = 1;
11032:                             $confhash{'scantron'}{'scantronformat'} =
11033:                                 $scantronurls{'custom'};
11034:                             my $putresult =
11035:                                 &Apache::lonnet::put_dom('configuration',
11036:                                                          \%confhash,$dom);
11037:                             if ($putresult ne 'ok') {
11038:                                 $error{'custom'} =
11039:                                     '<span class="LC_error">'.
11040:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11041:                             }
11042:                         }
11043:                     } else {
11044:                         ($scantronurls{'default'},my $error) =
11045:                             &legacy_scantronformat($r,$dom,$confname,
11046:                                           'default',$legacyfile{'default'},
11047:                                           $scantronurls{'default'},
11048:                                           $scantronfiles{'default'});
11049:                         if ($error eq '') {
11050:                             $confhash{'scantron'}{'scantronformat'} = ''; 
11051:                             my $putresult =
11052:                                 &Apache::lonnet::put_dom('configuration',
11053:                                                          \%confhash,$dom);
11054:                             if ($putresult ne 'ok') {
11055:                                 $error{'default'} =
11056:                                     '<span class="LC_error">'.
11057:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11058:                             }
11059:                         } else {
11060:                             $error{'default'} = $error;
11061:                         }
11062:                     }
11063:                 }
11064:             }
11065:         } else {
11066:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
11067:         }
11068:     }
11069:     if (ref($settings) eq 'HASH') {
11070:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
11071:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
11072:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
11073:                 $scantronurl = '';
11074:             } else {
11075:                 $scantronurl = $settings->{'scantronformat'};
11076:             }
11077:             $is_custom = 1;
11078:         } else {
11079:             $scantronurl = $scantronurls{'default'};
11080:         }
11081:     } else {
11082:         if ($is_custom) {
11083:             $scantronurl = $scantronurls{'custom'};
11084:         } else {
11085:             $scantronurl = $scantronurls{'default'};
11086:         }
11087:     }
11088:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11089:     $datatable .= '<tr'.$css_class.'>';
11090:     if (!$is_custom) {
11091:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
11092:                       '<span class="LC_nobreak">';
11093:         if ($scantronurl) {
11094:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
11095:                                                          undef,undef,undef,undef,'background-color:#ffffff');
11096:         } else {
11097:             $datatable = &mt('File unavailable for display');
11098:         }
11099:         $datatable .= '</span></td>';
11100:         if (keys(%error) == 0) { 
11101:             $datatable .= '<td style="vertical-align: bottom">';
11102:             if (!$switchserver) {
11103:                 $datatable .= &mt('Upload:').'<br />';
11104:             }
11105:         } else {
11106:             my $errorstr;
11107:             foreach my $key (sort(keys(%error))) {
11108:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11109:             }
11110:             $datatable .= '<td>'.$errorstr;
11111:         }
11112:     } else {
11113:         if (keys(%error) > 0) {
11114:             my $errorstr;
11115:             foreach my $key (sort(keys(%error))) {
11116:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11117:             } 
11118:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
11119:         } elsif ($scantronurl) {
11120:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
11121:                                                        undef,undef,undef,undef,'background-color:#ffffff');
11122:             $datatable .= '<td><span class="LC_nobreak">'.
11123:                           $link.
11124:                           '<label><input type="checkbox" name="scantronformat_del"'.
11125:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
11126:                           '<td><span class="LC_nobreak">&nbsp;'.
11127:                           &mt('Replace:').'</span><br />';
11128:         }
11129:     }
11130:     if (keys(%error) == 0) {
11131:         if ($switchserver) {
11132:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
11133:         } else {
11134:             $datatable .='<span class="LC_nobreak">&nbsp;'.
11135:                          '<input type="file" name="scantronformat" /></span>';
11136:         }
11137:     }
11138:     $datatable .= '</td></tr>';
11139:     $$rowtotal ++;
11140:     return $datatable;
11141: }
11142: 
11143: sub legacy_scantronformat {
11144:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
11145:     my ($url,$error);
11146:     my @statinfo = &Apache::lonnet::stat_file($newurl);
11147:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
11148:         my $modified = [];
11149:         (my $result,$url) =
11150:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
11151:                                                     'scantron','','',$newfile,$modified);
11152:         if ($result eq 'ok') {
11153:             &update_modify_urls($r,$modified);
11154:         } else {
11155:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
11156:         }
11157:     }
11158:     return ($url,$error);
11159: }
11160: 
11161: sub print_scantronconfig {
11162:     my ($dom,$settings,$rowtotal) = @_;
11163:     my $itemcount = 2;
11164:     my $is_checked = ' checked="checked"';
11165:     my %optionson = (
11166:                      hdr => ' checked="checked"',
11167:                      pad => ' checked="checked"',
11168:                      rem => ' checked="checked"',
11169:                     );
11170:     my %optionsoff = (
11171:                       hdr => '',
11172:                       pad => '',
11173:                       rem => '',
11174:                      );
11175:     my $currcsvsty = 'none';
11176:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
11177:     my @fields = &scantroncsv_fields();
11178:     my %titles = &scantronconfig_titles();
11179:     if (ref($settings) eq 'HASH') {
11180:         if (ref($settings->{config}) eq 'HASH') {
11181:             if ($settings->{config}->{dat}) {
11182:                 $checked{'dat'} = $is_checked;
11183:             }
11184:             if (ref($settings->{config}->{csv}) eq 'HASH') {
11185:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
11186:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
11187:                     if (keys(%csvfields) > 0) {
11188:                         $checked{'csv'} = $is_checked;
11189:                         $currcsvsty = 'block';
11190:                     }
11191:                 }
11192:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
11193:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
11194:                     foreach my $option (keys(%optionson)) {
11195:                         unless ($csvoptions{$option}) {
11196:                             $optionsoff{$option} = $optionson{$option};
11197:                             $optionson{$option} = '';
11198:                         }
11199:                     }
11200:                 }
11201:             }
11202:         } else {
11203:             $checked{'dat'} = $is_checked;
11204:         }
11205:     } else {
11206:         $checked{'dat'} = $is_checked;
11207:     }
11208:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
11209:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11210:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
11211:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
11212:     foreach my $item ('dat','csv') {
11213:         my $id;
11214:         if ($item eq 'csv') {
11215:             $id = 'id="scantronconfcsv" ';
11216:         }
11217:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
11218:                       $titles{$item}.'</label>'.('&nbsp;'x3);
11219:         if ($item eq 'csv') {
11220:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
11221:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
11222:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
11223:             foreach my $col (@fields) {
11224:                 my $selnone;
11225:                 if ($csvfields{$col} eq '') {
11226:                     $selnone = ' selected="selected"';
11227:                 }
11228:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
11229:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
11230:                               '<option value=""'.$selnone.'></option>';
11231:                 for (my $i=0; $i<20; $i++) {
11232:                     my $shown = $i+1;
11233:                     my $sel;
11234:                     unless ($selnone) {
11235:                         if (exists($csvfields{$col})) {
11236:                             if ($csvfields{$col} == $i) {
11237:                                 $sel = ' selected="selected"';
11238:                             }
11239:                         }
11240:                     }
11241:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
11242:                 }
11243:                 $datatable .= '</select></td></tr>';
11244:            }
11245:            $datatable .= '</table></fieldset>'.
11246:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
11247:                          '<legend>'.&mt('CSV Options').'</legend>';
11248:            foreach my $option ('hdr','pad','rem') {
11249:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
11250:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
11251:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
11252:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
11253:            }
11254:            $datatable .= '</fieldset>';
11255:            $itemcount ++;
11256:         }
11257:     }
11258:     $datatable .= '</td></tr>';
11259:     $$rowtotal ++;
11260:     return $datatable;
11261: }
11262: 
11263: sub scantronconfig_titles {
11264:     return &Apache::lonlocal::texthash(
11265:                                           dat => 'Standard format (.dat)',
11266:                                           csv => 'Comma separated values (.csv)',
11267:                                           hdr => 'Remove first line in file (contains column titles)',
11268:                                           pad => 'Prepend 0s to PaperID',
11269:                                           rem => 'Remove leading spaces (except Question Response columns)',
11270:                                           CODE => 'CODE',
11271:                                           ID   => 'Student ID',
11272:                                           PaperID => 'Paper ID',
11273:                                           FirstName => 'First Name',
11274:                                           LastName => 'Last Name',
11275:                                           FirstQuestion => 'First Question Response',
11276:                                           Section => 'Section',
11277:     );
11278: }
11279: 
11280: sub scantroncsv_fields {
11281:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
11282: }
11283: 
11284: sub print_coursecategories {
11285:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
11286:     my $datatable;
11287:     if ($position eq 'top') {
11288:         my (%checked);
11289:         my @catitems = ('unauth','auth');
11290:         my @cattypes = ('std','domonly','codesrch','none');
11291:         $checked{'unauth'} = 'std';
11292:         $checked{'auth'} = 'std';
11293:         if (ref($settings) eq 'HASH') {
11294:             foreach my $type (@cattypes) {
11295:                 if ($type eq $settings->{'unauth'}) {
11296:                     $checked{'unauth'} = $type;
11297:                 }
11298:                 if ($type eq $settings->{'auth'}) {
11299:                     $checked{'auth'} = $type;
11300:                 }
11301:             }
11302:         }
11303:         my %lt = &Apache::lonlocal::texthash (
11304:                                                unauth   => 'Catalog type for unauthenticated users',
11305:                                                auth     => 'Catalog type for authenticated users',
11306:                                                none     => 'No catalog',
11307:                                                std      => 'Standard catalog',
11308:                                                domonly  => 'Domain-only catalog',
11309:                                                codesrch => "Code search form",
11310:                                              );
11311:        my $itemcount = 0;
11312:        foreach my $item (@catitems) {
11313:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11314:            $datatable .= '<tr '.$css_class.'>'.
11315:                          '<td>'.$lt{$item}.'</td>'.
11316:                          '<td class="LC_right_item"><span class="LC_nobreak">';
11317:            foreach my $type (@cattypes) {
11318:                my $ischecked;
11319:                if ($checked{$item} eq $type) {
11320:                    $ischecked=' checked="checked"';
11321:                }
11322:                $datatable .= '<label>'.
11323:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
11324:                              ' />'.$lt{$type}.'</label>&nbsp;';
11325:            }
11326:            $datatable .= '</span></td></tr>';
11327:            $itemcount ++;
11328:         }
11329:         $$rowtotal += $itemcount;
11330:     } elsif ($position eq 'middle') {
11331:         my $toggle_cats_crs = ' ';
11332:         my $toggle_cats_dom = ' checked="checked" ';
11333:         my $can_cat_crs = ' ';
11334:         my $can_cat_dom = ' checked="checked" ';
11335:         my $toggle_catscomm_comm = ' ';
11336:         my $toggle_catscomm_dom = ' checked="checked" ';
11337:         my $can_catcomm_comm = ' ';
11338:         my $can_catcomm_dom = ' checked="checked" ';
11339:         my $toggle_catsplace_place = ' ';
11340:         my $toggle_catsplace_dom = ' checked="checked" ';
11341:         my $can_catplace_place = ' ';
11342:         my $can_catplace_dom = ' checked="checked" ';
11343: 
11344:         if (ref($settings) eq 'HASH') {
11345:             if ($settings->{'togglecats'} eq 'crs') {
11346:                 $toggle_cats_crs = $toggle_cats_dom;
11347:                 $toggle_cats_dom = ' ';
11348:             }
11349:             if ($settings->{'categorize'} eq 'crs') {
11350:                 $can_cat_crs = $can_cat_dom;
11351:                 $can_cat_dom = ' ';
11352:             }
11353:             if ($settings->{'togglecatscomm'} eq 'comm') {
11354:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
11355:                 $toggle_catscomm_dom = ' ';
11356:             }
11357:             if ($settings->{'categorizecomm'} eq 'comm') {
11358:                 $can_catcomm_comm = $can_catcomm_dom;
11359:                 $can_catcomm_dom = ' ';
11360:             }
11361:             if ($settings->{'togglecatsplace'} eq 'place') {
11362:                 $toggle_catsplace_place = $toggle_catsplace_dom;
11363:                 $toggle_catsplace_dom = ' ';
11364:             }
11365:             if ($settings->{'categorizeplace'} eq 'place') {
11366:                 $can_catplace_place = $can_catplace_dom;
11367:                 $can_catplace_dom = ' ';
11368:             }
11369:         }
11370:         my %title = &Apache::lonlocal::texthash (
11371:                      togglecats      => 'Show/Hide a course in catalog',
11372:                      togglecatscomm  => 'Show/Hide a community in catalog',
11373:                      togglecatsplace => 'Show/Hide a placement test in catalog',
11374:                      categorize      => 'Assign a category to a course',
11375:                      categorizecomm  => 'Assign a category to a community',
11376:                      categorizeplace => 'Assign a category to a placement test',
11377:                     );
11378:         my %level = &Apache::lonlocal::texthash (
11379:                      dom   => 'Set in Domain',
11380:                      crs   => 'Set in Course',
11381:                      comm  => 'Set in Community',
11382:                      place => 'Set in Placement Test',
11383:                     );
11384:         $datatable = '<tr class="LC_odd_row">'.
11385:                   '<td>'.$title{'togglecats'}.'</td>'.
11386:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11387:                   '<input type="radio" name="togglecats"'.
11388:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11389:                   '<label><input type="radio" name="togglecats"'.
11390:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
11391:                   '</tr><tr>'.
11392:                   '<td>'.$title{'categorize'}.'</td>'.
11393:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11394:                   '<label><input type="radio" name="categorize"'.
11395:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11396:                   '<label><input type="radio" name="categorize"'.
11397:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
11398:                   '</tr><tr class="LC_odd_row">'.
11399:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
11400:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11401:                   '<input type="radio" name="togglecatscomm"'.
11402:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11403:                   '<label><input type="radio" name="togglecatscomm"'.
11404:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
11405:                   '</tr><tr>'.
11406:                   '<td>'.$title{'categorizecomm'}.'</td>'.
11407:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11408:                   '<label><input type="radio" name="categorizecomm"'.
11409:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11410:                   '<label><input type="radio" name="categorizecomm"'.
11411:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
11412:                   '</tr><tr class="LC_odd_row">'.
11413:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
11414:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11415:                   '<input type="radio" name="togglecatsplace"'.
11416:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11417:                   '<label><input type="radio" name="togglecatscomm"'.
11418:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
11419:                   '</tr><tr>'.
11420:                   '<td>'.$title{'categorizeplace'}.'</td>'.
11421:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11422:                   '<label><input type="radio" name="categorizeplace"'.
11423:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11424:                   '<label><input type="radio" name="categorizeplace"'.
11425:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
11426:                   '</tr>';
11427:         $$rowtotal += 6;
11428:     } else {
11429:         my $css_class;
11430:         my $itemcount = 1;
11431:         my $cathash; 
11432:         if (ref($settings) eq 'HASH') {
11433:             $cathash = $settings->{'cats'};
11434:         }
11435:         if (ref($cathash) eq 'HASH') {
11436:             my (@cats,@trails,%allitems,%idx,@jsarray);
11437:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11438:                                                    \%allitems,\%idx,\@jsarray);
11439:             my $maxdepth = scalar(@cats);
11440:             my $colattrib = '';
11441:             if ($maxdepth > 2) {
11442:                 $colattrib = ' colspan="2" ';
11443:             }
11444:             my @path;
11445:             if (@cats > 0) {
11446:                 if (ref($cats[0]) eq 'ARRAY') {
11447:                     my $numtop = @{$cats[0]};
11448:                     my $maxnum = $numtop;
11449:                     my %default_names = (
11450:                           instcode    => &mt('Official courses'),
11451:                           communities => &mt('Communities'),
11452:                           placement   => &mt('Placement Tests'),
11453:                     );
11454: 
11455:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11456:                         ($cathash->{'instcode::0'} eq '') ||
11457:                         (!grep(/^communities$/,@{$cats[0]})) || 
11458:                         ($cathash->{'communities::0'} eq '') ||
11459:                         (!grep(/^placement$/,@{$cats[0]})) ||
11460:                         ($cathash->{'placement::0'} eq '')) {
11461:                         $maxnum ++;
11462:                     }
11463:                     my $lastidx;
11464:                     for (my $i=0; $i<$numtop; $i++) {
11465:                         my $parent = $cats[0][$i];
11466:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11467:                         my $item = &escape($parent).'::0';
11468:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11469:                         $lastidx = $idx{$item};
11470:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11471:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11472:                         for (my $k=0; $k<=$maxnum; $k++) {
11473:                             my $vpos = $k+1;
11474:                             my $selstr;
11475:                             if ($k == $i) {
11476:                                 $selstr = ' selected="selected" ';
11477:                             }
11478:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11479:                         }
11480:                         $datatable .= '</select></span></td><td>';
11481:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11482:                             $datatable .=  '<span class="LC_nobreak">'
11483:                                            .$default_names{$parent}.'</span>';
11484:                             if ($parent eq 'instcode') {
11485:                                 $datatable .= '<br /><span class="LC_nobreak">('
11486:                                               .&mt('with institutional codes')
11487:                                               .')</span></td><td'.$colattrib.'>';
11488:                             } else {
11489:                                 $datatable .= '<table><tr><td>';
11490:                             }
11491:                             $datatable .= '<span class="LC_nobreak">'
11492:                                           .'<label><input type="radio" name="'
11493:                                           .$parent.'" value="1" checked="checked" />'
11494:                                           .&mt('Display').'</label>';
11495:                             if ($parent eq 'instcode') {
11496:                                 $datatable .= '&nbsp;';
11497:                             } else {
11498:                                 $datatable .= '</span></td></tr><tr><td>'
11499:                                               .'<span class="LC_nobreak">';
11500:                             }
11501:                             $datatable .= '<label><input type="radio" name="'
11502:                                           .$parent.'" value="0" />'
11503:                                           .&mt('Do not display').'</label></span>';
11504:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11505:                                 $datatable .= '</td></tr></table>';
11506:                             }
11507:                             $datatable .= '</td>';
11508:                         } else {
11509:                             $datatable .= $parent
11510:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11511:                                           .'<input type="checkbox" name="deletecategory" '
11512:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11513:                         }
11514:                         my $depth = 1;
11515:                         push(@path,$parent);
11516:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11517:                         pop(@path);
11518:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11519:                         $itemcount ++;
11520:                     }
11521:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11522:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11523:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11524:                     for (my $k=0; $k<=$maxnum; $k++) {
11525:                         my $vpos = $k+1;
11526:                         my $selstr;
11527:                         if ($k == $numtop) {
11528:                             $selstr = ' selected="selected" ';
11529:                         }
11530:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11531:                     }
11532:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11533:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11534:                                   .'</tr>'."\n";
11535:                     $itemcount ++;
11536:                     foreach my $default ('instcode','communities','placement') {
11537:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11538:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11539:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11540:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11541:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11542:                             for (my $k=0; $k<=$maxnum; $k++) {
11543:                                 my $vpos = $k+1;
11544:                                 my $selstr;
11545:                                 if ($k == $maxnum) {
11546:                                     $selstr = ' selected="selected" ';
11547:                                 }
11548:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11549:                             }
11550:                             $datatable .= '</select></span></td>'.
11551:                                           '<td><span class="LC_nobreak">'.
11552:                                           $default_names{$default}.'</span>';
11553:                             if ($default eq 'instcode') {
11554:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11555:                                               .&mt('with institutional codes').')</span>';
11556:                             }
11557:                             $datatable .= '</td>'
11558:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11559:                                           .&mt('Display').'</label>&nbsp;'
11560:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11561:                                           .&mt('Do not display').'</label></span></td></tr>';
11562:                         }
11563:                     }
11564:                 }
11565:             } else {
11566:                 $datatable .= &initialize_categories($itemcount);
11567:             }
11568:         } else {
11569:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11570:                           .&initialize_categories($itemcount);
11571:         }
11572:         $$rowtotal += $itemcount;
11573:     }
11574:     return $datatable;
11575: }
11576: 
11577: sub print_serverstatuses {
11578:     my ($dom,$settings,$rowtotal) = @_;
11579:     my $datatable;
11580:     my @pages = &serverstatus_pages();
11581:     my (%namedaccess,%machineaccess);
11582:     foreach my $type (@pages) {
11583:         $namedaccess{$type} = '';
11584:         $machineaccess{$type}= '';
11585:     }
11586:     if (ref($settings) eq 'HASH') {
11587:         foreach my $type (@pages) {
11588:             if (exists($settings->{$type})) {
11589:                 if (ref($settings->{$type}) eq 'HASH') {
11590:                     foreach my $key (keys(%{$settings->{$type}})) {
11591:                         if ($key eq 'namedusers') {
11592:                             $namedaccess{$type} = $settings->{$type}->{$key};
11593:                         } elsif ($key eq 'machines') {
11594:                             $machineaccess{$type} = $settings->{$type}->{$key};
11595:                         }
11596:                     }
11597:                 }
11598:             }
11599:         }
11600:     }
11601:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11602:     my $rownum = 0;
11603:     my $css_class;
11604:     foreach my $type (@pages) {
11605:         $rownum ++;
11606:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11607:         $datatable .= '<tr'.$css_class.'>'.
11608:                       '<td><span class="LC_nobreak">'.
11609:                       $titles->{$type}.'</span></td>'.
11610:                       '<td class="LC_left_item">'.
11611:                       '<input type="text" name="'.$type.'_namedusers" '.
11612:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11613:                       '<td class="LC_right_item">'.
11614:                       '<span class="LC_nobreak">'.
11615:                       '<input type="text" name="'.$type.'_machines" '.
11616:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11617:                       '</span></td></tr>'."\n";
11618:     }
11619:     $$rowtotal += $rownum;
11620:     return $datatable;
11621: }
11622: 
11623: sub serverstatus_pages {
11624:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11625:             'checksums','clusterstatus','certstatus','metadata_keywords',
11626:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11627:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11628: }
11629: 
11630: sub defaults_javascript {
11631:     my ($settings) = @_;
11632:     return unless (ref($settings) eq 'HASH');
11633:     my $portal_js = <<"ENDPORTAL";
11634: 
11635: function portalExtras(caller) {
11636:     var x = caller.value;
11637:     var y = new Array('email','web');
11638:     for (var i=0; i<y.length; i++) {
11639:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11640:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11641:             if (x.length > 0) {
11642:                 z.style.display = 'block';
11643:             } else {
11644:                 z.style.display = 'none';
11645:             }
11646:         }
11647:     }
11648: }
11649: ENDPORTAL
11650:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11651:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11652:         if ($maxnum eq '') {
11653:             $maxnum = 0;
11654:         }
11655:         $maxnum ++;
11656:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11657:         return <<"ENDSCRIPT";
11658: <script type="text/javascript">
11659: // <![CDATA[
11660: function reorderTypes(form,caller) {
11661:     var changedVal;
11662: $jstext 
11663:     var newpos = 'addinststatus_pos';
11664:     var current = new Array;
11665:     var maxh = $maxnum;
11666:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11667:     var oldVal;
11668:     if (caller == newpos) {
11669:         changedVal = newitemVal;
11670:     } else {
11671:         var curritem = 'inststatus_pos_'+caller;
11672:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11673:         current[newitemVal] = newpos;
11674:     }
11675:     for (var i=0; i<inststatuses.length; i++) {
11676:         if (inststatuses[i] != caller) {
11677:             var elementName = 'inststatus_pos_'+inststatuses[i];
11678:             if (form.elements[elementName]) {
11679:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11680:                 current[currVal] = elementName;
11681:             }
11682:         }
11683:     }
11684:     for (var j=0; j<maxh; j++) {
11685:         if (current[j] == undefined) {
11686:             oldVal = j;
11687:         }
11688:     }
11689:     if (oldVal < changedVal) {
11690:         for (var k=oldVal+1; k<=changedVal ; k++) {
11691:            var elementName = current[k];
11692:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11693:         }
11694:     } else {
11695:         for (var k=changedVal; k<oldVal; k++) {
11696:             var elementName = current[k];
11697:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11698:         }
11699:     }
11700:     return;
11701: }
11702: 
11703: $portal_js
11704: 
11705: // ]]>
11706: </script>
11707: 
11708: ENDSCRIPT
11709:     } else {
11710: return <<"ENDSCRIPT";
11711: <script type="text/javascript">
11712: // <![CDATA[
11713: $portal_js
11714: // ]]>
11715: </script>
11716: 
11717: ENDSCRIPT
11718:     }
11719:     return;
11720: }
11721: 
11722: sub passwords_javascript {
11723:     my ($prefix) = @_;
11724:     my %intalert;
11725:     if ($prefix eq 'passwords') {
11726:         %intalert = &Apache::lonlocal::texthash (
11727:             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.',
11728:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11729:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11730:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11731:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11732:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11733:         );
11734:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11735:         %intalert = &Apache::lonlocal::texthash (
11736:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11737:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11738:         );
11739:     }
11740:     &js_escape(\%intalert);
11741:     my $defmin = $Apache::lonnet::passwdmin;
11742:     my $intauthjs;
11743:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11744: 
11745: function warnIntAuth(field) {
11746:     if (field.name == 'intauth_check') {
11747:         if (field.value == '2') {
11748:             alert('$intalert{authcheck}');
11749:         }
11750:     }
11751:     if (field.name == 'intauth_cost') {
11752:         field.value.replace(/\s/g,'');
11753:         if (field.value != '') {
11754:             var regexdigit=/^\\d+\$/;
11755:             if (!regexdigit.test(field.value)) {
11756:                 alert('$intalert{authcost}');
11757:             }
11758:         }
11759:     }
11760:     return;
11761: }
11762: 
11763: ENDSCRIPT
11764: 
11765:      }
11766: 
11767:      $intauthjs .= <<"ENDSCRIPT";
11768: 
11769: function warnInt$prefix(field) {
11770:     field.value.replace(/^\s+/,'');
11771:     field.value.replace(/\s+\$/,'');
11772:     var regexdigit=/^\\d+\$/;
11773:     if (field.name == '${prefix}_min') {
11774:         if (field.value == '') {
11775:             alert('$intalert{passmin}');
11776:             field.value = '$defmin';
11777:         } else {
11778:             if (!regexdigit.test(field.value)) {
11779:                 alert('$intalert{passmin}');
11780:                 field.value = '$defmin';
11781:             }
11782:             var minval = parseInt(field.value,10);
11783:             if (minval < $defmin) {
11784:                 alert('$intalert{passmin}');
11785:                 field.value = '$defmin';
11786:             }
11787:         }
11788:     } else {
11789:         if (field.value == '0') {
11790:             field.value = '';
11791:         }
11792:         if (field.value != '') {
11793:             if (field.name == '${prefix}_expire') {
11794:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11795:                 if (!regexpposnum.test(field.value)) {
11796:                     alert('$intalert{passexp}');
11797:                     field.value = '';
11798:                 } else {
11799:                     var expval = parseFloat(field.value);
11800:                     if (expval == 0) {
11801:                         alert('$intalert{passexp}');
11802:                         field.value = '';
11803:                     }
11804:                 }
11805:             } else {
11806:                 if (!regexdigit.test(field.value)) {
11807:                     if (field.name == '${prefix}_max') {
11808:                         alert('$intalert{passmax}');
11809:                     } else {
11810:                         if (field.name == '${prefix}_numsaved') {
11811:                             alert('$intalert{passnum}');
11812:                         }
11813:                     }
11814:                     field.value = '';
11815:                 }
11816:             }
11817:         }
11818:     }
11819:     return;
11820: }
11821: 
11822: ENDSCRIPT
11823:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11824: }
11825: 
11826: sub coursecategories_javascript {
11827:     my ($settings) = @_;
11828:     my ($output,$jstext,$cathash);
11829:     if (ref($settings) eq 'HASH') {
11830:         $cathash = $settings->{'cats'};
11831:     }
11832:     if (ref($cathash) eq 'HASH') {
11833:         my (@cats,@jsarray,%idx);
11834:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11835:         if (@jsarray > 0) {
11836:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11837:             for (my $i=0; $i<@jsarray; $i++) {
11838:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11839:                     my $catstr = join('","',@{$jsarray[$i]});
11840:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11841:                 }
11842:             }
11843:         }
11844:     } else {
11845:         $jstext  = '    var categories = Array(1);'."\n".
11846:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11847:     }
11848:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11849:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11850:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11851:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11852:     &js_escape(\$instcode_reserved);
11853:     &js_escape(\$communities_reserved);
11854:     &js_escape(\$placement_reserved);
11855:     &js_escape(\$choose_again);
11856:     $output = <<"ENDSCRIPT";
11857: <script type="text/javascript">
11858: // <![CDATA[
11859: function reorderCats(form,parent,item,idx) {
11860:     var changedVal;
11861: $jstext
11862:     var newpos = 'addcategory_pos';
11863:     if (parent == '') {
11864:         var has_instcode = 0;
11865:         var maxtop = categories[idx].length;
11866:         for (var j=0; j<maxtop; j++) {
11867:             if (categories[idx][j] == 'instcode::0') {
11868:                 has_instcode == 1;
11869:             }
11870:         }
11871:         if (has_instcode == 0) {
11872:             categories[idx][maxtop] = 'instcode_pos';
11873:         }
11874:     } else {
11875:         newpos += '_'+parent;
11876:     }
11877:     var maxh = 1 + categories[idx].length;
11878:     var current = new Array;
11879:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11880:     if (item == newpos) {
11881:         changedVal = newitemVal;
11882:     } else {
11883:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11884:         current[newitemVal] = newpos;
11885:     }
11886:     for (var i=0; i<categories[idx].length; i++) {
11887:         var elementName = categories[idx][i];
11888:         if (elementName != item) {
11889:             if (form.elements[elementName]) {
11890:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11891:                 current[currVal] = elementName;
11892:             }
11893:         }
11894:     }
11895:     var oldVal;
11896:     for (var j=0; j<maxh; j++) {
11897:         if (current[j] == undefined) {
11898:             oldVal = j;
11899:         }
11900:     }
11901:     if (oldVal < changedVal) {
11902:         for (var k=oldVal+1; k<=changedVal ; k++) {
11903:            var elementName = current[k];
11904:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11905:         }
11906:     } else {
11907:         for (var k=changedVal; k<oldVal; k++) {
11908:             var elementName = current[k];
11909:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11910:         }
11911:     }
11912:     return;
11913: }
11914: 
11915: function categoryCheck(form) {
11916:     if (form.elements['addcategory_name'].value == 'instcode') {
11917:         alert('$instcode_reserved\\n$choose_again');
11918:         return false;
11919:     }
11920:     if (form.elements['addcategory_name'].value == 'communities') {
11921:         alert('$communities_reserved\\n$choose_again');
11922:         return false;
11923:     }
11924:     if (form.elements['addcategory_name'].value == 'placement') {
11925:         alert('$placement_reserved\\n$choose_again');
11926:         return false;
11927:     }
11928:     return true;
11929: }
11930: 
11931: // ]]>
11932: </script>
11933: 
11934: ENDSCRIPT
11935:     return $output;
11936: }
11937: 
11938: sub initialize_categories {
11939:     my ($itemcount) = @_;
11940:     my ($datatable,$css_class,$chgstr);
11941:     my %default_names = &Apache::lonlocal::texthash (
11942:                       instcode    => 'Official courses (with institutional codes)',
11943:                       communities => 'Communities',
11944:                       placement   => 'Placement Tests',
11945:                         );
11946:     my %selnum = (
11947:                    instcode    => '0',
11948:                    communities => '1',
11949:                    placement   => '2',
11950:                  );
11951:     my %selected;
11952:     foreach my $default ('instcode','communities','placement') {
11953:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11954:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11955:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11956:         $selected{$selnum{$default}} = ' selected="selected"';
11957:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11958:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11959:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11960:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11961:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11962:                      .'<option value="3">4</option></select>&nbsp;'
11963:                      .$default_names{$default}
11964:                      .'</span></td><td><span class="LC_nobreak">'
11965:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11966:                      .&mt('Display').'</label>&nbsp;<label>'
11967:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11968:                  .'</label></span></td></tr>';
11969:         $itemcount ++;
11970:     }
11971:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11972:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11973:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11974:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11975:                   .'<option value="0">1</option>'
11976:                   .'<option value="1">2</option>'
11977:                   .'<option value="2">3</option>'
11978:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11979:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11980:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11981:                   .'</td></tr>';
11982:     return $datatable;
11983: }
11984: 
11985: sub build_category_rows {
11986:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11987:     my ($text,$name,$item,$chgstr);
11988:     if (ref($cats) eq 'ARRAY') {
11989:         my $maxdepth = scalar(@{$cats});
11990:         if (ref($cats->[$depth]) eq 'HASH') {
11991:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11992:                 my $numchildren = @{$cats->[$depth]{$parent}};
11993:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11994:                 $text .= '<td><table class="LC_data_table">';
11995:                 my ($idxnum,$parent_name,$parent_item);
11996:                 my $higher = $depth - 1;
11997:                 if ($higher == 0) {
11998:                     $parent_name = &escape($parent).'::'.$higher;
11999:                 } else {
12000:                     if (ref($path) eq 'ARRAY') {
12001:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12002:                     }
12003:                 }
12004:                 $parent_item = 'addcategory_pos_'.$parent_name;
12005:                 for (my $j=0; $j<=$numchildren; $j++) {
12006:                     if ($j < $numchildren) {
12007:                         $name = $cats->[$depth]{$parent}[$j];
12008:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
12009:                         $idxnum = $idx->{$item};
12010:                     } else {
12011:                         $name = $parent_name;
12012:                         $item = $parent_item;
12013:                     }
12014:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
12015:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
12016:                     for (my $i=0; $i<=$numchildren; $i++) {
12017:                         my $vpos = $i+1;
12018:                         my $selstr;
12019:                         if ($j == $i) {
12020:                             $selstr = ' selected="selected" ';
12021:                         }
12022:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
12023:                     }
12024:                     $text .= '</select>&nbsp;';
12025:                     if ($j < $numchildren) {
12026:                         my $deeper = $depth+1;
12027:                         $text .= $name.'&nbsp;'
12028:                                  .'<label><input type="checkbox" name="deletecategory" value="'
12029:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
12030:                         if(ref($path) eq 'ARRAY') {
12031:                             push(@{$path},$name);
12032:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
12033:                             pop(@{$path});
12034:                         }
12035:                     } else {
12036:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
12037:                         if ($j == $numchildren) {
12038:                             $text .= $name;
12039:                         } else {
12040:                             $text .= $item;
12041:                         }
12042:                         $text .= '" value="" />';
12043:                     }
12044:                     $text .= '</td></tr>';
12045:                 }
12046:                 $text .= '</table></td>';
12047:             } else {
12048:                 my $higher = $depth-1;
12049:                 if ($higher == 0) {
12050:                     $name = &escape($parent).'::'.$higher;
12051:                 } else {
12052:                     if (ref($path) eq 'ARRAY') {
12053:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12054:                     }
12055:                 }
12056:                 my $colspan;
12057:                 if ($parent ne 'instcode') {
12058:                     $colspan = $maxdepth - $depth - 1;
12059:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
12060:                 }
12061:             }
12062:         }
12063:     }
12064:     return $text;
12065: }
12066: 
12067: sub modifiable_userdata_row {
12068:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
12069:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
12070:     my ($role,$rolename,$statustype);
12071:     $role = $item;
12072:     if ($context eq 'cancreate') {
12073:         if ($item =~ /^(emailusername)_(.+)$/) {
12074:             $role = $1;
12075:             $statustype = $2;
12076:             if (ref($usertypes) eq 'HASH') {
12077:                 if ($usertypes->{$statustype}) {
12078:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
12079:                 } else {
12080:                     $rolename = &mt('Data provided by user');
12081:                 }
12082:             }
12083:         }
12084:     } elsif ($context eq 'selfcreate') {
12085:         if (ref($usertypes) eq 'HASH') {
12086:             $rolename = $usertypes->{$role};
12087:         } else {
12088:             $rolename = $role;
12089:         }
12090:     } elsif ($context eq 'lti') {
12091:         $rolename = &mt('Institutional data used (if available)');
12092:     } elsif ($context eq 'privacy') {
12093:         $rolename = $itemdesc;
12094:     } else {
12095:         if ($role eq 'cr') {
12096:             $rolename = &mt('Custom role');
12097:         } else {
12098:             $rolename = &Apache::lonnet::plaintext($role);
12099:         }
12100:     }
12101:     my (@fields,%fieldtitles);
12102:     if (ref($fieldsref) eq 'ARRAY') {
12103:         @fields = @{$fieldsref};
12104:     } else {
12105:         @fields = ('lastname','firstname','middlename','generation',
12106:                    'permanentemail','id');
12107:     }
12108:     if ((ref($titlesref) eq 'HASH')) {
12109:         %fieldtitles = %{$titlesref};
12110:     } else {
12111:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12112:     }
12113:     my $output;
12114:     my $css_class;
12115:     if ($rowcount%2) {
12116:         $css_class = 'LC_odd_row';
12117:     }
12118:     if ($customcss) {
12119:         $css_class .= " $customcss";
12120:     }
12121:     $css_class =~ s/^\s+//;
12122:     if ($css_class) {
12123:         $css_class = ' class="'.$css_class.'"';
12124:     }
12125:     if ($rowstyle) {
12126:         $css_class .= ' style="'.$rowstyle.'"';
12127:     }
12128:     if ($rowid) {
12129:         $rowid = ' id="'.$rowid.'"';
12130:     }
12131:     $output = '<tr '.$css_class.$rowid.'>'.
12132:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
12133:               '<td class="LC_left_item" colspan="2"><table>';
12134:     my $rem;
12135:     my %checks;
12136:     if (ref($settings) eq 'HASH') {
12137:         my $hashref;
12138:         if ($context eq 'lti') {
12139:             if (ref($settings) eq 'HASH') {
12140:                 $hashref = $settings->{'instdata'};
12141:             }
12142:         } elsif ($context eq 'privacy') {
12143:             my ($key,$inner) = split(/_/,$role);
12144:             if (ref($settings) eq 'HASH') {
12145:                 if (ref($settings->{$key}) eq 'HASH') {
12146:                     $hashref = $settings->{$key}->{$inner};
12147:                 }
12148:             }
12149:         } elsif (ref($settings->{$context}) eq 'HASH') {
12150:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
12151:                 $hashref = $settings->{'lti_instdata'};
12152:             }
12153:             if ($role eq 'emailusername') {
12154:                 if ($statustype) {
12155:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
12156:                         $hashref = $settings->{$context}->{$role}->{$statustype};
12157:                     }
12158:                 }
12159:             }
12160:         }
12161:         if (ref($hashref) eq 'HASH') {
12162:             foreach my $field (@fields) {
12163:                 if ($hashref->{$field}) {
12164:                     if ($role eq 'emailusername') {
12165:                         $checks{$field} = $hashref->{$field};
12166:                     } else {
12167:                         $checks{$field} = ' checked="checked" ';
12168:                     }
12169:                 }
12170:             }
12171:         }
12172:     }
12173:     my $total = scalar(@fields);
12174:     for (my $i=0; $i<$total; $i++) {
12175:         $rem = $i%($numinrow);
12176:         if ($rem == 0) {
12177:             if ($i > 0) {
12178:                 $output .= '</tr>';
12179:             }
12180:             $output .= '<tr>';
12181:         }
12182:         my $check = ' ';
12183:         unless ($role eq 'emailusername') {
12184:             if (exists($checks{$fields[$i]})) {
12185:                 $check = $checks{$fields[$i]};
12186:             } elsif ($context eq 'privacy') {
12187:                 if ($role =~ /^priv_(domain|course)$/) {
12188:                     if (ref($settings) ne 'HASH') {
12189:                         $check = ' checked="checked" ';
12190:                     }
12191:                 } elsif ($role =~ /^priv_(author|community)$/) {
12192:                     if (ref($settings) ne 'HASH') {
12193:                         unless ($fields[$i] eq 'id') {
12194:                             $check = ' checked="checked" ';
12195:                         }
12196:                     }
12197:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
12198:                     if (ref($settings) ne 'HASH') {
12199:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
12200:                             $check = ' checked="checked" ';
12201:                         }
12202:                     }
12203:                 }
12204:             } elsif ($context ne 'lti') {
12205:                 if ($role eq 'st') {
12206:                     if (ref($settings) ne 'HASH') {
12207:                         $check = ' checked="checked" '; 
12208:                     }
12209:                 }
12210:             }
12211:         }
12212:         $output .= '<td class="LC_left_item">'.
12213:                    '<span class="LC_nobreak">';
12214:         my $prefix = 'canmodify';
12215:         if ($role eq 'emailusername') {
12216:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
12217:                 $checks{$fields[$i]} = 'omit';
12218:             }
12219:             foreach my $option ('required','optional','omit') {
12220:                 my $checked='';
12221:                 if ($checks{$fields[$i]} eq $option) {
12222:                     $checked='checked="checked" ';
12223:                 }
12224:                 $output .= '<label>'.
12225:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
12226:                            &mt($option).'</label>'.('&nbsp;' x2);
12227:             }
12228:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
12229:         } else {
12230:             if ($context eq 'lti') {
12231:                 $prefix = 'lti';
12232:             } elsif ($context eq 'privacy') {
12233:                 $prefix = 'privacy';
12234:             }
12235:             $output .= '<label>'.
12236:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
12237:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
12238:                        '</label>';
12239:         }
12240:         $output .= '</span></td>';
12241:     }
12242:     $rem = $total%$numinrow;
12243:     my $colsleft;
12244:     if ($rem) {
12245:         $colsleft = $numinrow - $rem;
12246:     }
12247:     if ($colsleft > 1) {
12248:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
12249:                    '&nbsp;</td>';
12250:     } elsif ($colsleft == 1) {
12251:         $output .= '<td class="LC_left_item">&nbsp;</td>';
12252:     }
12253:     $output .= '</tr></table></td></tr>';
12254:     return $output;
12255: }
12256: 
12257: sub insttypes_row {
12258:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
12259:         $customcss,$rowstyle) = @_;
12260:     my %lt = &Apache::lonlocal::texthash (
12261:                       cansearch      => 'Users allowed to search',
12262:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
12263:                       lockablenames  => 'User preference to lock name',
12264:                       selfassign     => 'Self-reportable affiliations',
12265:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
12266:                       webdav         => 'WebDAV access available',
12267:                       authorquota    => 'Authoring Space quota (MB)',
12268:              );
12269:     my ($showdom,$defaultquota);
12270:     if ($context eq 'cansearch') {
12271:         $showdom = ' ('.$dom.')';
12272:     } elsif ($context eq 'authorquota') {
12273:         $defaultquota = 500;
12274:     }
12275:     my $class = 'LC_left_item';
12276:     if ($context eq 'statustocreate') {
12277:         $class = 'LC_right_item';
12278:     }
12279:     my $css_class;
12280:     if ($$rowtotal%2) {
12281:         $css_class = 'LC_odd_row';
12282:     }
12283:     if ($customcss) {
12284:         $css_class .= ' '.$customcss;
12285:     }
12286:     $css_class =~ s/^\s+//;
12287:     if ($css_class) {
12288:         $css_class = ' class="'.$css_class.'"';
12289:     }
12290:     if ($rowstyle) {
12291:         $css_class .= ' style="'.$rowstyle.'"';
12292:     }
12293:     if ($onclick) {
12294:         $onclick = 'onclick="'.$onclick.'" ';
12295:     }
12296:     my $output = '<tr'.$css_class.'>'.
12297:                  '<td>'.$lt{$context}.$showdom.
12298:                  '</td><td class="'.$class.'" colspan="2"><table>';
12299:     my $rem;
12300:     if (ref($types) eq 'ARRAY') {
12301:         for (my $i=0; $i<@{$types}; $i++) {
12302:             if (defined($usertypes->{$types->[$i]})) {
12303:                 my $rem = $i%($numinrow);
12304:                 if ($rem == 0) {
12305:                     if ($i > 0) {
12306:                         $output .= '</tr>';
12307:                     }
12308:                     $output .= '<tr>';
12309:                 }
12310:                 if ($context eq 'authorquota') {
12311:                     my $currquota;
12312:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
12313:                         $currquota = $settings->{$context}->{$types->[$i]};
12314:                     } else {
12315:                         $currquota = $defaultquota;
12316:                     }
12317:                     $output .= '<td class="LC_left_item">'."\n".
12318:                                '<label><span class="LC_nobreak">'."\n".
12319:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
12320:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
12321:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12322:                                '</label></td>';
12323:                 } else {
12324:                     my $check = ' ';
12325:                     if (ref($settings) eq 'HASH') {
12326:                         if (ref($settings->{$context}) eq 'ARRAY') {
12327:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
12328:                                 $check = ' checked="checked" ';
12329:                             }
12330:                         } elsif (ref($settings->{$context}) eq 'HASH') {
12331:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
12332:                                 $check = ' checked="checked" ';
12333:                             } elsif ($context eq 'webdav') {
12334:                                 if ($settings->{$context}->{$types->[$i]}) {
12335:                                     $check = ' checked="checked" ';
12336:                                 }
12337:                             }
12338:                         } elsif ($context eq 'statustocreate') {
12339:                             $check = ' checked="checked" ';
12340:                         }
12341:                     }
12342:                     $output .= '<td class="LC_left_item">'.
12343:                                '<span class="LC_nobreak"><label>'.
12344:                                '<input type="checkbox" name="'.$context.'" '.
12345:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
12346:                                $usertypes->{$types->[$i]}.'</label></span></td>';
12347:                 }
12348:             }
12349:         }
12350:         $rem = @{$types}%($numinrow);
12351:     }
12352:     my $colsleft = $numinrow - $rem;
12353:     if ($context eq 'overrides') {
12354:         if ($colsleft > 1) {
12355:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12356:         } else {
12357:             $output .= '<td class="LC_left_item">';
12358:         }
12359:         $output .= '&nbsp;';
12360:     } else {
12361:         if ($rem == 0) {
12362:             $output .= '<tr>';
12363:         }
12364:         if ($colsleft > 1) {
12365:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12366:         } else {
12367:             $output .= '<td class="LC_left_item">';
12368:         }
12369:         if ($context eq 'authorquota') {
12370:             my $currquota = 500;
12371:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
12372:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
12373:                     $currquota = $settings->{$context}{'default'};
12374:                 }
12375:             }
12376:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
12377:                        '<input type="text" name="'.$context.'_default" '.
12378:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12379:                        '</label>';
12380:         } else {
12381:             my $defcheck = ' ';
12382:             if (ref($settings) eq 'HASH') {
12383:                 if (ref($settings->{$context}) eq 'ARRAY') {
12384:                     if (grep(/^default$/,@{$settings->{$context}})) {
12385:                         $defcheck = ' checked="checked" ';
12386:                     }
12387:                 } elsif (ref($settings->{$context}) eq 'HASH') {
12388:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
12389:                         $defcheck = ' checked="checked" ';
12390:                     } elsif ($context eq 'webdav') {
12391:                         if ($settings->{$context}->{'default'}) {
12392:                             $defcheck = ' checked="checked" ';
12393:                         }
12394:                     }
12395:                 } elsif ($context eq 'statustocreate') {
12396:                     $defcheck = ' checked="checked" ';
12397:                 }
12398:             }
12399:             $output .= '<span class="LC_nobreak"><label>'.
12400:                        '<input type="checkbox" name="'.$context.'" '.
12401:                        'value="default"'.$defcheck.$onclick.'/>'.
12402:                        $othertitle.'</label></span>';
12403:         }
12404:     }
12405:     $output .= '</td></tr></table></td></tr>';
12406:     return $output;
12407: }
12408: 
12409: sub sorted_searchtitles {
12410:     my %searchtitles = &Apache::lonlocal::texthash(
12411:                          'uname' => 'username',
12412:                          'lastname' => 'last name',
12413:                          'lastfirst' => 'last name, first name',
12414:                      );
12415:     my @titleorder = ('uname','lastname','lastfirst');
12416:     return (\%searchtitles,\@titleorder);
12417: }
12418: 
12419: sub sorted_searchtypes {
12420:     my %srchtypes_desc = (
12421:                            exact    => 'is exact match',
12422:                            contains => 'contains ..',
12423:                            begins   => 'begins with ..',
12424:                          );
12425:     my @srchtypeorder = ('exact','begins','contains');
12426:     return (\%srchtypes_desc,\@srchtypeorder);
12427: }
12428: 
12429: sub usertype_update_row {
12430:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
12431:     my $datatable;
12432:     my $numinrow = 4;
12433:     foreach my $type (@{$types}) {
12434:         if (defined($usertypes->{$type})) {
12435:             $$rownums ++;
12436:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
12437:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
12438:                           '</td><td class="LC_left_item"><table>';
12439:             for (my $i=0; $i<@{$fields}; $i++) {
12440:                 my $rem = $i%($numinrow);
12441:                 if ($rem == 0) {
12442:                     if ($i > 0) {
12443:                         $datatable .= '</tr>';
12444:                     }
12445:                     $datatable .= '<tr>';
12446:                 }
12447:                 my $check = ' ';
12448:                 if (ref($settings) eq 'HASH') {
12449:                     if (ref($settings->{'fields'}) eq 'HASH') {
12450:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
12451:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
12452:                                 $check = ' checked="checked" ';
12453:                             }
12454:                         }
12455:                     }
12456:                 }
12457: 
12458:                 if ($i == @{$fields}-1) {
12459:                     my $colsleft = $numinrow - $rem;
12460:                     if ($colsleft > 1) {
12461:                         $datatable .= '<td colspan="'.$colsleft.'">';
12462:                     } else {
12463:                         $datatable .= '<td>';
12464:                     }
12465:                 } else {
12466:                     $datatable .= '<td>';
12467:                 }
12468:                 $datatable .= '<span class="LC_nobreak"><label>'.
12469:                               '<input type="checkbox" name="updateable_'.$type.
12470:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
12471:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
12472:             }
12473:             $datatable .= '</tr></table></td></tr>';
12474:         }
12475:     }
12476:     return $datatable;
12477: }
12478: 
12479: sub modify_login {
12480:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12481:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12482:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12483:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12484:     %title = ( coursecatalog => 'Display course catalog',
12485:                adminmail => 'Display administrator E-mail address',
12486:                helpdesk  => 'Display "Contact Helpdesk" link',
12487:                newuser => 'Link for visitors to create a user account',
12488:                loginheader => 'Log-in box header',
12489:                saml => 'Dual SSO and non-SSO login');
12490:     @offon = ('off','on');
12491:     if (ref($domconfig{login}) eq 'HASH') {
12492:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12493:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12494:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12495:             }
12496:         }
12497:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12498:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12499:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12500:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12501:                     $saml{$lonhost} = 1;
12502:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12503:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12504:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12505:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12506:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12507:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12508:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12509:                 }
12510:             }
12511:         }
12512:     }
12513:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12514:                                            \%domconfig,\%loginhash);
12515:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12516:     foreach my $item (@toggles) {
12517:         $loginhash{login}{$item} = $env{'form.'.$item};
12518:     }
12519:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12520:     if (ref($colchanges{'login'}) eq 'HASH') {  
12521:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12522:                                          \%loginhash);
12523:     }
12524: 
12525:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12526:     my %domservers = &Apache::lonnet::get_servers($dom);
12527:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12528:     if (keys(%servers) > 1) {
12529:         foreach my $lonhost (keys(%servers)) {
12530:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12531:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12532:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12533:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12534:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12535:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12536:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12537:                         $changes{'loginvia'}{$lonhost} = 1;
12538:                     } else {
12539:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12540:                         $changes{'loginvia'}{$lonhost} = 1;
12541:                     }
12542:                 } else {
12543:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12544:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12545:                         $changes{'loginvia'}{$lonhost} = 1;
12546:                     }
12547:                 }
12548:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12549:                     foreach my $item (@loginvia_attribs) {
12550:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12551:                     }
12552:                 } else {
12553:                     foreach my $item (@loginvia_attribs) {
12554:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12555:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12556:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12557:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12558:                                 $new = '/';
12559:                             }
12560:                         }
12561:                         if (($item eq 'custompath') && 
12562:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12563:                             $new = '';
12564:                         }
12565:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12566:                             $changes{'loginvia'}{$lonhost} = 1;
12567:                         }
12568:                         if ($item eq 'exempt') {
12569:                             $new = &check_exempt_addresses($new);
12570:                         }
12571:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12572:                     }
12573:                 }
12574:             } else {
12575:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12576:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12577:                     $changes{'loginvia'}{$lonhost} = 1;
12578:                     foreach my $item (@loginvia_attribs) {
12579:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12580:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12581:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12582:                                 $new = '/';
12583:                             }
12584:                         }
12585:                         if (($item eq 'custompath') && 
12586:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12587:                             $new = '';
12588:                         }
12589:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12590:                     }
12591:                 }
12592:             }
12593:         }
12594:     }
12595: 
12596:     my $servadm = $r->dir_config('lonAdmEMail');
12597:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12598:     if (ref($domconfig{'login'}) eq 'HASH') {
12599:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12600:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12601:                 if ($lang eq 'nolang') {
12602:                     push(@currlangs,$lang);
12603:                 } elsif (defined($langchoices{$lang})) {
12604:                     push(@currlangs,$lang);
12605:                 } else {
12606:                     next;
12607:                 }
12608:             }
12609:         }
12610:     }
12611:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12612:     if (@currlangs > 0) {
12613:         foreach my $lang (@currlangs) {
12614:             if (grep(/^\Q$lang\E$/,@delurls)) {
12615:                 $changes{'helpurl'}{$lang} = 1;
12616:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12617:                 $changes{'helpurl'}{$lang} = 1;
12618:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12619:                 push(@newlangs,$lang);
12620:             } else {
12621:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12622:             }
12623:         }
12624:     }
12625:     unless (grep(/^nolang$/,@currlangs)) {
12626:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12627:             $changes{'helpurl'}{'nolang'} = 1;
12628:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12629:             push(@newlangs,'nolang');
12630:         }
12631:     }
12632:     if ($env{'form.loginhelpurl_add_lang'}) {
12633:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12634:             ($env{'form.loginhelpurl_add_file.filename'})) {
12635:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12636:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12637:         }
12638:     }
12639:     if ((@newlangs > 0) || ($addedfile)) {
12640:         my $error;
12641:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12642:         if ($configuserok eq 'ok') {
12643:             if ($switchserver) {
12644:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12645:             } elsif ($author_ok eq 'ok') {
12646:                 my @allnew = @newlangs;
12647:                 if ($addedfile ne '') {
12648:                     push(@allnew,$addedfile);
12649:                 }
12650:                 my $modified = [];
12651:                 foreach my $lang (@allnew) {
12652:                     my $formelem = 'loginhelpurl_'.$lang;
12653:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12654:                         $formelem = 'loginhelpurl_add_file';
12655:                     }
12656:                     (my $result,$newurl{$lang}) =
12657:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12658:                                                                 "help/$lang",'','',$newfile{$lang},
12659:                                                                 $modified);
12660:                     if ($result eq 'ok') {
12661:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12662:                         $changes{'helpurl'}{$lang} = 1;
12663:                     } else {
12664:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12665:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12666:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12667:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12668:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12669:                         }
12670:                     }
12671:                 }
12672:                 &update_modify_urls($r,$modified);
12673:             } else {
12674:                 $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);
12675:             }
12676:         } else {
12677:             $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);
12678:         }
12679:         if ($error) {
12680:             &Apache::lonnet::logthis($error);
12681:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12682:         }
12683:     }
12684: 
12685:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12686:     if (ref($domconfig{'login'}) eq 'HASH') {
12687:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12688:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12689:                 if ($domservers{$lonhost}) {
12690:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12691:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12692:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12693:                     }
12694:                 }
12695:             }
12696:         }
12697:     }
12698:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12699:     foreach my $lonhost (sort(keys(%domservers))) {
12700:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12701:             $changes{'headtag'}{$lonhost} = 1;
12702:         } else {
12703:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12704:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12705:             }
12706:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12707:                 push(@newhosts,$lonhost);
12708:             } elsif ($currheadtagurls{$lonhost}) {
12709:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12710:                 if ($currexempt{$lonhost}) {
12711:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12712:                         $changes{'headtag'}{$lonhost} = 1;
12713:                     }
12714:                 } elsif ($possexempt{$lonhost}) {
12715:                     $changes{'headtag'}{$lonhost} = 1;
12716:                 }
12717:                 if ($possexempt{$lonhost}) {
12718:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12719:                 }
12720:             }
12721:         }
12722:     }
12723:     if (@newhosts) {
12724:         my $error;
12725:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12726:         if ($configuserok eq 'ok') {
12727:             if ($switchserver) {
12728:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12729:             } elsif ($author_ok eq 'ok') {
12730:                 my $modified = [];
12731:                 foreach my $lonhost (@newhosts) {
12732:                     my $formelem = 'loginheadtag_'.$lonhost;
12733:                     (my $result,$newheadtagurls{$lonhost}) =
12734:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12735:                                                                 "login/headtag/$lonhost",'','',
12736:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12737:                                                                 $modified);
12738:                     if ($result eq 'ok') {
12739:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12740:                         $changes{'headtag'}{$lonhost} = 1;
12741:                         if ($possexempt{$lonhost}) {
12742:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12743:                         }
12744:                     } else {
12745:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12746:                                            $newheadtagurls{$lonhost},$result);
12747:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12748:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12749:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12750:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12751:                         }
12752:                     }
12753:                 }
12754:                 &update_modify_urls($r,$modified);
12755:             } else {
12756:                 $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);
12757:             }
12758:         } else {
12759:             $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);
12760:         }
12761:         if ($error) {
12762:             &Apache::lonnet::logthis($error);
12763:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12764:         }
12765:     }
12766:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12767:     my @newsamlimgs;
12768:     foreach my $lonhost (keys(%domservers)) {
12769:         if ($env{'form.saml_'.$lonhost}) {
12770:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12771:                 push(@newsamlimgs,$lonhost);
12772:             }
12773:             foreach my $item ('text','alt','url','title','window','notsso') {
12774:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12775:             }
12776:             if ($saml{$lonhost}) {
12777:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12778:                     $env{'form.saml_window_'.$lonhost} = '';
12779:                 }
12780:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12781: #FIXME Need to obsolete published image
12782:                     delete($currsaml{$lonhost}{'img'});
12783:                     $changes{'saml'}{$lonhost} = 1;
12784:                 }
12785:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12786:                     $changes{'saml'}{$lonhost} = 1;
12787:                 }
12788:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12789:                     $changes{'saml'}{$lonhost} = 1;
12790:                 }
12791:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12792:                     $changes{'saml'}{$lonhost} = 1;
12793:                 }
12794:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12795:                     $changes{'saml'}{$lonhost} = 1;
12796:                 }
12797:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12798:                     $changes{'saml'}{$lonhost} = 1;
12799:                 }
12800:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12801:                     $changes{'saml'}{$lonhost} = 1;
12802:                 }
12803:             } else {
12804:                 $changes{'saml'}{$lonhost} = 1;
12805:             }
12806:             foreach my $item ('text','alt','url','title','window','notsso') {
12807:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12808:             }
12809:         } else {
12810:             if ($saml{$lonhost}) {
12811:                 $changes{'saml'}{$lonhost} = 1;
12812:                 delete($currsaml{$lonhost});
12813:             }
12814:         }
12815:     }
12816:     foreach my $posshost (keys(%currsaml)) {
12817:         unless (exists($domservers{$posshost})) {
12818:             delete($currsaml{$posshost});
12819:         }
12820:     }
12821:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12822:     if (@newsamlimgs) {
12823:         my $error;
12824:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12825:         if ($configuserok eq 'ok') {
12826:             if ($switchserver) {
12827:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12828:             } elsif ($author_ok eq 'ok') {
12829:                 my $modified = [];
12830:                 foreach my $lonhost (@newsamlimgs) {
12831:                     my $formelem = 'saml_img_'.$lonhost;
12832:                     my ($result,$imgurl) =
12833:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12834:                                                                 "login/saml/$lonhost",'','',
12835:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12836:                                                                 $modified);
12837:                     if ($result eq 'ok') {
12838:                         $currsaml{$lonhost}{'img'} = $imgurl;
12839:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12840:                         $changes{'saml'}{$lonhost} = 1;
12841:                     } else {
12842:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12843:                                            $lonhost,$result);
12844:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12845:                     }
12846:                 }
12847:                 &update_modify_urls($r,$modified);
12848:             } else {
12849:                 $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);
12850:             }
12851:         } else {
12852:             $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);
12853:         }
12854:         if ($error) {
12855:             &Apache::lonnet::logthis($error);
12856:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12857:         }
12858:     }
12859:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12860: 
12861:     my $defaulthelpfile = '/adm/loginproblems.html';
12862:     my $defaulttext = &mt('Default in use');
12863: 
12864:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12865:                                              $dom);
12866:     if ($putresult eq 'ok') {
12867:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12868:         my %defaultchecked = (
12869:                     'coursecatalog' => 'on',
12870:                     'helpdesk'      => 'on',
12871:                     'adminmail'     => 'off',
12872:                     'newuser'       => 'off',
12873:         );
12874:         if (ref($domconfig{'login'}) eq 'HASH') {
12875:             foreach my $item (@toggles) {
12876:                 if ($defaultchecked{$item} eq 'on') { 
12877:                     if (($domconfig{'login'}{$item} eq '0') &&
12878:                         ($env{'form.'.$item} eq '1')) {
12879:                         $changes{$item} = 1;
12880:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12881:                               $domconfig{'login'}{$item} eq '1') &&
12882:                              ($env{'form.'.$item} eq '0')) {
12883:                         $changes{$item} = 1;
12884:                     }
12885:                 } elsif ($defaultchecked{$item} eq 'off') {
12886:                     if (($domconfig{'login'}{$item} eq '1') &&
12887:                         ($env{'form.'.$item} eq '0')) {
12888:                         $changes{$item} = 1;
12889:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12890:                               $domconfig{'login'}{$item} eq '0') &&
12891:                              ($env{'form.'.$item} eq '1')) {
12892:                         $changes{$item} = 1;
12893:                     }
12894:                 }
12895:             }
12896:         }
12897:         if (keys(%changes) > 0 || $colchgtext) {
12898:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12899:             if (exists($changes{'saml'})) {
12900:                 my $hostid_in_use;
12901:                 my @hosts = &Apache::lonnet::current_machine_ids();
12902:                 if (@hosts > 1) {
12903:                     foreach my $hostid (@hosts) {
12904:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12905:                             $hostid_in_use = $hostid;
12906:                             last;
12907:                         }
12908:                     }
12909:                 } else {
12910:                     $hostid_in_use = $r->dir_config('lonHostID');
12911:                 }
12912:                 if (($hostid_in_use) &&
12913:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12914:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12915:                 }
12916:                 if (ref($lastactref) eq 'HASH') {
12917:                     if (ref($changes{'saml'}) eq 'HASH') {
12918:                         my %updates;
12919:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12920:                         $lastactref->{'samllanding'} = \%updates;
12921:                     }
12922:                 }
12923:             }
12924:             if (ref($lastactref) eq 'HASH') {
12925:                 $lastactref->{'domainconfig'} = 1;
12926:             }
12927:             $resulttext = &mt('Changes made:').'<ul>';
12928:             foreach my $item (sort(keys(%changes))) {
12929:                 if ($item eq 'loginvia') {
12930:                     if (ref($changes{$item}) eq 'HASH') {
12931:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12932:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12933:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12934:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12935:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12936:                                     $protocol = 'http' if ($protocol ne 'https');
12937:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12938: 
12939:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12940:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12941:                                     } else {
12942:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12943:                                     }
12944:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12945:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12946:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12947:                                     }
12948:                                     $resulttext .= '</li>';
12949:                                 } else {
12950:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12951:                                 }
12952:                             } else {
12953:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12954:                             }
12955:                         }
12956:                         $resulttext .= '</ul></li>';
12957:                     }
12958:                 } elsif ($item eq 'helpurl') {
12959:                     if (ref($changes{$item}) eq 'HASH') {
12960:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12961:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12962:                                 my ($chg,$link);
12963:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12964:                                 if ($lang eq 'nolang') {
12965:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12966:                                 } else {
12967:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12968:                                 }
12969:                                 $resulttext .= '<li>'.$chg.'</li>';
12970:                             } else {
12971:                                 my $chg;
12972:                                 if ($lang eq 'nolang') {
12973:                                     $chg = &mt('custom log-in help file for no preferred language');
12974:                                 } else {
12975:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12976:                                 }
12977:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12978:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12979:                                                       '?inhibitmenu=yes',$chg,600,500).
12980:                                                '</li>';
12981:                             }
12982:                         }
12983:                     }
12984:                 } elsif ($item eq 'headtag') {
12985:                     if (ref($changes{$item}) eq 'HASH') {
12986:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12987:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12988:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12989:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12990:                                 $resulttext .= '<li><a href="'.
12991:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12992:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12993:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12994:                                 if ($possexempt{$lonhost}) {
12995:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12996:                                 } else {
12997:                                     $resulttext .= &mt('included for any client IP');
12998:                                 }
12999:                                 $resulttext .= '</li>';
13000:                             }
13001:                         }
13002:                     }
13003:                 } elsif ($item eq 'saml') {
13004:                     if (ref($changes{$item}) eq 'HASH') {
13005:                         my %notlt = (
13006:                                        text   => 'Text for log-in by SSO',
13007:                                        img    => 'SSO button image',
13008:                                        alt    => 'Alt text for button image',
13009:                                        url    => 'SSO URL',
13010:                                        title  => 'Tooltip for SSO link',
13011:                                        window => 'Pop-up window if iframe',
13012:                                        notsso => 'Text for non-SSO log-in',
13013:                                     );
13014:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
13015:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
13016:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
13017:                                                '<ul>';
13018:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
13019:                                     if ($currsaml{$lonhost}{$key} eq '') {
13020:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
13021:                                     } else {
13022:                                         my $value = "'$currsaml{$lonhost}{$key}'";
13023:                                         if ($key eq 'img') {
13024:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
13025:                                         } elsif ($key eq 'window') {
13026:                                             $value = 'On';
13027:                                         }
13028:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
13029:                                                                   $value).'</li>';
13030:                                     }
13031:                                 }
13032:                                 $resulttext .= '</ul></li>';
13033:                             } else {
13034:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
13035:                             }
13036:                         }
13037:                     }
13038:                 } elsif ($item eq 'captcha') {
13039:                     if (ref($loginhash{'login'}) eq 'HASH') {
13040:                         my $chgtxt;
13041:                         if ($loginhash{'login'}{$item} eq 'notused') {
13042:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
13043:                         } else {
13044:                             my %captchas = &captcha_phrases();
13045:                             if ($captchas{$loginhash{'login'}{$item}}) {
13046:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
13047:                             } else {
13048:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
13049:                             }
13050:                         }
13051:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13052:                     }
13053:                 } elsif ($item eq 'recaptchakeys') {
13054:                     if (ref($loginhash{'login'}) eq 'HASH') {
13055:                         my ($privkey,$pubkey);
13056:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
13057:                             $pubkey = $loginhash{'login'}{$item}{'public'};
13058:                             $privkey = $loginhash{'login'}{$item}{'private'};
13059:                         }
13060:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
13061:                         if (!$pubkey) {
13062:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
13063:                         } else {
13064:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13065:                         }
13066:                         if (!$privkey) {
13067:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
13068:                         } else {
13069:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
13070:                         }
13071:                         $chgtxt .= '</ul>';
13072:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13073:                     }
13074:                 } elsif ($item eq 'recaptchaversion') {
13075:                     if (ref($loginhash{'login'}) eq 'HASH') {
13076:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
13077:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
13078:                                            '</li>';
13079:                         }
13080:                     }
13081:                 } else {
13082:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
13083:                 }
13084:             }
13085:             $resulttext .= $colchgtext.'</ul>';
13086:         } else {
13087:             $resulttext = &mt('No changes made to log-in page settings');
13088:         }
13089:     } else {
13090:         $resulttext = '<span class="LC_error">'.
13091: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13092:     }
13093:     if ($errors) {
13094:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13095:                        $errors.'</ul>';
13096:     }
13097:     return $resulttext;
13098: }
13099: 
13100: sub check_exempt_addresses {
13101:     my ($iplist) = @_;
13102:     $iplist =~ s/^\s+//;
13103:     $iplist =~ s/\s+$//;
13104:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
13105:     my (@okips,$new);
13106:     foreach my $ip (@poss_ips) {
13107:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
13108:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
13109:                 push(@okips,$ip);
13110:             }
13111:         }
13112:     }
13113:     if (@okips > 0) {
13114:         $new = join(',',@okips);
13115:     } else {
13116:         $new = '';
13117:     }
13118:     return $new;
13119: }
13120: 
13121: sub color_font_choices {
13122:     my %choices =
13123:         &Apache::lonlocal::texthash (
13124:             img => "Header",
13125:             bgs => "Background colors",
13126:             links => "Link colors",
13127:             images => "Images",
13128:             font => "Font color",
13129:             fontmenu => "Font menu",
13130:             pgbg => "Page",
13131:             tabbg => "Header",
13132:             sidebg => "Border",
13133:             link => "Link",
13134:             alink => "Active link",
13135:             vlink => "Visited link",
13136:         );
13137:     return %choices;
13138: }
13139: 
13140: sub modify_ipaccess {
13141:     my ($dom,$lastactref,%domconfig) = @_;
13142:     my (@allpos,%changes,%confhash,$errors,$resulttext);
13143:     my (@items,%deletions,%itemids,@warnings);
13144:     my ($typeorder,$types) = &commblocktype_text();
13145:     if ($env{'form.ipaccess_add'}) {
13146:         my $name = $env{'form.ipaccess_name_add'};
13147:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
13148:         if ($newid) {
13149:             $itemids{'add'} = $newid;
13150:             push(@items,'add');
13151:             $changes{$newid} = 1;
13152:         } else {
13153:             $error = &mt('Failed to acquire unique ID for new IP access control item');
13154:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13155:         }
13156:     }
13157:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13158:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
13159:         if (@todelete) {
13160:             map { $deletions{$_} = 1; } @todelete;
13161:         }
13162:         my $maxnum = $env{'form.ipaccess_maxnum'};
13163:         for (my $i=0; $i<$maxnum; $i++) {
13164:             my $itemid = $env{'form.ipaccess_id_'.$i};
13165:             $itemid =~ s/\D+//g;
13166:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13167:                 if ($deletions{$itemid}) {
13168:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
13169:                 } else {
13170:                     push(@items,$i);
13171:                     $itemids{$i} = $itemid;
13172:                 }
13173:             }
13174:         }
13175:     }
13176:     foreach my $idx (@items) {
13177:         my $itemid = $itemids{$idx};
13178:         next unless ($itemid);
13179:         my %current;
13180:         unless ($idx eq 'add') {
13181:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13182:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
13183:             }
13184:         }
13185:         my $position = $env{'form.ipaccess_pos_'.$itemid};
13186:         $position =~ s/\D+//g;
13187:         if ($position ne '') {
13188:             $allpos[$position] = $itemid;
13189:         }
13190:         my $name = $env{'form.ipaccess_name_'.$idx};
13191:         $name =~ s/^\s+|\s+$//g;
13192:         $confhash{$itemid}{'name'} = $name;
13193:         my $possrange = $env{'form.ipaccess_range_'.$idx};
13194:         $possrange =~ s/^\s+|\s+$//g;
13195:         unless ($possrange eq '') {
13196:             $possrange =~ s/[\r\n]+/\s/g;
13197:             $possrange =~ s/\s*-\s*/-/g;
13198:             $possrange =~ s/\s+/,/g;
13199:             $possrange =~ s/,+/,/g;
13200:             if ($possrange ne '') {
13201:                 my (@ok,$count);
13202:                 $count = 0;
13203:                 foreach my $poss (split(/\,/,$possrange)) {
13204:                     $count ++;
13205:                     $poss = &validate_ip_pattern($poss);
13206:                     if ($poss ne '') {
13207:                         push(@ok,$poss);
13208:                     }
13209:                 }
13210:                 my $diff = $count - scalar(@ok);
13211:                 if ($diff) {
13212:                     $errors .= '<li><span class="LC_error">'.
13213:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
13214:                                    $diff,$name).
13215:                                '</span></li>';
13216:                 }
13217:                 if (@ok) {
13218:                     my @cidr_list;
13219:                     foreach my $item (@ok) {
13220:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
13221:                     }
13222:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
13223:                 }
13224:             }
13225:         }
13226:         foreach my $field ('name','ip') {
13227:             unless (($idx eq 'add') || ($changes{$itemid})) {
13228:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
13229:                     $changes{$itemid} = 1;
13230:                     last;
13231:                 }
13232:             }
13233:         }
13234:         $confhash{$itemid}{'commblocks'} = {};
13235: 
13236:         my %commblocks;
13237:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
13238:         foreach my $type (@{$typeorder}) {
13239:             if ($commblocks{$type}) {
13240:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
13241:             }
13242:             unless (($idx eq 'add') || ($changes{$itemid})) {
13243:                 if (ref($current{'commblocks'}) eq 'HASH') {
13244:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
13245:                         $changes{$itemid} = 1;
13246:                     }
13247:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
13248:                     $changes{$itemid} = 1;
13249:                 }
13250:             }
13251:         }
13252:         $confhash{$itemid}{'courses'} = {};
13253:         my %crsdeletions;
13254:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
13255:         if (@delcrs) {
13256:             map { $crsdeletions{$_} = 1; } @delcrs;
13257:         }
13258:         if (ref($current{'courses'}) eq 'HASH') {
13259:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
13260:                 if ($crsdeletions{$cid}) {
13261:                     $changes{$itemid} = 1;
13262:                 } else {
13263:                     $confhash{$itemid}{'courses'}{$cid} = 1;
13264:                 }
13265:             }
13266:         }
13267:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
13268:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
13269:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
13270:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
13271:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
13272:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
13273:                 $errors .= '<li><span class="LC_error">'.
13274:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
13275:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
13276:                            '</span></li>';
13277:             } else {
13278:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
13279:                 $changes{$itemid} = 1;
13280:             }
13281:         }
13282:     }
13283:     if (@allpos > 0) {
13284:         my $idx = 0;
13285:         foreach my $itemid (@allpos) {
13286:             if ($itemid ne '') {
13287:                 $confhash{$itemid}{'order'} = $idx;
13288:                 unless ($changes{$itemid}) {
13289:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13290:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13291:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
13292:                                 $changes{$itemid} = 1;
13293:                             }
13294:                         }
13295:                     }
13296:                 }
13297:                 $idx ++;
13298:             }
13299:         }
13300:     }
13301:     if (keys(%changes)) {
13302:         my %defaultshash = (
13303:                               ipaccess => \%confhash,
13304:                            );
13305:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13306:                                                  $dom);
13307:         if ($putresult eq 'ok') {
13308:             my $cachetime = 1800;
13309:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
13310:             if (ref($lastactref) eq 'HASH') {
13311:                 $lastactref->{'ipaccess'} = 1;
13312:             }
13313:             $resulttext = &mt('Changes made:').'<ul>';
13314:             my %bynum;
13315:             foreach my $itemid (sort(keys(%changes))) {
13316:                 if (ref($confhash{$itemid}) eq 'HASH') {
13317:                     my $position = $confhash{$itemid}{'order'};
13318:                     if ($position =~ /^\d+$/) {
13319:                         $bynum{$position} = $itemid;
13320:                     }
13321:                 }
13322:             }
13323:             if (keys(%deletions)) {
13324:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
13325:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13326:                 }
13327:             }
13328:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13329:                 my $itemid = $bynum{$pos};
13330:                 if (ref($confhash{$itemid}) eq 'HASH') {
13331:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
13332:                     my $position = $pos + 1;
13333:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13334:                     if ($confhash{$itemid}{'ip'} eq '') {
13335:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
13336:                     } else {
13337:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
13338:                     }
13339:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
13340:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
13341:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
13342:                                        '</li>';
13343:                     } else {
13344:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
13345:                     }
13346:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
13347:                         my @courses;
13348:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
13349:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
13350:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
13351:                         }
13352:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
13353:                                              join('</li><li>',@courses).'</li></ul>';
13354:                     } else {
13355:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
13356:                     }
13357:                     $resulttext .= '</ul></li>';
13358:                 }
13359:             }
13360:             $resulttext .= '</ul>';
13361:         } else {
13362:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13363:         }
13364:     } else {
13365:         $resulttext = &mt('No changes made');
13366:     }
13367:     if ($errors) {
13368:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13369:                        $errors.'</ul></p>';
13370:     }
13371:     return $resulttext;
13372: }
13373: 
13374: sub get_ipaccess_id {
13375:     my ($domain,$location) = @_;
13376:     # get lock on ipaccess db
13377:     my $lockhash = {
13378:                       lock => $env{'user.name'}.
13379:                               ':'.$env{'user.domain'},
13380:                    };
13381:     my $tries = 0;
13382:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13383:     my ($id,$error);
13384: 
13385:     while (($gotlock ne 'ok') && ($tries<10)) {
13386:         $tries ++;
13387:         sleep (0.1);
13388:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13389:     }
13390:     if ($gotlock eq 'ok') {
13391:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
13392:         if ($currids{'lock'}) {
13393:             delete($currids{'lock'});
13394:             if (keys(%currids)) {
13395:                 my @curr = sort { $a <=> $b } keys(%currids);
13396:                 if ($curr[-1] =~ /^\d+$/) {
13397:                     $id = 1 + $curr[-1];
13398:                 }
13399:             } else {
13400:                 $id = 1;
13401:             }
13402:             if ($id) {
13403:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
13404:                     $error = 'nostore';
13405:                 }
13406:             } else {
13407:                 $error = 'nonumber';
13408:             }
13409:         }
13410:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
13411:     } else {
13412:         $error = 'nolock';
13413:     }
13414:     return ($id,$error);
13415: }
13416: 
13417: sub modify_authordefaults {
13418:     my ($dom,$lastactref,%domconfig) = @_;
13419: #
13420: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
13421: #
13422:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
13423:     if (ref($domconfig{'quotas'}) eq 'HASH') {
13424:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
13425:             if ($key =~ /^webdav|authorquota$/) {
13426:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
13427:             } else {
13428:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
13429:             }
13430:         }
13431:     }
13432:     my %staticdefaults = (
13433:                            'copyright'    => 'default',
13434:                            'sourceavail'  => 'closed',
13435:                            'nocodemirror' => 'off',
13436:                            'daxecollapse' => 'off',
13437:                            'domcoordacc'  => 'on',
13438:                            'editors'      => ['edit','xml'],
13439:                            'authorquota'  => 500,
13440:                            'webdav'       => 0,
13441:                            'archive'      => 'off',
13442:                          );
13443:     my %titles = &authordefaults_titles();
13444:     foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13445:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13446:             $confhash{$item} = $env{'form.'.$item};
13447:         }
13448:     }
13449:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
13450:         $confhash{'copyright'} = $1;
13451:     }
13452:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
13453:         $confhash{'sourceavail'} = $1;
13454:     }
13455:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
13456:     my @okeditors = ('edit','xml','daxe');
13457:     my @editors;
13458:     foreach my $item (@posseditors) {
13459:         if (grep(/^\Q$item\E$/,@okeditors)) {
13460:             push(@editors,$item);
13461:         }
13462:     }
13463:     $confhash{'editors'} = \@editors;
13464: 
13465:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13466:     my @insttypes;
13467:     if (ref($types) eq 'ARRAY') {
13468:         @insttypes = @{$types};
13469:     }
13470:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
13471:     my %webdav;
13472:     map { $webdav{$_} = 1; } @webdavon;
13473:     foreach my $type (@insttypes,'default') {
13474:         my $possquota = $env{'form.authorquota_'.$type};
13475:         if ($possquota =~ /^\d+$/) {
13476:             $save_quotas{'authorquota'}{$type} = $possquota;
13477:         }
13478:         if ($webdav{$type}) {
13479:             $save_quotas{'webdav'}{$type} = 1;
13480:         } else {
13481:             $save_quotas{'webdav'}{$type} = 0;
13482:         }
13483:     }
13484:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
13485:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
13486:     }
13487:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
13488:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail','archive') {
13489:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
13490:                 $changes{$item} = 1;
13491:              }
13492:         }
13493:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
13494:             my @diffs =
13495:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13496:                                                    $domconfig{'authordefaults'}{'editors'});
13497:             unless (@diffs == 0) {
13498:                 $changes{'editors'} = 1;
13499:             }
13500:         } else {
13501:             my @diffs =
13502:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13503:                                                    $staticdefaults{'editors'});
13504:             unless (@diffs == 0) {
13505:                 $changes{'editors'} = 1;
13506:             }
13507:         }
13508:     } else {
13509:         my @offon = ('off','on');
13510:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13511:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) {
13512:                 $changes{$item} = 1; 
13513:             }
13514:         }
13515:         foreach my $item ('copyright','sourceavail') {
13516:             if ($confhash{$item} ne $staticdefaults{$item}) {
13517:                 $changes{$item} = 1;
13518:             }
13519:         }
13520:         my @diffs =
13521:             &Apache::loncommon::compare_arrays($confhash{'editors'},
13522:                                                $staticdefaults{'editors'});
13523:         unless (@diffs == 0) {
13524:             $changes{'editors'} = 1;
13525:         }
13526:     }
13527:     foreach my $key ('authorquota','webdav') {
13528:         if (ref($curr_quotas{$key}) eq 'HASH') {
13529:             foreach my $type (@insttypes,'default') {
13530:                 if (exists($save_quotas{$key}{$type})) {
13531:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
13532:                         $changes{$key}{$type} = 1;
13533:                     }
13534:                 } elsif (exists($curr_quotas{$key}{$type})) {
13535:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
13536:                 } else {
13537:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13538:                 }
13539:             }
13540:         } else {
13541:             foreach my $type (@insttypes,'default') {
13542:                 if (exists($save_quotas{$key}{$type})) {
13543:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
13544:                         $changes{$key}{$type} = 1;
13545:                     }
13546:                 } else {
13547:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13548:                 }
13549:             }
13550:         }
13551:     }
13552:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
13553:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13554:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) {
13555:                 $changes{'webdav_LC_adv'} = 1;
13556:             }
13557:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
13558:             $changes{'webdav_LC_adv'} = 1;
13559:         }
13560:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13561:         $changes{'webdav_LC_adv'} = 1;
13562:     }
13563:     my %confighash = (
13564:                         quotas  => \%save_quotas,
13565:                         authordefaults => \%confhash,
13566:                      );
13567:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
13568:                                              $dom);
13569:     my $resulttext;
13570:     if ($putresult eq 'ok') {
13571:         if (keys(%changes)) {
13572:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13573:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
13574:                 ($changes{'webdav_LC_adv'})) {
13575:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
13576:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
13577:                 }
13578:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
13579:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
13580:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
13581:                 }
13582:             }
13583:             $resulttext = &mt('Changes made:').'<ul>';
13584:             my $authoroverride;
13585:             foreach my $key ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail') {
13586:                 if (exists($changes{$key})) {
13587:                     $domdefaults{$key} = $confhash{$key};
13588:                     my $shown;
13589:                     unless ($authoroverride) {
13590:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
13591:                         $authoroverride = 1;
13592:                     }
13593:                     if (($key eq 'nocodemirror') || ($key eq 'daxecollapse') || ($key eq 'domcoordacc')) {
13594:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13595:                     } elsif ($key eq 'copyright') {
13596:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
13597:                     } elsif ($key eq 'sourceavail') {
13598:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
13599:                     }
13600:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13601:                 }
13602:             }
13603:             if ($authoroverride) {
13604:                 $resulttext .= '</ul></li>';
13605:             }
13606:             my $domcoordoverride;
13607:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv','archive') {
13608:                 if (exists($changes{$key})) {
13609:                     my $shown;
13610:                     unless ($domcoordoverride) {
13611:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
13612:                         $domcoordoverride = 1;
13613:                     }
13614:                     if ($key eq 'editors') {
13615:                         if (ref($confhash{'editors'}) eq 'ARRAY') {
13616:                             $domdefaults{'editors'} = join(',',@{$confhash{'editors'}});
13617:                             if (@{$confhash{'editors'}}) {
13618:                                 $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
13619:                             } else {
13620:                                 $shown = &mt('None');
13621:                             }
13622:                         }
13623:                     } elsif ($key eq 'authorquota') {
13624:                         foreach my $type (@insttypes) {
13625:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
13626:                         }
13627:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
13628:                     } elsif ($key eq 'webdav') {
13629:                         foreach my $type (@insttypes) {
13630:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
13631:                         }
13632:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
13633:                     } elsif ($key eq 'webdav_LC_adv') {
13634:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13635:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
13636:                         } else {
13637:                             $shown = $titles{'none'};
13638:                         }
13639:                     } elsif ($key eq 'archive') {
13640:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13641:                     }
13642:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13643:                 }
13644:             }
13645:             if ($domcoordoverride) {
13646:                 $resulttext .= '</ul></li>';
13647:             }
13648:             $resulttext .= '</ul>';
13649:             my $cachetime = 24*60*60;
13650:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13651:             if (ref($lastactref) eq 'HASH') {
13652:                 $lastactref->{'domdefaults'} = 1;
13653:             }
13654:         } else {
13655:             $resulttext = &mt('No changes made to Authoring Space defaults');
13656:         }
13657:     }
13658:     return $resulttext;
13659: }
13660: 
13661: sub modify_rolecolors {
13662:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
13663:     my ($resulttext,%rolehash);
13664:     $rolehash{'rolecolors'} = {};
13665:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
13666:         if ($domconfig{'rolecolors'} eq '') {
13667:             $domconfig{'rolecolors'} = {};
13668:         }
13669:     }
13670:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
13671:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
13672:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
13673:                                              $dom);
13674:     if ($putresult eq 'ok') {
13675:         if (keys(%changes) > 0) {
13676:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13677:             if (ref($lastactref) eq 'HASH') {
13678:                 $lastactref->{'domainconfig'} = 1;
13679:             }
13680:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
13681:                                              $rolehash{'rolecolors'});
13682:         } else {
13683:             $resulttext = &mt('No changes made to default color schemes');
13684:         }
13685:     } else {
13686:         $resulttext = '<span class="LC_error">'.
13687: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13688:     }
13689:     if ($errors) {
13690:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13691:                        $errors.'</ul>';
13692:     }
13693:     return $resulttext;
13694: }
13695: 
13696: sub modify_colors {
13697:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
13698:     my (%changes,%choices);
13699:     my @bgs;
13700:     my @links = ('link','alink','vlink');
13701:     my @logintext;
13702:     my @images;
13703:     my $servadm = $r->dir_config('lonAdmEMail');
13704:     my $errors;
13705:     my %defaults;
13706:     foreach my $role (@{$roles}) {
13707:         if ($role eq 'login') {
13708:             %choices = &login_choices();
13709:             @logintext = ('textcol','bgcol');
13710:         } else {
13711:             %choices = &color_font_choices();
13712:         }
13713:         if ($role eq 'login') {
13714:             @images = ('img','logo','domlogo','login');
13715:             @bgs = ('pgbg','mainbg','sidebg');
13716:         } else {
13717:             @images = ('img');
13718:             @bgs = ('pgbg','tabbg','sidebg');
13719:         }
13720:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
13721:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
13722:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13723:         }
13724:         if ($role eq 'login') {
13725:             foreach my $item (@logintext) {
13726:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13727:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13728:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13729:                 }
13730:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13731:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13732:                 }
13733:             }
13734:         } else {
13735:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13736:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13737:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13738:             }
13739:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13740:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13741:             }
13742:         }
13743:         foreach my $item (@bgs) {
13744:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13745:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13746:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13747:             }
13748:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13749:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13750:             }
13751:         }
13752:         foreach my $item (@links) {
13753:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13754:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13755:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13756:             }
13757:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13758:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13759:             }
13760:         }
13761:         my ($configuserok,$author_ok,$switchserver) = 
13762:             &config_check($dom,$confname,$servadm);
13763:         my ($width,$height) = &thumb_dimensions();
13764:         if (ref($domconfig->{$role}) ne 'HASH') {
13765:             $domconfig->{$role} = {};
13766:         }
13767:         foreach my $img (@images) {
13768:             if ($role eq 'login') {
13769:                 if (($img eq 'img') || ($img eq 'logo')) {  
13770:                     if (defined($env{'form.login_showlogo_'.$img})) {
13771:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13772:                     } else { 
13773:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13774:                     }
13775:                 }
13776:                 if ($env{'form.login_alt_'.$img} ne '') {
13777:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13778:                 }
13779:             }
13780: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13781: 		 && !defined($domconfig->{$role}{$img})
13782: 		 && !$env{'form.'.$role.'_del_'.$img}
13783: 		 && $env{'form.'.$role.'_import_'.$img}) {
13784: 		# import the old configured image from the .tab setting
13785: 		# if they haven't provided a new one 
13786: 		$domconfig->{$role}{$img} = 
13787: 		    $env{'form.'.$role.'_import_'.$img};
13788: 	    }
13789:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13790:                 my $error;
13791:                 if ($configuserok eq 'ok') {
13792:                     if ($switchserver) {
13793:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13794:                     } else {
13795:                         if ($author_ok eq 'ok') {
13796:                             my $modified = [];
13797:                             my ($result,$logourl) = 
13798:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13799:                                                                         $dom,$confname,$img,$width,$height,
13800:                                                                         '',$modified);
13801:                             if ($result eq 'ok') {
13802:                                 $confhash->{$role}{$img} = $logourl;
13803:                                 $changes{$role}{'images'}{$img} = 1;
13804:                                 &update_modify_urls($r,$modified);
13805:                             } else {
13806:                                 $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);
13807:                             }
13808:                         } else {
13809:                             $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);
13810:                         }
13811:                     }
13812:                 } else {
13813:                     $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);
13814:                 }
13815:                 if ($error) {
13816:                     &Apache::lonnet::logthis($error);
13817:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13818:                 }
13819:             } elsif ($domconfig->{$role}{$img} ne '') {
13820:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13821:                     my $error;
13822:                     if ($configuserok eq 'ok') {
13823: # is confname an author?
13824:                         if ($switchserver eq '') {
13825:                             if ($author_ok eq 'ok') {
13826:                                 my $modified = [];
13827:                                 my ($result,$logourl) = 
13828:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13829:                                                                             $dom,$confname,$img,$width,$height,
13830:                                                                             '',$modified);
13831:                                 if ($result eq 'ok') {
13832:                                     $confhash->{$role}{$img} = $logourl;
13833: 				    $changes{$role}{'images'}{$img} = 1;
13834:                                     &update_modify_urls($r,$modified);
13835:                                 }
13836:                             }
13837:                         }
13838:                     }
13839:                 }
13840:             }
13841:         }
13842:         if (ref($domconfig) eq 'HASH') {
13843:             if (ref($domconfig->{$role}) eq 'HASH') {
13844:                 foreach my $img (@images) {
13845:                     if ($domconfig->{$role}{$img} ne '') {
13846:                         if ($env{'form.'.$role.'_del_'.$img}) {
13847:                             $confhash->{$role}{$img} = '';
13848:                             $changes{$role}{'images'}{$img} = 1;
13849:                         } else {
13850:                             if ($confhash->{$role}{$img} eq '') {
13851:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13852:                             }
13853:                         }
13854:                     } else {
13855:                         if ($env{'form.'.$role.'_del_'.$img}) {
13856:                             $confhash->{$role}{$img} = '';
13857:                             $changes{$role}{'images'}{$img} = 1;
13858:                         } 
13859:                     }
13860:                     if ($role eq 'login') {
13861:                         if (($img eq 'logo') || ($img eq 'img')) {
13862:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13863:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13864:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13865:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13866:                                 }
13867:                             } else {
13868:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13869:                                     $changes{$role}{'showlogo'}{$img} = 1;
13870:                                 }
13871:                             }
13872:                         }
13873:                         if ($img ne 'login') {
13874:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13875:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13876:                                     $domconfig->{$role}{'alttext'}{$img}) {
13877:                                     $changes{$role}{'alttext'}{$img} = 1;
13878:                                 }
13879:                             } else {
13880:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13881:                                     $changes{$role}{'alttext'}{$img} = 1;
13882:                                 }
13883:                             }
13884:                         }
13885:                     }
13886:                 }
13887:                 if ($domconfig->{$role}{'font'} ne '') {
13888:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13889:                         $changes{$role}{'font'} = 1;
13890:                     }
13891:                 } else {
13892:                     if ($confhash->{$role}{'font'}) {
13893:                         $changes{$role}{'font'} = 1;
13894:                     }
13895:                 }
13896:                 if ($role ne 'login') {
13897:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13898:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13899:                             $changes{$role}{'fontmenu'} = 1;
13900:                         }
13901:                     } else {
13902:                         if ($confhash->{$role}{'fontmenu'}) {
13903:                             $changes{$role}{'fontmenu'} = 1;
13904:                         }
13905:                     }
13906:                 }
13907:                 foreach my $item (@bgs) {
13908:                     if ($domconfig->{$role}{$item} ne '') {
13909:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13910:                             $changes{$role}{'bgs'}{$item} = 1;
13911:                         } 
13912:                     } else {
13913:                         if ($confhash->{$role}{$item}) {
13914:                             $changes{$role}{'bgs'}{$item} = 1;
13915:                         }
13916:                     }
13917:                 }
13918:                 foreach my $item (@links) {
13919:                     if ($domconfig->{$role}{$item} ne '') {
13920:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13921:                             $changes{$role}{'links'}{$item} = 1;
13922:                         }
13923:                     } else {
13924:                         if ($confhash->{$role}{$item}) {
13925:                             $changes{$role}{'links'}{$item} = 1;
13926:                         }
13927:                     }
13928:                 }
13929:                 foreach my $item (@logintext) {
13930:                     if ($domconfig->{$role}{$item} ne '') {
13931:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13932:                             $changes{$role}{'logintext'}{$item} = 1;
13933:                         }
13934:                     } else {
13935:                         if ($confhash->{$role}{$item}) {
13936:                             $changes{$role}{'logintext'}{$item} = 1;
13937:                         }
13938:                     }
13939:                 }
13940:             } else {
13941:                 &default_change_checker($role,\@images,\@links,\@bgs,
13942:                                         \@logintext,$confhash,\%changes); 
13943:             }
13944:         } else {
13945:             &default_change_checker($role,\@images,\@links,\@bgs,
13946:                                     \@logintext,$confhash,\%changes); 
13947:         }
13948:     }
13949:     return ($errors,%changes);
13950: }
13951: 
13952: sub config_check {
13953:     my ($dom,$confname,$servadm) = @_;
13954:     my ($configuserok,$author_ok,$switchserver,%currroles);
13955:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13956:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13957:                                                    $confname,$servadm);
13958:     if ($configuserok eq 'ok') {
13959:         $switchserver = &check_switchserver($dom,$confname);
13960:         if ($switchserver eq '') {
13961:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13962:         }
13963:     }
13964:     return ($configuserok,$author_ok,$switchserver);
13965: }
13966: 
13967: sub default_change_checker {
13968:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13969:     foreach my $item (@{$links}) {
13970:         if ($confhash->{$role}{$item}) {
13971:             $changes->{$role}{'links'}{$item} = 1;
13972:         }
13973:     }
13974:     foreach my $item (@{$bgs}) {
13975:         if ($confhash->{$role}{$item}) {
13976:             $changes->{$role}{'bgs'}{$item} = 1;
13977:         }
13978:     }
13979:     foreach my $item (@{$logintext}) {
13980:         if ($confhash->{$role}{$item}) {
13981:             $changes->{$role}{'logintext'}{$item} = 1;
13982:         }
13983:     }
13984:     foreach my $img (@{$images}) {
13985:         if ($env{'form.'.$role.'_del_'.$img}) {
13986:             $confhash->{$role}{$img} = '';
13987:             $changes->{$role}{'images'}{$img} = 1;
13988:         }
13989:         if ($role eq 'login') {
13990:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13991:                 $changes->{$role}{'showlogo'}{$img} = 1;
13992:             }
13993:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13994:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13995:                     $changes->{$role}{'alttext'}{$img} = 1;
13996:                 }
13997:             }
13998:         }
13999:     }
14000:     if ($confhash->{$role}{'font'}) {
14001:         $changes->{$role}{'font'} = 1;
14002:     }
14003: }
14004: 
14005: sub display_colorchgs {
14006:     my ($dom,$changes,$roles,$confhash) = @_;
14007:     my (%choices,$resulttext);
14008:     if (!grep(/^login$/,@{$roles})) {
14009:         $resulttext = &mt('Changes made:').'<br />';
14010:     }
14011:     foreach my $role (@{$roles}) {
14012:         if ($role eq 'login') {
14013:             %choices = &login_choices();
14014:         } else {
14015:             %choices = &color_font_choices();
14016:         }
14017:         if (ref($changes->{$role}) eq 'HASH') {
14018:             if ($role ne 'login') {
14019:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
14020:             }
14021:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
14022:                 if ($role ne 'login') {
14023:                     $resulttext .= '<ul>';
14024:                 }
14025:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
14026:                     if ($role ne 'login') {
14027:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
14028:                     }
14029:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
14030:                         if (($role eq 'login') && ($key eq 'showlogo')) {
14031:                             if ($confhash->{$role}{$key}{$item}) {
14032:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
14033:                             } else {
14034:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
14035:                             }
14036:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
14037:                             if ($confhash->{$role}{$key}{$item} ne '') {
14038:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
14039:                                                $confhash->{$role}{$key}{$item}).'</li>';
14040:                             } else {
14041:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
14042:                             }
14043:                         } elsif ($confhash->{$role}{$item} eq '') {
14044:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
14045:                         } else {
14046:                             my $newitem = $confhash->{$role}{$item};
14047:                             if ($key eq 'images') {
14048:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
14049:                             }
14050:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
14051:                         }
14052:                     }
14053:                     if ($role ne 'login') {
14054:                         $resulttext .= '</ul></li>';
14055:                     }
14056:                 } else {
14057:                     if ($confhash->{$role}{$key} eq '') {
14058:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
14059:                     } else {
14060:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
14061:                     }
14062:                 }
14063:                 if ($role ne 'login') {
14064:                     $resulttext .= '</ul>';
14065:                 }
14066:             }
14067:         }
14068:     }
14069:     return $resulttext;
14070: }
14071: 
14072: sub thumb_dimensions {
14073:     return ('200','50');
14074: }
14075: 
14076: sub check_dimensions {
14077:     my ($inputfile) = @_;
14078:     my ($fullwidth,$fullheight);
14079:     if ($inputfile =~ m|^[/\w.\-]+$|) {
14080:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
14081:             my $imageinfo = <PIPE>;
14082:             if (!close(PIPE)) {
14083:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
14084:             }
14085:             chomp($imageinfo);
14086:             my ($fullsize) = 
14087:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
14088:             if ($fullsize) {
14089:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
14090:             }
14091:         }
14092:     }
14093:     return ($fullwidth,$fullheight);
14094: }
14095: 
14096: sub check_configuser {
14097:     my ($uhome,$dom,$confname,$servadm) = @_;
14098:     my ($configuserok,%currroles);
14099:     if ($uhome eq 'no_host') {
14100:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
14101:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
14102:         $configuserok = 
14103:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
14104:                              $configpass,'','','','','',undef,$servadm);
14105:     } else {
14106:         $configuserok = 'ok';
14107:         %currroles = 
14108:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
14109:     }
14110:     return ($configuserok,%currroles);
14111: }
14112: 
14113: sub check_authorstatus {
14114:     my ($dom,$confname,%currroles) = @_;
14115:     my $author_ok;
14116:     if (!$currroles{':'.$dom.':au'}) {
14117:         my $start = time;
14118:         my $end = 0;
14119:         $author_ok = 
14120:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
14121:                                         'au',$end,$start,'','','domconfig');
14122:     } else {
14123:         $author_ok = 'ok';
14124:     }
14125:     return $author_ok;
14126: }
14127: 
14128: sub update_modify_urls {
14129:     my ($r,$modified) = @_;
14130:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
14131:         push(@{$modified_urls},$modified);
14132:         unless ($registered_cleanup) {
14133:             my $handlers = $r->get_handlers('PerlCleanupHandler');
14134:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
14135:             $registered_cleanup=1;
14136:         }
14137:     }
14138: }
14139: 
14140: sub notifysubscribed {
14141:     foreach my $targetsource (@{$modified_urls}){
14142:         next unless (ref($targetsource) eq 'ARRAY');
14143:         my ($target,$source)=@{$targetsource};
14144:         if ($source ne '') {
14145:             if (open(my $logfh,">>",$source.'.log')) {
14146:                 print $logfh "\nCleanup phase: Notifications\n";
14147:                 my @subscribed=&subscribed_hosts($target);
14148:                 foreach my $subhost (@subscribed) {
14149:                     print $logfh "\nNotifying host ".$subhost.':';
14150:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
14151:                     print $logfh $reply;
14152:                 }
14153:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
14154:                 foreach my $subhost (@subscribedmeta) {
14155:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
14156:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
14157:                                                         $subhost);
14158:                     print $logfh $reply;
14159:                 }
14160:                 print $logfh "\n============ Done ============\n";
14161:                 close($logfh);
14162:             }
14163:         }
14164:     }
14165:     return OK;
14166: }
14167: 
14168: sub subscribed_hosts {
14169:     my ($target) = @_;
14170:     my @subscribed;
14171:     if (open(my $fh,"<","$target.subscription")) {
14172:         while (my $subline=<$fh>) {
14173:             if ($subline =~ /^($match_lonid):/) {
14174:                 my $host = $1;
14175:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
14176:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
14177:                         push(@subscribed,$host);
14178:                     }
14179:                 }
14180:             }
14181:         }
14182:     }
14183:     return @subscribed;
14184: }
14185: 
14186: sub check_switchserver {
14187:     my ($dom,$confname) = @_;
14188:     my ($allowed,$switchserver,$home);
14189:     if ($confname eq '') {
14190:         $home = &Apache::lonnet::domain($dom,'primary');
14191:     } else {
14192:         $home = &Apache::lonnet::homeserver($confname,$dom);
14193:         if ($home eq 'no_host') {
14194:             $home = &Apache::lonnet::domain($dom,'primary');
14195:         }
14196:     }
14197:     my @ids=&Apache::lonnet::current_machine_ids();
14198:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
14199:     if (!$allowed) {
14200: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
14201:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
14202:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
14203:     }
14204:     return $switchserver;
14205: }
14206: 
14207: sub modify_quotas {
14208:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14209:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
14210:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
14211:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
14212:         $validationfieldsref);
14213:     if ($action eq 'quotas') {
14214:         $context = 'tools';
14215:     } else {
14216:         $context = $action;
14217:     }
14218:     if ($context eq 'requestcourses') {
14219:         @usertools = ('official','unofficial','community','textbook','placement','lti');
14220:         @options =('norequest','approval','validate','autolimit');
14221:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
14222:         %titles = &courserequest_titles();
14223:         $toolregexp = join('|',@usertools);
14224:         %conditions = &courserequest_conditions();
14225:         $confname = $dom.'-domainconfig';
14226:         my $servadm = $r->dir_config('lonAdmEMail');
14227:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14228:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
14229:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
14230:     } elsif ($context eq 'requestauthor') {
14231:         @usertools = ('author');
14232:         %titles = &authorrequest_titles();
14233:     } else {
14234:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
14235:         %titles = &tool_titles();
14236:     }
14237:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14238:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14239:     foreach my $key (keys(%env)) {
14240:         if ($context eq 'requestcourses') {
14241:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
14242:                 my $item = $1;
14243:                 my $type = $2;
14244:                 if ($type =~ /^limit_(.+)/) {
14245:                     $limithash{$item}{$1} = $env{$key};
14246:                 } else {
14247:                     $confhash{$item}{$type} = $env{$key};
14248:                 }
14249:             }
14250:         } elsif ($context eq 'requestauthor') {
14251:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
14252:                 $confhash{$1} = $env{$key};
14253:             }
14254:         } else {
14255:             if ($key =~ /^form\.quota_(.+)$/) {
14256:                 $confhash{'defaultquota'}{$1} = $env{$key};
14257:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
14258:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
14259:             }
14260:         }
14261:     }
14262:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14263:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
14264:         @approvalnotify = sort(@approvalnotify);
14265:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
14266:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
14267:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
14268:         foreach my $type (@hasuniquecode) {
14269:             if (grep(/^\Q$type\E$/,@crstypes)) {
14270:                 $confhash{'uniquecode'}{$type} = 1;
14271:             }
14272:         }
14273:         my (%newbook,%allpos);
14274:         if ($context eq 'requestcourses') {
14275:             foreach my $type ('textbooks','templates') {
14276:                 @{$allpos{$type}} = (); 
14277:                 my $invalid;
14278:                 if ($type eq 'textbooks') {
14279:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
14280:                 } else {
14281:                     $invalid = &mt('Invalid LON-CAPA course for template');
14282:                 }
14283:                 if ($env{'form.'.$type.'_addbook'}) {
14284:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
14285:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
14286:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
14287:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
14288:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14289:                         } else {
14290:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
14291:                             my $position = $env{'form.'.$type.'_addbook_pos'};
14292:                             $position =~ s/\D+//g;
14293:                             if ($position ne '') {
14294:                                 $allpos{$type}[$position] = $newbook{$type};
14295:                             }
14296:                         }
14297:                     } else {
14298:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14299:                     }
14300:                 }
14301:             } 
14302:         }
14303:         if (ref($domconfig{$action}) eq 'HASH') {
14304:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
14305:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
14306:                     $changes{'notify'}{'approval'} = 1;
14307:                 }
14308:             } else {
14309:                 if ($confhash{'notify'}{'approval'}) {
14310:                     $changes{'notify'}{'approval'} = 1;
14311:                 }
14312:             }
14313:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
14314:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
14315:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
14316:                         unless ($confhash{'uniquecode'}{$crstype}) {
14317:                             $changes{'uniquecode'} = 1;
14318:                         }
14319:                     }
14320:                     unless ($changes{'uniquecode'}) {
14321:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
14322:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
14323:                                 $changes{'uniquecode'} = 1;
14324:                             }
14325:                         }
14326:                     }
14327:                } else {
14328:                    $changes{'uniquecode'} = 1;
14329:                }
14330:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
14331:                 $changes{'uniquecode'} = 1;
14332:             }
14333:             if ($context eq 'requestcourses') {
14334:                 foreach my $type ('textbooks','templates') {
14335:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
14336:                         my %deletions;
14337:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
14338:                         if (@todelete) {
14339:                             map { $deletions{$_} = 1; } @todelete;
14340:                         }
14341:                         my %imgdeletions;
14342:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
14343:                         if (@todeleteimages) {
14344:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
14345:                         }
14346:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
14347:                         for (my $i=0; $i<=$maxnum; $i++) {
14348:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
14349:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
14350:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
14351:                                 if ($deletions{$key}) {
14352:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
14353:                                         #FIXME need to obsolete item in RES space
14354:                                     }
14355:                                     next;
14356:                                 } else {
14357:                                     my $newpos = $env{'form.'.$itemid};
14358:                                     $newpos =~ s/\D+//g;
14359:                                     foreach my $item ('subject','title','publisher','author') {
14360:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14361:                                                  ($type eq 'templates'));
14362:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
14363:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
14364:                                             $changes{$type}{$key} = 1;
14365:                                         }
14366:                                     }
14367:                                     $allpos{$type}[$newpos] = $key;
14368:                                 }
14369:                                 if ($imgdeletions{$key}) {
14370:                                     $changes{$type}{$key} = 1;
14371:                                     #FIXME need to obsolete item in RES space
14372:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
14373:                                     my ($cdom,$cnum) = split(/_/,$key);
14374:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14375:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14376:                                     } else {
14377:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
14378:                                                                                       $cdom,$cnum,$type,$configuserok,
14379:                                                                                       $switchserver,$author_ok);
14380:                                         if ($imgurl) {
14381:                                             $confhash{$type}{$key}{'image'} = $imgurl;
14382:                                             $changes{$type}{$key} = 1; 
14383:                                         }
14384:                                         if ($error) {
14385:                                             &Apache::lonnet::logthis($error);
14386:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14387:                                         }
14388:                                     }
14389:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
14390:                                     $confhash{$type}{$key}{'image'} = 
14391:                                         $domconfig{$action}{$type}{$key}{'image'};
14392:                                 }
14393:                             }
14394:                         }
14395:                     }
14396:                 }
14397:             }
14398:         } else {
14399:             if ($confhash{'notify'}{'approval'}) {
14400:                 $changes{'notify'}{'approval'} = 1;
14401:             }
14402:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
14403:                 $changes{'uniquecode'} = 1;
14404:             }
14405:         }
14406:         if ($context eq 'requestcourses') {
14407:             foreach my $type ('textbooks','templates') {
14408:                 if ($newbook{$type}) {
14409:                     $changes{$type}{$newbook{$type}} = 1;
14410:                     foreach my $item ('subject','title','publisher','author') {
14411:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14412:                                  ($type eq 'template'));
14413:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
14414:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
14415:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
14416:                         }
14417:                     }
14418:                     if ($type eq 'textbooks') {
14419:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
14420:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
14421:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14422:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14423:                             } else {
14424:                                 my ($imageurl,$error) =
14425:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
14426:                                                             $configuserok,$switchserver,$author_ok);
14427:                                 if ($imageurl) {
14428:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
14429:                                 }
14430:                                 if ($error) {
14431:                                     &Apache::lonnet::logthis($error);
14432:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14433:                                 }
14434:                             }
14435:                         }
14436:                     }
14437:                 }
14438:                 if (@{$allpos{$type}} > 0) {
14439:                     my $idx = 0;
14440:                     foreach my $item (@{$allpos{$type}}) {
14441:                         if ($item ne '') {
14442:                             $confhash{$type}{$item}{'order'} = $idx;
14443:                             if (ref($domconfig{$action}) eq 'HASH') {
14444:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
14445:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
14446:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
14447:                                             $changes{$type}{$item} = 1;
14448:                                         }
14449:                                     }
14450:                                 }
14451:                             }
14452:                             $idx ++;
14453:                         }
14454:                     }
14455:                 }
14456:             }
14457:             if (ref($validationitemsref) eq 'ARRAY') {
14458:                 foreach my $item (@{$validationitemsref}) {
14459:                     if ($item eq 'fields') {
14460:                         my @changed;
14461:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
14462:                         if (@{$confhash{'validation'}{$item}} > 0) {
14463:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
14464:                         }
14465:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14466:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14467:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
14468:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
14469:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
14470:                                 } else {
14471:                                     @changed = @{$confhash{'validation'}{$item}};
14472:                                 }
14473:                             } else {
14474:                                 @changed = @{$confhash{'validation'}{$item}};
14475:                             }
14476:                         } else {
14477:                             @changed = @{$confhash{'validation'}{$item}};
14478:                         }
14479:                         if (@changed) {
14480:                             if ($confhash{'validation'}{$item}) {
14481:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
14482:                             } else {
14483:                                 $changes{'validation'}{$item} = &mt('None');
14484:                             }
14485:                         }
14486:                     } else {
14487:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
14488:                         if ($item eq 'markup') {
14489:                             if ($env{'form.requestcourses_validation_'.$item}) {
14490:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14491:                             }
14492:                         }
14493:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14494:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14495:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
14496:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14497:                                 }
14498:                             } else {
14499:                                 if ($confhash{'validation'}{$item} ne '') {
14500:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14501:                                 }
14502:                             }
14503:                         } else {
14504:                             if ($confhash{'validation'}{$item} ne '') {
14505:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14506:                             }
14507:                         }
14508:                     }
14509:                 }
14510:             }
14511:             if ($env{'form.validationdc'}) {
14512:                 my $newval = $env{'form.validationdc'};
14513:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14514:                 if (exists($domcoords{$newval})) {
14515:                     $confhash{'validation'}{'dc'} = $newval;
14516:                 }
14517:             }
14518:             if (ref($confhash{'validation'}) eq 'HASH') {
14519:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14520:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14521:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14522:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
14523:                                 if ($confhash{'validation'}{'dc'} eq '') {
14524:                                     $changes{'validation'}{'dc'} = &mt('None');
14525:                                 } else {
14526:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14527:                                 }
14528:                             }
14529:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
14530:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14531:                         }
14532:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
14533:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14534:                     }
14535:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
14536:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14537:                 }
14538:             } else {
14539:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14540:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14541:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14542:                             $changes{'validation'}{'dc'} = &mt('None');
14543:                         }
14544:                     }
14545:                 }
14546:             }
14547:         }
14548:     } else {
14549:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
14550:     }
14551:     foreach my $item (@usertools) {
14552:         foreach my $type (@{$types},'default','_LC_adv') {
14553:             my $unset; 
14554:             if ($context eq 'requestcourses') {
14555:                 $unset = '0';
14556:                 if ($type eq '_LC_adv') {
14557:                     $unset = '';
14558:                 }
14559:                 if ($confhash{$item}{$type} eq 'autolimit') {
14560:                     $confhash{$item}{$type} .= '=';
14561:                     unless ($limithash{$item}{$type} =~ /\D/) {
14562:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
14563:                     }
14564:                 }
14565:             } elsif ($context eq 'requestauthor') {
14566:                 $unset = '0';
14567:                 if ($type eq '_LC_adv') {
14568:                     $unset = '';
14569:                 }
14570:             } else {
14571:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
14572:                     $confhash{$item}{$type} = 1;
14573:                 } else {
14574:                     $confhash{$item}{$type} = 0;
14575:                 }
14576:             }
14577:             if (ref($domconfig{$action}) eq 'HASH') {
14578:                 if ($action eq 'requestauthor') {
14579:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
14580:                         $changes{$type} = 1;
14581:                     }
14582:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
14583:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
14584:                         $changes{$item}{$type} = 1;
14585:                     }
14586:                 } else {
14587:                     if ($context eq 'requestcourses') {
14588:                         if ($confhash{$item}{$type} ne $unset) {
14589:                             $changes{$item}{$type} = 1;
14590:                         }
14591:                     } else {
14592:                         if (!$confhash{$item}{$type}) {
14593:                             $changes{$item}{$type} = 1;
14594:                         }
14595:                     }
14596:                 }
14597:             } else {
14598:                 if ($context eq 'requestcourses') {
14599:                     if ($confhash{$item}{$type} ne $unset) {
14600:                         $changes{$item}{$type} = 1;
14601:                     }
14602:                 } elsif ($context eq 'requestauthor') {
14603:                     if ($confhash{$type} ne $unset) {
14604:                         $changes{$type} = 1;
14605:                     }
14606:                 } else {
14607:                     if (!$confhash{$item}{$type}) {
14608:                         $changes{$item}{$type} = 1;
14609:                     }
14610:                 }
14611:             }
14612:         }
14613:     }
14614:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14615:         if (ref($domconfig{'quotas'}) eq 'HASH') {
14616:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14617:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
14618:                     if (exists($confhash{'defaultquota'}{$key})) {
14619:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
14620:                             $changes{'defaultquota'}{$key} = 1;
14621:                         }
14622:                     } else {
14623:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
14624:                     }
14625:                 }
14626:             } else {
14627:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
14628:                     if (exists($confhash{'defaultquota'}{$key})) {
14629:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
14630:                             $changes{'defaultquota'}{$key} = 1;
14631:                         }
14632:                     } else {
14633:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
14634:                     }
14635:                 }
14636:             }
14637:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
14638:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
14639:             }
14640:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
14641:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
14642:             }
14643:         }
14644:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
14645:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
14646:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
14647:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14648:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
14649:                             $changes{'defaultquota'}{$key} = 1;
14650:                         }
14651:                     } else {
14652:                         if (!exists($domconfig{'quotas'}{$key})) {
14653:                             $changes{'defaultquota'}{$key} = 1;
14654:                         }
14655:                     }
14656:                 } else {
14657:                     $changes{'defaultquota'}{$key} = 1;
14658:                 }
14659:             }
14660:         }
14661:     }
14662: 
14663:     if ($context eq 'requestauthor') {
14664:         $domdefaults{'requestauthor'} = \%confhash;
14665:     } else {
14666:         foreach my $key (keys(%confhash)) {
14667:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
14668:                 $domdefaults{$key} = $confhash{$key};
14669:             }
14670:         }
14671:     }
14672: 
14673:     my %quotahash = (
14674:                       $action => { %confhash }
14675:                     );
14676:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
14677:                                              $dom);
14678:     if ($putresult eq 'ok') {
14679:         if (keys(%changes) > 0) {
14680:             my $cachetime = 24*60*60;
14681:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14682:             if (ref($lastactref) eq 'HASH') {
14683:                 $lastactref->{'domdefaults'} = 1;
14684:             }
14685:             $resulttext = &mt('Changes made:').'<ul>';
14686:             unless (($context eq 'requestcourses') ||
14687:                     ($context eq 'requestauthor')) {
14688:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
14689:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
14690:                     foreach my $type (@{$types},'default') {
14691:                         if (defined($changes{'defaultquota'}{$type})) {
14692:                             my $typetitle = $usertypes->{$type};
14693:                             if ($type eq 'default') {
14694:                                 $typetitle = $othertitle;
14695:                             }
14696:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
14697:                         }
14698:                     }
14699:                     $resulttext .= '</ul></li>';
14700:                 }
14701:             }
14702:             my %newenv;
14703:             foreach my $item (@usertools) {
14704:                 my (%haschgs,%inconf);
14705:                 if ($context eq 'requestauthor') {
14706:                     %haschgs = %changes;
14707:                     %inconf = %confhash;
14708:                 } else {
14709:                     if (ref($changes{$item}) eq 'HASH') {
14710:                         %haschgs = %{$changes{$item}};
14711:                     }
14712:                     if (ref($confhash{$item}) eq 'HASH') {
14713:                         %inconf = %{$confhash{$item}};
14714:                     }
14715:                 }
14716:                 if (keys(%haschgs) > 0) {
14717:                     my $newacc = 
14718:                         &Apache::lonnet::usertools_access($env{'user.name'},
14719:                                                           $env{'user.domain'},
14720:                                                           $item,'reload',$context);
14721:                     if (($context eq 'requestcourses') ||
14722:                         ($context eq 'requestauthor')) {
14723:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14724:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14725:                         }
14726:                     } else {
14727:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14728:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14729:                         }
14730:                     }
14731:                     unless ($context eq 'requestauthor') {
14732:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14733:                     }
14734:                     foreach my $type (@{$types},'default','_LC_adv') {
14735:                         if ($haschgs{$type}) {
14736:                             my $typetitle = $usertypes->{$type};
14737:                             if ($type eq 'default') {
14738:                                 $typetitle = $othertitle;
14739:                             } elsif ($type eq '_LC_adv') {
14740:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14741:                             }
14742:                             if ($inconf{$type}) {
14743:                                 if ($context eq 'requestcourses') {
14744:                                     my $cond;
14745:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14746:                                         if ($1 eq '') {
14747:                                             $cond = &mt('(Automatic processing of any request).');
14748:                                         } else {
14749:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14750:                                         }
14751:                                     } else { 
14752:                                         $cond = $conditions{$inconf{$type}};
14753:                                     }
14754:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14755:                                 } elsif ($context eq 'requestauthor') {
14756:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14757:                                                              $titles{$inconf{$type}},$typetitle);
14758: 
14759:                                 } else {
14760:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14761:                                 }
14762:                             } else {
14763:                                 if ($type eq '_LC_adv') {
14764:                                     if ($inconf{$type} eq '0') {
14765:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14766:                                     } else { 
14767:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14768:                                     }
14769:                                 } else {
14770:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14771:                                 }
14772:                             }
14773:                         }
14774:                     }
14775:                     unless ($context eq 'requestauthor') {
14776:                         $resulttext .= '</ul></li>';
14777:                     }
14778:                 }
14779:             }
14780:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14781:                 if (ref($changes{'notify'}) eq 'HASH') {
14782:                     if ($changes{'notify'}{'approval'}) {
14783:                         if (ref($confhash{'notify'}) eq 'HASH') {
14784:                             if ($confhash{'notify'}{'approval'}) {
14785:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14786:                             } else {
14787:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14788:                             }
14789:                         }
14790:                     }
14791:                 }
14792:             }
14793:             if ($action eq 'requestcourses') {
14794:                 my @offon = ('off','on');
14795:                 if ($changes{'uniquecode'}) {
14796:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14797:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14798:                         $resulttext .= '<li>'.
14799:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14800:                                        '</li>';
14801:                     } else {
14802:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14803:                                        '</li>';
14804:                     }
14805:                 }
14806:                 foreach my $type ('textbooks','templates') {
14807:                     if (ref($changes{$type}) eq 'HASH') {
14808:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14809:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14810:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14811:                             my $coursetitle = $coursehash{'description'};
14812:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14813:                             $resulttext .= '<li>';
14814:                             foreach my $item ('subject','title','publisher','author') {
14815:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14816:                                          ($type eq 'templates'));
14817:                                 my $name = $item.':';
14818:                                 $name =~ s/^(\w)/\U$1/;
14819:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14820:                             }
14821:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14822:                             if ($type eq 'textbooks') {
14823:                                 if ($confhash{$type}{$key}{'image'}) {
14824:                                     $resulttext .= ' '.&mt('Image: [_1]',
14825:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14826:                                                    ' alt="Textbook cover" />').'<br />';
14827:                                 }
14828:                             }
14829:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14830:                         }
14831:                         $resulttext .= '</ul></li>';
14832:                     }
14833:                 }
14834:                 if (ref($changes{'validation'}) eq 'HASH') {
14835:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14836:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14837:                         foreach my $item (@{$validationitemsref}) {
14838:                             if (exists($changes{'validation'}{$item})) {
14839:                                 if ($item eq 'markup') {
14840:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14841:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14842:                                 } else {
14843:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14844:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14845:                                 }
14846:                             }
14847:                         }
14848:                         if (exists($changes{'validation'}{'dc'})) {
14849:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14850:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14851:                         }
14852:                     }
14853:                 }
14854:             }
14855:             $resulttext .= '</ul>';
14856:             if (keys(%newenv)) {
14857:                 &Apache::lonnet::appenv(\%newenv);
14858:             }
14859:         } else {
14860:             if ($context eq 'requestcourses') {
14861:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14862:             } elsif ($context eq 'requestauthor') {
14863:                 $resulttext = &mt('No changes made to rights to request author space.');
14864:             } else {
14865:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14866:             }
14867:         }
14868:     } else {
14869:         $resulttext = '<span class="LC_error">'.
14870: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14871:     }
14872:     if ($errors) {
14873:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14874:                        '<ul>'.$errors.'</ul></p>';
14875:     }
14876:     return $resulttext;
14877: }
14878: 
14879: sub process_textbook_image {
14880:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14881:     my $filename = $env{'form.'.$caller.'.filename'};
14882:     my ($error,$url);
14883:     my ($width,$height) = (50,50);
14884:     if ($configuserok eq 'ok') {
14885:         if ($switchserver) {
14886:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14887:                          $switchserver);
14888:         } elsif ($author_ok eq 'ok') {
14889:             my $modified = [];
14890:             my ($result,$imageurl) =
14891:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14892:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14893:                                                         '',$modified);
14894:             if ($result eq 'ok') {
14895:                 $url = $imageurl;
14896:                 &update_modify_urls($r,$modified);
14897:             } else {
14898:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14899:             }
14900:         } else {
14901:             $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);
14902:         }
14903:     } else {
14904:         $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);
14905:     }
14906:     return ($url,$error);
14907: }
14908: 
14909: sub modify_ltitools {
14910:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14911:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14912:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14913: 
14914:     my $confname = $dom.'-domainconfig';
14915:     my $servadm = $r->dir_config('lonAdmEMail');
14916:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14917: 
14918:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14919:     my $toolserror =
14920:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14921:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14922: 
14923:     my $home = &Apache::lonnet::domain($dom,'primary');
14924:     unless (($home eq 'no_host') || ($home eq '')) {
14925:         my @ids=&Apache::lonnet::current_machine_ids();
14926:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14927:     }
14928: 
14929:     if (keys(%ltitoolschg)) {
14930:         foreach my $id (keys(%ltitoolschg)) {
14931:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14932:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14933:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14934:                         if ($is_home) {
14935:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14936:                         }
14937:                     }
14938:                 }
14939:             }
14940:         }
14941:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14942:         if (keys(%ltitoolschg)) {
14943:             %newltitools = %ltitoolschg;
14944:         }
14945:     }
14946:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14947:         foreach my $id (%{$domconfig{'ltitools'}}) {
14948:             next if ($id !~ /^\d+$/);
14949:             unless (exists($ltitoolschg{$id})) {
14950:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14951:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14952:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14953:                             if ($is_home) {
14954:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14955:                             }
14956:                         } else {
14957:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14958:                         }
14959:                     }
14960:                 } else {
14961:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14962:                 }
14963:             }
14964:         }
14965:     }
14966:     if ($toolserror) {
14967:         $errors = '<li>'.$toolserror.'</li>';
14968:     }
14969:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14970:         $resulttext = &mt('No changes made.');
14971:         if ($errors) {
14972:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14973:                                  $errors.'</ul>';
14974:         }
14975:         return $resulttext;
14976:     }
14977:     my %ltitoolshash = (
14978:                           $action => { %newltitools }
14979:                        );
14980:     if (keys(%secchanges)) {
14981:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14982:     }
14983:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14984:     if ($putresult eq 'ok') {
14985:         my %keystore;
14986:         if ($is_home) {
14987:             my %toolsenchash = (
14988:                                    $action => { %newtoolsenc }
14989:                                );
14990:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14991:             my $cachetime = 24*60*60;
14992:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
14993:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14994:         }
14995:         $resulttext = &mt('Changes made:').'<ul>';
14996:         if (keys(%secchanges) > 0) {
14997:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14998:         }
14999:         if (keys(%ltitoolschg) > 0) {
15000:             $resulttext .= $ltitoolsoutput;
15001:         }
15002:         my $cachetime = 24*60*60;
15003:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
15004:         if (ref($lastactref) eq 'HASH') {
15005:             $lastactref->{'ltitools'} = 1;
15006:         }
15007:     } else {
15008:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15009:     }
15010:     if ($errors) {
15011:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
15012:                        $errors.'</ul></p>';
15013:     }
15014:     return $resulttext;
15015: }
15016: 
15017: sub fetch_secrets {
15018:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
15019:     my %keyset;
15020:     %{$currsec} = ();
15021:     $newsec->{'private'}{'keys'} = [];
15022:     $newsec->{'encrypt'} = {};
15023:     $newsec->{'rules'} = {};
15024:     if ($context eq 'ltisec') {
15025:         $newsec->{'linkprot'} = {};
15026:     }
15027:     if (ref($domconfig->{$context}) eq 'HASH') {
15028:         %{$currsec} = %{$domconfig->{$context}};
15029:         if ($context eq 'ltisec') {
15030:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
15031:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
15032:                     unless ($id =~ /^\d+$/) {
15033:                         delete($currsec->{'linkprot'}{$id});
15034:                     }
15035:                 }
15036:             }
15037:         }
15038:         if (ref($currsec->{'private'}) eq 'HASH') {
15039:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
15040:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
15041:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
15042:             }
15043:         }
15044:     }
15045:     my @items= ('crs','dom');
15046:     if ($context eq 'ltisec') {
15047:         push(@items,'consumers');
15048:     }
15049:     foreach my $item (@items) {
15050:         my $formelement;
15051:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
15052:             $formelement = 'form.'.$context.'_'.$item;
15053:         } else {
15054:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
15055:         }
15056:         if ($env{$formelement}) {
15057:             $newsec->{'encrypt'}{$item} = 1;
15058:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
15059:                 unless ($currsec->{'encrypt'}{$item}) {
15060:                     $secchanges->{'encrypt'} = 1;
15061:                 }
15062:             } else {
15063:                 $secchanges->{'encrypt'} = 1;
15064:             }
15065:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
15066:             if ($currsec->{'encrypt'}{$item}) {
15067:                 $secchanges->{'encrypt'} = 1;
15068:             }
15069:         }
15070:     }
15071:     my $secrets;
15072:     if ($context eq 'ltisec') {
15073:         $secrets = 'ltisecrets';
15074:     } else {
15075:         $secrets = 'toolsecrets';
15076:     }
15077:     unless (exists($currsec->{'rules'})) {
15078:         $currsec->{'rules'} = {};
15079:     }
15080:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
15081: 
15082:     my @ids=&Apache::lonnet::current_machine_ids();
15083:     my %servers = &Apache::lonnet::get_servers($dom,'library');
15084: 
15085:     foreach my $hostid (keys(%servers)) {
15086:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
15087:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
15088:             if (exists($env{$keyitem})) {
15089:                 $env{$keyitem} =~ s/(`)/'/g;
15090:                 if ($keyset{$hostid}) {
15091:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
15092:                         if ($env{$keyitem} ne '') {
15093:                             $secchanges->{'private'} = 1;
15094:                             $newkeyset->{$hostid} = $env{$keyitem};
15095:                         }
15096:                     }
15097:                 } elsif ($env{$keyitem} ne '') {
15098:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
15099:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
15100:                     }
15101:                     $secchanges->{'private'} = 1;
15102:                     $newkeyset->{$hostid} = $env{$keyitem};
15103:                 }
15104:             }
15105:         }
15106:     }
15107: }
15108: 
15109: sub store_security {
15110:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
15111:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
15112:                    (ref($keystore) eq 'HASH'));
15113:     if (keys(%{$secchanges})) {
15114:         if ($secchanges->{'private'}) {
15115:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
15116:             foreach my $hostid (keys(%{$newkeyset})) {
15117:                 my $storehash = {
15118:                                    key => $newkeyset->{$hostid},
15119:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
15120:                                 };
15121:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
15122:                                                                   $dom,$hostid);
15123:             }
15124:         }
15125:     }
15126: }
15127: 
15128: sub lti_security_results {
15129:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
15130:     my $output;
15131:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15132:     my $needs_update;
15133:     foreach my $item (keys(%{$secchanges})) {
15134:         if ($item eq 'encrypt') {
15135:             $needs_update = 1;
15136:             my %encrypted;
15137:             if ($context eq 'lti') {
15138:                 %encrypted = (
15139:                               crs  => {
15140:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
15141:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
15142:                                       },
15143:                               dom => {
15144:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
15145:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
15146:                                      },
15147:                               consumers => {
15148:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
15149:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
15150:                                            },
15151:                              );
15152:             } else {
15153:                 %encrypted = (
15154:                               crs  => {
15155:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
15156:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
15157:                                       },
15158:                               dom => {
15159:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
15160:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
15161:                                      },
15162:                              );
15163:             }
15164:             my @types= ('crs','dom');
15165:             if ($context eq 'lti') {
15166:                 foreach my $type (@types) {
15167:                     undef($domdefaults{'linkprotenc_'.$type});
15168:                 }
15169:                 push(@types,'consumers');
15170:                 undef($domdefaults{'ltienc_consumers'});
15171:             } elsif ($context eq 'ltitools') {
15172:                 foreach my $type (@types) {
15173:                     undef($domdefaults{'toolenc_'.$type});
15174:                 }
15175:             }
15176:             foreach my $type (@types) {
15177:                 my $shown = $encrypted{$type}{'off'};
15178:                 if (ref($newsec->{$item}) eq 'HASH') {
15179:                     if ($newsec->{$item}{$type}) {
15180:                         if ($context eq 'lti') {
15181:                             if ($type eq 'consumers') {
15182:                                 $domdefaults{'ltienc_consumers'} = 1;
15183:                             } else {
15184:                                 $domdefaults{'linkprotenc_'.$type} = 1;
15185:                             }
15186:                         } elsif ($context eq 'ltitools') {
15187:                             $domdefaults{'toolenc_'.$type} = 1;
15188:                         }
15189:                         $shown = $encrypted{$type}{'on'};
15190:                     }
15191:                 }
15192:                 $output .= '<li>'.$shown.'</li>';
15193:             }
15194:         } elsif ($item eq 'rules') {
15195:             my %titles = &Apache::lonlocal::texthash(
15196:                                       min   => 'Minimum password length',
15197:                                       max   => 'Maximum password length',
15198:                                       chars => 'Required characters',
15199:                          );
15200:             foreach my $rule ('min','max') {
15201:                 if ($newsec->{rules}{$rule} eq '') {
15202:                     if ($rule eq 'min') {
15203:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15204:                                    ' '.&mt('Default of [_1] will be used',
15205:                                            $Apache::lonnet::passwdmin).'</li>';
15206:                     } else {
15207:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15208:                     }
15209:                 } else {
15210:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
15211:                 }
15212:             }
15213:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
15214:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
15215:                     my %rulenames = &Apache::lonlocal::texthash(
15216:                                              uc => 'At least one upper case letter',
15217:                                              lc => 'At least one lower case letter',
15218:                                              num => 'At least one number',
15219:                                              spec => 'At least one non-alphanumeric',
15220:                                     );
15221:                     my $needed = '<ul><li>'.
15222:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
15223:                                  '</li></ul>';
15224:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15225:                 } else {
15226:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15227:                 }
15228:             } else {
15229:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15230:             }
15231:         } elsif ($item eq 'private') {
15232:             $needs_update = 1;
15233:             if ($context eq 'lti') {
15234:                 undef($domdefaults{'ltiprivhosts'});
15235:             } elsif ($context eq 'ltitools') {
15236:                 undef($domdefaults{'toolprivhosts'});
15237:             }
15238:             if (keys(%{$newkeyset})) {
15239:                 my @privhosts;
15240:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
15241:                     if ($keystore->{$hostid} eq 'ok') {
15242:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
15243:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
15244:                             push(@privhosts,$hostid);
15245:                         }
15246:                     }
15247:                 }
15248:                 if (@privhosts) {
15249:                     if ($context eq 'lti') {
15250:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
15251:                     } elsif ($context eq 'ltitools') {
15252:                         $domdefaults{'toolprivhosts'} = \@privhosts;
15253:                     }
15254:                 }
15255:             }
15256:         } elsif ($item eq 'linkprot') {
15257:             next;
15258:         } elsif ($item eq 'suggested') {
15259:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
15260:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
15261:                 my $suggestions;
15262:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
15263:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
15264:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
15265:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
15266:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
15267:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
15268:                                         '</li>';
15269:                     } else {
15270:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
15271:                                                    $newsec->{'suggested'}->{$id}).'</li>';
15272:                     }
15273:                 }
15274:                 if ($suggestions) {
15275:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
15276:                                '<ul>'.$suggestions.'</ul>'.
15277:                                '</li>';
15278:                 }
15279:             }
15280:         }
15281:     }
15282:     if ($needs_update) {
15283:         my $cachetime = 24*60*60;
15284:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15285:     }
15286:     return $output;
15287: }
15288: 
15289: sub modify_proctoring {
15290:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15291:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15292:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
15293:     my $confname = $dom.'-domainconfig';
15294:     my $servadm = $r->dir_config('lonAdmEMail');
15295:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
15296:     my %providernames = &proctoring_providernames();
15297:     my $maxnum = scalar(keys(%providernames));
15298: 
15299:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
15300:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
15301:     if (ref($requref) eq 'HASH') {
15302:         %requserfields = %{$requref};
15303:     }
15304:     if (ref($opturef) eq 'HASH') {
15305:         %optuserfields = %{$opturef};
15306:     }
15307:     if (ref($defref) eq 'HASH') {
15308:         %defaults = %{$defref};
15309:     }
15310:     if (ref($extref) eq 'HASH') {
15311:         %extended = %{$extref};
15312:     }
15313:     if (ref($crsref) eq 'HASH') {
15314:         %crsconf = %{$crsref};
15315:     }
15316:     if (ref($rolesref) eq 'ARRAY') {
15317:         @courseroles = @{$rolesref};
15318:     }
15319:     if (ref($ltiref) eq 'ARRAY') {
15320:         @ltiroles = @{$ltiref};
15321:     }
15322: 
15323:     if (ref($domconfig{$action}) eq 'HASH') {
15324:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
15325:         if (@todeleteimages) {
15326:             map { $imgdeletions{$_} = 1; } @todeleteimages;
15327:         }
15328:     }
15329:     my %customadds;
15330:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
15331:     if (@newcustom) {
15332:         map { $customadds{$_} = 1; } @newcustom;
15333:     }
15334:     foreach my $provider (sort(keys(%providernames))) {
15335:         $confhash{$provider} = {};
15336:         my $pos = $env{'form.proctoring_pos_'.$provider};
15337:         $pos =~ s/\D+//g;
15338:         $allpos[$pos] = $provider;
15339:         my (%current,%currentenc);
15340:         my $showroles = 0;
15341:         if (ref($domconfig{$action}) eq 'HASH') {
15342:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15343:                 %current = %{$domconfig{$action}{$provider}};
15344:                 foreach my $item ('key','secret') { 
15345:                     $currentenc{$item} = $current{$item};
15346:                     delete($current{$item});
15347:                 }
15348:             }
15349:         }
15350:         if ($env{'form.proctoring_available_'.$provider}) {
15351:             $confhash{$provider}{'available'} = 1;
15352:             unless ($current{'available'}) {
15353:                 $changes{$provider} = 1;
15354:             }
15355:         } else {
15356:             %{$confhash{$provider}} = %current;
15357:             %{$encconfhash{$provider}} = %currentenc;
15358:             $confhash{$provider}{'available'} = 0;
15359:             if ($current{'available'}) {
15360:                 $changes{$provider} = 1;
15361:             }
15362:         }
15363:         if ($confhash{$provider}{'available'}) {
15364:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
15365:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
15366:                 if ($field eq 'lifetime') {
15367:                     if ($possval =~ /^\d+$/) {
15368:                         $confhash{$provider}{$field} = $possval;
15369:                     }
15370:                 } elsif ($field eq 'version') {
15371:                     if ($possval =~ /^\d+\.\d+$/) {
15372:                         $confhash{$provider}{$field} = $possval;
15373:                     }
15374:                 } elsif ($field eq 'sigmethod') {
15375:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
15376:                         $confhash{$provider}{$field} = $possval;
15377:                     }
15378:                 } elsif ($field eq 'url') {
15379:                     $confhash{$provider}{$field} = $possval;
15380:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
15381:                     $encconfhash{$provider}{$field} = $possval;
15382:                     unless ($currentenc{$field} eq $possval) {
15383:                         $changes{$provider} = 1;
15384:                     }
15385:                 }
15386:                 unless (($field eq 'key') || ($field eq 'secret')) {
15387:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
15388:                         $changes{$provider} = 1;
15389:                     }
15390:                 }
15391:             }
15392:             if ($imgdeletions{$provider}) {
15393:                 $changes{$provider} = 1;
15394:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
15395:                 my ($imageurl,$error) =
15396:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
15397:                                               $configuserok,$switchserver,$author_ok);
15398:                 if ($imageurl) {
15399:                     $confhash{$provider}{'image'} = $imageurl;
15400:                     $changes{$provider} = 1; 
15401:                 }
15402:                 if ($error) {
15403:                     &Apache::lonnet::logthis($error);
15404:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15405:                 }
15406:             } elsif (exists($current{'image'})) {
15407:                 $confhash{$provider}{'image'} = $current{'image'};
15408:             }
15409:             if (ref($requserfields{$provider}) eq 'ARRAY') {
15410:                 if (@{$requserfields{$provider}} > 0) {
15411:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
15412:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
15413:                             $confhash{$provider}{'incdom'} = 1;
15414:                         }
15415:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
15416:                             $changes{$provider} = 1;
15417:                         }
15418:                     }
15419:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
15420:                         $showroles = 1;
15421:                     }
15422:                 }
15423:             }
15424:             $confhash{$provider}{'fields'} = [];
15425:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
15426:                 if (@{$optuserfields{$provider}} > 0) {
15427:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
15428:                     foreach my $field (@{$optuserfields{$provider}}) {
15429:                         if (grep(/^\Q$field\E$/,@optfields)) {
15430:                             push(@{$confhash{$provider}{'fields'}},$field);
15431:                         }
15432:                     }
15433:                 }
15434:                 if (ref($current{'fields'}) eq 'ARRAY') {
15435:                     unless ($changes{$provider}) {
15436:                         my @new = sort(@{$confhash{$provider}{'fields'}});
15437:                         my @old = sort(@{$current{'fields'}}); 
15438:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15439:                         if (@diffs) {
15440:                             $changes{$provider} = 1;
15441:                         }
15442:                     }
15443:                 } elsif (@{$confhash{$provider}{'fields'}}) {
15444:                     $changes{$provider} = 1;
15445:                 }
15446:             }
15447:             if (ref($defaults{$provider}) eq 'ARRAY') {  
15448:                 if (@{$defaults{$provider}} > 0) {
15449:                     my %options;
15450:                     if (ref($extended{$provider}) eq 'HASH') {
15451:                         %options = %{$extended{$provider}};
15452:                     }
15453:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
15454:                     foreach my $field (@{$defaults{$provider}}) {
15455:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
15456:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
15457:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
15458:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
15459:                             }
15460:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
15461:                             foreach my $inner (keys(%{$options{$field}})) {
15462:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
15463:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
15464:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
15465:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
15466:                                     }
15467:                                 } else {
15468:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
15469:                                 }
15470:                             }
15471:                         } else {
15472:                             if (grep(/^\Q$field\E$/,@checked)) {
15473:                                 push(@{$confhash{$provider}{'defaults'}},$field);
15474:                             }
15475:                         }
15476:                     }
15477:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15478:                         if (ref($current{'defaults'}) eq 'ARRAY') {
15479:                             unless ($changes{$provider}) {
15480:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
15481:                                 my @old = sort(@{$current{'defaults'}});
15482:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15483:                                 if (@diffs) {
15484:                                     $changes{$provider} = 1; 
15485:                                 }
15486:                             }
15487:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
15488:                             if (@{$current{'defaults'}}) {
15489:                                 $changes{$provider} = 1;
15490:                             }
15491:                         }
15492:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15493:                         if (ref($current{'defaults'}) eq 'HASH') {
15494:                             unless ($changes{$provider}) {
15495:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15496:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15497:                                         $changes{$provider} = 1;
15498:                                         last;
15499:                                     }
15500:                                 }
15501:                             }
15502:                             unless ($changes{$provider}) {
15503:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15504:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15505:                                         $changes{$provider} = 1;
15506:                                         last;
15507:                                     }
15508:                                 }
15509:                             }
15510:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15511:                             $changes{$provider} = 1;
15512:                         }
15513:                     }
15514:                 }
15515:             }
15516:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15517:                 if (@{$crsconf{$provider}} > 0) {
15518:                     $confhash{$provider}{'crsconf'} = [];
15519:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15520:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15521:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15522:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15523:                         }
15524:                     }
15525:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15526:                         unless ($changes{$provider}) {  
15527:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15528:                             my @old = sort(@{$current{'crsconf'}});
15529:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15530:                             if (@diffs) {
15531:                                 $changes{$provider} = 1;
15532:                             }
15533:                         }
15534:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15535:                         $changes{$provider} = 1;
15536:                     }
15537:                 }
15538:             }
15539:             if ($showroles) {
15540:                 $confhash{$provider}{'roles'} = {};
15541:                 foreach my $role (@courseroles) {
15542:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15543:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15544:                         $confhash{$provider}{'roles'}{$role} = $poss;
15545:                     }
15546:                 }
15547:                 unless ($changes{$provider}) {
15548:                     if (ref($current{'roles'}) eq 'HASH') {
15549:                         foreach my $role (keys(%{$current{'roles'}})) {
15550:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15551:                                 $changes{$provider} = 1;
15552:                                 last
15553:                             }
15554:                         }
15555:                         unless ($changes{$provider}) {
15556:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15557:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15558:                                     $changes{$provider} = 1;
15559:                                     last;
15560:                                 }
15561:                             }
15562:                         }
15563:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15564:                         $changes{$provider} = 1;
15565:                     }
15566:                 }
15567:             }
15568:             if (ref($current{'custom'}) eq 'HASH') {
15569:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15570:                 foreach my $key (keys(%{$current{'custom'}})) {
15571:                     if (grep(/^\Q$key\E$/,@customdels)) {
15572:                         $changes{$provider} = 1;
15573:                     } else {
15574:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15575:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15576:                             $changes{$provider} = 1;
15577:                         }
15578:                     }
15579:                 }
15580:             }
15581:             if ($customadds{$provider}) {
15582:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15583:                 $name =~ s/(`)/'/g;
15584:                 $name =~ s/^\s+//;
15585:                 $name =~ s/\s+$//;
15586:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15587:                 $value =~ s/(`)/'/g;
15588:                 $value =~ s/^\s+//;
15589:                 $value =~ s/\s+$//;
15590:                 if ($name ne '') {
15591:                     $confhash{$provider}{'custom'}{$name} = $value;
15592:                     $changes{$provider} = 1;
15593:                 }
15594:             }
15595:         }
15596:     }
15597:     if (@allpos > 0) {
15598:         my $idx = 0;
15599:         foreach my $provider (@allpos) {
15600:             if ($provider ne '') {
15601:                 $confhash{$provider}{'order'} = $idx;
15602:                 unless ($changes{$provider}) {
15603:                     if (ref($domconfig{$action}) eq 'HASH') {
15604:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15605:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15606:                                 $changes{$provider} = 1;
15607:                             }
15608:                         }
15609:                     }
15610:                 }
15611:                 $idx ++;
15612:             }
15613:         }
15614:     }
15615:     my %proc_hash = (
15616:                           $action => { %confhash }
15617:                        );
15618:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15619:                                              $dom);
15620:     if ($putresult eq 'ok') {
15621:         my %proc_enchash = (
15622:                              $action => { %encconfhash }
15623:                          );
15624:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15625:         if (keys(%changes) > 0) {
15626:             my $cachetime = 24*60*60;
15627:             my %procall = %confhash;
15628:             foreach my $provider (keys(%procall)) {
15629:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15630:                     foreach my $key ('key','secret') {
15631:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15632:                     }
15633:                 }
15634:             }
15635:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15636:             if (ref($lastactref) eq 'HASH') {
15637:                 $lastactref->{'proctoring'} = 1;
15638:             }
15639:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15640:             my %bynum;
15641:             foreach my $provider (sort(keys(%changes))) {
15642:                 my $position = $confhash{$provider}{'order'};
15643:                 $bynum{$position} = $provider;
15644:             }
15645:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15646:                 my $provider = $bynum{$pos};
15647:                 my %lt = &proctoring_titles($provider);
15648:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15649:                 if (!$confhash{$provider}{'available'}) {
15650:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15651:                 } else {
15652:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15653:                     if ($confhash{$provider}{'image'}) {
15654:                         $resulttext .= '&nbsp;'.
15655:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15656:                                        ' alt="'.&mt('Proctoring icon').'" />';
15657:                     }
15658:                     $resulttext .= '<ul>';
15659:                     my $position = $pos + 1;
15660:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15661:                     foreach my $key ('version','sigmethod','url','lifetime') {
15662:                         if ($confhash{$provider}{$key} ne '') {
15663:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15664:                         }
15665:                     }
15666:                     if ($encconfhash{$provider}{'key'} ne '') {
15667:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15668:                     }
15669:                     if ($encconfhash{$provider}{'secret'} ne '') {
15670:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15671:                         my $num = length($encconfhash{$provider}{'secret'});
15672:                         $resulttext .= ('*'x$num).'</li>';
15673:                     }
15674:                     my (@fields,$showroles);
15675:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15676:                         push(@fields,@{$requserfields{$provider}});
15677:                     }
15678:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15679:                         push(@fields,@{$confhash{$provider}{'fields'}});
15680:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15681:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15682:                     }
15683:                     if (@fields) {
15684:                         if (grep(/^roles$/,@fields)) {
15685:                             $showroles = 1;
15686:                         }
15687:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15688:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15689:                     }
15690:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15691:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15692:                             if ($confhash{$provider}{'incdom'}) {
15693:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15694:                             } else { 
15695:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15696:                             }
15697:                         }
15698:                     }
15699:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15700:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15701:                             $resulttext .= '<li>'.$lt{'defa'};
15702:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15703:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15704:                             }
15705:                             $resulttext =~ s/,$//;
15706:                             $resulttext .= '</li>';
15707:                         }
15708:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15709:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15710:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15711:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15712:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15713:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15714:                                 }
15715:                             }
15716:                             $resulttext .= '</ul></li>';
15717:                         }
15718:                     }
15719:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15720:                         if (@{$crsconf{$provider}} > 0) {
15721:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15722:                             my $numconfig = 0;
15723:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15724:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15725:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15726:                                         $numconfig ++;
15727:                                         if ($provider eq 'examity') {
15728:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15729:                                         } else {
15730:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15731:                                         }
15732:                                     }
15733:                                     $resulttext =~ s/,$//;
15734:                                 }
15735:                             }
15736:                             if (!$numconfig) {
15737:                                 $resulttext .= '&nbsp;'.&mt('None');
15738:                             }
15739:                             $resulttext .= '</li>';
15740:                         }
15741:                     }
15742:                     if ($showroles) {
15743:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15744:                             my $rolemaps;
15745:                             foreach my $role (@courseroles) {
15746:                                 if ($confhash{$provider}{'roles'}{$role}) {
15747:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15748:                                                  $confhash{$provider}{'roles'}{$role}.',';
15749:                                 }
15750:                             }
15751:                             if ($rolemaps) {
15752:                                 $rolemaps =~ s/,$//;
15753:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15754:                             }
15755:                         }
15756:                     }
15757:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15758:                         my $customlist;
15759:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15760:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15761:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15762:                             }
15763:                             $customlist =~ s/,$//;
15764:                         }
15765:                         if ($customlist) {
15766:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15767:                         }
15768:                     } 
15769:                     $resulttext .= '</ul></li>';
15770:                 }
15771:             }
15772:             $resulttext .= '</ul>';
15773:         } else {
15774:             $resulttext = &mt('No changes made.');
15775:         }
15776:     } else {
15777:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15778:     }
15779:     if ($errors) {
15780:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15781:                        $errors.'</ul>';
15782:     }
15783:     return $resulttext;
15784: }
15785: 
15786: sub process_proctoring_image {
15787:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15788:     my $filename = $env{'form.'.$caller.'.filename'};
15789:     my ($error,$url);
15790:     my ($width,$height) = (21,21);
15791:     if ($configuserok eq 'ok') {
15792:         if ($switchserver) {
15793:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15794:                          $switchserver);
15795:         } elsif ($author_ok eq 'ok') {
15796:             my $modified = [];
15797:             my ($result,$imageurl,$madethumb) =
15798:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15799:                                                         "proctoring/$provider/icon",$width,$height,
15800:                                                         '',$modified);
15801:             if ($result eq 'ok') {
15802:                 if ($madethumb) {
15803:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15804:                     my $imagethumb = "$path/tn-".$imagefile;
15805:                     $url = $imagethumb;
15806:                 } else {
15807:                     $url = $imageurl;
15808:                 }
15809:                 &update_modify_urls($r,$modified);
15810:             } else {
15811:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15812:             }
15813:         } else {
15814:             $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);
15815:         }
15816:     } else {
15817:         $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);
15818:     }
15819:     return ($url,$error);
15820: }
15821: 
15822: sub modify_lti {
15823:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15824:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15825:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15826:     my (%posslti,%posslticrs,%posscrstype);
15827:     my @courseroles = ('cc','in','ta','ep','st');
15828:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15829:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15830:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15831:     my %coursetypetitles = &Apache::lonlocal::texthash (
15832:                                official   => 'Official',
15833:                                unofficial => 'Unofficial',
15834:                                community  => 'Community',
15835:                                textbook   => 'Textbook',
15836:                                placement  => 'Placement Test',
15837:                                lti        => 'LTI Provider',
15838:     );
15839:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15840:     my %lt = &lti_names();
15841:     map { $posslti{$_} = 1; } @ltiroles;
15842:     map { $posslticrs{$_} = 1; } @lticourseroles;
15843:     map { $posscrstype{$_} = 1; } @coursetypes;
15844: 
15845:     my %menutitles = &ltimenu_titles();
15846:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15847: 
15848:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15849: 
15850:     my (%linkprotchg,$linkprotoutput,$is_home);
15851:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15852:                                                            \%linkprotchg,'domain');
15853:     my $home = &Apache::lonnet::domain($dom,'primary');
15854:     unless (($home eq 'no_host') || ($home eq '')) {
15855:         my @ids=&Apache::lonnet::current_machine_ids();
15856:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15857:     }
15858: 
15859:     if (keys(%linkprotchg)) {
15860:         $secchanges{'linkprot'} = 1;
15861:         my %oldlinkprot;
15862:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15863:             %oldlinkprot = %{$currltisec{'linkprot'}};
15864:         }
15865:         foreach my $id (keys(%linkprotchg)) {
15866:             if (ref($linkprotchg{$id}) eq 'HASH') {
15867:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15868:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15869:                         if ($is_home) {
15870:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15871:                         }
15872:                     }
15873:                 }
15874:             } else {
15875:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15876:             }
15877:         }
15878:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15879:         if (keys(%linkprotchg)) {
15880:             %{$newltisec{'linkprot'}} = %linkprotchg;
15881:         }
15882:     }
15883:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15884:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
15885:             next if ($id !~ /^\d+$/);
15886:             unless (exists($linkprotchg{$id})) {
15887:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15888:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15889:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15890:                             if ($is_home) {
15891:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15892:                             }
15893:                         } else {
15894:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15895:                         }
15896:                     }
15897:                 } else {
15898:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15899:                 }
15900:             }
15901:         }
15902:     }
15903:     if ($proterror) {
15904:         $errors .= '<li>'.$proterror.'</li>';
15905:     }
15906: 
15907:     my (%delsuggested,%suggids,@suggested);;
15908:     if (ref($currltisec{'suggested'}) eq 'HASH') {
15909:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
15910:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
15911:         for (my $i=0; $i<$maxnum; $i++) {
15912:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
15913:             $itemid =~ s/\D+//g;
15914:             if ($itemid) {
15915:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
15916:                     push(@suggested,$i);
15917:                     $suggids{$i} = $itemid;
15918:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
15919:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
15920:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15921:                         }
15922:                     } else {
15923:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
15924:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15925:                         } else {
15926:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
15927:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
15928:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
15929:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
15930:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
15931:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
15932:                                 $secchanges{'suggested'}{$itemid} = 1;
15933:                             }
15934:                         }
15935:                     }
15936:                 }
15937:             }
15938:         }
15939:     }
15940:     foreach my $key (keys(%delsuggested)) {
15941:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
15942:         $secchanges{'suggested'}{$key} = 1;
15943:     }
15944:     if (($env{'form.linkprot_suggested_add'}) &&
15945:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
15946:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
15947:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
15948:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
15949:         if ($newsuggid) {
15950:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
15951:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
15952:             $secchanges{'suggested'}{$newsuggid} = 1;
15953:         } else {
15954:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
15955:             if ($errormsg) {
15956:                 $error .= ' ('.$errormsg.')';
15957:             }
15958:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15959:         }
15960:     }
15961:     my (@items,%deletions,%itemids);
15962:     if ($env{'form.lti_add'}) {
15963:         my $consumer = $env{'form.lti_consumer_add'};
15964:         $consumer =~ s/(`)/'/g;
15965:         ($newid,my $errormsg) = &get_lti_id($dom,$consumer,'lti');
15966:         if ($newid) {
15967:             $itemids{'add'} = $newid;
15968:             push(@items,'add');
15969:             $changes{$newid} = 1;
15970:         } else {
15971:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15972:             if ($errormsg) {
15973:                 $error .= ' ('.$errormsg.')';
15974:             }
15975:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15976:         }
15977:     }
15978:     if (ref($domconfig{$action}) eq 'HASH') {
15979:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15980:         if (@todelete) {
15981:             map { $deletions{$_} = 1; } @todelete;
15982:         }
15983:         my $maxnum = $env{'form.lti_maxnum'};
15984:         for (my $i=0; $i<$maxnum; $i++) {
15985:             my $itemid = $env{'form.lti_id_'.$i};
15986:             $itemid =~ s/\D+//g;
15987:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15988:                 if ($deletions{$itemid}) {
15989:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15990:                 } else {
15991:                     push(@items,$i);
15992:                     $itemids{$i} = $itemid;
15993:                 }
15994:             }
15995:         }
15996:     }
15997:     my (%keystore,$secstored);
15998:     if ($is_home) {
15999:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
16000:     }
16001: 
16002:     my ($cipher,$privnum);
16003:     if ((@items > 0) && ($is_home)) {
16004:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
16005:                                              $newltisec{'encrypt'},$keystore{$home});
16006:     }
16007:     foreach my $idx (@items) {
16008:         my $itemid = $itemids{$idx};
16009:         next unless ($itemid);
16010:         my %currlti;
16011:         unless ($idx eq 'add') {
16012:             if (ref($domconfig{$action}) eq 'HASH') {
16013:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16014:                     %currlti = %{$domconfig{$action}{$itemid}};
16015:                 }
16016:             }
16017:         }
16018:         my $position = $env{'form.lti_pos_'.$itemid};
16019:         $position =~ s/\D+//g;
16020:         if ($position ne '') {
16021:             $allpos[$position] = $itemid;
16022:         }
16023:         foreach my $item ('consumer','lifetime','requser','crsinc') {
16024:             my $formitem = 'form.lti_'.$item.'_'.$idx;
16025:             $env{$formitem} =~ s/(`)/'/g;
16026:             if ($item eq 'lifetime') {
16027:                 $env{$formitem} =~ s/[^\d.]//g;
16028:             }
16029:             if ($env{$formitem} ne '') {
16030:                 $confhash{$itemid}{$item} = $env{$formitem};
16031:                 unless (($idx eq 'add') || ($changes{$itemid})) {
16032:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
16033:                         $changes{$itemid} = 1;
16034:                     }
16035:                 }
16036:             }
16037:         }
16038:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
16039:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
16040:         }
16041:         if ($confhash{$itemid}{'requser'}) {
16042:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
16043:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
16044:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
16045:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
16046:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
16047:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
16048:                 $mapuser =~ s/(`)/'/g;
16049:                 $mapuser =~ s/^\s+|\s+$//g;
16050:                 $confhash{$itemid}{'mapuser'} = $mapuser;
16051:             }
16052:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
16053:             my @makeuser;
16054:             foreach my $ltirole (sort(@possmakeuser)) {
16055:                 if ($posslti{$ltirole}) {
16056:                     push(@makeuser,$ltirole);
16057:                 }
16058:             }
16059:             $confhash{$itemid}{'makeuser'} = \@makeuser;
16060:             if (@makeuser) {
16061:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
16062:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
16063:                     $confhash{$itemid}{'lcauth'} = $lcauth;
16064:                     if ($lcauth ne 'internal') {
16065:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
16066:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
16067:                         $lcauthparm =~ s/`//g;
16068:                         if ($lcauthparm ne '') {
16069:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
16070:                         }
16071:                     }
16072:                 } else {
16073:                     $confhash{$itemid}{'lcauth'} = 'lti';
16074:                 }
16075:             }
16076:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
16077:             if (@possinstdata) {
16078:                 foreach my $field (@possinstdata) {
16079:                     if (exists($fieldtitles{$field})) {
16080:                         push(@{$confhash{$itemid}{'instdata'}});
16081:                     }
16082:                 }
16083:             }
16084:             if ($env{'form.lti_callback_'.$idx}) {
16085:                 if ($env{'form.lti_callbackparam_'.$idx}) {
16086:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
16087:                     $callback =~ s/^\s+|\s+$//g;
16088:                     $confhash{$itemid}{'callback'} = $callback;
16089:                 }
16090:             }
16091:             foreach my $field ('topmenu','inlinemenu') {
16092:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
16093:                     $confhash{$itemid}{$field} = 1;
16094:                 }
16095:             }
16096:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
16097:                 $confhash{$itemid}{lcmenu} = [];
16098:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
16099:                 foreach my $field (@possmenu) {
16100:                     if (exists($menutitles{$field})) {
16101:                         if ($field eq 'grades') {
16102:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
16103:                         }
16104:                         push(@{$confhash{$itemid}{lcmenu}},$field);
16105:                     }
16106:                 }
16107:             }
16108:             if ($confhash{$itemid}{'crsinc'}) {
16109:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
16110:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
16111:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
16112:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
16113:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
16114:                     $mapcrs =~ s/(`)/'/g;
16115:                     $mapcrs =~ s/^\s+|\s+$//g;
16116:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
16117:                 }
16118:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
16119:                 my @crstypes;
16120:                 foreach my $type (sort(@posstypes)) {
16121:                     if ($posscrstype{$type}) {
16122:                         push(@crstypes,$type);
16123:                     }
16124:                 }
16125:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
16126:                 if ($env{'form.lti_storecrs_'.$idx}) {
16127:                     $confhash{$itemid}{'storecrs'} = 1;
16128:                 }       
16129:                 if ($env{'form.lti_makecrs_'.$idx}) {
16130:                     $confhash{$itemid}{'makecrs'} = 1;
16131:                 }
16132:                 foreach my $ltirole (@lticourseroles) {
16133:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
16134:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
16135:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
16136:                     }
16137:                 }
16138:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
16139:                 my @selfenroll;
16140:                 foreach my $type (sort(@possenroll)) {
16141:                     if ($posslticrs{$type}) {
16142:                         push(@selfenroll,$type);
16143:                     }
16144:                 }
16145:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
16146:                 if ($env{'form.lti_crssec_'.$idx}) {
16147:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
16148:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
16149:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
16150:                         my $section = $env{'form.lti_customsection_'.$idx};
16151:                         $section =~ s/(`)/'/g;
16152:                         $section =~ s/^\s+|\s+$//g;
16153:                         if ($section ne '') {
16154:                             $confhash{$itemid}{'section'} = $section;
16155:                         }
16156:                     }
16157:                 }
16158:                 foreach my $field ('passback','roster') {
16159:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
16160:                         $confhash{$itemid}{$field} = 1;
16161:                     }
16162:                 }
16163:                 if ($env{'form.lti_passback_'.$idx}) {
16164:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
16165:                         $confhash{$itemid}{'passbackformat'} = '1.0';
16166:                     } else {
16167:                         $confhash{$itemid}{'passbackformat'} = '1.1';
16168:                     }
16169:                 }
16170:             }
16171:             unless (($idx eq 'add') || ($changes{$itemid})) {
16172:                 if ($confhash{$itemid}{'crsinc'}) {
16173:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
16174:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16175:                             $changes{$itemid} = 1;
16176:                         }
16177:                     }
16178:                     unless ($changes{$itemid}) {
16179:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
16180:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
16181:                                 $changes{$itemid} = 1;
16182:                             }
16183:                         }
16184:                     }
16185:                     foreach my $field ('mapcrstype','selfenroll') {
16186:                         unless ($changes{$itemid}) {
16187:                             if (ref($currlti{$field}) eq 'ARRAY') {
16188:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16189:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16190:                                                                                    $confhash{$itemid}{$field});
16191:                                     if (@diffs) {
16192:                                         $changes{$itemid} = 1;
16193:                                     }
16194:                                 } elsif (@{$currlti{$field}} > 0) {
16195:                                     $changes{$itemid} = 1;
16196:                                 }
16197:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16198:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16199:                                     $changes{$itemid} = 1;
16200:                                 }
16201:                             }
16202:                         }
16203:                     }
16204:                     unless ($changes{$itemid}) {
16205:                         if (ref($currlti{'maproles'}) eq 'HASH') {
16206:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16207:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
16208:                                     if ($currlti{'maproles'}{$ltirole} ne 
16209:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
16210:                                         $changes{$itemid} = 1;
16211:                                         last;
16212:                                     }
16213:                                 }
16214:                                 unless ($changes{$itemid}) {
16215:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
16216:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
16217:                                             $currlti{'maproles'}{$ltirole}) {
16218:                                             $changes{$itemid} = 1;
16219:                                             last;
16220:                                         }
16221:                                     }
16222:                                 }
16223:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
16224:                                 $changes{$itemid} = 1;
16225:                             }
16226:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16227:                             unless ($changes{$itemid}) {
16228:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
16229:                                     $changes{$itemid} = 1;
16230:                                 }
16231:                             }
16232:                         }
16233:                     }
16234:                 }
16235:                 unless ($changes{$itemid}) {
16236:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
16237:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16238:                             $changes{$itemid} = 1;
16239:                         }
16240:                     }
16241:                     unless ($changes{$itemid}) {
16242:                         foreach my $field ('makeuser','lcmenu') {
16243:                             if (ref($currlti{$field}) eq 'ARRAY') {
16244:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16245:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16246:                                                                                    $confhash{$itemid}{$field});
16247:                                     if (@diffs) {
16248:                                         $changes{$itemid} = 1;
16249:                                     }
16250:                                 } elsif (@{$currlti{$field}} > 0) {
16251:                                     $changes{$itemid} = 1;
16252:                                 }
16253:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16254:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16255:                                     $changes{$itemid} = 1;
16256:                                 }
16257:                             }
16258:                         }
16259:                     }
16260:                 }
16261:             }
16262:         }
16263:         if ($is_home) {
16264:             my $keyitem = 'form.lti_key_'.$idx;
16265:             $env{$keyitem} =~ s/(`)/'/g;
16266:             if ($env{$keyitem} ne '') {
16267:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
16268:                 unless ($changes{$itemid}) {
16269:                     if ($currlti{'key'} ne $env{$keyitem}) {
16270:                         $changes{$itemid} = 1;
16271:                     }
16272:                 }
16273:             }
16274:             my $secretitem = 'form.lti_secret_'.$idx;
16275:             $env{$secretitem} =~ s/(`)/'/g;
16276:             if ($currlti{'usable'}) {
16277:                 if ($env{'form.lti_changesecret_'.$idx}) {
16278:                     if ($env{$secretitem} ne '') {
16279:                         if ($privnum && $cipher) {
16280:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16281:                             $confhash{$itemid}{'cipher'} = $privnum;
16282:                         } else {
16283:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
16284:                         }
16285:                         $changes{$itemid} = 1;
16286:                     }
16287:                 } else {
16288:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
16289:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
16290:                 }
16291:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16292:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
16293:                         $confhash{$itemid}{'usable'} = 1;
16294:                     }
16295:                 }
16296:             } elsif ($env{$secretitem} ne '') {
16297:                 if ($privnum && $cipher) {
16298:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16299:                     $confhash{$itemid}{'cipher'} = $privnum;
16300:                 } else {
16301:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
16302:                 }
16303:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16304:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
16305:                         $confhash{$itemid}{'usable'} = 1;
16306:                     }
16307:                 }
16308:                 $changes{$itemid} = 1;
16309:             }
16310:         }
16311:         unless ($changes{$itemid}) {
16312:             foreach my $key (keys(%currlti)) {
16313:                 if (ref($currlti{$key}) eq 'HASH') {
16314:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
16315:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
16316:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
16317:                                 $changes{$itemid} = 1;
16318:                                 last;
16319:                             }
16320:                         }
16321:                     } elsif (keys(%{$currlti{$key}}) > 0) {
16322:                         $changes{$itemid} = 1;
16323:                     }
16324:                 }
16325:                 last if ($changes{$itemid});
16326:             }
16327:         }
16328:     }
16329:     if (@allpos > 0) {
16330:         my $idx = 0;
16331:         foreach my $itemid (@allpos) {
16332:             if ($itemid ne '') {
16333:                 $confhash{$itemid}{'order'} = $idx;
16334:                 if (ref($domconfig{$action}) eq 'HASH') {
16335:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16336:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
16337:                             $changes{$itemid} = 1;
16338:                         }
16339:                     }
16340:                 }
16341:                 $idx ++;
16342:             }
16343:         }
16344:     }
16345: 
16346:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
16347:         return &mt('No changes made.');
16348:     }
16349: 
16350:     my %ltihash = (
16351:                       $action => { %confhash }
16352:                   );
16353:     my %ltienchash;
16354: 
16355:     if ($is_home) {
16356:         %ltienchash = (
16357:                          $action => { %ltienc }
16358:                       );
16359:     }
16360:     if (keys(%secchanges)) {
16361:         $ltihash{'ltisec'} = \%newltisec;
16362:         if ($secchanges{'linkprot'}) {
16363:             if ($is_home) {
16364:                 $ltienchash{'linkprot'} = \%newltienc;
16365:             }
16366:         }
16367:     }
16368:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
16369:     if ($putresult eq 'ok') {
16370:         if (keys(%ltienchash)) {
16371:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
16372:         }
16373:         $resulttext = &mt('Changes made:').'<ul>';
16374:         if (keys(%secchanges) > 0) {
16375:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
16376:             if (exists($secchanges{'linkprot'})) {
16377:                 $resulttext .= $linkprotoutput;
16378:             }
16379:         }
16380:         if (keys(%changes) > 0) {
16381:             my $cachetime = 24*60*60;
16382:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
16383:             if (ref($lastactref) eq 'HASH') {
16384:                 $lastactref->{'lti'} = 1;
16385:             }
16386:             my %bynum;
16387:             foreach my $itemid (sort(keys(%changes))) {
16388:                 if (ref($confhash{$itemid}) eq 'HASH') {
16389:                     my $position = $confhash{$itemid}{'order'};
16390:                     $bynum{$position} = $itemid;
16391:                 }
16392:             }
16393:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
16394:                 my $itemid = $bynum{$pos};
16395:                 if (ref($confhash{$itemid}) eq 'HASH') {
16396:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
16397:                     my $position = $pos + 1;
16398:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
16399:                     foreach my $item ('version','lifetime') {
16400:                         if ($confhash{$itemid}{$item} ne '') {
16401:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
16402:                         }
16403:                     }
16404:                     if ($ltienc{$itemid}{'key'} ne '') {
16405:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
16406:                     }
16407:                     if ($ltienc{$itemid}{'secret'} ne '') {
16408:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
16409:                     }
16410:                     if ($confhash{$itemid}{'requser'}) {
16411:                         if ($confhash{$itemid}{'callback'}) {
16412:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
16413:                         } else {
16414:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
16415:                         }
16416:                         if ($confhash{$itemid}{'mapuser'}) {
16417:                             my $shownmapuser;
16418:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
16419:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
16420:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
16421:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
16422:                             } else {
16423:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
16424:                             }
16425:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
16426:                         }
16427:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
16428:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
16429:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
16430:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
16431:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
16432:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
16433:                                 } else {
16434:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
16435:                                                        $confhash{$itemid}{'lcauth'});
16436:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
16437:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
16438:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
16439:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
16440:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
16441:                                         }
16442:                                     } else {
16443:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
16444:                                     }
16445:                                 }
16446:                                 $resulttext .= '</li>';
16447:                             } else {
16448:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
16449:                             }
16450:                         }
16451:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
16452:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
16453:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
16454:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
16455:                             } else {
16456:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
16457:                             }
16458:                         }
16459:                         foreach my $item ('topmenu','inlinemenu') {
16460:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16461:                             if ($confhash{$itemid}{$item}) {
16462:                                 $resulttext .= &mt('Yes');
16463:                             } else {
16464:                                 $resulttext .= &mt('No');
16465:                             }
16466:                             $resulttext .= '</li>';
16467:                         }
16468:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16469:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16470:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
16471:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
16472:                             } else {
16473:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
16474:                             }
16475:                         }
16476:                         if ($confhash{$itemid}{'crsinc'}) {
16477:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16478:                                 my $rolemaps;
16479:                                 foreach my $role (@ltiroles) {
16480:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
16481:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
16482:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
16483:                                                                                 'Course').',';
16484:                                     }
16485:                                 }
16486:                                 if ($rolemaps) {
16487:                                     $rolemaps =~ s/,$//;
16488:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
16489:                                 }
16490:                             }
16491:                             if ($confhash{$itemid}{'mapcrs'}) {
16492:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
16493:                             }
16494:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
16495:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
16496:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
16497:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
16498:                                                    '</li>';
16499:                                 } else {
16500:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
16501:                                 }
16502:                             }
16503:                             if ($confhash{$itemid}{'storecrs'}) {
16504:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
16505:                             }
16506:                             if ($confhash{$itemid}{'makecrs'}) {
16507:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
16508:                             } else {
16509:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
16510:                             }
16511:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
16512:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
16513:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
16514:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
16515:                                                    '</li>';
16516:                                 } else {
16517:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
16518:                                 }
16519:                             }
16520:                             if ($confhash{$itemid}{'section'}) {
16521:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
16522:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
16523:                                                          ' (course_section_sourcedid)'.'</li>';  
16524:                                 } else {
16525:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
16526:                                                           $confhash{$itemid}{'section'}.'</li>';
16527:                                 }
16528:                             } else {
16529:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
16530:                             }
16531:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
16532:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16533:                                 if ($confhash{$itemid}{$item}) {
16534:                                     $resulttext .= &mt('Yes');
16535:                                     if ($item eq 'passback') {
16536:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
16537:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
16538:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
16539:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
16540:                                         }
16541:                                     }
16542:                                 } else {
16543:                                     $resulttext .= &mt('No');
16544:                                 }
16545:                                 $resulttext .= '</li>';
16546:                             }
16547:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16548:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16549:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
16550:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
16551:                                 } else {
16552:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
16553:                                 }
16554:                             }
16555:                         }
16556:                     }
16557:                     $resulttext .= '</ul></li>';
16558:                 }
16559:             }
16560:             if (keys(%deletions)) {
16561:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
16562:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
16563:                 }
16564:             }
16565:         }
16566:         $resulttext .= '</ul>';
16567:         if (ref($lastactref) eq 'HASH') {
16568:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
16569:                 &Apache::lonnet::get_domain_defaults($dom,1);
16570:                 $lastactref->{'domdefaults'} = 1;
16571:             }
16572:         }
16573:     } else {
16574:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
16575:     }
16576:     if ($errors) {
16577:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
16578:                        $errors.'</ul>';
16579:     }
16580:     return $resulttext;
16581: }
16582: 
16583: sub get_priv_creds {
16584:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
16585:     my ($needenc,$cipher,$privnum);
16586:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
16587:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
16588:         $needenc = $encrypt->{'consumers'} 
16589:     } else {
16590:         $needenc = $domdefs{'ltienc_consumers'};
16591:     }
16592:     if ($needenc) {
16593:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
16594:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
16595:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
16596:             my $privkey = $privhash{'key'};
16597:             $privnum = $privhash{'version'};
16598:             if (($privnum) && ($privkey ne '')) {
16599:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
16600:                                           'cipher'  => 'DES'});
16601:             }
16602:         }
16603:     }
16604:     return ($cipher,$privnum);
16605: }
16606: 
16607: sub get_lti_id {
16608:     my ($domain,$consumer,$dbname) = @_;
16609:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
16610:         return ('','invalid db');
16611:     }
16612:     # get lock on db
16613:     my $lockhash = {
16614:                       lock => $env{'user.name'}.
16615:                               ':'.$env{'user.domain'},
16616:                    };
16617:     my $tries = 0;
16618:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16619:     my ($id,$error);
16620: 
16621:     while (($gotlock ne 'ok') && ($tries<10)) {
16622:         $tries ++;
16623:         sleep (0.1);
16624:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16625:     }
16626:     if ($gotlock eq 'ok') {
16627:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
16628:         if ($currids{'lock'}) {
16629:             delete($currids{'lock'});
16630:             if (keys(%currids)) {
16631:                 my @curr = sort { $a <=> $b } keys(%currids);
16632:                 if ($curr[-1] =~ /^\d+$/) {
16633:                     $id = 1 + $curr[-1];
16634:                 }
16635:             } else {
16636:                 $id = 1;
16637:             }
16638:             if ($id) {
16639:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
16640:                     $error = 'nostore';
16641:                 }
16642:             } else {
16643:                 $error = 'nonumber';
16644:             }
16645:         }
16646:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
16647:     } else {
16648:         $error = 'nolock';
16649:     }
16650:     return ($id,$error);
16651: }
16652: 
16653: sub modify_autoenroll {
16654:     my ($dom,$lastactref,%domconfig) = @_;
16655:     my ($resulttext,%changes);
16656:     my %currautoenroll;
16657:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
16658:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
16659:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
16660:         }
16661:     }
16662:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
16663:     my %title = ( run => 'Auto-enrollment active',
16664:                   sender => 'Sender for notification messages',
16665:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
16666:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
16667:     my @offon = ('off','on');
16668:     my $sender_uname = $env{'form.sender_uname'};
16669:     my $sender_domain = $env{'form.sender_domain'};
16670:     if ($sender_domain eq '') {
16671:         $sender_uname = '';
16672:     } elsif ($sender_uname eq '') {
16673:         $sender_domain = '';
16674:     }
16675:     my $coowners = $env{'form.autoassign_coowners'};
16676:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
16677:     $autofailsafe =~ s{^\s+|\s+$}{}g;
16678:     if ($autofailsafe =~ /\D/) {
16679:         undef($autofailsafe);
16680:     }
16681:     my $failsafe = $env{'form.autoenroll_failsafe'};
16682:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
16683:         $failsafe = 'off';
16684:         undef($autofailsafe);
16685:     }
16686:     my %autoenrollhash =  (
16687:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
16688:                                        'sender_uname' => $sender_uname,
16689:                                        'sender_domain' => $sender_domain,
16690:                                        'co-owners' => $coowners,
16691:                                        'autofailsafe' => $autofailsafe,
16692:                                        'failsafe' => $failsafe,
16693:                                 }
16694:                      );
16695:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
16696:                                              $dom);
16697:     if ($putresult eq 'ok') {
16698:         if (exists($currautoenroll{'run'})) {
16699:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
16700:                  $changes{'run'} = 1;
16701:              }
16702:         } elsif ($autorun) {
16703:             if ($env{'form.autoenroll_run'} ne '1') {
16704:                  $changes{'run'} = 1;
16705:             }
16706:         }
16707:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
16708:             $changes{'sender'} = 1;
16709:         }
16710:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
16711:             $changes{'sender'} = 1;
16712:         }
16713:         if ($currautoenroll{'co-owners'} ne '') {
16714:             if ($currautoenroll{'co-owners'} ne $coowners) {
16715:                 $changes{'coowners'} = 1;
16716:             }
16717:         } elsif ($coowners) {
16718:             $changes{'coowners'} = 1;
16719:         }
16720:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
16721:             $changes{'autofailsafe'} = 1;
16722:         }
16723:         if ($currautoenroll{'failsafe'} ne $failsafe) {
16724:             $changes{'failsafe'} = 1;
16725:         }
16726:         if (keys(%changes) > 0) {
16727:             $resulttext = &mt('Changes made:').'<ul>';
16728:             if ($changes{'run'}) {
16729:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
16730:             }
16731:             if ($changes{'sender'}) {
16732:                 if ($sender_uname eq '' || $sender_domain eq '') {
16733:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
16734:                 } else {
16735:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
16736:                 }
16737:             }
16738:             if ($changes{'coowners'}) {
16739:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
16740:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16741:                 if (ref($lastactref) eq 'HASH') {
16742:                     $lastactref->{'domainconfig'} = 1;
16743:                 }
16744:             }
16745:             if ($changes{'autofailsafe'}) {
16746:                 if ($autofailsafe ne '') {
16747:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
16748:                 } else {
16749:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16750:                 }
16751:             }
16752:             if ($changes{'failsafe'}) {
16753:                 if ($failsafe eq 'off') {
16754:                     unless ($changes{'autofailsafe'}) {
16755:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16756:                     }
16757:                 } elsif ($failsafe eq 'zero') {
16758:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
16759:                 } else {
16760:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
16761:                 }
16762:             }
16763:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
16764:                 &Apache::lonnet::get_domain_defaults($dom,1);
16765:                 if (ref($lastactref) eq 'HASH') {
16766:                     $lastactref->{'domdefaults'} = 1;
16767:                 }
16768:             }
16769:             $resulttext .= '</ul>';
16770:         } else {
16771:             $resulttext = &mt('No changes made to auto-enrollment settings');
16772:         }
16773:     } else {
16774:         $resulttext = '<span class="LC_error">'.
16775: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16776:     }
16777:     return $resulttext;
16778: }
16779: 
16780: sub modify_autoupdate {
16781:     my ($dom,%domconfig) = @_;
16782:     my ($resulttext,%currautoupdate,%fields,%changes);
16783:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16784:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16785:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16786:         }
16787:     }
16788:     my @offon = ('off','on');
16789:     my %title = &Apache::lonlocal::texthash (
16790:                     run        => 'Auto-update:',
16791:                     classlists => 'Updates to user information in classlists?',
16792:                     unexpired  => 'Skip updates for users without active or future roles?',
16793:                     lastactive => 'Skip updates for inactive users?',
16794:                 );
16795:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16796:     my %fieldtitles = &Apache::lonlocal::texthash (
16797:                         id => 'Student/Employee ID',
16798:                         permanentemail => 'E-mail address',
16799:                         lastname => 'Last Name',
16800:                         firstname => 'First Name',
16801:                         middlename => 'Middle Name',
16802:                         generation => 'Generation',
16803:                       );
16804:     $othertitle = &mt('All users');
16805:     if (keys(%{$usertypes}) >  0) {
16806:         $othertitle = &mt('Other users');
16807:     }
16808:     foreach my $key (keys(%env)) {
16809:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16810:             my ($usertype,$item) = ($1,$2);
16811:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16812:                 if ($usertype eq 'default') {   
16813:                     push(@{$fields{$1}},$2);
16814:                 } elsif (ref($types) eq 'ARRAY') {
16815:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16816:                         push(@{$fields{$1}},$2);
16817:                     }
16818:                 }
16819:             }
16820:         }
16821:     }
16822:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16823:     @lockablenames = sort(@lockablenames);
16824:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16825:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16826:         if (@changed) {
16827:             $changes{'lockablenames'} = 1;
16828:         }
16829:     } else {
16830:         if (@lockablenames) {
16831:             $changes{'lockablenames'} = 1;
16832:         }
16833:     }
16834:     my %updatehash = (
16835:                       autoupdate => { run => $env{'form.autoupdate_run'},
16836:                                       classlists => $env{'form.classlists'},
16837:                                       unexpired  => $env{'form.unexpired'},
16838:                                       fields => {%fields},
16839:                                       lockablenames => \@lockablenames,
16840:                                     }
16841:                      );
16842:     my $lastactivedays;
16843:     if ($env{'form.lastactive'}) {
16844:         $lastactivedays = $env{'form.lastactivedays'};
16845:         $lastactivedays =~ s/^\s+|\s+$//g;
16846:         unless ($lastactivedays =~ /^\d+$/) {
16847:             undef($lastactivedays);
16848:             $env{'form.lastactive'} = 0;
16849:         }
16850:     }
16851:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16852:     foreach my $key (keys(%currautoupdate)) {
16853:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16854:             if (exists($updatehash{autoupdate}{$key})) {
16855:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16856:                     $changes{$key} = 1;
16857:                 }
16858:             }
16859:         } elsif ($key eq 'fields') {
16860:             if (ref($currautoupdate{$key}) eq 'HASH') {
16861:                 foreach my $item (@{$types},'default') {
16862:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16863:                         my $change = 0;
16864:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16865:                             if (!exists($fields{$item})) {
16866:                                 $change = 1;
16867:                                 last;
16868:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16869:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16870:                                     $change = 1;
16871:                                     last;
16872:                                 }
16873:                             }
16874:                         }
16875:                         if ($change) {
16876:                             push(@{$changes{$key}},$item);
16877:                         }
16878:                     } 
16879:                 }
16880:             }
16881:         } elsif ($key eq 'lockablenames') {
16882:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16883:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16884:                 if (@changed) {
16885:                     $changes{'lockablenames'} = 1;
16886:                 }
16887:             } else {
16888:                 if (@lockablenames) {
16889:                     $changes{'lockablenames'} = 1;
16890:                 }
16891:             }
16892:         }
16893:     }
16894:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16895:         if (@lockablenames) {
16896:             $changes{'lockablenames'} = 1;
16897:         }
16898:     }
16899:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16900:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16901:             $changes{'unexpired'} = 1;
16902:         }
16903:     }
16904:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16905:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16906:             $changes{'lastactive'} = 1;
16907:         }
16908:     }
16909:     foreach my $item (@{$types},'default') {
16910:         if (defined($fields{$item})) {
16911:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16912:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16913:                     my $change = 0;
16914:                     if (ref($fields{$item}) eq 'ARRAY') {
16915:                         foreach my $type (@{$fields{$item}}) {
16916:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16917:                                 $change = 1;
16918:                                 last;
16919:                             }
16920:                         }
16921:                     }
16922:                     if ($change) {
16923:                         push(@{$changes{'fields'}},$item);
16924:                     }
16925:                 } else {
16926:                     push(@{$changes{'fields'}},$item);
16927:                 }
16928:             } else {
16929:                 push(@{$changes{'fields'}},$item);
16930:             }
16931:         }
16932:     }
16933:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16934:                                              $dom);
16935:     if ($putresult eq 'ok') {
16936:         if (keys(%changes) > 0) {
16937:             $resulttext = &mt('Changes made:').'<ul>';
16938:             foreach my $key (sort(keys(%changes))) {
16939:                 if ($key eq 'lockablenames') {
16940:                     $resulttext .= '<li>';
16941:                     if (@lockablenames) {
16942:                         $usertypes->{'default'} = $othertitle;
16943:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16944:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16945:                     } else {
16946:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16947:                     }
16948:                     $resulttext .= '</li>';
16949:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16950:                     foreach my $item (@{$changes{$key}}) {
16951:                         my @newvalues;
16952:                         foreach my $type (@{$fields{$item}}) {
16953:                             push(@newvalues,$fieldtitles{$type});
16954:                         }
16955:                         my $newvaluestr;
16956:                         if (@newvalues > 0) {
16957:                             $newvaluestr = join(', ',@newvalues);
16958:                         } else {
16959:                             $newvaluestr = &mt('none');
16960:                         }
16961:                         if ($item eq 'default') {
16962:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16963:                         } else {
16964:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16965:                         }
16966:                     }
16967:                 } else {
16968:                     my $newvalue;
16969:                     if ($key eq 'run') {
16970:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16971:                     } elsif ($key eq 'lastactive') {
16972:                         $newvalue = $offon[$env{'form.lastactive'}];
16973:                         unless ($lastactivedays eq '') {
16974:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16975:                         }
16976:                     } else {
16977:                         $newvalue = $offon[$env{'form.'.$key}];
16978:                     }
16979:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16980:                 }
16981:             }
16982:             $resulttext .= '</ul>';
16983:         } else {
16984:             $resulttext = &mt('No changes made to autoupdates');
16985:         }
16986:     } else {
16987:         $resulttext = '<span class="LC_error">'.
16988: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16989:     }
16990:     return $resulttext;
16991: }
16992: 
16993: sub modify_autocreate {
16994:     my ($dom,%domconfig) = @_;
16995:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16996:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16997:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16998:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16999:         }
17000:     }
17001:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
17002:                  req => 'Auto-creation of validated requests for official courses',
17003:                  xmldc => 'Identity of course creator of courses from XML files',
17004:                );
17005:     my @types = ('xml','req');
17006:     foreach my $item (@types) {
17007:         $newvals{$item} = $env{'form.autocreate_'.$item};
17008:         $newvals{$item} =~ s/\D//g;
17009:         $newvals{$item} = 0 if ($newvals{$item} eq '');
17010:     }
17011:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
17012:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17013:     unless (exists($domcoords{$newvals{'xmldc'}})) {
17014:         $newvals{'xmldc'} = '';
17015:     } 
17016:     %autocreatehash =  (
17017:                         autocreate => { xml => $newvals{'xml'},
17018:                                         req => $newvals{'req'},
17019:                                       }
17020:                        );
17021:     if ($newvals{'xmldc'} ne '') {
17022:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
17023:     }
17024:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
17025:                                              $dom);
17026:     if ($putresult eq 'ok') {
17027:         my @items = @types;
17028:         if ($newvals{'xml'}) {
17029:             push(@items,'xmldc');
17030:         }
17031:         foreach my $item (@items) {
17032:             if (exists($currautocreate{$item})) {
17033:                 if ($currautocreate{$item} ne $newvals{$item}) {
17034:                     $changes{$item} = 1;
17035:                 }
17036:             } elsif ($newvals{$item}) {
17037:                 $changes{$item} = 1;
17038:             }
17039:         }
17040:         if (keys(%changes) > 0) {
17041:             my @offon = ('off','on'); 
17042:             $resulttext = &mt('Changes made:').'<ul>';
17043:             foreach my $item (@types) {
17044:                 if ($changes{$item}) {
17045:                     my $newtxt = $offon[$newvals{$item}];
17046:                     $resulttext .= '<li>'.
17047:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
17048:                                        '<b>','</b>').
17049:                                    '</li>';
17050:                 }
17051:             }
17052:             if ($changes{'xmldc'}) {
17053:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
17054:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
17055:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
17056:             }
17057:             $resulttext .= '</ul>';
17058:         } else {
17059:             $resulttext = &mt('No changes made to auto-creation settings');
17060:         }
17061:     } else {
17062:         $resulttext = '<span class="LC_error">'.
17063:             &mt('An error occurred: [_1]',$putresult).'</span>';
17064:     }
17065:     return $resulttext;
17066: }
17067: 
17068: sub modify_directorysrch {
17069:     my ($dom,$lastactref,%domconfig) = @_;
17070:     my ($resulttext,%changes);
17071:     my %currdirsrch;
17072:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
17073:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
17074:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
17075:         }
17076:     }
17077:     my %title = ( available => 'Institutional directory search available',
17078:                   localonly => 'Other domains can search institution',
17079:                   lcavailable => 'LON-CAPA directory search available',
17080:                   lclocalonly => 'Other domains can search LON-CAPA domain',
17081:                   searchby => 'Search types',
17082:                   searchtypes => 'Search latitude');
17083:     my @offon = ('off','on');
17084:     my @otherdoms = ('Yes','No');
17085: 
17086:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
17087:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
17088:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
17089: 
17090:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17091:     if (keys(%{$usertypes}) == 0) {
17092:         @cansearch = ('default');
17093:     } else {
17094:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
17095:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
17096:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
17097:                     push(@{$changes{'cansearch'}},$type);
17098:                 }
17099:             }
17100:             foreach my $type (@cansearch) {
17101:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
17102:                     push(@{$changes{'cansearch'}},$type);
17103:                 }
17104:             }
17105:         } else {
17106:             push(@{$changes{'cansearch'}},@cansearch);
17107:         }
17108:     }
17109: 
17110:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
17111:         foreach my $by (@{$currdirsrch{'searchby'}}) {
17112:             if (!grep(/^\Q$by\E$/,@searchby)) {
17113:                 push(@{$changes{'searchby'}},$by);
17114:             }
17115:         }
17116:         foreach my $by (@searchby) {
17117:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
17118:                 push(@{$changes{'searchby'}},$by);
17119:             }
17120:         }
17121:     } else {
17122:         push(@{$changes{'searchby'}},@searchby);
17123:     }
17124: 
17125:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
17126:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
17127:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
17128:                 push(@{$changes{'searchtypes'}},$type);
17129:             }
17130:         }
17131:         foreach my $type (@searchtypes) {
17132:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
17133:                 push(@{$changes{'searchtypes'}},$type);
17134:             }
17135:         }
17136:     } else {
17137:         if (exists($currdirsrch{'searchtypes'})) {
17138:             foreach my $type (@searchtypes) {  
17139:                 if ($type ne $currdirsrch{'searchtypes'}) { 
17140:                     push(@{$changes{'searchtypes'}},$type);
17141:                 }
17142:             }
17143:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
17144:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
17145:             }   
17146:         } else {
17147:             push(@{$changes{'searchtypes'}},@searchtypes); 
17148:         }
17149:     }
17150: 
17151:     my %dirsrch_hash =  (
17152:             directorysrch => { available => $env{'form.dirsrch_available'},
17153:                                cansearch => \@cansearch,
17154:                                localonly => $env{'form.dirsrch_instlocalonly'},
17155:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
17156:                                lcavailable => $env{'form.dirsrch_domavailable'},
17157:                                searchby => \@searchby,
17158:                                searchtypes => \@searchtypes,
17159:                              }
17160:             );
17161:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
17162:                                              $dom);
17163:     if ($putresult eq 'ok') {
17164:         if (exists($currdirsrch{'available'})) {
17165:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
17166:                  $changes{'available'} = 1;
17167:              }
17168:         } else {
17169:             if ($env{'form.dirsrch_available'} eq '1') {
17170:                 $changes{'available'} = 1;
17171:             }
17172:         }
17173:         if (exists($currdirsrch{'lcavailable'})) {
17174:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
17175:                 $changes{'lcavailable'} = 1;
17176:             }
17177:         } else {
17178:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
17179:                 $changes{'lcavailable'} = 1;
17180:             }
17181:         }
17182:         if (exists($currdirsrch{'localonly'})) {
17183:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
17184:                 $changes{'localonly'} = 1;
17185:             }
17186:         } else {
17187:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
17188:                 $changes{'localonly'} = 1;
17189:             }
17190:         }
17191:         if (exists($currdirsrch{'lclocalonly'})) {
17192:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
17193:                 $changes{'lclocalonly'} = 1;
17194:             }
17195:         } else {
17196:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
17197:                 $changes{'lclocalonly'} = 1;
17198:             }
17199:         }
17200:         if (keys(%changes) > 0) {
17201:             $resulttext = &mt('Changes made:').'<ul>';
17202:             if ($changes{'available'}) {
17203:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
17204:             }
17205:             if ($changes{'lcavailable'}) {
17206:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
17207:             }
17208:             if ($changes{'localonly'}) {
17209:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
17210:             }
17211:             if ($changes{'lclocalonly'}) {
17212:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
17213:             }
17214:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
17215:                 my $chgtext;
17216:                 if (ref($usertypes) eq 'HASH') {
17217:                     if (keys(%{$usertypes}) > 0) {
17218:                         foreach my $type (@{$types}) {
17219:                             if (grep(/^\Q$type\E$/,@cansearch)) {
17220:                                 $chgtext .= $usertypes->{$type}.'; ';
17221:                             }
17222:                         }
17223:                         if (grep(/^default$/,@cansearch)) {
17224:                             $chgtext .= $othertitle;
17225:                         } else {
17226:                             $chgtext =~ s/\; $//;
17227:                         }
17228:                         $resulttext .=
17229:                             '<li>'.
17230:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
17231:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
17232:                             '</li>';
17233:                     }
17234:                 }
17235:             }
17236:             if (ref($changes{'searchby'}) eq 'ARRAY') {
17237:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
17238:                 my $chgtext;
17239:                 foreach my $type (@{$titleorder}) {
17240:                     if (grep(/^\Q$type\E$/,@searchby)) {
17241:                         if (defined($searchtitles->{$type})) {
17242:                             $chgtext .= $searchtitles->{$type}.'; ';
17243:                         }
17244:                     }
17245:                 }
17246:                 $chgtext =~ s/\; $//;
17247:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
17248:             }
17249:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
17250:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
17251:                 my $chgtext;
17252:                 foreach my $type (@{$srchtypeorder}) {
17253:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
17254:                         if (defined($srchtypes_desc->{$type})) {
17255:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
17256:                         }
17257:                     }
17258:                 }
17259:                 $chgtext =~ s/\; $//;
17260:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
17261:             }
17262:             $resulttext .= '</ul>';
17263:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
17264:             if (ref($lastactref) eq 'HASH') {
17265:                 $lastactref->{'directorysrch'} = 1;
17266:             }
17267:         } else {
17268:             $resulttext = &mt('No changes made to directory search settings');
17269:         }
17270:     } else {
17271:         $resulttext = '<span class="LC_error">'.
17272:                       &mt('An error occurred: [_1]',$putresult).'</span>';
17273:     }
17274:     return $resulttext;
17275: }
17276: 
17277: sub modify_contacts {
17278:     my ($dom,$lastactref,%domconfig) = @_;
17279:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
17280:     if (ref($domconfig{'contacts'}) eq 'HASH') {
17281:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
17282:             $currsetting{$key} = $domconfig{'contacts'}{$key};
17283:         }
17284:     }
17285:     my (%others,%to,%bcc,%includestr,%includeloc);
17286:     my @contacts = ('supportemail','adminemail');
17287:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
17288:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
17289:     my @toggles = ('reporterrors','reportupdates','reportstatus');
17290:     my @lonstatus = ('threshold','sysmail','weights','excluded');
17291:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
17292:     foreach my $type (@mailings) {
17293:         @{$newsetting{$type}} = 
17294:             &Apache::loncommon::get_env_multiple('form.'.$type);
17295:         foreach my $item (@contacts) {
17296:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
17297:                 $contacts_hash{contacts}{$type}{$item} = 1;
17298:             } else {
17299:                 $contacts_hash{contacts}{$type}{$item} = 0;
17300:             }
17301:         }
17302:         $others{$type} = $env{'form.'.$type.'_others'};
17303:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
17304:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17305:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
17306:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
17307:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
17308:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
17309:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
17310:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17311:             }
17312:         }
17313:     }
17314:     foreach my $item (@contacts) {
17315:         $to{$item} = $env{'form.'.$item};
17316:         $contacts_hash{'contacts'}{$item} = $to{$item};
17317:     }
17318:     foreach my $item (@toggles) {
17319:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
17320:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
17321:         }
17322:     }
17323:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
17324:     foreach my $item (@lonstatus) {
17325:         if ($item eq 'excluded') {
17326:             my (%serverhomes,@excluded);
17327:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
17328:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
17329:             if (@possexcluded) {
17330:                 foreach my $id (sort(@possexcluded)) {
17331:                     if ($serverhomes{$id}) {
17332:                         push(@excluded,$id);
17333:                     }
17334:                 }
17335:             }
17336:             if (@excluded) {
17337:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
17338:             }
17339:         } elsif ($item eq 'weights') {
17340:             foreach my $type ('E','W','N','U') {
17341:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
17342:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
17343:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
17344:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
17345:                             $env{'form.error'.$item.'_'.$type};
17346:                     }
17347:                 }
17348:             }
17349:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
17350:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
17351:             if ($env{'form.error'.$item} =~ /^\d+$/) {
17352:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
17353:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
17354:                 }
17355:             }
17356:         }
17357:     }
17358:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
17359:         foreach my $field (@{$fields}) {
17360:             if (ref($possoptions->{$field}) eq 'ARRAY') {
17361:                 my $value = $env{'form.helpform_'.$field};
17362:                 $value =~ s/^\s+|\s+$//g;
17363:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
17364:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
17365:                     if ($field eq 'screenshot') {
17366:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
17367:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
17368:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
17369:                         }
17370:                     }
17371:                 }
17372:             }
17373:         }
17374:     }
17375:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17376:     my (@statuses,%usertypeshash,@overrides);
17377:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
17378:         @statuses = @{$types};
17379:         if (ref($usertypes) eq 'HASH') {
17380:             %usertypeshash = %{$usertypes};
17381:         }
17382:     }
17383:     if (@statuses) {
17384:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
17385:         foreach my $type (@possoverrides) {
17386:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
17387:                 push(@overrides,$type);
17388:             }
17389:         }
17390:         if (@overrides) {
17391:             foreach my $type (@overrides) {
17392:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
17393:                 foreach my $item (@contacts) {
17394:                     if (grep(/^\Q$item\E$/,@standard)) {
17395:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
17396:                         $newsetting{'override_'.$type}{$item} = 1;
17397:                     } else {
17398:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
17399:                         $newsetting{'override_'.$type}{$item} = 0;
17400:                     }
17401:                 }
17402:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
17403:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17404:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
17405:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17406:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
17407:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
17408:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
17409:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17410:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
17411:                 }
17412:             }
17413:         }
17414:     }
17415:     if (keys(%currsetting) > 0) {
17416:         foreach my $item (@contacts) {
17417:             if ($to{$item} ne $currsetting{$item}) {
17418:                 $changes{$item} = 1;
17419:             }
17420:         }
17421:         foreach my $type (@mailings) {
17422:             foreach my $item (@contacts) {
17423:                 if (ref($currsetting{$type}) eq 'HASH') {
17424:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
17425:                         push(@{$changes{$type}},$item);
17426:                     }
17427:                 } else {
17428:                     push(@{$changes{$type}},@{$newsetting{$type}});
17429:                 }
17430:             }
17431:             if ($others{$type} ne $currsetting{$type}{'others'}) {
17432:                 push(@{$changes{$type}},'others');
17433:             }
17434:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17435:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
17436:                     push(@{$changes{$type}},'bcc'); 
17437:                 }
17438:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
17439:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
17440:                     push(@{$changes{$type}},'include');
17441:                 }
17442:             }
17443:         }
17444:         if (ref($fields) eq 'ARRAY') {
17445:             if (ref($currsetting{'helpform'}) eq 'HASH') {
17446:                 foreach my $field (@{$fields}) {
17447:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
17448:                         push(@{$changes{'helpform'}},$field);
17449:                     }
17450:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17451:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
17452:                             push(@{$changes{'helpform'}},'maxsize');
17453:                         }
17454:                     }
17455:                 }
17456:             } else {
17457:                 foreach my $field (@{$fields}) {
17458:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17459:                         push(@{$changes{'helpform'}},$field);
17460:                     }
17461:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17462:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17463:                             push(@{$changes{'helpform'}},'maxsize');
17464:                         }
17465:                     }
17466:                 }
17467:             }
17468:         }
17469:         if (@statuses) {
17470:             if (ref($currsetting{'overrides'}) eq 'HASH') {
17471:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
17472:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
17473:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
17474:                             foreach my $item (@contacts,'bcc','others','include') {
17475:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
17476:                                     push(@{$changes{'overrides'}},$key);
17477:                                     last;
17478:                                 }
17479:                             }
17480:                         } else {
17481:                             push(@{$changes{'overrides'}},$key);
17482:                         }
17483:                     }
17484:                 }
17485:                 foreach my $key (@overrides) {
17486:                     unless (exists($currsetting{'overrides'}{$key})) {
17487:                         push(@{$changes{'overrides'}},$key);
17488:                     }
17489:                 }
17490:             } else {
17491:                 foreach my $key (@overrides) {
17492:                     push(@{$changes{'overrides'}},$key);
17493:                 }
17494:             }
17495:         }
17496:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
17497:             foreach my $key ('excluded','weights','threshold','sysmail') {
17498:                 if ($key eq 'excluded') {
17499:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17500:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
17501:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17502:                             (@{$currsetting{'lonstatus'}{$key}})) {
17503:                             my @diffs =
17504:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
17505:                                                                    $currsetting{'lonstatus'}{$key});
17506:                             if (@diffs) {
17507:                                 push(@{$changes{'lonstatus'}},$key);
17508:                             }
17509:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
17510:                             push(@{$changes{'lonstatus'}},$key);
17511:                         }
17512:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17513:                              (@{$currsetting{'lonstatus'}{$key}})) {
17514:                         push(@{$changes{'lonstatus'}},$key);
17515:                     }
17516:                 } elsif ($key eq 'weights') {
17517:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17518:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
17519:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17520:                             foreach my $type ('E','W','N','U') {
17521:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
17522:                                         $currsetting{'lonstatus'}{$key}{$type}) {
17523:                                     push(@{$changes{'lonstatus'}},$key);
17524:                                     last;
17525:                                 }
17526:                             }
17527:                         } else {
17528:                             foreach my $type ('E','W','N','U') {
17529:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
17530:                                     push(@{$changes{'lonstatus'}},$key);
17531:                                     last;
17532:                                 }
17533:                             }
17534:                         }
17535:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17536:                         foreach my $type ('E','W','N','U') {
17537:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
17538:                                 push(@{$changes{'lonstatus'}},$key);
17539:                                 last;
17540:                             }
17541:                         }
17542:                     }
17543:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
17544:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17545:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17546:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
17547:                                 push(@{$changes{'lonstatus'}},$key);
17548:                             }
17549:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
17550:                             push(@{$changes{'lonstatus'}},$key);
17551:                         }
17552:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17553:                         push(@{$changes{'lonstatus'}},$key);
17554:                     }
17555:                 }
17556:             }
17557:         } else {
17558:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17559:                 foreach my $key ('excluded','weights','threshold','sysmail') {
17560:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17561:                         push(@{$changes{'lonstatus'}},$key);
17562:                     }
17563:                 }
17564:             }
17565:         }
17566:     } else {
17567:         my %default;
17568:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
17569:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
17570:         $default{'errormail'} = 'adminemail';
17571:         $default{'packagesmail'} = 'adminemail';
17572:         $default{'helpdeskmail'} = 'supportemail';
17573:         $default{'otherdomsmail'} = 'supportemail';
17574:         $default{'lonstatusmail'} = 'adminemail';
17575:         $default{'requestsmail'} = 'adminemail';
17576:         $default{'updatesmail'} = 'adminemail';
17577:         $default{'hostipmail'} = 'adminemail';
17578:         foreach my $item (@contacts) {
17579:            if ($to{$item} ne $default{$item}) {
17580:                $changes{$item} = 1;
17581:            }
17582:         }
17583:         foreach my $type (@mailings) {
17584:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
17585:                 push(@{$changes{$type}},@{$newsetting{$type}});
17586:             }
17587:             if ($others{$type} ne '') {
17588:                 push(@{$changes{$type}},'others');
17589:             }
17590:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17591:                 if ($bcc{$type} ne '') {
17592:                     push(@{$changes{$type}},'bcc');
17593:                 }
17594:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
17595:                     push(@{$changes{$type}},'include');
17596:                 }
17597:             }
17598:         }
17599:         if (ref($fields) eq 'ARRAY') {
17600:             foreach my $field (@{$fields}) {
17601:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17602:                     push(@{$changes{'helpform'}},$field);
17603:                 }
17604:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17605:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17606:                         push(@{$changes{'helpform'}},'maxsize');
17607:                     }
17608:                 }
17609:             }
17610:         }
17611:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17612:             foreach my $key ('excluded','weights','threshold','sysmail') {
17613:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17614:                     push(@{$changes{'lonstatus'}},$key);
17615:                 }
17616:             }
17617:         }
17618:     }
17619:     foreach my $item (@toggles) {
17620:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
17621:             $changes{$item} = 1;
17622:         } elsif ((!$env{'form.'.$item}) &&
17623:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
17624:             $changes{$item} = 1;
17625:         }
17626:     }
17627:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
17628:                                              $dom);
17629:     if ($putresult eq 'ok') {
17630:         if (keys(%changes) > 0) {
17631:             &Apache::loncommon::devalidate_domconfig_cache($dom);
17632:             if (ref($lastactref) eq 'HASH') {
17633:                 $lastactref->{'domainconfig'} = 1;
17634:             }
17635:             my ($titles,$short_titles)  = &contact_titles();
17636:             $resulttext = &mt('Changes made:').'<ul>';
17637:             foreach my $item (@contacts) {
17638:                 if ($changes{$item}) {
17639:                     $resulttext .= '<li>'.$titles->{$item}.
17640:                                     &mt(' set to: ').
17641:                                     '<span class="LC_cusr_emph">'.
17642:                                     $to{$item}.'</span></li>';
17643:                 }
17644:             }
17645:             foreach my $type (@mailings) {
17646:                 if (ref($changes{$type}) eq 'ARRAY') {
17647:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17648:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
17649:                     } else {
17650:                         $resulttext .= '<li>'.$titles->{$type}.': ';
17651:                     }
17652:                     my @text;
17653:                     foreach my $item (@{$newsetting{$type}}) {
17654:                         push(@text,$short_titles->{$item});
17655:                     }
17656:                     if ($others{$type} ne '') {
17657:                         push(@text,$others{$type});
17658:                     }
17659:                     if (@text) {
17660:                         $resulttext .= '<span class="LC_cusr_emph">'.
17661:                                        join(', ',@text).'</span>';
17662:                     }
17663:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17664:                         if ($bcc{$type} ne '') {
17665:                             my $bcctext;
17666:                             if (@text) {
17667:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
17668:                             } else {
17669:                                 $bcctext = '(Bcc)';
17670:                             }
17671:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
17672:                         } elsif (!@text) {
17673:                             $resulttext .= &mt('No one');
17674:                         }
17675:                         if ($includestr{$type} ne '') {
17676:                             if ($includeloc{$type} eq 'b') {
17677:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
17678:                             } elsif ($includeloc{$type} eq 's') {
17679:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
17680:                             }
17681:                         }
17682:                     } elsif (!@text) {
17683:                         $resulttext .= &mt('No recipients');
17684:                     }
17685:                     $resulttext .= '</li>';
17686:                 }
17687:             }
17688:             if (ref($changes{'overrides'}) eq 'ARRAY') {
17689:                 my @deletions;
17690:                 foreach my $type (@{$changes{'overrides'}}) {
17691:                     if ($usertypeshash{$type}) {
17692:                         if (grep(/^\Q$type\E/,@overrides)) {
17693:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
17694:                                                       $usertypeshash{$type}).'<ul><li>';
17695:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
17696:                                 my @text;
17697:                                 foreach my $item (@contacts) {
17698:                                     if ($newsetting{'override_'.$type}{$item}) {
17699:                                         push(@text,$short_titles->{$item});
17700:                                     }
17701:                                 }
17702:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
17703:                                     push(@text,$newsetting{'override_'.$type}{'others'});
17704:                                 }
17705: 
17706:                                 if (@text) {
17707:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
17708:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
17709:                                 }
17710:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
17711:                                     my $bcctext;
17712:                                     if (@text) {
17713:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
17714:                                     } else {
17715:                                         $bcctext = '(Bcc)';
17716:                                     }
17717:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
17718:                                 } elsif (!@text) {
17719:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
17720:                                 }
17721:                                 $resulttext .= '</li>';
17722:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
17723:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
17724:                                     if ($loc eq 'b') {
17725:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
17726:                                     } elsif ($loc eq 's') {
17727:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
17728:                                     }
17729:                                 }
17730:                             }
17731:                             $resulttext .= '</li></ul></li>';
17732:                         } else {
17733:                             push(@deletions,$usertypeshash{$type});
17734:                         }
17735:                     }
17736:                 }
17737:                 if (@deletions) {
17738:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
17739:                                               join(', ',@deletions)).'</li>';
17740:                 }
17741:             }
17742:             my @offon = ('off','on');
17743:             my $corelink = &core_link_msu();
17744:             if ($changes{'reporterrors'}) {
17745:                 $resulttext .= '<li>'.
17746:                                &mt('E-mail error reports to [_1] set to "'.
17747:                                    $offon[$env{'form.reporterrors'}].'".',
17748:                                    $corelink).
17749:                                '</li>';
17750:             }
17751:             if ($changes{'reportupdates'}) {
17752:                 $resulttext .= '<li>'.
17753:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
17754:                                     $offon[$env{'form.reportupdates'}].'".',
17755:                                     $corelink).
17756:                                 '</li>';
17757:             }
17758:             if ($changes{'reportstatus'}) {
17759:                 $resulttext .= '<li>'.
17760:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
17761:                                     $offon[$env{'form.reportstatus'}].'".',
17762:                                     $corelink).
17763:                                 '</li>';
17764:             }
17765:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
17766:                 $resulttext .= '<li>'.
17767:                                &mt('Nightly status check e-mail settings').':<ul>';
17768:                 my (%defval,%use_def,%shown);
17769:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17770:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17771:                 $defval{'weights'} =
17772:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17773:                 $defval{'excluded'} = &mt('None');
17774:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17775:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17776:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17777:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17778:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17779:                             } elsif ($item eq 'weights') {
17780:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17781:                                     foreach my $type ('E','W','N','U') {
17782:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17783:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17784:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17785:                                         } else {
17786:                                             $shown{$item} .= $lonstatus_defs->{$type};
17787:                                         }
17788:                                         $shown{$item} .= ', ';
17789:                                     }
17790:                                     $shown{$item} =~ s/, $//;
17791:                                 } else {
17792:                                     $shown{$item} = $defval{$item};
17793:                                 }
17794:                             } elsif ($item eq 'excluded') {
17795:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17796:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17797:                                 } else {
17798:                                     $shown{$item} = $defval{$item};
17799:                                 }
17800:                             }
17801:                         } else {
17802:                             $shown{$item} = $defval{$item};
17803:                         }
17804:                     }
17805:                 } else {
17806:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17807:                         $shown{$item} = $defval{$item};
17808:                     }
17809:                 }
17810:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17811:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17812:                                           $shown{$item}).'</li>';
17813:                 }
17814:                 $resulttext .= '</ul></li>';
17815:             }
17816:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17817:                 my (@optional,@required,@unused,$maxsizechg);
17818:                 foreach my $field (@{$changes{'helpform'}}) {
17819:                     if ($field eq 'maxsize') {
17820:                         $maxsizechg = 1;
17821:                         next;
17822:                     }
17823:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17824:                         push(@optional,$field);
17825:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17826:                         push(@unused,$field);
17827:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17828:                         push(@required,$field);
17829:                     }
17830:                 }
17831:                 if (@optional) {
17832:                     $resulttext .= '<li>'.
17833:                                    &mt('Help form fields changed to "Optional": [_1].',
17834:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17835:                                    '</li>';
17836:                 }
17837:                 if (@required) {
17838:                     $resulttext .= '<li>'.
17839:                                    &mt('Help form fields changed to "Required": [_1].',
17840:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17841:                                    '</li>';
17842:                 }
17843:                 if (@unused) {
17844:                     $resulttext .= '<li>'.
17845:                                    &mt('Help form fields changed to "Not shown": [_1].',
17846:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17847:                                    '</li>';
17848:                 }
17849:                 if ($maxsizechg) {
17850:                     $resulttext .= '<li>'.
17851:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17852:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17853:                                    '</li>';
17854:                 }
17855:             }
17856:             $resulttext .= '</ul>';
17857:         } else {
17858:             $resulttext = &mt('No changes made to contacts and form settings');
17859:         }
17860:     } else {
17861:         $resulttext = '<span class="LC_error">'.
17862:             &mt('An error occurred: [_1].',$putresult).'</span>';
17863:     }
17864:     return $resulttext;
17865: }
17866: 
17867: sub modify_privacy {
17868:     my ($dom,$lastactref,%domconfig) = @_;
17869:     my ($resulttext,%current,%changes);
17870:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17871:         %current = %{$domconfig{'privacy'}};
17872:     }
17873:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17874:     my @items = ('domain','author','course','community');
17875:     my %names = &Apache::lonlocal::texthash (
17876:                    domain => 'Assigned domain role(s)',
17877:                    author => 'Assigned co-author role(s)',
17878:                    course => 'Assigned course role(s)',
17879:                    community => 'Assigned community role(s)',
17880:                 );
17881:     my %roles = &Apache::lonlocal::texthash (
17882:                    domain => 'Domain role',
17883:                    author => 'Co-author role',
17884:                    course => 'Course role',
17885:                    community => 'Community role',
17886:                 );
17887:     my %titles = &Apache::lonlocal::texthash (
17888:                   approval => 'Approval for role in different domain',
17889:                   othdom   => 'User information available in other domain',
17890:                   priv     => 'Information viewable by privileged user in same domain',
17891:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17892:                   instdom  => 'Other domain shares institution/provider',
17893:                   extdom   => 'Other domain has different institution/provider',
17894:                   none     => 'Not allowed',
17895:                   user     => 'User authorizes',
17896:                   domain   => 'Domain Coordinator authorizes',
17897:                   auto     => 'Unrestricted',
17898:                   notify   => 'Notify when role needs authorization',
17899:     );
17900:     my %fieldnames = &Apache::lonlocal::texthash (
17901:                         id => 'Student/Employee ID',
17902:                         permanentemail => 'E-mail address',
17903:                         lastname => 'Last Name',
17904:                         firstname => 'First Name',
17905:                         middlename => 'Middle Name',
17906:                         generation => 'Generation',
17907:     );
17908:     my ($othertitle,$usertypes,$types) =
17909:         &Apache::loncommon::sorted_inst_types($dom);
17910:     my (%by_ip,%by_location,@intdoms,@instdoms);
17911:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17912: 
17913:     my %privacyhash = (
17914:                        'approval' => {
17915:                                        instdom => {},
17916:                                        extdom  => {},
17917:                                      },
17918:                        'othdom'   => {},
17919:                        'priv'     => {},
17920:                        'unpriv'   => {},
17921:                       );
17922:     foreach my $item (@items) {
17923:         if (@instdoms > 1) {
17924:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17925:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17926:             }
17927:             if (ref($current{'approval'}) eq 'HASH') {
17928:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17929:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17930:                         $changes{'approval'} = 1;
17931:                     }
17932:                 }
17933:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17934:                 $changes{'approval'} = 1;
17935:             }
17936:         }
17937:         if (keys(%by_location) > 0) {
17938:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17939:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17940:             }
17941:             if (ref($current{'approval'}) eq 'HASH') {
17942:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17943:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17944:                         $changes{'approval'} = 1;
17945:                     }
17946:                 }
17947:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17948:                 $changes{'approval'} = 1;
17949:             }
17950:         }
17951:         foreach my $status ('priv','unpriv') {
17952:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17953:             my @newvalues;
17954:             foreach my $field (@possibles) {
17955:                 if (grep(/^\Q$field\E$/,@fields)) {
17956:                     $privacyhash{$status}{$item}{$field} = 1;
17957:                     push(@newvalues,$field);
17958:                 }
17959:             }
17960:             @newvalues = sort(@newvalues);
17961:             if (ref($current{$status}) eq 'HASH') {
17962:                 if (ref($current{$status}{$item}) eq 'HASH') {
17963:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17964:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17965:                     if (@diffs > 0) {
17966:                         $changes{$status} = 1;
17967:                     }
17968:                 }
17969:             } else {
17970:                 my @stdfields;
17971:                 foreach my $field (@fields) {
17972:                     if ($field eq 'id') {
17973:                         next if ($status eq 'unpriv');
17974:                         next if (($status eq 'priv') && ($item eq 'community'));
17975:                     }
17976:                     push(@stdfields,$field);
17977:                 }
17978:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17979:                 if (@diffs > 0) {
17980:                     $changes{$status} = 1;
17981:                 }
17982:             }
17983:         }
17984:     }
17985:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17986:         my @statuses;
17987:         if (ref($types) eq 'ARRAY') {
17988:             @statuses = @{$types};
17989:         }
17990:         foreach my $type (@statuses,'default') {
17991:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17992:             my @newvalues;
17993:             foreach my $field (sort(@possfields)) {
17994:                 if (grep(/^\Q$field\E$/,@fields)) {
17995:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17996:                     push(@newvalues,$field);
17997:                 }
17998:             }
17999:             @newvalues = sort(@newvalues);
18000:             if (ref($current{'othdom'}) eq 'HASH') {
18001:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
18002:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
18003:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
18004:                     if (@diffs > 0) {
18005:                         $changes{'othdom'} = 1;
18006:                     }
18007:                 }
18008:             } else {
18009:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
18010:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
18011:                 if (@diffs > 0) {
18012:                     $changes{'othdom'} = 1;
18013:                 }
18014:             }
18015:         }
18016:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
18017:         my %notify;
18018:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
18019:             if (exists($domcoords{$possdc})) {
18020:                 $notify{$possdc} = 1;
18021:             }
18022:         }
18023:         my $notify = join(',',sort(keys(%notify)));
18024:         if ($current{'notify'} ne $notify) {
18025:             $changes{'notify'} = 1;
18026:         }
18027:         $privacyhash{'notify'} = $notify;
18028:     }
18029:     my %confighash = (
18030:                         privacy => \%privacyhash,
18031:                      );
18032:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18033:     if ($putresult eq 'ok') {
18034:         if (keys(%changes) > 0) {
18035:             $resulttext = &mt('Changes made: ').'<ul>';
18036:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
18037:                 if ($changes{$key}) {
18038:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18039:                     if ($key eq 'approval') {
18040:                         if (keys(%{$privacyhash{$key}{instdom}})) {
18041:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
18042:                             foreach my $item (@items) {
18043:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
18044:                             }
18045:                             $resulttext .= '</ul></li>';
18046:                         }
18047:                         if (keys(%{$privacyhash{$key}{extdom}})) {
18048:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
18049:                             foreach my $item (@items) {
18050:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
18051:                             }
18052:                             $resulttext .= '</ul></li>';
18053:                         }
18054:                     } elsif ($key eq 'notify') {
18055:                         if ($privacyhash{$key}) {
18056:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
18057:                                 my ($dcname,$dcdom) = split(/:/,$dc);
18058:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
18059:                             }
18060:                         } else {
18061:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
18062:                         }
18063:                     } elsif ($key eq 'othdom') {
18064:                         my @statuses;
18065:                         if (ref($types) eq 'ARRAY') {
18066:                             @statuses = @{$types};
18067:                         }
18068:                         if (ref($privacyhash{$key}) eq 'HASH') {
18069:                             foreach my $status (@statuses,'default') {
18070:                                 if ($status eq 'default') {
18071:                                     $resulttext .= '<li>'.$othertitle.': ';
18072:                                 } elsif (ref($usertypes) eq 'HASH') {
18073:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
18074:                                 } else {
18075:                                     next;
18076:                                 }
18077:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
18078:                                     if (keys(%{$privacyhash{$key}{$status}})) {
18079:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
18080:                                     } else {
18081:                                         $resulttext .= &mt('none');
18082:                                     }
18083:                                 }
18084:                                 $resulttext .= '</li>';
18085:                             }
18086:                         }
18087:                     } else {
18088:                         foreach my $item (@items) {
18089:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
18090:                                 $resulttext .= '<li>'.$names{$item}.': ';
18091:                                 if (keys(%{$privacyhash{$key}{$item}})) {
18092:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
18093:                                 } else {
18094:                                     $resulttext .= &mt('none');
18095:                                 }
18096:                                 $resulttext .= '</li>';
18097:                             }
18098:                         }
18099:                     }
18100:                     $resulttext .= '</ul></li>';
18101:                 }
18102:             }
18103:             $resulttext .= '</ul>';
18104:             if ($changes{'approval'}) {
18105:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18106:                 delete($domdefaults{'userapprovals'});
18107:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
18108:                     foreach my $domtype ('instdom','extdom') {
18109:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
18110:                             foreach my $roletype ('domain','author','course','community') {
18111:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
18112:                                     $domdefaults{'userapprovals'} = 1;
18113:                                     last;
18114:                                 }
18115:                             }
18116:                         }
18117:                         last if ($domdefaults{'userapprovals'});               
18118:                     }
18119:                 }
18120:                 my $cachetime = 24*60*60;
18121:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18122:                 if (ref($lastactref) eq 'HASH') {
18123:                     $lastactref->{'domdefaults'} = 1;
18124:                 }
18125:             }
18126:         } else {
18127:             $resulttext = &mt('No changes made to user information settings');
18128:         }
18129:     } else {
18130:         $resulttext = '<span class="LC_error">'.
18131:             &mt('An error occurred: [_1]',$putresult).'</span>';
18132:     }
18133:     return $resulttext;
18134: }
18135: 
18136: sub modify_passwords {
18137:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18138:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
18139:         $updatedefaults,$updateconf);
18140:     my $customfn = 'resetpw.html';
18141:     if (ref($domconfig{'passwords'}) eq 'HASH') {
18142:         %current = %{$domconfig{'passwords'}};
18143:     }
18144:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18145:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18146:     if (ref($types) eq 'ARRAY') {
18147:         @oktypes = @{$types};
18148:     }
18149:     push(@oktypes,'default');
18150: 
18151:     my %titles = &Apache::lonlocal::texthash (
18152:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
18153:         intauth_check  => 'Check bcrypt cost if authenticated',
18154:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
18155:         permanent      => 'Permanent e-mail address',
18156:         critical       => 'Critical notification address',
18157:         notify         => 'Notification address',
18158:         min            => 'Minimum password length',
18159:         max            => 'Maximum password length',
18160:         chars          => 'Required characters',
18161:         expire         => 'Password expiration (days)',
18162:         numsaved       => 'Number of previous passwords to save',
18163:         reset          => 'Resetting Forgotten Password',
18164:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
18165:         rules          => 'Rules for LON-CAPA Passwords',
18166:         crsownerchg    => 'Course Owner Changing Student Passwords',
18167:         username       => 'Username',
18168:         email          => 'E-mail address',
18169:     );
18170: 
18171: #
18172: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
18173: #
18174:     my (%curr_defaults,%save_defaults);
18175:     if (ref($domconfig{'defaults'}) eq 'HASH') {
18176:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
18177:             if ($key =~ /^intauth_(cost|check|switch)$/) {
18178:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
18179:             } else {
18180:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
18181:             }
18182:         }
18183:     }
18184:     my %staticdefaults = (
18185:         'resetlink'      => 2,
18186:         'resetcase'      => \@oktypes,
18187:         'resetprelink'   => 'both',
18188:         'resetemail'     => ['critical','notify','permanent'],
18189:         'intauth_cost'   => 10,
18190:         'intauth_check'  => 0,
18191:         'intauth_switch' => 0,
18192:     );
18193:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18194:     foreach my $type (@oktypes) {
18195:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
18196:     }
18197:     my $linklife = $env{'form.passwords_link'};
18198:     $linklife =~ s/^\s+|\s+$//g;
18199:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
18200:         $newvalues{'resetlink'} = $linklife;
18201:         if ($current{'resetlink'}) {
18202:             if ($current{'resetlink'} ne $linklife) {
18203:                 $changes{'reset'} = 1;
18204:             }
18205:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18206:             if ($staticdefaults{'resetlink'} ne $linklife) {
18207:                 $changes{'reset'} = 1;
18208:             }
18209:         }
18210:     } elsif ($current{'resetlink'}) {
18211:         $changes{'reset'} = 1;
18212:     }
18213:     my @casesens;
18214:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
18215:     foreach my $case (sort(@posscase)) {
18216:         if (grep(/^\Q$case\E$/,@oktypes)) {
18217:             push(@casesens,$case);
18218:         }
18219:     }
18220:     $newvalues{'resetcase'} = \@casesens;
18221:     if (ref($current{'resetcase'}) eq 'ARRAY') {
18222:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
18223:         if (@diffs > 0) {
18224:             $changes{'reset'} = 1;
18225:         }
18226:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18227:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
18228:         if (@diffs > 0) {
18229:             $changes{'reset'} = 1;
18230:         }
18231:     }
18232:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
18233:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
18234:         if (exists($current{'resetprelink'})) {
18235:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
18236:                 $changes{'reset'} = 1;
18237:             }
18238:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18239:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
18240:                 $changes{'reset'} = 1;
18241:             }
18242:         }
18243:     } elsif ($current{'resetprelink'}) {
18244:         $changes{'reset'} = 1;
18245:     }
18246:     foreach my $type (@oktypes) {
18247:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
18248:         my @postlink;
18249:         foreach my $item (sort(@possplink)) {
18250:             if ($item =~ /^(email|username)$/) {
18251:                 push(@postlink,$item);
18252:             }
18253:         }
18254:         $newvalues{'resetpostlink'}{$type} = \@postlink;
18255:         unless ($changes{'reset'}) {
18256:             if (ref($current{'resetpostlink'}) eq 'HASH') {
18257:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
18258:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
18259:                     if (@diffs > 0) {
18260:                         $changes{'reset'} = 1;
18261:                     }
18262:                 } else {
18263:                     $changes{'reset'} = 1;
18264:                 }
18265:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18266:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
18267:                 if (@diffs > 0) {
18268:                     $changes{'reset'} = 1;
18269:                 }
18270:             }
18271:         }
18272:     }
18273:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
18274:     my @resetemail;
18275:     foreach my $item (sort(@possemailsrc)) {
18276:         if ($item =~ /^(permanent|critical|notify)$/) {
18277:             push(@resetemail,$item);
18278:         }
18279:     }
18280:     $newvalues{'resetemail'} = \@resetemail;
18281:     unless ($changes{'reset'}) {
18282:         if (ref($current{'resetemail'}) eq 'ARRAY') {
18283:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
18284:             if (@diffs > 0) {
18285:                 $changes{'reset'} = 1;
18286:             }
18287:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18288:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
18289:             if (@diffs > 0) {
18290:                 $changes{'reset'} = 1;
18291:             }
18292:         }
18293:     }
18294:     if ($env{'form.passwords_stdtext'} == 0) {
18295:         $newvalues{'resetremove'} = 1;
18296:         unless ($current{'resetremove'}) {
18297:             $changes{'reset'} = 1;
18298:         }
18299:     } elsif ($current{'resetremove'}) {
18300:         $changes{'reset'} = 1;
18301:     }
18302:     if ($env{'form.passwords_customfile.filename'} ne '') {
18303:         my $servadm = $r->dir_config('lonAdmEMail');
18304:         my ($configuserok,$author_ok,$switchserver) =
18305:             &config_check($dom,$confname,$servadm);
18306:         my $error;
18307:         if ($configuserok eq 'ok') {
18308:             if ($switchserver) {
18309:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
18310:             } else {
18311:                 if ($author_ok eq 'ok') {
18312:                     my $modified = [];
18313:                     my ($result,$customurl) =
18314:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
18315:                                                                 $confname,'customtext/resetpw','','',$customfn,
18316:                                                                 $modified);
18317:                     if ($result eq 'ok') {
18318:                         $newvalues{'resetcustom'} = $customurl;
18319:                         $changes{'reset'} = 1;
18320:                         &update_modify_urls($r,$modified);
18321:                     } else {
18322:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
18323:                     }
18324:                 } else {
18325:                     $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);
18326:                 }
18327:             }
18328:         } else {
18329:             $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);
18330:         }
18331:         if ($error) {
18332:             &Apache::lonnet::logthis($error);
18333:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18334:         }
18335:     } elsif ($current{'resetcustom'}) {
18336:         if ($env{'form.passwords_custom_del'}) {
18337:             $changes{'reset'} = 1;
18338:         } else {
18339:             $newvalues{'resetcustom'} = $current{'resetcustom'};
18340:         }
18341:     }
18342:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
18343:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
18344:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
18345:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
18346:             $changes{'intauth'} = 1;
18347:         }
18348:     } else {
18349:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
18350:     }
18351:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
18352:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
18353:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
18354:             $changes{'intauth'} = 1;
18355:         }
18356:     } else {
18357:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18358:     }
18359:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
18360:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
18361:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
18362:             $changes{'intauth'} = 1;
18363:         }
18364:     } else {
18365:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18366:     }
18367:     foreach my $item ('cost','check','switch') {
18368:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
18369:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
18370:             $updatedefaults = 1;
18371:         }
18372:     }
18373:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
18374:     my %crsownerchg = (
18375:                         by => [],
18376:                         for => [],
18377:                       );
18378:     foreach my $item ('by','for') {
18379:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
18380:         foreach my $type (sort(@posstypes)) {
18381:             if (grep(/^\Q$type\E$/,@oktypes)) {
18382:                 push(@{$crsownerchg{$item}},$type);
18383:             }
18384:         }
18385:     }
18386:     $newvalues{'crsownerchg'} = \%crsownerchg;
18387:     if (ref($current{'crsownerchg'}) eq 'HASH') {
18388:         foreach my $item ('by','for') {
18389:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
18390:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
18391:                 if (@diffs > 0) {
18392:                     $changes{'crsownerchg'} = 1;
18393:                     last;
18394:                 }
18395:             }
18396:         }
18397:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
18398:         foreach my $item ('by','for') {
18399:             if (@{$crsownerchg{$item}} > 0) {
18400:                 $changes{'crsownerchg'} = 1;
18401:                 last;
18402:             }
18403:         }
18404:     }
18405: 
18406:     my %confighash = (
18407:                         defaults  => \%save_defaults,
18408:                         passwords => \%newvalues,
18409:                      );
18410:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
18411: 
18412:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18413:     if ($putresult eq 'ok') {
18414:         if (keys(%changes) > 0) {
18415:             $resulttext = &mt('Changes made: ').'<ul>';
18416:             foreach my $key ('reset','intauth','rules','crsownerchg') {
18417:                 if ($changes{$key}) {
18418:                     unless ($key eq 'intauth') {
18419:                         $updateconf = 1;
18420:                     }
18421:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18422:                     if ($key eq 'reset') {
18423:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
18424:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
18425:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
18426:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
18427:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
18428:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
18429:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
18430:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
18431:                             }
18432:                         } else {
18433:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
18434:                         }
18435:                         if ($confighash{'passwords'}{'resetlink'}) {
18436:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
18437:                         } else {
18438:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
18439:                                                   &mt('Will default to 2 hours').'</li>';
18440:                         }
18441:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
18442:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
18443:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
18444:                             } else {
18445:                                 my $casesens;
18446:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
18447:                                     if ($type eq 'default') {
18448:                                         $casesens .= $othertitle.', ';
18449:                                     } elsif ($usertypes->{$type} ne '') {
18450:                                         $casesens .= $usertypes->{$type}.', ';
18451:                                     }
18452:                                 }
18453:                                 $casesens =~ s/\Q, \E$//;
18454:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
18455:                             }
18456:                         } else {
18457:                             $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>';
18458:                         }
18459:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
18460:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
18461:                         } else {
18462:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
18463:                         }
18464:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
18465:                             my $output;
18466:                             if (ref($types) eq 'ARRAY') {
18467:                                 foreach my $type (@{$types}) {
18468:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
18469:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
18470:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
18471:                                         } else {
18472:                                             $output .= $usertypes->{$type}.' -- '.
18473:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
18474:                                         }
18475:                                     }
18476:                                 }
18477:                             }
18478:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
18479:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
18480:                                     $output .= $othertitle.' -- '.&mt('none');
18481:                                 } else {
18482:                                     $output .= $othertitle.' -- '.
18483:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
18484:                                 }
18485:                             }
18486:                             if ($output) {
18487:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
18488:                             } else {
18489:                                 $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>';
18490:                             }
18491:                         } else {
18492:                             $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>';
18493:                         }
18494:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
18495:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
18496:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
18497:                             } else {
18498:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18499:                             }
18500:                         } else {
18501:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18502:                         }
18503:                         if ($confighash{'passwords'}{'resetremove'}) {
18504:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
18505:                         } else {
18506:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
18507:                         }
18508:                         if ($confighash{'passwords'}{'resetcustom'}) {
18509:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
18510:                                                                             &mt('custom text'),600,500,undef,undef,
18511:                                                                             undef,undef,'background-color:#ffffff');
18512:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
18513:                         } else {
18514:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
18515:                         }
18516:                     } elsif ($key eq 'intauth') {
18517:                         foreach my $item ('cost','switch','check') {
18518:                             my $value = $save_defaults{$key.'_'.$item};
18519:                             if ($item eq 'switch') {
18520:                                 my %optiondesc = &Apache::lonlocal::texthash (
18521:                                                      0 => 'No',
18522:                                                      1 => 'Yes',
18523:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
18524:                                                  );
18525:                                 if ($value =~ /^(0|1|2)$/) {
18526:                                     $value = $optiondesc{$value};
18527:                                 } else {
18528:                                     $value = &mt('none -- defaults to No');
18529:                                 }
18530:                             } elsif ($item eq 'check') {
18531:                                 my %optiondesc = &Apache::lonlocal::texthash (
18532:                                                      0 => 'No',
18533:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
18534:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
18535:                                                  );
18536:                                 if ($value =~ /^(0|1|2)$/) {
18537:                                     $value = $optiondesc{$value};
18538:                                 } else {
18539:                                     $value = &mt('none -- defaults to No');
18540:                                 }
18541:                             }
18542:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
18543:                         }
18544:                     } elsif ($key eq 'rules') {
18545:                         foreach my $rule ('min','max','expire','numsaved') {
18546:                             if ($confighash{'passwords'}{$rule} eq '') {
18547:                                 if ($rule eq 'min') {
18548:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
18549:                                                    ' '.&mt('Default of [_1] will be used',
18550:                                                            $Apache::lonnet::passwdmin).'</li>';
18551:                                 } else {
18552:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
18553:                                 }
18554:                             } else {
18555:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
18556:                             }
18557:                         }
18558:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
18559:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
18560:                                 my %rulenames = &Apache::lonlocal::texthash(
18561:                                                      uc => 'At least one upper case letter',
18562:                                                      lc => 'At least one lower case letter',
18563:                                                      num => 'At least one number',
18564:                                                      spec => 'At least one non-alphanumeric',
18565:                                                    );
18566:                                 my $needed = '<ul><li>'.
18567:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
18568:                                              '</li></ul>';
18569:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
18570:                             } else {
18571:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18572:                             }
18573:                         } else {
18574:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18575:                         }
18576:                     } elsif ($key eq 'crsownerchg') {
18577:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
18578:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
18579:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
18580:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18581:                             } else {
18582:                                 my %crsownerstr;
18583:                                 foreach my $item ('by','for') {
18584:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
18585:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
18586:                                             if ($type eq 'default') {
18587:                                                 $crsownerstr{$item} .= $othertitle.', ';
18588:                                             } elsif ($usertypes->{$type} ne '') {
18589:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
18590:                                             }
18591:                                         }
18592:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
18593:                                     }
18594:                                 }
18595:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
18596:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
18597:                             }
18598:                         } else {
18599:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18600:                         }
18601:                     }
18602:                     $resulttext .= '</ul></li>';
18603:                 }
18604:             }
18605:             $resulttext .= '</ul>';
18606:         } else {
18607:             $resulttext = &mt('No changes made to password settings');
18608:         }
18609:         my $cachetime = 24*60*60;
18610:         if ($updatedefaults) {
18611:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18612:             if (ref($lastactref) eq 'HASH') {
18613:                 $lastactref->{'domdefaults'} = 1;
18614:             }
18615:         }
18616:         if ($updateconf) {
18617:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
18618:             if (ref($lastactref) eq 'HASH') {
18619:                 $lastactref->{'passwdconf'} = 1;
18620:             }
18621:         }
18622:     } else {
18623:         $resulttext = '<span class="LC_error">'.
18624:             &mt('An error occurred: [_1]',$putresult).'</span>';
18625:     }
18626:     if ($errors) {
18627:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18628:                        $errors.'</ul></p>';
18629:     }
18630:     return $resulttext;
18631: }
18632: 
18633: sub password_rule_changes {
18634:     my ($prefix,$newvalues,$current,$changes) = @_;
18635:     return unless ((ref($newvalues) eq 'HASH') &&
18636:                    (ref($current) eq 'HASH') &&
18637:                    (ref($changes) eq 'HASH'));
18638:     my (@rules,%staticdefaults);
18639:     if ($prefix eq 'passwords') {
18640:         @rules = ('min','max','expire','numsaved');
18641:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
18642:         @rules = ('min','max');
18643:     }
18644:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18645:     foreach my $rule (@rules) {
18646:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
18647:         my $ruleok;
18648:         if ($rule eq 'expire') {
18649:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
18650:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18651:                 $ruleok = 1;
18652:             }
18653:         } elsif ($rule eq 'min') {
18654:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
18655:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
18656:                     $ruleok = 1;
18657:                 }
18658:             }
18659:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
18660:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18661:             $ruleok = 1;
18662:         }
18663:         if ($ruleok) {
18664:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
18665:             if (exists($current->{$rule})) {
18666:                 if ($newvalues->{$rule} ne $current->{$rule}) {
18667:                     $changes->{'rules'} = 1;
18668:                 }
18669:             } elsif ($rule eq 'min') {
18670:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
18671:                     $changes->{'rules'} = 1;
18672:                 }
18673:             } else {
18674:                 $changes->{'rules'} = 1;
18675:             }
18676:         } elsif (exists($current->{$rule})) {
18677:             $changes->{'rules'} = 1;
18678:         }
18679:     }
18680:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
18681:     my @chars;
18682:     foreach my $item (sort(@posschars)) {
18683:         if ($item =~ /^(uc|lc|num|spec)$/) {
18684:             push(@chars,$item);
18685:         }
18686:     }
18687:     $newvalues->{'chars'} = \@chars;
18688:     unless ($changes->{'rules'}) {
18689:         if (ref($current->{'chars'}) eq 'ARRAY') {
18690:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
18691:             if (@diffs > 0) {
18692:                 $changes->{'rules'} = 1;
18693:             }
18694:         } else {
18695:             if (@chars > 0) {
18696:                 $changes->{'rules'} = 1;
18697:             }
18698:         }
18699:     }
18700:     return;
18701: }
18702: 
18703: sub modify_usercreation {
18704:     my ($dom,%domconfig) = @_;
18705:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
18706:     my $warningmsg;
18707:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18708:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18709:             if ($key eq 'cancreate') {
18710:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18711:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18712:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
18713:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18714:                         } else {
18715:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18716:                         }
18717:                     }
18718:                 }
18719:             } elsif ($key eq 'email_rule') {
18720:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18721:             } else {
18722:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18723:             }
18724:         }
18725:     }
18726:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
18727:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
18728:     my @contexts = ('author','course','requestcrs');
18729:     foreach my $item(@contexts) {
18730:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
18731:     }
18732:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18733:         foreach my $item (@contexts) {
18734:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
18735:                 push(@{$changes{'cancreate'}},$item);
18736:             }
18737:         }
18738:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
18739:         foreach my $item (@contexts) {
18740:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
18741:                 if ($cancreate{$item} ne 'any') {
18742:                     push(@{$changes{'cancreate'}},$item);
18743:                 }
18744:             } else {
18745:                 if ($cancreate{$item} ne 'none') {
18746:                     push(@{$changes{'cancreate'}},$item);
18747:                 }
18748:             }
18749:         }
18750:     } else {
18751:         foreach my $item (@contexts)  {
18752:             push(@{$changes{'cancreate'}},$item);
18753:         }
18754:     }
18755: 
18756:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
18757:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
18758:             if (!grep(/^\Q$type\E$/,@username_rule)) {
18759:                 push(@{$changes{'username_rule'}},$type);
18760:             }
18761:         }
18762:         foreach my $type (@username_rule) {
18763:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
18764:                 push(@{$changes{'username_rule'}},$type);
18765:             }
18766:         }
18767:     } else {
18768:         push(@{$changes{'username_rule'}},@username_rule);
18769:     }
18770: 
18771:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18772:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18773:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18774:                 push(@{$changes{'id_rule'}},$type);
18775:             }
18776:         }
18777:         foreach my $type (@id_rule) {
18778:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18779:                 push(@{$changes{'id_rule'}},$type);
18780:             }
18781:         }
18782:     } else {
18783:         push(@{$changes{'id_rule'}},@id_rule);
18784:     }
18785: 
18786:     my @authen_contexts = ('author','course','domain');
18787:     my @authtypes = ('int','krb4','krb5','loc','lti');
18788:     my %authhash;
18789:     foreach my $item (@authen_contexts) {
18790:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18791:         foreach my $auth (@authtypes) {
18792:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18793:                 $authhash{$item}{$auth} = 1;
18794:             } else {
18795:                 $authhash{$item}{$auth} = 0;
18796:             }
18797:         }
18798:     }
18799:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18800:         foreach my $item (@authen_contexts) {
18801:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18802:                 foreach my $auth (@authtypes) {
18803:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18804:                         push(@{$changes{'authtypes'}},$item);
18805:                         last;
18806:                     }
18807:                 }
18808:             }
18809:         }
18810:     } else {
18811:         foreach my $item (@authen_contexts) {
18812:             push(@{$changes{'authtypes'}},$item);
18813:         }
18814:     }
18815: 
18816:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18817:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18818:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18819:     $save_usercreate{'id_rule'} = \@id_rule;
18820:     $save_usercreate{'username_rule'} = \@username_rule,
18821:     $save_usercreate{'authtypes'} = \%authhash;
18822: 
18823:     my %usercreation_hash =  (
18824:         usercreation     => \%save_usercreate,
18825:     );
18826: 
18827:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18828:                                              $dom);
18829: 
18830:     if ($putresult eq 'ok') {
18831:         if (keys(%changes) > 0) {
18832:             $resulttext = &mt('Changes made:').'<ul>';
18833:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18834:                 my %lt = &usercreation_types();
18835:                 foreach my $type (@{$changes{'cancreate'}}) {
18836:                     my $chgtext = $lt{$type}.', ';
18837:                     if ($cancreate{$type} eq 'none') {
18838:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18839:                     } elsif ($cancreate{$type} eq 'any') {
18840:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18841:                     } elsif ($cancreate{$type} eq 'official') {
18842:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18843:                     } elsif ($cancreate{$type} eq 'unofficial') {
18844:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18845:                     }
18846:                     $resulttext .= '<li>'.$chgtext.'</li>';
18847:                 }
18848:             }
18849:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18850:                 my ($rules,$ruleorder) = 
18851:                     &Apache::lonnet::inst_userrules($dom,'username');
18852:                 my $chgtext = '<ul>';
18853:                 foreach my $type (@username_rule) {
18854:                     if (ref($rules->{$type}) eq 'HASH') {
18855:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18856:                     }
18857:                 }
18858:                 $chgtext .= '</ul>';
18859:                 if (@username_rule > 0) {
18860:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18861:                 } else {
18862:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18863:                 }
18864:             }
18865:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18866:                 my ($idrules,$idruleorder) = 
18867:                     &Apache::lonnet::inst_userrules($dom,'id');
18868:                 my $chgtext = '<ul>';
18869:                 foreach my $type (@id_rule) {
18870:                     if (ref($idrules->{$type}) eq 'HASH') {
18871:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18872:                     }
18873:                 }
18874:                 $chgtext .= '</ul>';
18875:                 if (@id_rule > 0) {
18876:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18877:                 } else {
18878:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18879:                 }
18880:             }
18881:             my %authname = &authtype_names();
18882:             my %context_title = &context_names();
18883:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18884:                 my $chgtext = '<ul>';
18885:                 foreach my $type (@{$changes{'authtypes'}}) {
18886:                     my @allowed;
18887:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18888:                     foreach my $auth (@authtypes) {
18889:                         if ($authhash{$type}{$auth}) {
18890:                             push(@allowed,$authname{$auth});
18891:                         }
18892:                     }
18893:                     if (@allowed > 0) {
18894:                         $chgtext .= join(', ',@allowed).'</li>';
18895:                     } else {
18896:                         $chgtext .= &mt('none').'</li>';
18897:                     }
18898:                 }
18899:                 $chgtext .= '</ul>';
18900:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18901:                 $resulttext .= '</li>';
18902:             }
18903:             $resulttext .= '</ul>';
18904:         } else {
18905:             $resulttext = &mt('No changes made to user creation settings');
18906:         }
18907:     } else {
18908:         $resulttext = '<span class="LC_error">'.
18909:             &mt('An error occurred: [_1]',$putresult).'</span>';
18910:     }
18911:     if ($warningmsg ne '') {
18912:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18913:     }
18914:     return $resulttext;
18915: }
18916: 
18917: sub modify_selfcreation {
18918:     my ($dom,$lastactref,%domconfig) = @_;
18919:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18920:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18921:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18922:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18923:     if (ref($typesref) eq 'ARRAY') {
18924:         @types = @{$typesref};
18925:     }
18926:     if (ref($usertypesref) eq 'HASH') {
18927:         %usertypes = %{$usertypesref};
18928:     }
18929:     $usertypes{'default'} = $othertitle;
18930: #
18931: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18932: #
18933:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18934:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18935:             if ($key eq 'cancreate') {
18936:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18937:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18938:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18939:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18940:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18941:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18942:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18943:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18944:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18945:                         } else {
18946:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18947:                         }
18948:                     }
18949:                 }
18950:             } elsif ($key eq 'email_rule') {
18951:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18952:             } else {
18953:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18954:             }
18955:         }
18956:     }
18957: #
18958: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18959: #
18960:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18961:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18962:             if ($key eq 'selfcreate') {
18963:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18964:             } else {
18965:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18966:             }
18967:         }
18968:     }
18969: #
18970: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18971: #
18972:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18973:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18974:             if ($key eq 'inststatusguest') {
18975:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18976:             } else {
18977:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18978:             }
18979:         }
18980:     }
18981: 
18982:     my @contexts = ('selfcreate');
18983:     @{$cancreate{'selfcreate'}} = ();
18984:     %{$cancreate{'emailusername'}} = ();
18985:     if (@types) {
18986:         @{$cancreate{'statustocreate'}} = ();
18987:     }
18988:     %{$cancreate{'selfcreateprocessing'}} = ();
18989:     %{$cancreate{'shibenv'}} = ();
18990:     %{$cancreate{'emailverified'}} = ();
18991:     %{$cancreate{'emailoptions'}} = ();
18992:     %{$cancreate{'emaildomain'}} = ();
18993:     my %selfcreatetypes = (
18994:                              sso   => 'users authenticated by institutional single sign on',
18995:                              login => 'users authenticated by institutional log-in',
18996:                              email => 'users verified by e-mail',
18997:                           );
18998: #
18999: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
19000: # is permitted.
19001: #
19002:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
19003: 
19004:     my (@statuses,%email_rule);
19005:     foreach my $item ('login','sso','email') {
19006:         if ($item eq 'email') {
19007:             if ($env{'form.cancreate_email'}) {
19008:                 if (@types) {
19009:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
19010:                     foreach my $status (@poss_statuses) {
19011:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
19012:                             push(@statuses,$status);
19013:                         }
19014:                     }
19015:                     $save_inststatus{'inststatusguest'} = \@statuses;
19016:                 } else {
19017:                     push(@statuses,'default');
19018:                 }
19019:                 if (@statuses) {
19020:                     my %curr_rule;
19021:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
19022:                         foreach my $type (@statuses) {
19023:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
19024:                         }
19025:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
19026:                         foreach my $type (@statuses) {
19027:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
19028:                         }
19029:                     }
19030:                     push(@{$cancreate{'selfcreate'}},'email');
19031:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
19032:                     my %curremaildom;
19033:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
19034:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
19035:                     }
19036:                     foreach my $type (@statuses) {
19037:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
19038:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
19039:                         }
19040:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
19041:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
19042:                         }
19043:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
19044: #
19045: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
19046: #
19047:                             my $chosen = $1;
19048:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
19049:                                 my $emaildom;
19050:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
19051:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
19052:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
19053:                                     if (ref($curremaildom{$type}) eq 'HASH') {
19054:                                         if (exists($curremaildom{$type}{$chosen})) {
19055:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
19056:                                                 push(@{$changes{'cancreate'}},'emaildomain');
19057:                                             }
19058:                                         } elsif ($emaildom ne '') {
19059:                                             push(@{$changes{'cancreate'}},'emaildomain');
19060:                                         }
19061:                                     } elsif ($emaildom ne '') {
19062:                                         push(@{$changes{'cancreate'}},'emaildomain');
19063:                                     }
19064:                                 }
19065:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19066:                             } elsif ($chosen eq 'custom') {
19067:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
19068:                                 $email_rule{$type} = [];
19069:                                 if (ref($emailrules) eq 'HASH') {
19070:                                     foreach my $rule (@possemail_rules) {
19071:                                         if (exists($emailrules->{$rule})) {
19072:                                             push(@{$email_rule{$type}},$rule);
19073:                                         }
19074:                                     }
19075:                                 }
19076:                                 if (@{$email_rule{$type}}) {
19077:                                     $cancreate{'emailoptions'}{$type} = 'custom';
19078:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
19079:                                         if (@{$curr_rule{$type}} > 0) {
19080:                                             foreach my $rule (@{$curr_rule{$type}}) {
19081:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
19082:                                                     push(@{$changes{'email_rule'}},$type);
19083:                                                 }
19084:                                             }
19085:                                         }
19086:                                         foreach my $type (@{$email_rule{$type}}) {
19087:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
19088:                                                 push(@{$changes{'email_rule'}},$type);
19089:                                             }
19090:                                         }
19091:                                     } else {
19092:                                         push(@{$changes{'email_rule'}},$type);
19093:                                     }
19094:                                 }
19095:                             } else {
19096:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19097:                             }
19098:                         }
19099:                     }
19100:                     if (@types) {
19101:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19102:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
19103:                             if (@changed) {
19104:                                 push(@{$changes{'inststatus'}},'inststatusguest');
19105:                             }
19106:                         } else {
19107:                             push(@{$changes{'inststatus'}},'inststatusguest');
19108:                         }
19109:                     }
19110:                 } else {
19111:                     delete($env{'form.cancreate_email'});
19112:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19113:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19114:                             push(@{$changes{'inststatus'}},'inststatusguest');
19115:                         }
19116:                     }
19117:                 }
19118:             } else {
19119:                 $save_inststatus{'inststatusguest'} = [];
19120:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19121:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19122:                         push(@{$changes{'inststatus'}},'inststatusguest');
19123:                     }
19124:                 }
19125:             }
19126:         } else {
19127:             if ($env{'form.cancreate_'.$item}) {
19128:                 push(@{$cancreate{'selfcreate'}},$item);
19129:             }
19130:         }
19131:     }
19132:     my (%userinfo,%savecaptcha);
19133:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
19134: #
19135: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
19136: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
19137: #
19138: 
19139:     if ($env{'form.cancreate_email'}) {
19140:         push(@contexts,'emailusername');
19141:         if (@statuses) {
19142:             foreach my $type (@statuses) {
19143:                 if (ref($infofields) eq 'ARRAY') {
19144:                     foreach my $field (@{$infofields}) {
19145:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
19146:                             $cancreate{'emailusername'}{$type}{$field} = $1;
19147:                         }
19148:                     }
19149:                 }
19150:             }
19151:         }
19152: #
19153: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
19154: # queued requests for self-creation of account verified by e-mail.
19155: #
19156: 
19157:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
19158:         @approvalnotify = sort(@approvalnotify);
19159:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
19160:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19161:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
19162:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
19163:                     push(@{$changes{'cancreate'}},'notify');
19164:                 }
19165:             } else {
19166:                 if ($cancreate{'notify'}{'approval'}) {
19167:                     push(@{$changes{'cancreate'}},'notify');
19168:                 }
19169:             }
19170:         } elsif ($cancreate{'notify'}{'approval'}) {
19171:             push(@{$changes{'cancreate'}},'notify');
19172:         }
19173: 
19174:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
19175:     }
19176: #  
19177: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
19178: # institutional log-in.
19179: #
19180:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
19181:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
19182:                ($domdefaults{'auth_def'} eq 'localauth'))) {
19183:             $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.').' '.
19184:                           &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.');
19185:         }
19186:     }
19187:     my @fields = ('lastname','firstname','middlename','generation',
19188:                   'permanentemail','id');
19189:     my @shibfields = (@fields,'inststatus');
19190:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19191: #
19192: # Where usernames may created for institutional log-in and/or institutional single sign on:
19193: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
19194: # may self-create accounts 
19195: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
19196: # which the user may supply, if institutional data is unavailable.
19197: #
19198:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
19199:         if (@types) {
19200:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
19201:             push(@contexts,'statustocreate');
19202:             foreach my $type (@types) {
19203:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
19204:                 foreach my $field (@fields) {
19205:                     if (grep(/^\Q$field\E$/,@modifiable)) {
19206:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
19207:                     } else {
19208:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
19209:                     }
19210:                 }
19211:             }
19212:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
19213:                 foreach my $type (@types) {
19214:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
19215:                         foreach my $field (@fields) {
19216:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
19217:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
19218:                                 push(@{$changes{'selfcreate'}},$type);
19219:                                 last;
19220:                             }
19221:                         }
19222:                     }
19223:                 }
19224:             } else {
19225:                 foreach my $type (@types) {
19226:                     push(@{$changes{'selfcreate'}},$type);
19227:                 }
19228:             }
19229:         }
19230:         foreach my $field (@shibfields) {
19231:             if ($env{'form.shibenv_'.$field} ne '') {
19232:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
19233:             }
19234:         }
19235:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19236:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
19237:                 foreach my $field (@shibfields) {
19238:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
19239:                         push(@{$changes{'cancreate'}},'shibenv');
19240:                     }
19241:                 }
19242:             } else {
19243:                 foreach my $field (@shibfields) {
19244:                     if ($env{'form.shibenv_'.$field}) {
19245:                         push(@{$changes{'cancreate'}},'shibenv');
19246:                         last;
19247:                     }
19248:                 }
19249:             }
19250:         }
19251:     }
19252:     foreach my $item (@contexts) {
19253:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
19254:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
19255:                 if (ref($cancreate{$item}) eq 'ARRAY') {
19256:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
19257:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19258:                             push(@{$changes{'cancreate'}},$item);
19259:                         }
19260:                     }
19261:                 }
19262:             }
19263:             if (ref($cancreate{$item}) eq 'ARRAY') {
19264:                 foreach my $type (@{$cancreate{$item}}) {
19265:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
19266:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19267:                             push(@{$changes{'cancreate'}},$item);
19268:                         }
19269:                     }
19270:                 }
19271:             }
19272:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
19273:             if (ref($cancreate{$item}) eq 'HASH') {
19274:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
19275:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19276:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
19277:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19278:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19279:                                     push(@{$changes{'cancreate'}},$item);
19280:                                 }
19281:                             }
19282:                         }
19283:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19284:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19285:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19286:                                 push(@{$changes{'cancreate'}},$item);
19287:                             }
19288:                         }
19289:                     }
19290:                 }
19291:                 foreach my $type (keys(%{$cancreate{$item}})) {
19292:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19293:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19294:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19295:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19296:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19297:                                         push(@{$changes{'cancreate'}},$item);
19298:                                     }
19299:                                 }
19300:                             } else {
19301:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19302:                                     push(@{$changes{'cancreate'}},$item);
19303:                                 }
19304:                             }
19305:                         }
19306:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19307:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19308:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19309:                                 push(@{$changes{'cancreate'}},$item);
19310:                             }
19311:                         }
19312:                     }
19313:                 }
19314:             }
19315:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
19316:             if (ref($cancreate{$item}) eq 'ARRAY') {
19317:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
19318:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19319:                         push(@{$changes{'cancreate'}},$item);
19320:                     }
19321:                 }
19322:             }
19323:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19324:             if (ref($cancreate{$item}) eq 'HASH') {
19325:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19326:                     push(@{$changes{'cancreate'}},$item);
19327:                 }
19328:             }
19329:         } elsif ($item eq 'emailusername') {
19330:             if (ref($cancreate{$item}) eq 'HASH') {
19331:                 foreach my $type (keys(%{$cancreate{$item}})) {
19332:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19333:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19334:                             if ($cancreate{$item}{$type}{$field}) {
19335:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19336:                                     push(@{$changes{'cancreate'}},$item);
19337:                                 }
19338:                                 last;
19339:                             }
19340:                         }
19341:                     }
19342:                 }
19343:             }
19344:         }
19345:     }
19346: #
19347: # Populate %save_usercreate hash with updates to self-creation configuration.
19348: #
19349:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
19350:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
19351:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
19352:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
19353:     if (ref($cancreate{'notify'}) eq 'HASH') {
19354:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
19355:     }
19356:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
19357:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
19358:     }
19359:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
19360:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
19361:     }
19362:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
19363:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
19364:     }
19365:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
19366:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
19367:     }
19368:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19369:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
19370:     }
19371:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
19372:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
19373:     }
19374:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
19375:     $save_usercreate{'email_rule'} = \%email_rule;
19376: 
19377:     my %userconfig_hash = (
19378:             usercreation     => \%save_usercreate,
19379:             usermodification => \%save_usermodify,
19380:             inststatus       => \%save_inststatus,
19381:     );
19382: 
19383:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
19384:                                              $dom);
19385: #
19386: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
19387: #
19388:     if ($putresult eq 'ok') {
19389:         if (keys(%changes) > 0) {
19390:             $resulttext = &mt('Changes made:').'<ul>';
19391:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
19392:                 my %lt = &selfcreation_types();
19393:                 foreach my $type (@{$changes{'cancreate'}}) {
19394:                     my $chgtext = '';
19395:                     if ($type eq 'selfcreate') {
19396:                         if (@{$cancreate{$type}} == 0) {
19397:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
19398:                         } else {
19399:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
19400:                                         '<ul>';
19401:                             foreach my $case (@{$cancreate{$type}}) {
19402:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
19403:                             }
19404:                             $chgtext .= '</ul>';
19405:                             if (ref($cancreate{$type}) eq 'ARRAY') {
19406:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
19407:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19408:                                         if (@{$cancreate{'statustocreate'}} == 0) {
19409:                                             $chgtext .= '<span class="LC_warning">'.
19410:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
19411:                                                         '</span><br />';
19412:                                         }
19413:                                     }
19414:                                 }
19415:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
19416:                                     if (!@statuses) {
19417:                                         $chgtext .= '<span class="LC_warning">'.
19418:                                                     &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.").
19419:                                                     '</span><br />';
19420: 
19421:                                     }
19422:                                 }
19423:                             }
19424:                         }
19425:                     } elsif ($type eq 'shibenv') {
19426:                         if (keys(%{$cancreate{$type}}) == 0) {
19427:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
19428:                         } else {
19429:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
19430:                                         '<ul>';
19431:                             foreach my $field (@shibfields) {
19432:                                 next if ($cancreate{$type}{$field} eq '');
19433:                                 if ($field eq 'inststatus') {
19434:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
19435:                                 } else {
19436:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
19437:                                 }
19438:                             }
19439:                             $chgtext .= '</ul>';
19440:                         }
19441:                     } elsif ($type eq 'statustocreate') {
19442:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
19443:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
19444:                             if (@{$cancreate{'selfcreate'}} > 0) {
19445:                                 if (@{$cancreate{'statustocreate'}} == 0) {
19446:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
19447:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
19448:                                         $chgtext .= '<br />'.
19449:                                                     '<span class="LC_warning">'.
19450:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
19451:                                                     '</span>';
19452:                                     }
19453:                                 } elsif (keys(%usertypes) > 0) {
19454:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19455:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
19456:                                     } else {
19457:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
19458:                                     }
19459:                                     $chgtext .= '<ul>';
19460:                                     foreach my $case (@{$cancreate{$type}}) {
19461:                                         if ($case eq 'default') {
19462:                                             $chgtext .= '<li>'.$othertitle.'</li>';
19463:                                         } else {
19464:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
19465:                                         }
19466:                                     }
19467:                                     $chgtext .= '</ul>';
19468:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19469:                                         $chgtext .= '<span class="LC_warning">'.
19470:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
19471:                                                     '</span>';
19472:                                     }
19473:                                 }
19474:                             } else {
19475:                                 if (@{$cancreate{$type}} == 0) {
19476:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
19477:                                 } else {
19478:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
19479:                                 }
19480:                             }
19481:                             $chgtext .= '<br />';
19482:                         }
19483:                     } elsif ($type eq 'selfcreateprocessing') {
19484:                         my %choices = &Apache::lonlocal::texthash (
19485:                                                                     automatic => 'Automatic approval',
19486:                                                                     approval  => 'Queued for approval',
19487:                                                                   );
19488:                         if (@types) {
19489:                             if (@statuses) {
19490:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
19491:                                             '<ul>';
19492:                                 foreach my $status (@statuses) {
19493:                                     if ($status eq 'default') {
19494:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19495:                                     } else {
19496:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19497:                                     }
19498:                                 }
19499:                                 $chgtext .= '</ul>';
19500:                             }
19501:                         } else {
19502:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
19503:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
19504:                         }
19505:                     } elsif ($type eq 'emailverified') {
19506:                         my %options = &Apache::lonlocal::texthash (
19507:                                                                     all   => 'Same as e-mail',
19508:                                                                     first => 'Omit @domain',
19509:                                                                     free  => 'Free to choose',
19510:                                                                   );
19511:                         if (@types) {
19512:                             if (@statuses) {
19513:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
19514:                                             '<ul>';
19515:                                 foreach my $status (@statuses) {
19516:                                     if ($status eq 'default') {
19517:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19518:                                     } else {
19519:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19520:                                     }
19521:                                 }
19522:                                 $chgtext .= '</ul>';
19523:                             }
19524:                         } else {
19525:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
19526:                                             $options{$cancreate{'emailverified'}{'default'}});
19527:                         }
19528:                     } elsif ($type eq 'emailoptions') {
19529:                         my %options = &Apache::lonlocal::texthash (
19530:                                                                     any     => 'Any e-mail',
19531:                                                                     inst    => 'Institutional only',
19532:                                                                     noninst => 'Non-institutional only',
19533:                                                                     custom  => 'Custom restrictions',
19534:                                                                   );
19535:                         if (@types) {
19536:                             if (@statuses) {
19537:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
19538:                                             '<ul>';
19539:                                 foreach my $status (@statuses) {
19540:                                     if ($type eq 'default') {
19541:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19542:                                     } else {
19543:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19544:                                     }
19545:                                 }
19546:                                 $chgtext .= '</ul>';
19547:                             }
19548:                         } else {
19549:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
19550:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
19551:                             } else {
19552:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
19553:                                                 $options{$cancreate{'emailoptions'}{'default'}});
19554:                             }
19555:                         }
19556:                     } elsif ($type eq 'emaildomain') {
19557:                         my $output;
19558:                         if (@statuses) {
19559:                             foreach my $type (@statuses) {
19560:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
19561:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
19562:                                         if ($type eq 'default') {
19563:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19564:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19565:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19566:                                             } else {
19567:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
19568:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19569:                                             }
19570:                                         } else {
19571:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19572:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19573:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19574:                                             } else {
19575:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
19576:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19577:                                             }
19578:                                         }
19579:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
19580:                                         if ($type eq 'default') {
19581:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19582:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19583:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19584:                                             } else {
19585:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
19586:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19587:                                             }
19588:                                         } else {
19589:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19590:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19591:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19592:                                             } else {
19593:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
19594:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19595:                                             }
19596:                                         }
19597:                                     }
19598:                                 }
19599:                             }
19600:                         }
19601:                         if ($output ne '') {
19602:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
19603:                                         '<ul>'.$output.'</ul>';
19604:                         }
19605:                     } elsif ($type eq 'captcha') {
19606:                         if ($savecaptcha{$type} eq 'notused') {
19607:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
19608:                         } else {
19609:                             my %captchas = &captcha_phrases();
19610:                             if ($captchas{$savecaptcha{$type}}) {
19611:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
19612:                             } else {
19613:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
19614:                             }
19615:                         }
19616:                     } elsif ($type eq 'recaptchakeys') {
19617:                         my ($privkey,$pubkey);
19618:                         if (ref($savecaptcha{$type}) eq 'HASH') {
19619:                             $pubkey = $savecaptcha{$type}{'public'};
19620:                             $privkey = $savecaptcha{$type}{'private'};
19621:                         }
19622:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
19623:                         if (!$pubkey) {
19624:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
19625:                         } else {
19626:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
19627:                         }
19628:                         if (!$privkey) {
19629:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
19630:                         } else {
19631:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
19632:                         }
19633:                         $chgtext .= '</ul>';
19634:                     } elsif ($type eq 'recaptchaversion') {
19635:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
19636:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
19637:                         }
19638:                     } elsif ($type eq 'emailusername') {
19639:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
19640:                             if (@statuses) {
19641:                                 foreach my $type (@statuses) {
19642:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
19643:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
19644:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
19645:                                                     '<ul>';
19646:                                             foreach my $field (@{$infofields}) {
19647:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
19648:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
19649:                                                 }
19650:                                             }
19651:                                             $chgtext .= '</ul>';
19652:                                         } else {
19653:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19654:                                         }
19655:                                     } else {
19656:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19657:                                     }
19658:                                 }
19659:                             }
19660:                         }
19661:                     } elsif ($type eq 'notify') {
19662:                         my $numapprove = 0;
19663:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
19664:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
19665:                                 if ($cancreate{'notify'}{'approval'}) {
19666:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
19667:                                     $numapprove ++;
19668:                                 }
19669:                             }
19670:                         }
19671:                         unless ($numapprove) {
19672:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
19673:                         }
19674:                     }
19675:                     if ($chgtext) {
19676:                         $resulttext .= '<li>'.$chgtext.'</li>';
19677:                     }
19678:                 }
19679:             }
19680:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
19681:                 my ($emailrules,$emailruleorder) =
19682:                     &Apache::lonnet::inst_userrules($dom,'email');
19683:                 foreach my $type (@{$changes{'email_rule'}}) {
19684:                     if (ref($email_rule{$type}) eq 'ARRAY') {
19685:                         my $chgtext = '<ul>';
19686:                         foreach my $rule (@{$email_rule{$type}}) {
19687:                             if (ref($emailrules->{$rule}) eq 'HASH') {
19688:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
19689:                             }
19690:                         }
19691:                         $chgtext .= '</ul>';
19692:                         my $typename;
19693:                         if (@types) {
19694:                             if ($type eq 'default') {
19695:                                 $typename = $othertitle;
19696:                             } else {
19697:                                 $typename = $usertypes{$type};
19698:                             }
19699:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
19700:                         }
19701:                         if (@{$email_rule{$type}} > 0) {
19702:                             $resulttext .= '<li>'.
19703:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
19704:                                                $usertypes{$type}).
19705:                                            $chgtext.
19706:                                            '</li>';
19707:                         } else {
19708:                             $resulttext .= '<li>'.
19709:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
19710:                                            '</li>'.
19711:                                            &mt('(Affiliation: [_1])',$typename);
19712:                         }
19713:                     }
19714:                 }
19715:             }
19716:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
19717:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
19718:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
19719:                         my $chgtext = '<ul>';
19720:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
19721:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
19722:                         }
19723:                         $chgtext .= '</ul>';
19724:                         $resulttext .= '<li>'.
19725:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
19726:                                           $chgtext.
19727:                                        '</li>';
19728:                     } else {
19729:                         $resulttext .= '<li>'.
19730:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
19731:                                        '</li>';
19732:                     }
19733:                 }
19734:             }
19735:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
19736:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
19737:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19738:                 foreach my $type (@{$changes{'selfcreate'}}) {
19739:                     my $typename = $type;
19740:                     if (keys(%usertypes) > 0) {
19741:                         if ($usertypes{$type} ne '') {
19742:                             $typename = $usertypes{$type};
19743:                         }
19744:                     }
19745:                     my @modifiable;
19746:                     $resulttext .= '<li>'.
19747:                                     &mt('Self-creation of account by users with status: [_1]',
19748:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
19749:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
19750:                     foreach my $field (@fields) {
19751:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
19752:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
19753:                         }
19754:                     }
19755:                     if (@modifiable > 0) {
19756:                         $resulttext .= join(', ',@modifiable);
19757:                     } else {
19758:                         $resulttext .= &mt('none');
19759:                     }
19760:                     $resulttext .= '</li>';
19761:                 }
19762:                 $resulttext .= '</ul></li>';
19763:             }
19764:             $resulttext .= '</ul>';
19765:             my $cachetime = 24*60*60;
19766:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
19767:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19768:             if (ref($lastactref) eq 'HASH') {
19769:                 $lastactref->{'domdefaults'} = 1;
19770:             }
19771:         } else {
19772:             $resulttext = &mt('No changes made to self-creation settings');
19773:         }
19774:     } else {
19775:         $resulttext = '<span class="LC_error">'.
19776:             &mt('An error occurred: [_1]',$putresult).'</span>';
19777:     }
19778:     if ($warningmsg ne '') {
19779:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19780:     }
19781:     return $resulttext;
19782: }
19783: 
19784: sub process_captcha {
19785:     my ($container,$changes,$newsettings,$currsettings) = @_;
19786:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19787:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19788:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19789:         $newsettings->{'captcha'} = 'original';
19790:     }
19791:     my %current;
19792:     if (ref($currsettings) eq 'HASH') {
19793:         %current = %{$currsettings};
19794:     }
19795:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19796:         if ($container eq 'cancreate') {
19797:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19798:                 push(@{$changes->{'cancreate'}},'captcha');
19799:             } elsif (!defined($changes->{'cancreate'})) {
19800:                 $changes->{'cancreate'} = ['captcha'];
19801:             }
19802:         } elsif ($container eq 'passwords') {
19803:             $changes->{'reset'} = 1;
19804:         } else {
19805:             $changes->{'captcha'} = 1;
19806:         }
19807:     }
19808:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19809:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19810:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19811:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19812:         $newpub =~ s/[^\w\-]//g;
19813:         $newpriv =~ s/[^\w\-]//g;
19814:         $newsettings->{'recaptchakeys'} = {
19815:                                              public  => $newpub,
19816:                                              private => $newpriv,
19817:                                           };
19818:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19819:         $newversion =~ s/\D//g;
19820:         if ($newversion ne '2') {
19821:             $newversion = 1;
19822:         }
19823:         $newsettings->{'recaptchaversion'} = $newversion;
19824:     }
19825:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19826:         $currpub = $current{'recaptchakeys'}{'public'};
19827:         $currpriv = $current{'recaptchakeys'}{'private'};
19828:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19829:             $newsettings->{'recaptchakeys'} = {
19830:                                                  public  => '',
19831:                                                  private => '',
19832:                                               }
19833:         }
19834:     }
19835:     if ($current{'captcha'} eq 'recaptcha') {
19836:         $currversion = $current{'recaptchaversion'};
19837:         if ($currversion ne '2') {
19838:             $currversion = 1;
19839:         }
19840:     }
19841:     if ($currversion ne $newversion) {
19842:         if ($container eq 'cancreate') {
19843:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19844:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19845:             } elsif (!defined($changes->{'cancreate'})) {
19846:                 $changes->{'cancreate'} = ['recaptchaversion'];
19847:             }
19848:         } elsif ($container eq 'passwords') {
19849:             $changes->{'reset'} = 1;
19850:         } else {
19851:             $changes->{'recaptchaversion'} = 1;
19852:         }
19853:     }
19854:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19855:         if ($container eq 'cancreate') {
19856:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19857:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19858:             } elsif (!defined($changes->{'cancreate'})) {
19859:                 $changes->{'cancreate'} = ['recaptchakeys'];
19860:             }
19861:         } elsif ($container eq 'passwords') {
19862:             $changes->{'reset'} = 1;
19863:         } else {
19864:             $changes->{'recaptchakeys'} = 1;
19865:         }
19866:     }
19867:     return;
19868: }
19869: 
19870: sub modify_usermodification {
19871:     my ($dom,%domconfig) = @_;
19872:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19873:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19874:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19875:             if ($key eq 'selfcreate') {
19876:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19877:             } else {  
19878:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19879:             }
19880:         }
19881:     }
19882:     my @contexts = ('author','course');
19883:     my %context_title = (
19884:                            author => 'In author context',
19885:                            course => 'In course context',
19886:                         );
19887:     my @fields = ('lastname','firstname','middlename','generation',
19888:                   'permanentemail','id');
19889:     my %roles = (
19890:                   author => ['ca','aa'],
19891:                   course => ['st','ep','ta','in','cr'],
19892:                 );
19893:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19894:     foreach my $context (@contexts) {
19895:         foreach my $role (@{$roles{$context}}) {
19896:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19897:             foreach my $item (@fields) {
19898:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19899:                     $modifyhash{$context}{$role}{$item} = 1;
19900:                 } else {
19901:                     $modifyhash{$context}{$role}{$item} = 0;
19902:                 }
19903:             }
19904:         }
19905:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19906:             foreach my $role (@{$roles{$context}}) {
19907:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19908:                     foreach my $field (@fields) {
19909:                         if ($modifyhash{$context}{$role}{$field} ne 
19910:                                 $curr_usermodification{$context}{$role}{$field}) {
19911:                             push(@{$changes{$context}},$role);
19912:                             last;
19913:                         }
19914:                     }
19915:                 }
19916:             }
19917:         } else {
19918:             foreach my $context (@contexts) {
19919:                 foreach my $role (@{$roles{$context}}) {
19920:                     push(@{$changes{$context}},$role);
19921:                 }
19922:             }
19923:         }
19924:     }
19925:     my %usermodification_hash =  (
19926:                                    usermodification => \%modifyhash,
19927:                                  );
19928:     my $putresult = &Apache::lonnet::put_dom('configuration',
19929:                                              \%usermodification_hash,$dom);
19930:     if ($putresult eq 'ok') {
19931:         if (keys(%changes) > 0) {
19932:             $resulttext = &mt('Changes made: ').'<ul>';
19933:             foreach my $context (@contexts) {
19934:                 if (ref($changes{$context}) eq 'ARRAY') {
19935:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19936:                     if (ref($changes{$context}) eq 'ARRAY') {
19937:                         foreach my $role (@{$changes{$context}}) {
19938:                             my $rolename;
19939:                             if ($role eq 'cr') {
19940:                                 $rolename = &mt('Custom');
19941:                             } else {
19942:                                 $rolename = &Apache::lonnet::plaintext($role);
19943:                             }
19944:                             my @modifiable;
19945:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19946:                             foreach my $field (@fields) {
19947:                                 if ($modifyhash{$context}{$role}{$field}) {
19948:                                     push(@modifiable,$fieldtitles{$field});
19949:                                 }
19950:                             }
19951:                             if (@modifiable > 0) {
19952:                                 $resulttext .= join(', ',@modifiable);
19953:                             } else {
19954:                                 $resulttext .= &mt('none'); 
19955:                             }
19956:                             $resulttext .= '</li>';
19957:                         }
19958:                         $resulttext .= '</ul></li>';
19959:                     }
19960:                 }
19961:             }
19962:             $resulttext .= '</ul>';
19963:         } else {
19964:             $resulttext = &mt('No changes made to user modification settings');
19965:         }
19966:     } else {
19967:         $resulttext = '<span class="LC_error">'.
19968:             &mt('An error occurred: [_1]',$putresult).'</span>';
19969:     }
19970:     return $resulttext;
19971: }
19972: 
19973: sub modify_defaults {
19974:     my ($dom,$lastactref,%domconfig) = @_;
19975:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19976:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19977:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19978:                  'portal_def');
19979:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19980:     foreach my $item (@items) {
19981:         $newvalues{$item} = $env{'form.'.$item};
19982:         if ($item eq 'auth_def') {
19983:             if ($newvalues{$item} ne '') {
19984:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19985:                     push(@errors,$item);
19986:                 }
19987:             }
19988:         } elsif ($item eq 'lang_def') {
19989:             if ($newvalues{$item} ne '') {
19990:                 if ($newvalues{$item} =~ /^(\w+)/) {
19991:                     my $langcode = $1;
19992:                     if ($langcode ne 'x_chef') {
19993:                         if (code2language($langcode) eq '') {
19994:                             push(@errors,$item);
19995:                         }
19996:                     }
19997:                 } else {
19998:                     push(@errors,$item);
19999:                 }
20000:             }
20001:         } elsif ($item eq 'timezone_def') {
20002:             if ($newvalues{$item} ne '') {
20003:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
20004:                     push(@errors,$item);   
20005:                 }
20006:             }
20007:         } elsif ($item eq 'datelocale_def') {
20008:             if ($newvalues{$item} ne '') {
20009:                 my @datelocale_ids = DateTime::Locale->ids();
20010:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
20011:                     push(@errors,$item);
20012:                 }
20013:             }
20014:         } elsif ($item eq 'portal_def') {
20015:             if ($newvalues{$item} ne '') {
20016:                 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])\/?$/) {
20017:                     foreach my $field ('email','web') {
20018:                         if ($env{'form.'.$item.'_'.$field}) {
20019:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
20020:                         }
20021:                     }
20022:                 } else {
20023:                     push(@errors,$item);
20024:                 }
20025:             }
20026:         }
20027:         if (grep(/^\Q$item\E$/,@errors)) {
20028:             $newvalues{$item} = $domdefaults{$item};
20029:             if ($item eq 'portal_def') {
20030:                 if ($domdefaults{$item}) {
20031:                     foreach my $field ('email','web') {
20032:                         if (exists($domdefaults{$item.'_'.$field})) {
20033:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
20034:                         }
20035:                     }
20036:                 }
20037:             }
20038:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
20039:             $changes{$item} = 1;
20040:         }
20041:         if ($item eq 'portal_def') {
20042:             unless (grep(/^\Q$item\E$/,@errors)) {
20043:                 if ($newvalues{$item} eq '') {
20044:                     foreach my $field ('email','web') {
20045:                         if (exists($domdefaults{$item.'_'.$field})) {
20046:                             delete($domdefaults{$item.'_'.$field});
20047:                         }
20048:                     }
20049:                 } else {
20050:                     unless ($changes{$item}) {
20051:                         foreach my $field ('email','web') {
20052:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
20053:                                 $changes{$item} = 1;
20054:                                 last;
20055:                             }
20056:                         }
20057:                     }
20058:                     foreach my $field ('email','web') {
20059:                         if ($newvalues{$item.'_'.$field}) {
20060:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
20061:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
20062:                             delete($domdefaults{$item.'_'.$field});
20063:                         }
20064:                     }
20065:                 }
20066:             }
20067:         }
20068:         $domdefaults{$item} = $newvalues{$item};
20069:     }
20070:     my %staticdefaults = (
20071:                            'intauth_cost'   => 10,
20072:                            'intauth_check'  => 0,
20073:                            'intauth_switch' => 0,
20074:                          );
20075:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
20076:         if (exists($domdefaults{$item})) {
20077:             $newvalues{$item} = $domdefaults{$item};
20078:         } else {
20079:             $newvalues{$item} = $staticdefaults{$item};
20080:         }
20081:     }
20082:     my ($unamemaprules,$ruleorder);
20083:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
20084:     if (@possunamemaprules) {
20085:         ($unamemaprules,$ruleorder) =
20086:             &Apache::lonnet::inst_userrules($dom,'unamemap');
20087:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
20088:             if (@{$ruleorder} > 0) {
20089:                 my %possrules;
20090:                 map { $possrules{$_} = 1; } @possunamemaprules;
20091:                 foreach my $rule (@{$ruleorder}) {
20092:                     if ($possrules{$rule}) {
20093:                         push(@{$newvalues{'unamemap_rule'}},$rule);
20094:                     }
20095:                 }
20096:             }
20097:         }
20098:     }
20099:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
20100:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20101:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
20102:                                                                $newvalues{'unamemap_rule'});
20103:             if (@rulediffs) {
20104:                 $changes{'unamemap_rule'} = 1;
20105:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20106:             }
20107:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
20108:             $changes{'unamemap_rule'} = 1;
20109:             delete($domdefaults{'unamemap_rule'});
20110:         }
20111:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20112:         if (@{$newvalues{'unamemap_rule'}} > 0) {
20113:             $changes{'unamemap_rule'} = 1;
20114:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20115:         }
20116:     }
20117:     my %defaults_hash = (
20118:                          defaults => \%newvalues,
20119:                         );
20120:     my $title = &defaults_titles();
20121: 
20122:     my $currinststatus;
20123:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
20124:         $currinststatus = $domconfig{'inststatus'};
20125:     } else {
20126:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20127:         $currinststatus = {
20128:                              inststatustypes => $usertypes,
20129:                              inststatusorder => $types,
20130:                              inststatusguest => [],
20131:                           };
20132:     }
20133:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
20134:     my @allpos;
20135:     my %alltypes;
20136:     my @inststatusguest;
20137:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
20138:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
20139:             unless (grep(/^\Q$type\E$/,@todelete)) {
20140:                 push(@inststatusguest,$type);
20141:             }
20142:         }
20143:     }
20144:     my ($currtitles,$currorder);
20145:     if (ref($currinststatus) eq 'HASH') {
20146:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
20147:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
20148:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
20149:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
20150:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
20151:                     }
20152:                 }
20153:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
20154:                     my $position = $env{'form.inststatus_pos_'.$type};
20155:                     $position =~ s/\D+//g;
20156:                     $allpos[$position] = $type;
20157:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
20158:                     $alltypes{$type} =~ s/`//g;
20159:                 }
20160:             }
20161:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
20162:             $currtitles =~ s/,$//;
20163:         }
20164:     }
20165:     if ($env{'form.addinststatus'}) {
20166:         my $newtype = $env{'form.addinststatus'};
20167:         $newtype =~ s/\W//g;
20168:         unless (exists($alltypes{$newtype})) {
20169:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
20170:             $alltypes{$newtype} =~ s/`//g; 
20171:             my $position = $env{'form.addinststatus_pos'};
20172:             $position =~ s/\D+//g;
20173:             if ($position ne '') {
20174:                 $allpos[$position] = $newtype;
20175:             }
20176:         }
20177:     }
20178:     my @orderedstatus;
20179:     foreach my $type (@allpos) {
20180:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
20181:             push(@orderedstatus,$type);
20182:         }
20183:     }
20184:     foreach my $type (keys(%alltypes)) {
20185:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
20186:             delete($alltypes{$type});
20187:         }
20188:     }
20189:     $defaults_hash{'inststatus'} = {
20190:                                      inststatustypes => \%alltypes,
20191:                                      inststatusorder => \@orderedstatus,
20192:                                      inststatusguest => \@inststatusguest,
20193:                                    };
20194:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
20195:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
20196:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
20197:         }
20198:     }
20199:     if ($currorder ne join(',',@orderedstatus)) {
20200:         $changes{'inststatus'}{'inststatusorder'} = 1;
20201:     }
20202:     my $newtitles;
20203:     foreach my $item (@orderedstatus) {
20204:         $newtitles .= $alltypes{$item}.',';
20205:     }
20206:     $newtitles =~ s/,$//;
20207:     if ($currtitles ne $newtitles) {
20208:         $changes{'inststatus'}{'inststatustypes'} = 1;
20209:     }
20210:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
20211:                                              $dom);
20212:     if ($putresult eq 'ok') {
20213:         if (keys(%changes) > 0) {
20214:             $resulttext = &mt('Changes made:').'<ul>';
20215:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
20216:             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";
20217:             foreach my $item (sort(keys(%changes))) {
20218:                 if ($item eq 'inststatus') {
20219:                     if (ref($changes{'inststatus'}) eq 'HASH') {
20220:                         if (@orderedstatus) {
20221:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
20222:                             foreach my $type (@orderedstatus) { 
20223:                                 $resulttext .= $alltypes{$type}.', ';
20224:                             }
20225:                             $resulttext =~ s/, $//;
20226:                             $resulttext .= '</li>';
20227:                         } else {
20228:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
20229:                         }
20230:                     }
20231:                 } elsif ($item eq 'unamemap_rule') {
20232:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20233:                         my @rulenames;
20234:                         if (ref($unamemaprules) eq 'HASH') {
20235:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
20236:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
20237:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
20238:                                 }
20239:                             }
20240:                         }
20241:                         if (@rulenames) {
20242:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
20243:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
20244:                                            '</li>';
20245:                         } else {
20246:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
20247:                         }
20248:                     } else {
20249:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
20250:                     }
20251:                 } else {
20252:                     my $value = $env{'form.'.$item};
20253:                     if ($value eq '') {
20254:                         $value = &mt('none');
20255:                     } elsif ($item eq 'auth_def') {
20256:                         my %authnames = &authtype_names();
20257:                         my %shortauth = (
20258:                                           internal   => 'int',
20259:                                           krb4       => 'krb4',
20260:                                           krb5       => 'krb5',
20261:                                           localauth  => 'loc',
20262:                                           lti        => 'lti',
20263:                         );
20264:                         $value = $authnames{$shortauth{$value}};
20265:                     }
20266:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
20267:                     $mailmsgtext .= "$title->{$item} set to $value\n";
20268:                     if ($item eq 'portal_def') {
20269:                         if ($env{'form.'.$item} ne '') {
20270:                             foreach my $field ('email','web') {
20271:                                 $value = $env{'form.'.$item.'_'.$field};
20272:                                 if ($value) {
20273:                                     $value = &mt('Yes');
20274:                                 } else {
20275:                                     $value = &mt('No');
20276:                                 }
20277:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
20278:                             }
20279:                         }
20280:                     }
20281:                 }
20282:             }
20283:             $resulttext .= '</ul>';
20284:             $mailmsgtext .= "\n";
20285:             my $cachetime = 24*60*60;
20286:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20287:             if (ref($lastactref) eq 'HASH') {
20288:                 $lastactref->{'domdefaults'} = 1;
20289:             }
20290:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
20291:                 my $notify = 1;
20292:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
20293:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
20294:                         $notify = 0;
20295:                     }
20296:                 }
20297:                 if ($notify) {
20298:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
20299:                                                "LON-CAPA Domain Settings Change - $dom",
20300:                                                $mailmsgtext);
20301:                 }
20302:             }
20303:         } else {
20304:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
20305:         }
20306:     } else {
20307:         $resulttext = '<span class="LC_error">'.
20308:             &mt('An error occurred: [_1]',$putresult).'</span>';
20309:     }
20310:     if (@errors > 0) {
20311:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
20312:         foreach my $item (@errors) {
20313:             $resulttext .= ' "'.$title->{$item}.'",';
20314:         }
20315:         $resulttext =~ s/,$//;
20316:     }
20317:     return $resulttext;
20318: }
20319: 
20320: sub modify_scantron {
20321:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20322:     my ($resulttext,%confhash,%changes,$errors);
20323:     my $custom = 'custom.tab';
20324:     my $default = 'default.tab';
20325:     my $servadm = $r->dir_config('lonAdmEMail');
20326:     my ($configuserok,$author_ok,$switchserver) =
20327:         &config_check($dom,$confname,$servadm);
20328:     if ($env{'form.scantronformat.filename'} ne '') {
20329:         my $error;
20330:         if ($configuserok eq 'ok') {
20331:             if ($switchserver) {
20332:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
20333:             } else {
20334:                 if ($author_ok eq 'ok') {
20335:                     my $modified = [];
20336:                     my ($result,$scantronurl) =
20337:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
20338:                                                                 $confname,'scantron','','',$custom,
20339:                                                                 $modified);
20340:                     if ($result eq 'ok') {
20341:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
20342:                         $changes{'scantronformat'} = 1;
20343:                         &update_modify_urls($r,$modified);
20344:                     } else {
20345:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
20346:                     }
20347:                 } else {
20348:                     $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);
20349:                 }
20350:             }
20351:         } else {
20352:             $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);
20353:         }
20354:         if ($error) {
20355:             &Apache::lonnet::logthis($error);
20356:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
20357:         }
20358:     }
20359:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20360:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
20361:             if ($env{'form.scantronformat_del'}) {
20362:                 $confhash{'scantron'}{'scantronformat'} = '';
20363:                 $changes{'scantronformat'} = 1;
20364:             } else {
20365:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
20366:             }
20367:         }
20368:     }
20369:     my @options = ('hdr','pad','rem');
20370:     my @fields = &scantroncsv_fields();
20371:     my %titles = &scantronconfig_titles();
20372:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
20373:     my ($newdat,$currdat,%newcol,%currcol);
20374:     if (grep(/^dat$/,@formats)) {
20375:         $confhash{'scantron'}{config}{dat} = 1;
20376:         $newdat = 1;
20377:     } else {
20378:         $newdat = 0;
20379:     }
20380:     if (grep(/^csv$/,@formats)) {
20381:         my %bynum;
20382:         foreach my $field (@fields) {
20383:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
20384:                 my $posscol = $1;
20385:                 if (($posscol < 20) && (!$bynum{$posscol})) {
20386:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
20387:                     $bynum{$posscol} = $field;
20388:                     $newcol{$field} = $posscol;
20389:                 }
20390:             }
20391:         }
20392:         if (keys(%newcol)) {
20393:             foreach my $option (@options) {
20394:                 if ($env{'form.scantroncsv_'.$option}) {
20395:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
20396:                 }
20397:             }
20398:         }
20399:     }
20400:     $currdat = 1;
20401:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20402:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
20403:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
20404:                 $currdat = 0;
20405:             }
20406:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20407:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20408:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
20409:                 }
20410:             }
20411:         }
20412:     }
20413:     if ($currdat != $newdat) {
20414:         $changes{'config'} = 1;
20415:     } else {
20416:         foreach my $field (@fields) {
20417:             if ($currcol{$field} ne '') {
20418:                 if ($currcol{$field} ne $newcol{$field}) {
20419:                     $changes{'config'} = 1;
20420:                     last;
20421:                 }
20422:             } elsif ($newcol{$field} ne '') {
20423:                 $changes{'config'} = 1;
20424:                 last;
20425:             }
20426:         }
20427:     }
20428:     if (keys(%confhash) > 0) {
20429:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
20430:                                                  $dom);
20431:         if ($putresult eq 'ok') {
20432:             if (keys(%changes) > 0) {
20433:                 if (ref($confhash{'scantron'}) eq 'HASH') {
20434:                     $resulttext = &mt('Changes made:').'<ul>';
20435:                     if ($changes{'scantronformat'}) {
20436:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
20437:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
20438:                         } else {
20439:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
20440:                         }
20441:                     }
20442:                     if ($changes{'config'}) {
20443:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
20444:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
20445:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
20446:                             }
20447:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20448:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20449:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
20450:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
20451:                                         foreach my $field (@fields) {
20452:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
20453:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
20454:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
20455:                                             }
20456:                                         }
20457:                                         $resulttext .= '</ul></li>';
20458:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
20459:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
20460:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
20461:                                                 foreach my $option (@options) {
20462:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
20463:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
20464:                                                     }
20465:                                                 }
20466:                                                 $resulttext .= '</ul></li>';
20467:                                             }
20468:                                         }
20469:                                     }
20470:                                 }
20471:                             }
20472:                         } else {
20473:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
20474:                         }
20475:                     }
20476:                     $resulttext .= '</ul>';
20477:                 } else {
20478:                     $resulttext = &mt('Changes made to bubblesheet format file.');
20479:                 }
20480:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
20481:                 if (ref($lastactref) eq 'HASH') {
20482:                     $lastactref->{'domainconfig'} = 1;
20483:                 }
20484:             } else {
20485:                 $resulttext = &mt('No changes made to bubblesheet format settings');
20486:             }
20487:         } else {
20488:             $resulttext = '<span class="LC_error">'.
20489:                 &mt('An error occurred: [_1]',$putresult).'</span>';
20490:         }
20491:     } else {
20492:         $resulttext = &mt('No changes made to bubblesheet format file'); 
20493:     }
20494:     if ($errors) {
20495:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
20496:                        $errors.'</ul></p>';
20497:     }
20498:     return $resulttext;
20499: }
20500: 
20501: sub modify_coursecategories {
20502:     my ($dom,$lastactref,%domconfig) = @_;
20503:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
20504:         $cathash);
20505:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
20506:     my @catitems = ('unauth','auth');
20507:     my @cattypes = ('std','domonly','codesrch','none');
20508:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20509:         $cathash = $domconfig{'coursecategories'}{'cats'};
20510:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
20511:             $changes{'togglecats'} = 1;
20512:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
20513:         }
20514:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
20515:             $changes{'categorize'} = 1;
20516:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
20517:         }
20518:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
20519:             $changes{'togglecatscomm'} = 1;
20520:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
20521:         }
20522:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
20523:             $changes{'categorizecomm'} = 1;
20524:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
20525: 
20526:         }
20527:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
20528:             $changes{'togglecatsplace'} = 1;
20529:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
20530:         }
20531:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
20532:             $changes{'categorizeplace'} = 1;
20533:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
20534:         }
20535:         foreach my $item (@catitems) {
20536:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20537:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
20538:                     $changes{$item} = 1;
20539:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20540:                 }
20541:             }
20542:         }
20543:     } else {
20544:         $changes{'togglecats'} = 1;
20545:         $changes{'categorize'} = 1;
20546:         $changes{'togglecatscomm'} = 1;
20547:         $changes{'categorizecomm'} = 1;
20548:         $changes{'togglecatsplace'} = 1;
20549:         $changes{'categorizeplace'} = 1;
20550:         $domconfig{'coursecategories'} = {
20551:                                              togglecats => $env{'form.togglecats'},
20552:                                              categorize => $env{'form.categorize'},
20553:                                              togglecatscomm => $env{'form.togglecatscomm'},
20554:                                              categorizecomm => $env{'form.categorizecomm'},
20555:                                              togglecatsplace => $env{'form.togglecatsplace'},
20556:                                              categorizeplace => $env{'form.categorizeplace'},
20557:                                          };
20558:         foreach my $item (@catitems) {
20559:             if ($env{'form.coursecat_'.$item} ne 'std') {
20560:                 $changes{$item} = 1;
20561:             }
20562:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20563:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20564:             }
20565:         }
20566:     }
20567:     if (ref($cathash) eq 'HASH') {
20568:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
20569:             push (@deletecategory,'instcode::0');
20570:         }
20571:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
20572:             push(@deletecategory,'communities::0');
20573:         }
20574:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
20575:             push(@deletecategory,'placement::0');
20576:         }
20577:     }
20578:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
20579:     if (ref($cathash) eq 'HASH') {
20580:         if (@deletecategory > 0) {
20581:             #FIXME Need to remove category from all courses using a deleted category 
20582:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
20583:             foreach my $item (@deletecategory) {
20584:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
20585:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
20586:                     $deletions{$item} = 1;
20587:                     &recurse_cat_deletes($item,$cathash,\%deletions);
20588:                 }
20589:             }
20590:         }
20591:         foreach my $item (keys(%{$cathash})) {
20592:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20593:             if ($cathash->{$item} ne $env{'form.'.$item}) {
20594:                 $reorderings{$item} = 1;
20595:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
20596:             }
20597:             if ($env{'form.addcategory_name_'.$item} ne '') {
20598:                 my $newcat = $env{'form.addcategory_name_'.$item};
20599:                 my $newdepth = $depth+1;
20600:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20601:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
20602:                 $adds{$newitem} = 1; 
20603:             }
20604:             if ($env{'form.subcat_'.$item} ne '') {
20605:                 my $newcat = $env{'form.subcat_'.$item};
20606:                 my $newdepth = $depth+1;
20607:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20608:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
20609:                 $adds{$newitem} = 1;
20610:             }
20611:         }
20612:     }
20613:     if ($env{'form.instcode'} eq '1') {
20614:         if (ref($cathash) eq 'HASH') {
20615:             my $newitem = 'instcode::0';
20616:             if ($cathash->{$newitem} eq '') {  
20617:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20618:                 $adds{$newitem} = 1;
20619:             }
20620:         } else {
20621:             my $newitem = 'instcode::0';
20622:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20623:             $adds{$newitem} = 1;
20624:         }
20625:     }
20626:     if ($env{'form.communities'} eq '1') {
20627:         if (ref($cathash) eq 'HASH') {
20628:             my $newitem = 'communities::0';
20629:             if ($cathash->{$newitem} eq '') {
20630:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20631:                 $adds{$newitem} = 1;
20632:             }
20633:         } else {
20634:             my $newitem = 'communities::0';
20635:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20636:             $adds{$newitem} = 1;
20637:         }
20638:     }
20639:     if ($env{'form.placement'} eq '1') {
20640:         if (ref($cathash) eq 'HASH') {
20641:             my $newitem = 'placement::0';
20642:             if ($cathash->{$newitem} eq '') {
20643:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20644:                 $adds{$newitem} = 1;
20645:             }
20646:         } else {
20647:             my $newitem = 'placement::0';
20648:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20649:             $adds{$newitem} = 1;
20650:         }
20651:     }
20652:     if ($env{'form.addcategory_name'} ne '') {
20653:         if (($env{'form.addcategory_name'} ne 'instcode') &&
20654:             ($env{'form.addcategory_name'} ne 'communities') &&
20655:             ($env{'form.addcategory_name'} ne 'placement')) {
20656:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
20657:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
20658:             $adds{$newitem} = 1;
20659:         }
20660:     }
20661:     my $putresult;
20662:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20663:         if (keys(%deletions) > 0) {
20664:             foreach my $key (keys(%deletions)) {
20665:                 if ($predelallitems{$key} ne '') {
20666:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
20667:                 }
20668:             }
20669:         }
20670:         my (@chkcats,@chktrails,%chkallitems);
20671:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
20672:         if (ref($chkcats[0]) eq 'ARRAY') {
20673:             my $depth = 0;
20674:             my $chg = 0;
20675:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
20676:                 my $name = $chkcats[0][$i];
20677:                 my $item;
20678:                 if ($name eq '') {
20679:                     $chg ++;
20680:                 } else {
20681:                     $item = &escape($name).'::0';
20682:                     if ($chg) {
20683:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
20684:                     }
20685:                     $depth ++; 
20686:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
20687:                     $depth --;
20688:                 }
20689:             }
20690:         }
20691:     }
20692:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20693:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
20694:         if ($putresult eq 'ok') {
20695:             my %title = (
20696:                          togglecats     => 'Show/Hide a course in catalog',
20697:                          categorize     => 'Assign a category to a course',
20698:                          togglecatscomm => 'Show/Hide a community in catalog',
20699:                          categorizecomm => 'Assign a category to a community',
20700:                         );
20701:             my %level = (
20702:                          dom  => 'set in Domain ("Modify Course/Community")',
20703:                          crs  => 'set in Course ("Course Configuration")',
20704:                          comm => 'set in Community ("Community Configuration")',
20705:                          none     => 'No catalog',
20706:                          std      => 'Standard catalog',
20707:                          domonly  => 'Domain-only catalog',
20708:                          codesrch => 'Code search form',
20709:                         );
20710:             $resulttext = &mt('Changes made:').'<ul>';
20711:             if ($changes{'togglecats'}) {
20712:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
20713:             }
20714:             if ($changes{'categorize'}) {
20715:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
20716:             }
20717:             if ($changes{'togglecatscomm'}) {
20718:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
20719:             }
20720:             if ($changes{'categorizecomm'}) {
20721:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
20722:             }
20723:             if ($changes{'unauth'}) {
20724:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
20725:             }
20726:             if ($changes{'auth'}) {
20727:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
20728:             }
20729:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20730:                 my $cathash;
20731:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20732:                     $cathash = $domconfig{'coursecategories'}{'cats'};
20733:                 } else {
20734:                     $cathash = {};
20735:                 } 
20736:                 my (@cats,@trails,%allitems);
20737:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
20738:                 if (keys(%deletions) > 0) {
20739:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
20740:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
20741:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
20742:                     }
20743:                     $resulttext .= '</ul></li>';
20744:                 }
20745:                 if (keys(%reorderings) > 0) {
20746:                     my %sort_by_trail;
20747:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
20748:                     foreach my $key (keys(%reorderings)) {
20749:                         if ($allitems{$key} ne '') {
20750:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20751:                         }
20752:                     }
20753:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20754:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20755:                     }
20756:                     $resulttext .= '</ul></li>';
20757:                 }
20758:                 if (keys(%adds) > 0) {
20759:                     my %sort_by_trail;
20760:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
20761:                     foreach my $key (keys(%adds)) {
20762:                         if ($allitems{$key} ne '') {
20763:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20764:                         }
20765:                     }
20766:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20767:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20768:                     }
20769:                     $resulttext .= '</ul></li>';
20770:                 }
20771:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20772:                 if (ref($lastactref) eq 'HASH') {
20773:                     $lastactref->{'cats'} = 1;
20774:                 }
20775:             }
20776:             $resulttext .= '</ul>';
20777:             if ($changes{'unauth'} || $changes{'auth'}) {
20778:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20779:                 if ($changes{'auth'}) {
20780:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20781:                 }
20782:                 if ($changes{'unauth'}) {
20783:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20784:                 }
20785:                 my $cachetime = 24*60*60;
20786:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20787:                 if (ref($lastactref) eq 'HASH') {
20788:                     $lastactref->{'domdefaults'} = 1;
20789:                 }
20790:             }
20791:         } else {
20792:             $resulttext = '<span class="LC_error">'.
20793:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20794:         }
20795:     } else {
20796:         $resulttext = &mt('No changes made to course and community categories');
20797:     }
20798:     return $resulttext;
20799: }
20800: 
20801: sub modify_serverstatuses {
20802:     my ($dom,%domconfig) = @_;
20803:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20804:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20805:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20806:     }
20807:     my @pages = &serverstatus_pages();
20808:     foreach my $type (@pages) {
20809:         $newserverstatus{$type}{'namedusers'} = '';
20810:         $newserverstatus{$type}{'machines'} = '';
20811:         if (defined($env{'form.'.$type.'_namedusers'})) {
20812:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20813:             my @okusers;
20814:             foreach my $user (@users) {
20815:                 my ($uname,$udom) = split(/:/,$user);
20816:                 if (($udom =~ /^$match_domain$/) &&   
20817:                     (&Apache::lonnet::domain($udom)) &&
20818:                     ($uname =~ /^$match_username$/)) {
20819:                     if (!grep(/^\Q$user\E/,@okusers)) {
20820:                         push(@okusers,$user);
20821:                     }
20822:                 }
20823:             }
20824:             if (@okusers > 0) {
20825:                  @okusers = sort(@okusers);
20826:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20827:             }
20828:         }
20829:         if (defined($env{'form.'.$type.'_machines'})) {
20830:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20831:             my @okmachines;
20832:             foreach my $ip (@machines) {
20833:                 my @parts = split(/\./,$ip);
20834:                 next if (@parts < 4);
20835:                 my $badip = 0;
20836:                 for (my $i=0; $i<4; $i++) {
20837:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20838:                         $badip = 1;
20839:                         last;
20840:                     }
20841:                 }
20842:                 if (!$badip) {
20843:                     push(@okmachines,$ip);     
20844:                 }
20845:             }
20846:             @okmachines = sort(@okmachines);
20847:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20848:         }
20849:     }
20850:     my %serverstatushash =  (
20851:                                 serverstatuses => \%newserverstatus,
20852:                             );
20853:     foreach my $type (@pages) {
20854:         foreach my $setting ('namedusers','machines') {
20855:             my (@current,@new);
20856:             if (ref($currserverstatus{$type}) eq 'HASH') {
20857:                 if ($currserverstatus{$type}{$setting} ne '') { 
20858:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20859:                 }
20860:             }
20861:             if ($newserverstatus{$type}{$setting} ne '') {
20862:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20863:             }
20864:             if (@current > 0) {
20865:                 if (@new > 0) {
20866:                     foreach my $item (@current) {
20867:                         if (!grep(/^\Q$item\E$/,@new)) {
20868:                             $changes{$type}{$setting} = 1;
20869:                             last;
20870:                         }
20871:                     }
20872:                     foreach my $item (@new) {
20873:                         if (!grep(/^\Q$item\E$/,@current)) {
20874:                             $changes{$type}{$setting} = 1;
20875:                             last;
20876:                         }
20877:                     }
20878:                 } else {
20879:                     $changes{$type}{$setting} = 1;
20880:                 }
20881:             } elsif (@new > 0) {
20882:                 $changes{$type}{$setting} = 1;
20883:             }
20884:         }
20885:     }
20886:     if (keys(%changes) > 0) {
20887:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20888:         my $putresult = &Apache::lonnet::put_dom('configuration',
20889:                                                  \%serverstatushash,$dom);
20890:         if ($putresult eq 'ok') {
20891:             $resulttext .= &mt('Changes made:').'<ul>';
20892:             foreach my $type (@pages) {
20893:                 if (ref($changes{$type}) eq 'HASH') {
20894:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20895:                     if ($changes{$type}{'namedusers'}) {
20896:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20897:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20898:                         } else {
20899:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20900:                         }
20901:                     }
20902:                     if ($changes{$type}{'machines'}) {
20903:                         if ($newserverstatus{$type}{'machines'} eq '') {
20904:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20905:                         } else {
20906:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20907:                         }
20908: 
20909:                     }
20910:                     $resulttext .= '</ul></li>';
20911:                 }
20912:             }
20913:             $resulttext .= '</ul>';
20914:         } else {
20915:             $resulttext = '<span class="LC_error">'.
20916:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20917: 
20918:         }
20919:     } else {
20920:         $resulttext = &mt('No changes made to access to server status pages');
20921:     }
20922:     return $resulttext;
20923: }
20924: 
20925: sub modify_helpsettings {
20926:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20927:     my ($resulttext,$errors,%changes,%helphash);
20928:     my %defaultchecked = ('submitbugs' => 'on');
20929:     my @offon = ('off','on');
20930:     my @toggles = ('submitbugs');
20931:     my %current = ('submitbugs' => '',
20932:                    'adhoc'      => {},
20933:                   );
20934:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20935:         %current = %{$domconfig{'helpsettings'}};
20936:     }
20937:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20938:     foreach my $item (@toggles) {
20939:         if ($defaultchecked{$item} eq 'on') { 
20940:             if ($current{$item} eq '') {
20941:                 if ($env{'form.'.$item} eq '0') {
20942:                     $changes{$item} = 1;
20943:                 }
20944:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20945:                 $changes{$item} = 1;
20946:             }
20947:         } elsif ($defaultchecked{$item} eq 'off') {
20948:             if ($current{$item} eq '') {
20949:                 if ($env{'form.'.$item} eq '1') {
20950:                     $changes{$item} = 1;
20951:                 }
20952:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20953:                 $changes{$item} = 1;
20954:             }
20955:         }
20956:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20957:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20958:         }
20959:     }
20960:     my $maxnum = $env{'form.helproles_maxnum'};
20961:     my $confname = $dom.'-domainconfig';
20962:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20963:     my (@allpos,%newsettings,%changedprivs,$newrole);
20964:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20965:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20966:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20967:     my %lt = &Apache::lonlocal::texthash(
20968:                     s      => 'system',
20969:                     d      => 'domain',
20970:                     order  => 'Display order',
20971:                     access => 'Role usage',
20972:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20973:                     dh     => 'All with domain helpdesk role',
20974:                     da     => 'All with domain helpdesk assistant role',
20975:                     none   => 'None',
20976:                     status => 'Determined based on institutional status',
20977:                     inc    => 'Include all, but exclude specific personnel',
20978:                     exc    => 'Exclude all, but include specific personnel',
20979:     );
20980:     for (my $num=0; $num<=$maxnum; $num++) {
20981:         my ($prefix,$identifier,$rolename,%curr);
20982:         if ($num == $maxnum) {
20983:             next unless ($env{'form.newcusthelp'} == $maxnum);
20984:             $identifier = 'custhelp'.$num;
20985:             $prefix = 'helproles_'.$num;
20986:             $rolename = $env{'form.custhelpname'.$num};
20987:             $rolename=~s/[^A-Za-z0-9]//gs;
20988:             next if ($rolename eq '');
20989:             next if (exists($existing{'rolesdef_'.$rolename}));
20990:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20991:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20992:                                                      $newprivs{'c'},$confname,$dom);
20993:             if ($result ne 'ok') {
20994:                 $errors .= '<li><span class="LC_error">'.
20995:                            &mt('An error occurred storing the new custom role: [_1]',
20996:                            $result).'</span></li>';
20997:                 next;
20998:             } else {
20999:                 $changedprivs{$rolename} = \%newprivs;
21000:                 $newrole = $rolename;
21001:             }
21002:         } else {
21003:             $prefix = 'helproles_'.$num;
21004:             $rolename = $env{'form.'.$prefix};
21005:             next if ($rolename eq '');
21006:             next unless (exists($existing{'rolesdef_'.$rolename}));
21007:             $identifier = 'custhelp'.$num;
21008:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
21009:             my %currprivs;
21010:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
21011:                 split(/\_/,$existing{'rolesdef_'.$rolename});
21012:             foreach my $level ('c','d','s') {
21013:                 if ($newprivs{$level} ne $currprivs{$level}) {
21014:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
21015:                                                              $newprivs{'c'},$confname,$dom);
21016:                     if ($result ne 'ok') {
21017:                         $errors .= '<li><span class="LC_error">'.
21018:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
21019:                                        $rolename,$result).'</span></li>';
21020:                     } else {
21021:                         $changedprivs{$rolename} = \%newprivs;
21022:                     }
21023:                     last;
21024:                 }
21025:             }
21026:             if (ref($current{'adhoc'}) eq 'HASH') {
21027:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21028:                     %curr = %{$current{'adhoc'}{$rolename}};
21029:                 }
21030:             }
21031:         }
21032:         my $newpos = $env{'form.'.$prefix.'_pos'};
21033:         $newpos =~ s/\D+//g;
21034:         $allpos[$newpos] = $rolename;
21035:         my $newdesc = $env{'form.'.$prefix.'_desc'};
21036:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
21037:         if ($curr{'desc'}) {
21038:             if ($curr{'desc'} ne $newdesc) {
21039:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
21040:                 $newsettings{$rolename}{'desc'} = $newdesc;
21041:             }
21042:         } elsif ($newdesc ne '') {
21043:             $changes{'customrole'}{$rolename}{'desc'} = 1;
21044:             $newsettings{$rolename}{'desc'} = $newdesc;
21045:         }
21046:         my $access = $env{'form.'.$prefix.'_access'};
21047:         if (grep(/^\Q$access\E$/,@accesstypes)) {
21048:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
21049:             if ($access eq 'status') {
21050:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
21051:                 if (scalar(@statuses) == 0) {
21052:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
21053:                 } else {
21054:                     my (@shownstatus,$numtypes);
21055:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21056:                     if (ref($types) eq 'ARRAY') {
21057:                         $numtypes = scalar(@{$types});
21058:                         foreach my $type (sort(@statuses)) {
21059:                             if ($type eq 'default') {
21060:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21061:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
21062:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21063:                                 push(@shownstatus,$usertypes->{$type});
21064:                             }
21065:                         }
21066:                     }
21067:                     if (grep(/^default$/,@statuses)) {
21068:                         push(@shownstatus,$othertitle);
21069:                     }
21070:                     if (scalar(@shownstatus) == 1+$numtypes) {
21071:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
21072:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
21073:                     } else {
21074:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
21075:                         if (ref($curr{'status'}) eq 'ARRAY') {
21076:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21077:                             if (@diffs) {
21078:                                 $changes{'customrole'}{$rolename}{$access} = 1;
21079:                             }
21080:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21081:                             $changes{'customrole'}{$rolename}{$access} = 1;
21082:                         }
21083:                     }
21084:                 }
21085:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
21086:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
21087:                 my @newspecstaff;
21088:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21089:                 foreach my $person (sort(@personnel)) {
21090:                     if ($domhelpdesk{$person}) {
21091:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
21092:                     }
21093:                 }
21094:                 if (ref($curr{$access}) eq 'ARRAY') {
21095:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21096:                     if (@diffs) {
21097:                         $changes{'customrole'}{$rolename}{$access} = 1;
21098:                     }
21099:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21100:                     $changes{'customrole'}{$rolename}{$access} = 1;
21101:                 }
21102:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21103:                     my ($uname,$udom) = split(/:/,$person);
21104:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
21105:                 }
21106:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
21107:             }
21108:         } else {
21109:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
21110:         }
21111:         unless ($curr{'access'} eq $access) {
21112:             $changes{'customrole'}{$rolename}{'access'} = 1;
21113:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
21114:         }
21115:     }
21116:     if (@allpos > 0) {
21117:         my $idx = 0;
21118:         foreach my $rolename (@allpos) {
21119:             if ($rolename ne '') {
21120:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
21121:                 if (ref($current{'adhoc'}) eq 'HASH') {
21122:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21123:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
21124:                             $changes{'customrole'}{$rolename}{'order'} = 1;
21125:                             $newsettings{$rolename}{'order'} = $idx+1;
21126:                         }
21127:                     }
21128:                 }
21129:                 $idx ++;
21130:             }
21131:         }
21132:     }
21133:     my $putresult;
21134:     if (keys(%changes) > 0) {
21135:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
21136:         if ($putresult eq 'ok') {
21137:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
21138:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
21139:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
21140:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
21141:                 }
21142:             }
21143:             my $cachetime = 24*60*60;
21144:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21145:             if (ref($lastactref) eq 'HASH') {
21146:                 $lastactref->{'domdefaults'} = 1;
21147:             }
21148:         } else {
21149:             $errors .= '<li><span class="LC_error">'.
21150:                        &mt('An error occurred storing the settings: [_1]',
21151:                            $putresult).'</span></li>';
21152:         }
21153:     }
21154:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
21155:         $resulttext = &mt('Changes made:').'<ul>';
21156:         my (%shownprivs,@levelorder);
21157:         @levelorder = ('c','d','s');
21158:         if ((keys(%changes)) && ($putresult eq 'ok')) {
21159:             foreach my $item (sort(keys(%changes))) {
21160:                 if ($item eq 'submitbugs') {
21161:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
21162:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
21163:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
21164:                 } elsif ($item eq 'customrole') {
21165:                     if (ref($changes{'customrole'}) eq 'HASH') {
21166:                         my @keyorder = ('order','desc','access','status','exc','inc');
21167:                         my %keytext = &Apache::lonlocal::texthash(
21168:                                                                    order  => 'Order',
21169:                                                                    desc   => 'Role description',
21170:                                                                    access => 'Role usage',
21171:                                                                    status => 'Allowed institutional types',
21172:                                                                    exc    => 'Allowed personnel',
21173:                                                                    inc    => 'Disallowed personnel',
21174:                         );
21175:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
21176:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
21177:                                 if ($role eq $newrole) {
21178:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
21179:                                                               $role).'<ul>';
21180:                                 } else {
21181:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21182:                                                               $role).'<ul>';
21183:                                 }
21184:                                 foreach my $key (@keyorder) {
21185:                                     if ($changes{'customrole'}{$role}{$key}) {
21186:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
21187:                                                                   $keytext{$key},$newsettings{$role}{$key}).
21188:                                                        '</li>';
21189:                                     }
21190:                                 }
21191:                                 if (ref($changedprivs{$role}) eq 'HASH') {
21192:                                     $shownprivs{$role} = 1;
21193:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
21194:                                     foreach my $level (@levelorder) {
21195:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21196:                                             next if ($item eq '');
21197:                                             my ($priv) = split(/\&/,$item,2);
21198:                                             if (&Apache::lonnet::plaintext($priv)) {
21199:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21200:                                                 unless ($level eq 'c') {
21201:                                                     $resulttext .= ' ('.$lt{$level}.')';
21202:                                                 }
21203:                                                 $resulttext .= '</li>';
21204:                                             }
21205:                                         }
21206:                                     }
21207:                                     $resulttext .= '</ul>';
21208:                                 }
21209:                                 $resulttext .= '</ul></li>';
21210:                             }
21211:                         }
21212:                     }
21213:                 }
21214:             }
21215:         }
21216:         if (keys(%changedprivs)) {
21217:             foreach my $role (sort(keys(%changedprivs))) {
21218:                 unless ($shownprivs{$role}) {
21219:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21220:                                               $role).'<ul>'.
21221:                                    '<li>'.&mt('Privileges set to :').'<ul>';
21222:                     foreach my $level (@levelorder) {
21223:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21224:                             next if ($item eq '');
21225:                             my ($priv) = split(/\&/,$item,2);
21226:                             if (&Apache::lonnet::plaintext($priv)) {
21227:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21228:                                 unless ($level eq 'c') {
21229:                                     $resulttext .= ' ('.$lt{$level}.')';
21230:                                 }
21231:                                 $resulttext .= '</li>';
21232:                             }
21233:                         }
21234:                     }
21235:                     $resulttext .= '</ul></li></ul></li>';
21236:                 }
21237:             }
21238:         }
21239:         $resulttext .= '</ul>';
21240:     } else {
21241:         $resulttext = &mt('No changes made to help settings');
21242:     }
21243:     if ($errors) {
21244:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
21245:                                     $errors.'</ul>';
21246:     }
21247:     return $resulttext;
21248: }
21249: 
21250: sub modify_coursedefaults {
21251:     my ($dom,$lastactref,%domconfig) = @_;
21252:     my ($resulttext,$errors,%changes,%defaultshash);
21253:     my %defaultchecked = (
21254:                            'canuse_pdfforms' => 'off',
21255:                            'uselcmath'       => 'on',
21256:                            'usejsme'         => 'on',
21257:                            'inline_chem'     => 'on',
21258:                            'ltiauth'         => 'off',
21259:                          );
21260:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
21261:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
21262:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
21263:                    'coursequota_official','coursequota_unofficial','coursequota_community',
21264:                    'coursequota_textbook','coursequota_placement','mysqltables_official',
21265:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook',
21266:                    'mysqltables_placement');
21267:     my @types = ('official','unofficial','community','textbook','placement');
21268:     my %staticdefaults = (
21269:                            anonsurvey_threshold => 10,
21270:                            uploadquota          => 500,
21271:                            coursequota          => 20,
21272:                            postsubmit           => 60,
21273:                            mysqltables          => 172800,
21274:                            domexttool           => 1,
21275:                            crsauthor            => 1,
21276:                            crseditors           => ['edit','xml'],
21277:                          );
21278:     my %texoptions = (
21279:                         MathJax  => 'MathJax',
21280:                         mimetex  => &mt('Convert to Images'),
21281:                         tth      => &mt('TeX to HTML'),
21282:                      );
21283: 
21284:     my @editors = ('edit','xml','daxe');
21285:     my %editornames = &crseditor_titles();
21286: 
21287:     $defaultshash{'coursedefaults'} = {};
21288: 
21289:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
21290:         if ($domconfig{'coursedefaults'} eq '') {
21291:             $domconfig{'coursedefaults'} = {};
21292:         }
21293:     }
21294: 
21295:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
21296:         foreach my $item (@toggles) {
21297:             if ($defaultchecked{$item} eq 'on') {
21298:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21299:                     ($env{'form.'.$item} eq '0')) {
21300:                     $changes{$item} = 1;
21301:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21302:                     $changes{$item} = 1;
21303:                 }
21304:             } elsif ($defaultchecked{$item} eq 'off') {
21305:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21306:                     ($env{'form.'.$item} eq '1')) {
21307:                     $changes{$item} = 1;
21308:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21309:                     $changes{$item} = 1;
21310:                 }
21311:             }
21312:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
21313:         }
21314:         foreach my $item (@numbers) {
21315:             my ($currdef,$newdef);
21316:             $newdef = $env{'form.'.$item};
21317:             if ($item eq 'anonsurvey_threshold') {
21318:                 $currdef = $domconfig{'coursedefaults'}{$item};
21319:                 $newdef =~ s/\D//g;
21320:                 if ($newdef eq '' || $newdef < 1) {
21321:                     $newdef = 1;
21322:                 }
21323:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
21324:             } else {
21325:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
21326:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
21327:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
21328:                 }
21329:                 $newdef =~ s/[^\w.\-]//g;
21330:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
21331:             }
21332:             if ($currdef ne $newdef) {
21333:                 if ($item eq 'anonsurvey_threshold') {
21334:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
21335:                         $changes{$item} = 1;
21336:                     }
21337:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
21338:                     my $setting = $1;
21339:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
21340:                         $changes{$setting} = 1;
21341:                     }
21342:                 }
21343:             }
21344:         }
21345:         my $texengine;
21346:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
21347:             $texengine = $env{'form.texengine'};
21348:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
21349:             if ($currdef eq '') {
21350:                 unless ($texengine eq $Apache::lonnet::deftex) {
21351:                     $changes{'texengine'} = 1;
21352:                 }
21353:             } elsif ($currdef ne $texengine) {
21354:                 $changes{'texengine'} = 1;
21355:             }
21356:         }
21357:         if ($texengine ne '') {
21358:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
21359:         }
21360:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
21361:         my @currclonecode;
21362:         if (ref($currclone) eq 'HASH') {
21363:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
21364:                 @currclonecode = @{$currclone->{'instcode'}};
21365:             }
21366:         }
21367:         my $newclone;
21368:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
21369:             $newclone = $env{'form.canclone'};
21370:         }
21371:         if ($newclone eq 'instcode') {
21372:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
21373:             my (%codedefaults,@code_order,@clonecode);
21374:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
21375:                                                     \@code_order);
21376:             foreach my $item (@code_order) {
21377:                 if (grep(/^\Q$item\E$/,@newcodes)) {
21378:                     push(@clonecode,$item);
21379:                 }
21380:             }
21381:             if (@clonecode) {
21382:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
21383:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
21384:                 if (@diffs) {
21385:                     $changes{'canclone'} = 1;
21386:                 }
21387:             } else {
21388:                 $newclone eq '';
21389:             }
21390:         } elsif ($newclone ne '') {
21391:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
21392:         }
21393:         if ($newclone ne $currclone) {
21394:             $changes{'canclone'} = 1;
21395:         }
21396:         my %credits;
21397:         foreach my $type (@types) {
21398:             unless ($type eq 'community') {
21399:                 $credits{$type} = $env{'form.'.$type.'_credits'};
21400:                 $credits{$type} =~ s/[^\d.]+//g;
21401:             }
21402:         }
21403:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
21404:             ($env{'form.coursecredits'} eq '1')) {
21405:             $changes{'coursecredits'} = 1;
21406:             foreach my $type (keys(%credits)) {
21407:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21408:             }
21409:         } else {
21410:             if ($env{'form.coursecredits'} eq '1') {
21411:                 foreach my $type (@types) {
21412:                     unless ($type eq 'community') {
21413:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
21414:                             $changes{'coursecredits'} = 1;
21415:                         }
21416:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21417:                     }
21418:                 }
21419:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21420:                 foreach my $type (@types) {
21421:                     unless ($type eq 'community') {
21422:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
21423:                             $changes{'coursecredits'} = 1;
21424:                             last;
21425:                         }
21426:                     }
21427:                 }
21428:             }
21429:         }
21430:         if ($env{'form.postsubmit'} eq '1') {
21431:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
21432:             my %currtimeout;
21433:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21434:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
21435:                     $changes{'postsubmit'} = 1;
21436:                 }
21437:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21438:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
21439:                 }
21440:             } else {
21441:                 $changes{'postsubmit'} = 1;
21442:             }
21443:             foreach my $type (@types) {
21444:                 my $timeout = $env{'form.'.$type.'_timeout'};
21445:                 $timeout =~ s/\D//g;
21446:                 if ($timeout == $staticdefaults{'postsubmit'}) {
21447:                     $timeout = '';
21448:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
21449:                     $timeout = '0';
21450:                 }
21451:                 unless ($timeout eq '') {
21452:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
21453:                 }
21454:                 if (exists($currtimeout{$type})) {
21455:                     if ($timeout ne $currtimeout{$type}) {
21456:                         $changes{'postsubmit'} = 1;
21457:                     }
21458:                 } elsif ($timeout ne '') {
21459:                     $changes{'postsubmit'} = 1;
21460:                 }
21461:             }
21462:         } else {
21463:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
21464:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21465:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
21466:                     $changes{'postsubmit'} = 1;
21467:                 }
21468:             } else {
21469:                 $changes{'postsubmit'} = 1;
21470:             }
21471:         }
21472:         my (%newdomexttool,%newexttool,%newcrsauthor,%olddomexttool,%oldexttool,%oldcrsauthor,
21473:             %posscrseditors);
21474:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
21475:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
21476:         map { $newcrsauthor{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crsauthor');
21477:         map { $posscrseditors{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crseditors');
21478:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21479:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
21480:         } else {
21481:            foreach my $type (@types) {
21482:                if ($staticdefaults{'domexttool'}) {
21483:                    $olddomexttool{$type} = 1;
21484:                } else {
21485:                    $olddomexttool{$type} = 0;
21486:                }
21487:             }
21488:         }
21489:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
21490:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
21491:         } else {
21492:             foreach my $type (@types) {
21493:                if ($staticdefaults{'exttool'}) {
21494:                    $oldexttool{$type} = 1;
21495:                } else {
21496:                    $oldexttool{$type} = 0;
21497:                }
21498:             }
21499:         }
21500:         if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21501:             %oldcrsauthor = %{$domconfig{'coursedefaults'}{'crsauthor'}};
21502:         } else {
21503:             foreach my $type (@types) {
21504:                if ($staticdefaults{'crsauthor'}) {
21505:                    $oldcrsauthor{$type} = 1;
21506:                } else {
21507:                    $oldcrsauthor{$type} = 0;
21508:                }
21509:             }
21510:         }
21511:         my @newcrseditors = ();
21512:         foreach my $editor (@editors) {
21513:             if ($posscrseditors{$editor}) {
21514:                 push(@newcrseditors,$editor);
21515:             }
21516:         }
21517:         if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21518:             my @diffs =
21519:                 &Apache::loncommon::compare_arrays($domconfig{'coursedefaults'}{'crseditors'},
21520:                                                    \@newcrseditors);
21521:             if (@diffs) {
21522:                 $changes{'crseditors'} = 1; 
21523:             }
21524:         } else {
21525:             my @diffs =
21526:                 &Apache::loncommon::compare_arrays($staticdefaults{'crseditors'},
21527:                                                    \@newcrseditors);
21528:             unless (@diffs == 0) {
21529:                 $changes{'crseditors'} = 1;
21530:             }       
21531:         }
21532:         foreach my $type (@types) {
21533:             unless ($newdomexttool{$type}) {
21534:                 $newdomexttool{$type} = 0;
21535:             }
21536:             unless ($newexttool{$type}) {
21537:                 $newexttool{$type} = 0;
21538:             }
21539:             unless ($newcrsauthor{$type}) {
21540:                 $newcrsauthor{$type} = 0;
21541:             }
21542:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
21543:                 $changes{'domexttool'} = 1;
21544:             }
21545:             if ($newexttool{$type} != $oldexttool{$type}) {
21546:                 $changes{'exttool'} = 1;
21547:             }
21548:             if ($newcrsauthor{$type} != $oldcrsauthor{$type}) {
21549:                 $changes{'crsauthor'} = 1;
21550:             }
21551:         }
21552:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
21553:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
21554:         $defaultshash{'coursedefaults'}{'crsauthor'} = \%newcrsauthor;
21555:         $defaultshash{'coursedefaults'}{'crseditors'} = \@newcrseditors;
21556:     }
21557:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21558:                                              $dom);
21559:     if ($putresult eq 'ok') {
21560:         if (keys(%changes) > 0) {
21561:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21562:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
21563:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
21564:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
21565:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
21566:                 ($changes{'exttool'}) || ($changes{'coursequota'}) || ($changes{'crsauthor'})) {
21567:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
21568:                                   'ltiauth') {
21569:                     if ($changes{$item}) {
21570:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
21571:                     }
21572:                 }
21573:                 if ($changes{'coursecredits'}) {
21574:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21575:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
21576:                             $domdefaults{$type.'credits'} =
21577:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
21578:                         }
21579:                     }
21580:                 }
21581:                 if ($changes{'postsubmit'}) {
21582:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21583:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
21584:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21585:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
21586:                                 $domdefaults{$type.'postsubtimeout'} =
21587:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21588:                             }
21589:                         }
21590:                     }
21591:                 }
21592:                 if ($changes{'uploadquota'}) {
21593:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21594:                         foreach my $type (@types) {
21595:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
21596:                         }
21597:                     }
21598:                 }
21599:                 if ($changes{'coursequota'}) {
21600:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21601:                         foreach my $type (@types) {
21602:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
21603:                         }
21604:                     }
21605:                 }
21606:                 if ($changes{'canclone'}) {
21607:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21608:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21609:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
21610:                             if (@clonecodes) {
21611:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
21612:                             }
21613:                         }
21614:                     } else {
21615:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
21616:                     }
21617:                 }
21618:                 if ($changes{'domexttool'}) {
21619:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21620:                         foreach my $type (@types) {
21621:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
21622:                         }
21623:                     }
21624:                 }
21625:                 if ($changes{'exttool'}) {
21626:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21627:                         foreach my $type (@types) {
21628:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
21629:                         }
21630:                     }
21631:                 }
21632:                 if ($changes{'crsauthor'}) {
21633:                     if (ref($defaultshash{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21634:                         foreach my $type (@types) {
21635:                             $domdefaults{$type.'crsauthor'}=$defaultshash{'coursedefaults'}{'crsauthor'}{$type};
21636:                         }
21637:                     }
21638:                 }
21639:                 if ($changes{'crseditors'}) {
21640:                     if (ref($defaultshash{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21641:                         $domdefaults{'crseditors'}=join(',',@{$defaultshash{'coursedefaults'}{'crseditors'}});
21642:                     }
21643:                 }
21644:                 my $cachetime = 24*60*60;
21645:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21646:                 if (ref($lastactref) eq 'HASH') {
21647:                     $lastactref->{'domdefaults'} = 1;
21648:                 }
21649:             }
21650:             $resulttext = &mt('Changes made:').'<ul>';
21651:             foreach my $item (sort(keys(%changes))) {
21652:                 if ($item eq 'canuse_pdfforms') {
21653:                     if ($env{'form.'.$item} eq '1') {
21654:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
21655:                     } else {
21656:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
21657:                     }
21658:                 } elsif ($item eq 'uselcmath') {
21659:                     if ($env{'form.'.$item} eq '1') {
21660:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
21661:                     } else {
21662:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
21663:                     }
21664:                 } elsif ($item eq 'usejsme') {
21665:                     if ($env{'form.'.$item} eq '1') {
21666:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
21667:                     } else {
21668:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
21669:                     }
21670:                 } elsif ($item eq 'inline_chem') {
21671:                     if ($env{'form.'.$item} eq '1') {
21672:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
21673:                     } else {
21674:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
21675:                     }
21676:                 } elsif ($item eq 'texengine') {
21677:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
21678:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
21679:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
21680:                     }
21681:                 } elsif ($item eq 'anonsurvey_threshold') {
21682:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
21683:                 } elsif ($item eq 'uploadquota') {
21684:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21685:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
21686:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
21687:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
21688:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
21689:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
21690:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
21691:                                        '</ul>'.
21692:                                        '</li>';
21693:                     } else {
21694:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
21695:                     }
21696:                 } elsif ($item eq 'coursequota') {
21697:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21698:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
21699:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
21700:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
21701:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
21702:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'placement'}.'</b>').'</li>'.
21703:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
21704:                                        '</ul>'.
21705:                                        '</li>';
21706:                     } else {
21707:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
21708:                     }
21709:                 } elsif ($item eq 'mysqltables') {
21710:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
21711:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
21712:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
21713:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
21714:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
21715:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
21716:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
21717:                                        '</ul>'.
21718:                                        '</li>';
21719:                     } else {
21720:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
21721:                     }
21722:                 } elsif ($item eq 'postsubmit') {
21723:                     if ($domdefaults{'postsubmit'} eq 'off') {
21724:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
21725:                     } else {
21726:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
21727:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21728:                             $resulttext .= &mt('durations:').'<ul>';
21729:                             foreach my $type (@types) {
21730:                                 $resulttext .= '<li>';
21731:                                 my $timeout;
21732:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21733:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21734:                                 }
21735:                                 my $display;
21736:                                 if ($timeout eq '0') {
21737:                                     $display = &mt('unlimited');
21738:                                 } elsif ($timeout eq '') {
21739:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
21740:                                 } else {
21741:                                     $display = &mt('[quant,_1,second]',$timeout);
21742:                                 }
21743:                                 if ($type eq 'community') {
21744:                                     $resulttext .= &mt('Communities');
21745:                                 } elsif ($type eq 'official') {
21746:                                     $resulttext .= &mt('Official courses');
21747:                                 } elsif ($type eq 'unofficial') {
21748:                                     $resulttext .= &mt('Unofficial courses');
21749:                                 } elsif ($type eq 'textbook') {
21750:                                     $resulttext .= &mt('Textbook courses');
21751:                                 } elsif ($type eq 'placement') {
21752:                                     $resulttext .= &mt('Placement tests');
21753:                                 }
21754:                                 $resulttext .= ' -- '.$display.'</li>';
21755:                             }
21756:                             $resulttext .= '</ul>';
21757:                         }
21758:                         $resulttext .= '</li>';
21759:                     }
21760:                 } elsif ($item eq 'coursecredits') {
21761:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21762:                         if (($domdefaults{'officialcredits'} eq '') &&
21763:                             ($domdefaults{'unofficialcredits'} eq '') &&
21764:                             ($domdefaults{'textbookcredits'} eq '')) {
21765:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21766:                         } else {
21767:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
21768:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
21769:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
21770:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
21771:                                            '</ul>'.
21772:                                            '</li>';
21773:                         }
21774:                     } else {
21775:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21776:                     }
21777:                 } elsif ($item eq 'canclone') {
21778:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21779:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21780:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
21781:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
21782:                         }
21783:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
21784:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
21785:                     } else {
21786:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
21787:                     }
21788:                 } elsif ($item eq 'ltiauth') {
21789:                     if ($env{'form.'.$item} eq '1') {
21790:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
21791:                     } else {
21792:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
21793:                     }
21794:                 } elsif (($item eq 'domexttool') || ($item eq 'exttool') || ($item eq 'crsauthor')) {
21795:                     my @noyes = (&mt('no'),&mt('yes'));
21796:                     my %status = (
21797:                                    domexttool => {
21798:                                                   ishash => &mt('External Tools defined in the domain may be used as follows:'), 
21799:                                                   default => &mt('External Tools defined in the domain may be used in all course types, by default'),
21800:                                                  },
21801:                                    exttool => {
21802:                                                   ishash => &mt('External Tools can be defined and configured in course containers as follows:'),
21803:                                                   default => &mt('External Tools can not be defined in any course types, by default'),
21804:                                               },
21805:                                    crsauthor => {
21806:                                                   ishash => &mt('Standard Problems can be created within course containers as follows:'),
21807:                                                   default => &mt('Standard Problems can be created within any course type, by default'),
21808:                                                 },
21809:                                  );
21810:                                    
21811:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'HASH') {
21812:                         $resulttext .= '<li>'.$status{$item}{'ishash'}.'<ul>'.
21813:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'official'}].'</b>').'</li>'.
21814:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'unofficial'}].'</b>').'</li>'.
21815:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'textbook'}].'</b>').'</li>'.
21816:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'placement'}].'</b>').'</li>'.
21817:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'community'}].'</b>').'</li>'.
21818:                                        '</ul>'.
21819:                                        '</li>';
21820:                     } else {
21821:                         $resulttext .= '<li>'.$status{$item}{'default'}.'</li>';
21822:                     }
21823:                 } elsif ($item eq 'crseditors') {
21824:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'ARRAY') {
21825:                         my $shown;
21826:                         if (@{$defaultshash{'coursedefaults'}{$item}}) {
21827:                             $shown = join(', ', map { $editornames{$_} } @{$defaultshash{'coursedefaults'}{$item}});
21828:                         } else {
21829:                             $shown = &mt('None');
21830:                         }
21831:                         $resulttext .= '<li>'.&mt('Available editors for course/community resources: [_1]',$shown).'</li>';
21832:                     }
21833:                 }
21834:             }
21835:             $resulttext .= '</ul>';
21836:         } else {
21837:             $resulttext = &mt('No changes made to course defaults');
21838:         }
21839:     } else {
21840:         $resulttext = '<span class="LC_error">'.
21841:             &mt('An error occurred: [_1]',$putresult).'</span>';
21842:     }
21843:     return $resulttext;
21844: }
21845: 
21846: sub modify_selfenrollment {
21847:     my ($dom,$lastactref,%domconfig) = @_;
21848:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
21849:     my @types = ('official','unofficial','community','textbook','placement');
21850:     my %titles = &tool_titles();
21851:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
21852:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
21853:     $ordered{'default'} = ['types','registered','approval','limit'];
21854: 
21855:     my (%roles,%shown,%toplevel);
21856:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
21857: 
21858:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
21859:         if ($domconfig{'selfenrollment'} eq '') {
21860:             $domconfig{'selfenrollment'} = {};
21861:         }
21862:     }
21863:     %toplevel = (
21864:                   admin      => 'Configuration Rights',
21865:                   default    => 'Default settings',
21866:                   validation => 'Validation of self-enrollment requests',
21867:                 );
21868:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21869: 
21870:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21871:         foreach my $item (@{$ordered{'admin'}}) {
21872:             foreach my $type (@types) {
21873:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21874:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21875:                 } else {
21876:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21877:                 }
21878:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21879:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21880:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21881:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21882:                             push(@{$changes{'admin'}{$type}},$item);
21883:                         }
21884:                     } else {
21885:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21886:                             push(@{$changes{'admin'}{$type}},$item);
21887:                         }
21888:                     }
21889:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21890:                     push(@{$changes{'admin'}{$type}},$item);
21891:                 }
21892:             }
21893:         }
21894:     }
21895: 
21896:     foreach my $item (@{$ordered{'default'}}) {
21897:         foreach my $type (@types) {
21898:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21899:             if ($item eq 'types') {
21900:                 unless (($value eq 'all') || ($value eq 'dom')) {
21901:                     $value = '';
21902:                 }
21903:             } elsif ($item eq 'registered') {
21904:                 unless ($value eq '1') {
21905:                     $value = 0;
21906:                 }
21907:             } elsif ($item eq 'approval') {
21908:                 unless ($value =~ /^[012]$/) {
21909:                     $value = 0;
21910:                 }
21911:             } else {
21912:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21913:                     $value = 'none';
21914:                 }
21915:             }
21916:             $selfenrollhash{'default'}{$type}{$item} = $value;
21917:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21918:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21919:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21920:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21921:                          push(@{$changes{'default'}{$type}},$item);
21922:                     }
21923:                 } else {
21924:                     push(@{$changes{'default'}{$type}},$item);
21925:                 }
21926:             } else {
21927:                 push(@{$changes{'default'}{$type}},$item);
21928:             }
21929:             if ($item eq 'limit') {
21930:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21931:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21932:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21933:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21934:                     }
21935:                 } else {
21936:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21937:                 }
21938:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21939:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21940:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21941:                          push(@{$changes{'default'}{$type}},'cap');
21942:                     }
21943:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21944:                     push(@{$changes{'default'}{$type}},'cap');
21945:                 }
21946:             }
21947:         }
21948:     }
21949: 
21950:     foreach my $item (@{$itemsref}) {
21951:         if ($item eq 'fields') {
21952:             my @changed;
21953:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21954:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21955:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21956:             }
21957:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21958:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21959:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21960:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21961:                 } else {
21962:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21963:                 }
21964:             } else {
21965:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21966:             }
21967:             if (@changed) {
21968:                 if ($selfenrollhash{'validation'}{$item}) { 
21969:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21970:                 } else {
21971:                     $changes{'validation'}{$item} = &mt('None');
21972:                 }
21973:             }
21974:         } else {
21975:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
21976:             if ($item eq 'markup') {
21977:                if ($env{'form.selfenroll_validation_'.$item}) {
21978:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
21979:                }
21980:             }
21981:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21982:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
21983:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
21984:                 }
21985:             }
21986:         }
21987:     }
21988: 
21989:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
21990:                                              $dom);
21991:     if ($putresult eq 'ok') {
21992:         if (keys(%changes) > 0) {
21993:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21994:             $resulttext = &mt('Changes made:').'<ul>';
21995:             foreach my $key ('admin','default','validation') {
21996:                 if (ref($changes{$key}) eq 'HASH') {
21997:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
21998:                     if ($key eq 'validation') {
21999:                         foreach my $item (@{$itemsref}) {
22000:                             if (exists($changes{$key}{$item})) {
22001:                                 if ($item eq 'markup') {
22002:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
22003:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
22004:                                 } else {  
22005:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
22006:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
22007:                                 }
22008:                             }
22009:                         }
22010:                     } else {
22011:                         foreach my $type (@types) {
22012:                             if ($type eq 'community') {
22013:                                 $roles{'1'} = &mt('Community personnel');
22014:                             } else {
22015:                                 $roles{'1'} = &mt('Course personnel');
22016:                             }
22017:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
22018:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
22019:                                     if ($key eq 'admin') {
22020:                                         my @mgrdc = ();
22021:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22022:                                             foreach my $item (@{$ordered{'admin'}}) {
22023:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
22024:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
22025:                                                         push(@mgrdc,$item);
22026:                                                     }
22027:                                                 }
22028:                                             }
22029:                                             if (@mgrdc) {
22030:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
22031:                                             } else {
22032:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
22033:                                             }
22034:                                         }
22035:                                     } else {
22036:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22037:                                             foreach my $item (@{$ordered{$key}}) {
22038:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22039:                                                     $domdefaults{$type.'selfenroll'.$item} =
22040:                                                         $selfenrollhash{$key}{$type}{$item};
22041:                                                 }
22042:                                             }
22043:                                         }
22044:                                     }
22045:                                 }
22046:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
22047:                                 foreach my $item (@{$ordered{$key}}) {
22048:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22049:                                         $resulttext .= '<li>';
22050:                                         if ($key eq 'admin') {
22051:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
22052:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
22053:                                         } else {
22054:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
22055:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
22056:                                         }
22057:                                         $resulttext .= '</li>';
22058:                                     }
22059:                                 }
22060:                                 $resulttext .= '</ul></li>';
22061:                             }
22062:                         }
22063:                         $resulttext .= '</ul></li>'; 
22064:                     }
22065:                 }
22066:             }
22067:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
22068:                 my $cachetime = 24*60*60;
22069:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22070:                 if (ref($lastactref) eq 'HASH') {
22071:                     $lastactref->{'domdefaults'} = 1;
22072:                 }
22073:             }
22074:             $resulttext .= '</ul>';
22075:         } else {
22076:             $resulttext = &mt('No changes made to self-enrollment settings');
22077:         }
22078:     } else {
22079:         $resulttext = '<span class="LC_error">'.
22080:             &mt('An error occurred: [_1]',$putresult).'</span>';
22081:     }
22082:     return $resulttext;
22083: }
22084: 
22085: sub modify_wafproxy {
22086:     my ($dom,$action,$lastactref,%domconfig) = @_;
22087:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22088:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
22089:         %wafproxy,%changes,%expirecache,%expiresaml);
22090:     foreach my $server (sort(keys(%servers))) {
22091:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22092:         if ($serverhome eq $server) {
22093:             my $serverdom = &Apache::lonnet::host_domain($server);
22094:             if ($serverdom eq $dom) {
22095:                 $canset{$server} = 1;
22096:             }
22097:         }
22098:     }
22099:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
22100:         %{$values{$dom}} = ();
22101:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
22102:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
22103:         }
22104:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
22105:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
22106:         }
22107:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22108:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
22109:         }
22110:     }
22111:     my $output;
22112:     if (keys(%canset)) {
22113:         %{$wafproxy{'alias'}} = ();
22114:         %{$wafproxy{'saml'}} = ();
22115:         foreach my $key (sort(keys(%canset))) {
22116:             if ($env{'form.wafproxy_'.$dom}) {
22117:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
22118:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
22119:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
22120:                     $changes{'alias'} = 1;
22121:                 }
22122:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
22123:                     $wafproxy{'saml'}{$key} = 1;
22124:                 }
22125:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
22126:                     $changes{'saml'} = 1;
22127:                 }
22128:             } else {
22129:                 $wafproxy{'alias'}{$key} = '';
22130:                 $wafproxy{'saml'}{$key} = '';
22131:                 if ($curralias{$key}) {
22132:                     $changes{'alias'} = 1;
22133:                 }
22134:                 if ($currsaml{$key}) {
22135:                     $changes{'saml'} = 1;
22136:                 }
22137:             }
22138:             if ($wafproxy{'alias'}{$key} eq '') {
22139:                 if ($curralias{$key}) {
22140:                     $expirecache{$key} = 1;
22141:                 }
22142:                 delete($wafproxy{'alias'}{$key});
22143:             }
22144:             if ($wafproxy{'saml'}{$key} eq '') {
22145:                 if ($currsaml{$key}) {
22146:                     $expiresaml{$key} = 1;
22147:                 }
22148:                 delete($wafproxy{'saml'}{$key});
22149:             }
22150:         }
22151:         unless (keys(%{$wafproxy{'alias'}})) {
22152:             delete($wafproxy{'alias'});
22153:         }
22154:         unless (keys(%{$wafproxy{'saml'}})) {
22155:             delete($wafproxy{'saml'});
22156:         }
22157:         # Localization for values in %warn occurs in &mt() calls separately.
22158:         my %warn = (
22159:                      trusted => 'trusted IP range(s)',
22160:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
22161:                      vpnext => 'IP range(s) for backend WAF connections',
22162:                    );
22163:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22164:             my $possible = $env{'form.wafproxy_'.$item};
22165:             $possible =~ s/^\s+|\s+$//g;
22166:             if ($possible ne '') {
22167:                 if ($item eq 'remoteip') {
22168:                     if ($possible =~ /^[mhn]$/) {
22169:                         $wafproxy{$item} = $possible;
22170:                     }
22171:                 } elsif ($item eq 'ipheader') {
22172:                     if ($wafproxy{'remoteip'} eq 'h') {
22173:                         $wafproxy{$item} = $possible;
22174:                     }
22175:                 } elsif ($item eq 'sslopt') {
22176:                     if ($possible =~ /^0|1$/) {
22177:                         $wafproxy{$item} = $possible;
22178:                     }
22179:                 } else {
22180:                     my (@ok,$count);
22181:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
22182:                         unless ($env{'form.wafproxy_vpnaccess'}) {
22183:                             $possible = '';
22184:                         }
22185:                     } elsif ($item eq 'trusted') {
22186:                         unless ($wafproxy{'remoteip'} eq 'h') {
22187:                             $possible = '';
22188:                         }
22189:                     }
22190:                     unless ($possible eq '') {
22191:                         $possible =~ s/[\r\n]+/\s/g;
22192:                         $possible =~ s/\s*-\s*/-/g;
22193:                         $possible =~ s/\s+/,/g;
22194:                         $possible =~ s/,+/,/g;
22195:                     }
22196:                     $count = 0;
22197:                     if ($possible ne '') {
22198:                         foreach my $poss (split(/\,/,$possible)) {
22199:                             $count ++;
22200:                             $poss = &validate_ip_pattern($poss);
22201:                             if ($poss ne '') {
22202:                                 push(@ok,$poss);
22203:                             }
22204:                         }
22205:                         my $diff = $count - scalar(@ok);
22206:                         if ($diff) {
22207:                             push(@warnings,'<li>'.
22208:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
22209:                                      $diff,$warn{$item}).
22210:                                  '</li>');
22211:                         }
22212:                         if (@ok) {
22213:                             my @cidr_list;
22214:                             foreach my $item (@ok) {
22215:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
22216:                             }
22217:                             $wafproxy{$item} = join(',',@cidr_list);
22218:                         }
22219:                     }
22220:                 }
22221:                 if ($wafproxy{$item} ne $currvalue{$item}) {
22222:                     $changes{$item} = 1;
22223:                 }
22224:             } elsif ($currvalue{$item}) {
22225:                 $changes{$item} = 1;
22226:             }
22227:         }
22228:     } else {
22229:         if (keys(%curralias)) {
22230:             $changes{'alias'} = 1;
22231:         }
22232:         if (keys(%currsaml)) {
22233:             $changes{'saml'} = 1;
22234:         }
22235:         if (keys(%currvalue)) {
22236:             foreach my $key (keys(%currvalue)) {
22237:                 $changes{$key} = 1;
22238:             }
22239:         }
22240:     }
22241:     if (keys(%changes)) {
22242:         my %defaultshash = (
22243:                               wafproxy => \%wafproxy,
22244:                            );
22245:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22246:                                                  $dom);
22247:         if ($putresult eq 'ok') {
22248:             my $cachetime = 24*60*60;
22249:             my (%domdefaults,$updatedomdefs);
22250:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
22251:                 if ($changes{$item}) {
22252:                     unless ($updatedomdefs) {
22253:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
22254:                         $updatedomdefs = 1;
22255:                     }
22256:                     if ($wafproxy{$item}) {
22257:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
22258:                     } elsif (exists($domdefaults{'waf_'.$item})) {
22259:                         delete($domdefaults{'waf_'.$item});
22260:                     }
22261:                 }
22262:             }
22263:             if ($updatedomdefs) {
22264:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22265:                 if (ref($lastactref) eq 'HASH') {
22266:                     $lastactref->{'domdefaults'} = 1;
22267:                 }
22268:             }
22269:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
22270:                 my %updates = %expirecache;
22271:                 foreach my $key (keys(%expirecache)) {
22272:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
22273:                 }
22274:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
22275:                     my $cachetime = 24*60*60;
22276:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
22277:                         $updates{$key} = 1;
22278:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
22279:                                                       $cachetime);
22280:                     }
22281:                 }
22282:                 if (ref($lastactref) eq 'HASH') {
22283:                     $lastactref->{'proxyalias'} = \%updates;
22284:                 }
22285:             }
22286:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
22287:                 my %samlupdates = %expiresaml;
22288:                 foreach my $key (keys(%expiresaml)) {
22289:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
22290:                 }
22291:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
22292:                     my $cachetime = 24*60*60;
22293:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
22294:                         $samlupdates{$key} = 1;
22295:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
22296:                                                       $cachetime);
22297:                     }
22298:                 }
22299:                 if (ref($lastactref) eq 'HASH') {
22300:                     $lastactref->{'proxysaml'} = \%samlupdates;
22301:                 }
22302:             }
22303:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
22304:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22305:                 if ($changes{$item}) {
22306:                     if ($item eq 'alias') {
22307:                         my $numaliased = 0;
22308:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
22309:                             my $shown;
22310:                             if (keys(%{$wafproxy{'alias'}})) {
22311:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
22312:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
22313:                                                          &Apache::lonnet::hostname($server),
22314:                                                          $wafproxy{'alias'}{$server}).'</li>';
22315:                                     $numaliased ++;
22316:                                 }
22317:                                 if ($numaliased) {
22318:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
22319:                                                           '<ul>'.$shown.'</ul>').'</li>';
22320:                                 }
22321:                             }
22322:                         }
22323:                         unless ($numaliased) {
22324:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
22325:                         }
22326:                     } elsif ($item eq 'saml') {
22327:                         my $shown;
22328:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
22329:                             if (keys(%{$wafproxy{'saml'}})) {
22330:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
22331:                             }
22332:                         }
22333:                         if ($shown) {
22334:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
22335:                                                   $shown).'</li>';
22336:                         } else {
22337:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
22338:                         }
22339:                     } else {
22340:                         if ($item eq 'remoteip') {
22341:                             my %ip_methods = &remoteip_methods();
22342:                             if ($wafproxy{$item} =~ /^[mh]$/) {
22343:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
22344:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
22345:                             } else {
22346:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
22347:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
22348:                                                '</li>';
22349:                                 } else {
22350:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
22351:                                 }
22352:                             }
22353:                         } elsif ($item eq 'ipheader') {
22354:                             if ($wafproxy{$item}) {
22355:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
22356:                                                       $wafproxy{$item}).'</li>';
22357:                             } else {
22358:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
22359:                             }
22360:                         } elsif ($item eq 'trusted') {
22361:                             if ($wafproxy{$item}) {
22362:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
22363:                                                       $wafproxy{$item}).'</li>';
22364:                             } else {
22365:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
22366:                             }
22367:                         } elsif ($item eq 'vpnint') {
22368:                             if ($wafproxy{$item}) {
22369:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
22370:                                                        $wafproxy{$item}).'</li>';
22371:                             } else {
22372:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
22373:                             }
22374:                         } elsif ($item eq 'vpnext') {
22375:                             if ($wafproxy{$item}) {
22376:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
22377:                                                        $wafproxy{$item}).'</li>';
22378:                             } else {
22379:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
22380:                             }
22381:                         } elsif ($item eq 'sslopt') {
22382:                             if ($wafproxy{$item}) {
22383:                                 $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>';
22384:                             } else {
22385:                                 $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>';
22386:                             }
22387:                         }
22388:                     }
22389:                 }
22390:             }
22391:             $output .= '</ul>';
22392:         } else {
22393:             $output = '<span class="LC_error">'.
22394:                       &mt('An error occurred: [_1]',$putresult).'</span>';
22395:         }
22396:     } elsif (keys(%canset)) {
22397:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
22398:     }
22399:     if (@warnings) {
22400:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
22401:                        join("\n",@warnings).'</ul>';
22402:     }
22403:     return $output;
22404: }
22405: 
22406: sub validate_ip_pattern {
22407:     my ($pattern) = @_;
22408:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
22409:         my ($start,$end) = ($1,$2);
22410:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
22411:             if (($start !~ m{/}) && ($end !~ m{/})) {
22412:                 return $start.'-'.$end;
22413:             }
22414:         }
22415:     } elsif ($pattern ne '') {
22416:         $pattern = &Net::CIDR::cidrvalidate($pattern);
22417:         if ($pattern ne '') {
22418:             return $pattern;
22419:         }
22420:     }
22421:     return;
22422: }
22423: 
22424: sub modify_usersessions {
22425:     my ($dom,$lastactref,%domconfig) = @_;
22426:     my @hostingtypes = ('version','excludedomain','includedomain');
22427:     my @offloadtypes = ('primary','default');
22428:     my %types = (
22429:                   remote => \@hostingtypes,
22430:                   hosted => \@hostingtypes,
22431:                   spares => \@offloadtypes,
22432:                 );
22433:     my @prefixes = ('remote','hosted','spares');
22434:     my @lcversions = &Apache::lonnet::all_loncaparevs();
22435:     my (%by_ip,%by_location,@intdoms,@instdoms);
22436:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22437:     my @locations = sort(keys(%by_location));
22438:     my (%defaultshash,%changes);
22439:     foreach my $prefix (@prefixes) {
22440:         $defaultshash{'usersessions'}{$prefix} = {};
22441:     }
22442:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22443:     my $resulttext;
22444:     my %iphost = &Apache::lonnet::get_iphost();
22445:     foreach my $prefix (@prefixes) {
22446:         next if ($prefix eq 'spares');
22447:         foreach my $type (@{$types{$prefix}}) {
22448:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22449:             if ($type eq 'version') {
22450:                 my $value = $env{'form.'.$prefix.'_'.$type};
22451:                 my $okvalue;
22452:                 if ($value ne '') {
22453:                     if (grep(/^\Q$value\E$/,@lcversions)) {
22454:                         $okvalue = $value;
22455:                     }
22456:                 }
22457:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22458:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22459:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
22460:                             if ($inuse == 0) {
22461:                                 $changes{$prefix}{$type} = 1;
22462:                             } else {
22463:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
22464:                                     $changes{$prefix}{$type} = 1;
22465:                                 }
22466:                                 if ($okvalue ne '') {
22467:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22468:                                 } 
22469:                             }
22470:                         } else {
22471:                             if (($inuse == 1) && ($okvalue ne '')) {
22472:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22473:                                 $changes{$prefix}{$type} = 1;
22474:                             }
22475:                         }
22476:                     } else {
22477:                         if (($inuse == 1) && ($okvalue ne '')) {
22478:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22479:                             $changes{$prefix}{$type} = 1;
22480:                         }
22481:                     }
22482:                 } else {
22483:                     if (($inuse == 1) && ($okvalue ne '')) {
22484:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22485:                         $changes{$prefix}{$type} = 1;
22486:                     }
22487:                 }
22488:             } else {
22489:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22490:                 my @okvals;
22491:                 foreach my $val (@vals) {
22492:                     if ($val =~ /:/) {
22493:                         my @items = split(/:/,$val);
22494:                         foreach my $item (@items) {
22495:                             if (ref($by_location{$item}) eq 'ARRAY') {
22496:                                 push(@okvals,$item);
22497:                             }
22498:                         }
22499:                     } else {
22500:                         if (ref($by_location{$val}) eq 'ARRAY') {
22501:                             push(@okvals,$val);
22502:                         }
22503:                     }
22504:                 }
22505:                 @okvals = sort(@okvals);
22506:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22507:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22508:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22509:                             if ($inuse == 0) {
22510:                                 $changes{$prefix}{$type} = 1; 
22511:                             } else {
22512:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22513:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
22514:                                 if (@changed > 0) {
22515:                                     $changes{$prefix}{$type} = 1;
22516:                                 }
22517:                             }
22518:                         } else {
22519:                             if ($inuse == 1) {
22520:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22521:                                 $changes{$prefix}{$type} = 1;
22522:                             }
22523:                         } 
22524:                     } else {
22525:                         if ($inuse == 1) {
22526:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22527:                             $changes{$prefix}{$type} = 1;
22528:                         }
22529:                     }
22530:                 } else {
22531:                     if ($inuse == 1) {
22532:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22533:                         $changes{$prefix}{$type} = 1;
22534:                     }
22535:                 }
22536:             }
22537:         }
22538:     }
22539: 
22540:     my @alldoms = &Apache::lonnet::all_domains();
22541:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22542:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
22543:     my $savespares;
22544: 
22545:     foreach my $lonhost (sort(keys(%servers))) {
22546:         my $serverhomeID =
22547:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
22548:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
22549:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
22550:         my %spareschg;
22551:         foreach my $type (@{$types{'spares'}}) {
22552:             my @okspares;
22553:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
22554:             foreach my $server (@checked) {
22555:                 if (&Apache::lonnet::hostname($server) ne '') {
22556:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
22557:                         unless (grep(/^\Q$server\E$/,@okspares)) {
22558:                             push(@okspares,$server);
22559:                         }
22560:                     }
22561:                 }
22562:             }
22563:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
22564:             my $newspare;
22565:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
22566:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
22567:                     $newspare = $new;
22568:                 }
22569:             }
22570:             my @spares;
22571:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
22572:                 @spares = sort(@okspares,$newspare);
22573:             } else {
22574:                 @spares = sort(@okspares);
22575:             }
22576:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
22577:             if (ref($spareid{$lonhost}) eq 'HASH') {
22578:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
22579:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
22580:                     if (@diffs > 0) {
22581:                         $spareschg{$type} = 1;
22582:                     }
22583:                 }
22584:             }
22585:         }
22586:         if (keys(%spareschg) > 0) {
22587:             $changes{'spares'}{$lonhost} = \%spareschg;
22588:         }
22589:     }
22590:     $defaultshash{'usersessions'}{'offloadnow'} = {};
22591:     $defaultshash{'usersessions'}{'offloadoth'} = {};
22592:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
22593:     my @okoffload;
22594:     if (@offloadnow) {
22595:         foreach my $server (@offloadnow) {
22596:             if (&Apache::lonnet::hostname($server) ne '') {
22597:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
22598:                     push(@okoffload,$server);
22599:                 }
22600:             }
22601:         }
22602:         if (@okoffload) {
22603:             foreach my $lonhost (@okoffload) {
22604:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
22605:             }
22606:         }
22607:     }
22608:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
22609:     my @okoffloadoth;
22610:     if (@offloadoth) {
22611:         foreach my $server (@offloadoth) {
22612:             if (&Apache::lonnet::hostname($server) ne '') {
22613:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
22614:                     push(@okoffloadoth,$server);
22615:                 }
22616:             }
22617:         }
22618:         if (@okoffloadoth) {
22619:             foreach my $lonhost (@okoffloadoth) {
22620:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
22621:             }
22622:         }
22623:     }
22624:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
22625:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
22626:             if (ref($changes{'spares'}) eq 'HASH') {
22627:                 if (keys(%{$changes{'spares'}}) > 0) {
22628:                     $savespares = 1;
22629:                 }
22630:             }
22631:         } else {
22632:             $savespares = 1;
22633:         }
22634:         foreach my $offload ('offloadnow','offloadoth') {
22635:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
22636:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
22637:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
22638:                         $changes{$offload} = 1;
22639:                         last;
22640:                     }
22641:                 }
22642:                 unless ($changes{$offload}) {
22643:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
22644:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
22645:                             $changes{$offload} = 1;
22646:                             last;
22647:                         }
22648:                     }
22649:                 }
22650:             } else {
22651:                 if (($offload eq 'offloadnow') && (@okoffload)) {
22652:                      $changes{'offloadnow'} = 1;
22653:                 }
22654:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
22655:                     $changes{'offloadoth'} = 1;
22656:                 }
22657:             }
22658:         }
22659:     } else {
22660:         if (@okoffload) {
22661:             $changes{'offloadnow'} = 1;
22662:         }
22663:         if (@okoffloadoth) {
22664:             $changes{'offloadoth'} = 1;
22665:         }
22666:     }
22667:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
22668:     if ((keys(%changes) > 0) || ($savespares)) {
22669:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22670:                                                  $dom);
22671:         if ($putresult eq 'ok') {
22672:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22673:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
22674:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
22675:                 }
22676:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
22677:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
22678:                 }
22679:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22680:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
22681:                 }
22682:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22683:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
22684:                 }
22685:             }
22686:             my $cachetime = 24*60*60;
22687:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22688:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
22689:             if (ref($lastactref) eq 'HASH') {
22690:                 $lastactref->{'domdefaults'} = 1;
22691:                 $lastactref->{'usersessions'} = 1;
22692:             }
22693:             if (keys(%changes) > 0) {
22694:                 my %lt = &usersession_titles();
22695:                 $resulttext = &mt('Changes made:').'<ul>';
22696:                 foreach my $prefix (@prefixes) {
22697:                     if (ref($changes{$prefix}) eq 'HASH') {
22698:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22699:                         if ($prefix eq 'spares') {
22700:                             if (ref($changes{$prefix}) eq 'HASH') {
22701:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
22702:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
22703:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
22704:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
22705:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22706:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
22707:                                         foreach my $type (@{$types{$prefix}}) {
22708:                                             if ($changes{$prefix}{$lonhost}{$type}) {
22709:                                                 my $offloadto = &mt('None');
22710:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
22711:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
22712:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
22713:                                                     }
22714:                                                 }
22715:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
22716:                                             }
22717:                                         }
22718:                                     }
22719:                                     $resulttext .= '</li>';
22720:                                 }
22721:                             }
22722:                         } else {
22723:                             foreach my $type (@{$types{$prefix}}) {
22724:                                 if (defined($changes{$prefix}{$type})) {
22725:                                     my ($newvalue,$notinuse);
22726:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22727:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
22728:                                             if ($type eq 'version') {
22729:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
22730:                                             } else {
22731:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22732:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
22733:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
22734:                                                     }
22735:                                                 } else {
22736:                                                     $notinuse = 1;
22737:                                                 }
22738:                                             }
22739:                                         }
22740:                                     }
22741:                                     if ($newvalue eq '') {
22742:                                         if ($type eq 'version') {
22743:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
22744:                                         } elsif ($notinuse) {
22745:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22746:                                         } else {
22747:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22748:                                         }
22749:                                     } else {
22750:                                         if ($type eq 'version') {
22751:                                             $newvalue .= ' '.&mt('(or later)');
22752:                                         }
22753:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22754:                                     }
22755:                                 }
22756:                             }
22757:                         }
22758:                         $resulttext .= '</ul>';
22759:                     }
22760:                 }
22761:                 if ($changes{'offloadnow'}) {
22762:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22763:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
22764:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
22765:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
22766:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22767:                             }
22768:                             $resulttext .= '</ul>';
22769:                         } else {
22770:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
22771:                         }
22772:                     } else {
22773:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
22774:                     }
22775:                 }
22776:                 if ($changes{'offloadoth'}) {
22777:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22778:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
22779:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
22780:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
22781:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22782:                             }
22783:                             $resulttext .= '</ul>';
22784:                         } else {
22785:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
22786:                         }
22787:                     } else {
22788:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
22789:                     }
22790:                 }
22791:                 $resulttext .= '</ul>';
22792:             } else {
22793:                 $resulttext = $nochgmsg;
22794:             }
22795:         } else {
22796:             $resulttext = '<span class="LC_error">'.
22797:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22798:         }
22799:     } else {
22800:         $resulttext = $nochgmsg;
22801:     }
22802:     return $resulttext;
22803: }
22804: 
22805: sub modify_ssl {
22806:     my ($dom,$lastactref,%domconfig) = @_;
22807:     my (%by_ip,%by_location,@intdoms,@instdoms);
22808:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22809:     my @locations = sort(keys(%by_location));
22810:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22811:     my (%defaultshash,%changes);
22812:     my $action = 'ssl';
22813:     my @prefixes = ('connto','connfrom','replication');
22814:     foreach my $prefix (@prefixes) {
22815:         $defaultshash{$action}{$prefix} = {};
22816:     }
22817:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22818:     my $resulttext;
22819:     my %iphost = &Apache::lonnet::get_iphost();
22820:     my @reptypes = ('certreq','nocertreq');
22821:     my @connecttypes = ('dom','intdom','other');
22822:     my %types = (
22823:                   connto      => \@connecttypes,
22824:                   connfrom    => \@connecttypes,
22825:                   replication => \@reptypes,
22826:                 );
22827:     foreach my $prefix (sort(keys(%types))) {
22828:         foreach my $type (@{$types{$prefix}}) {
22829:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22830:                 my $value = 'yes';
22831:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
22832:                     $value = $env{'form.'.$prefix.'_'.$type};
22833:                 }
22834:                 if (ref($domconfig{$action}) eq 'HASH') {
22835:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22836:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
22837:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
22838:                                 $changes{$prefix}{$type} = 1;
22839:                             }
22840:                             $defaultshash{$action}{$prefix}{$type} = $value;
22841:                         } else {
22842:                             $defaultshash{$action}{$prefix}{$type} = $value;
22843:                             $changes{$prefix}{$type} = 1;
22844:                         }
22845:                     } else {
22846:                         $defaultshash{$action}{$prefix}{$type} = $value;
22847:                         $changes{$prefix}{$type} = 1;
22848:                     }
22849:                 } else {
22850:                     $defaultshash{$action}{$prefix}{$type} = $value;
22851:                     $changes{$prefix}{$type} = 1;
22852:                 }
22853:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
22854:                     delete($changes{$prefix}{$type});
22855:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
22856:                     delete($changes{$prefix}{$type});
22857:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
22858:                     delete($changes{$prefix}{$type});
22859:                 }
22860:             } elsif ($prefix eq 'replication') {
22861:                 if (@locations > 0) {
22862:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22863:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22864:                     my @okvals;
22865:                     foreach my $val (@vals) {
22866:                         if ($val =~ /:/) {
22867:                             my @items = split(/:/,$val);
22868:                             foreach my $item (@items) {
22869:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22870:                                     push(@okvals,$item);
22871:                                 }
22872:                             }
22873:                         } else {
22874:                             if (ref($by_location{$val}) eq 'ARRAY') {
22875:                                 push(@okvals,$val);
22876:                             }
22877:                         }
22878:                     }
22879:                     @okvals = sort(@okvals);
22880:                     if (ref($domconfig{$action}) eq 'HASH') {
22881:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22882:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22883:                                 if ($inuse == 0) {
22884:                                     $changes{$prefix}{$type} = 1;
22885:                                 } else {
22886:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22887:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22888:                                     if (@changed > 0) {
22889:                                         $changes{$prefix}{$type} = 1;
22890:                                     }
22891:                                 }
22892:                             } else {
22893:                                 if ($inuse == 1) {
22894:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22895:                                     $changes{$prefix}{$type} = 1;
22896:                                 }
22897:                             }
22898:                         } else {
22899:                             if ($inuse == 1) {
22900:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22901:                                 $changes{$prefix}{$type} = 1;
22902:                             }
22903:                         }
22904:                     } else {
22905:                         if ($inuse == 1) {
22906:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22907:                             $changes{$prefix}{$type} = 1;
22908:                         }
22909:                     }
22910:                 }
22911:             }
22912:         }
22913:     }
22914:     if (keys(%changes)) {
22915:         foreach my $prefix (keys(%changes)) {
22916:             if (ref($changes{$prefix}) eq 'HASH') {
22917:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22918:                     delete($changes{$prefix});
22919:                 }
22920:             } else {
22921:                 delete($changes{$prefix});
22922:             }
22923:         }
22924:     }
22925:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22926:     if (keys(%changes) > 0) {
22927:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22928:                                                  $dom);
22929:         if ($putresult eq 'ok') {
22930:             if (ref($defaultshash{$action}) eq 'HASH') {
22931:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22932:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22933:                 }
22934:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22935:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22936:                 }
22937:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22938:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22939:                 }
22940:             }
22941:             my $cachetime = 24*60*60;
22942:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22943:             if (ref($lastactref) eq 'HASH') {
22944:                 $lastactref->{'domdefaults'} = 1;
22945:             }
22946:             if (keys(%changes) > 0) {
22947:                 my %titles = &ssl_titles();
22948:                 $resulttext = &mt('Changes made:').'<ul>';
22949:                 foreach my $prefix (@prefixes) {
22950:                     if (ref($changes{$prefix}) eq 'HASH') {
22951:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22952:                         foreach my $type (@{$types{$prefix}}) {
22953:                             if (defined($changes{$prefix}{$type})) {
22954:                                 my ($newvalue,$notinuse);
22955:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22956:                                     if (ref($defaultshash{$action}{$prefix})) {
22957:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22958:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22959:                                         } else {
22960:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22961:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22962:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22963:                                                 }
22964:                                             } else {
22965:                                                 $notinuse = 1;
22966:                                             }
22967:                                         }
22968:                                     }
22969:                                     if ($notinuse) {
22970:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22971:                                     } elsif ($newvalue eq '') {
22972:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
22973:                                     } else {
22974:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
22975:                                     }
22976:                                 }
22977:                             }
22978:                         }
22979:                         $resulttext .= '</ul>';
22980:                     }
22981:                 }
22982:             } else {
22983:                 $resulttext = $nochgmsg;
22984:             }
22985:         } else {
22986:             $resulttext = '<span class="LC_error">'.
22987:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22988:         }
22989:     } else {
22990:         $resulttext = $nochgmsg;
22991:     }
22992:     return $resulttext;
22993: }
22994: 
22995: sub modify_trust {
22996:     my ($dom,$lastactref,%domconfig) = @_;
22997:     my (%by_ip,%by_location,@intdoms,@instdoms);
22998:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22999:     my @locations = sort(keys(%by_location));
23000:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
23001:     my @types = ('exc','inc');
23002:     my (%defaultshash,%changes);
23003:     foreach my $prefix (@prefixes) {
23004:         $defaultshash{'trust'}{$prefix} = {};
23005:     }
23006:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
23007:     my $resulttext;
23008:     foreach my $prefix (@prefixes) {
23009:         foreach my $type (@types) {
23010:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
23011:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
23012:             my @okvals;
23013:             foreach my $val (@vals) {
23014:                 if ($val =~ /:/) {
23015:                     my @items = split(/:/,$val);
23016:                     foreach my $item (@items) {
23017:                         if (ref($by_location{$item}) eq 'ARRAY') {
23018:                             push(@okvals,$item);
23019:                         }
23020:                     }
23021:                 } else {
23022:                     if (ref($by_location{$val}) eq 'ARRAY') {
23023:                         push(@okvals,$val);
23024:                     }
23025:                 }
23026:             }
23027:             @okvals = sort(@okvals);
23028:             if (ref($domconfig{'trust'}) eq 'HASH') {
23029:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
23030:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23031:                         if ($inuse == 0) {
23032:                             $changes{$prefix}{$type} = 1;
23033:                         } else {
23034:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23035:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
23036:                             if (@changed > 0) {
23037:                                 $changes{$prefix}{$type} = 1;
23038:                             }
23039:                         }
23040:                     } else {
23041:                         if ($inuse == 1) {
23042:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23043:                             $changes{$prefix}{$type} = 1;
23044:                         }
23045:                     }
23046:                 } else {
23047:                     if ($inuse == 1) {
23048:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23049:                         $changes{$prefix}{$type} = 1;
23050:                     }
23051:                 }
23052:             } else {
23053:                 if ($inuse == 1) {
23054:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23055:                     $changes{$prefix}{$type} = 1;
23056:                 }
23057:             }
23058:         }
23059:     }
23060:     my $nochgmsg = &mt('No changes made to trust settings.');
23061:     if (keys(%changes) > 0) {
23062:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
23063:                                                  $dom);
23064:         if ($putresult eq 'ok') {
23065:             if (ref($defaultshash{'trust'}) eq 'HASH') {
23066:                 foreach my $prefix (@prefixes) {
23067:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
23068:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
23069:                     }
23070:                 }
23071:             }
23072:             my $cachetime = 24*60*60;
23073:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
23074:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
23075:             if (ref($lastactref) eq 'HASH') {
23076:                 $lastactref->{'domdefaults'} = 1;
23077:                 $lastactref->{'trust'} = 1;
23078:             }
23079:             if (keys(%changes) > 0) {
23080:                 my %lt = &trust_titles();
23081:                 $resulttext = &mt('Changes made:').'<ul>';
23082:                 foreach my $prefix (@prefixes) {
23083:                     if (ref($changes{$prefix}) eq 'HASH') {
23084:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
23085:                         foreach my $type (@types) {
23086:                             if (defined($changes{$prefix}{$type})) {
23087:                                 my ($newvalue,$notinuse);
23088:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
23089:                                     if (ref($defaultshash{'trust'}{$prefix})) {
23090:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23091:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
23092:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
23093:                                             }
23094:                                         } else {
23095:                                             $notinuse = 1;
23096:                                         }
23097:                                     }
23098:                                 }
23099:                                 if ($notinuse) {
23100:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
23101:                                 } elsif ($newvalue eq '') {
23102:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
23103:                                 } else {
23104:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
23105:                                 }
23106:                             }
23107:                         }
23108:                         $resulttext .= '</ul>';
23109:                     }
23110:                 }
23111:                 $resulttext .= '</ul>';
23112:             } else {
23113:                 $resulttext = $nochgmsg;
23114:             }
23115:         } else {
23116:             $resulttext = '<span class="LC_error">'.
23117:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23118:         }
23119:     } else {
23120:         $resulttext = $nochgmsg;
23121:     }
23122:     return $resulttext;
23123: }
23124: 
23125: sub modify_loadbalancing {
23126:     my ($dom,%domconfig) = @_;
23127:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
23128:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
23129:     my ($othertitle,$usertypes,$types) =
23130:         &Apache::loncommon::sorted_inst_types($dom);
23131:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23132:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
23133:     my @sparestypes = ('primary','default');
23134:     my %typetitles = &sparestype_titles();
23135:     my $resulttext;
23136:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23137:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23138:         %existing = %{$domconfig{'loadbalancing'}};
23139:     }
23140:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
23141:                               \%currtargets,\%currrules,\%currcookies);
23142:     my ($saveloadbalancing,%defaultshash,%changes);
23143:     my ($alltypes,$othertypes,$titles) =
23144:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
23145:     my %ruletitles = &offloadtype_text();
23146:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
23147:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
23148:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
23149:         if ($balancer eq '') {
23150:             next;
23151:         }
23152:         if (!exists($servers{$balancer})) {
23153:             if (exists($currbalancer{$balancer})) {
23154:                 push(@{$changes{'delete'}},$balancer);
23155:             }
23156:             next;
23157:         }
23158:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
23159:             push(@{$changes{'delete'}},$balancer);
23160:             next;
23161:         }
23162:         if (!exists($currbalancer{$balancer})) {
23163:             push(@{$changes{'add'}},$balancer);
23164:         }
23165:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
23166:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
23167:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
23168:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23169:             $saveloadbalancing = 1;
23170:         }
23171:         foreach my $sparetype (@sparestypes) {
23172:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
23173:             my @offloadto;
23174:             foreach my $target (@targets) {
23175:                 if (($servers{$target}) && ($target ne $balancer)) {
23176:                     if ($sparetype eq 'default') {
23177:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
23178:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
23179:                         }
23180:                     }
23181:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
23182:                         push(@offloadto,$target);
23183:                     }
23184:                 }
23185:             }
23186:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
23187:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
23188:                     push(@offloadto,$balancer);
23189:                 }
23190:             }
23191:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
23192:         }
23193:         if ($env{'form.loadbalancing_cookie_'.$i}) {
23194:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
23195:             if (exists($currbalancer{$balancer})) {
23196:                 unless ($currcookies{$balancer}) {
23197:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
23198:                 }
23199:             }
23200:         } elsif (exists($currbalancer{$balancer})) {
23201:             if ($currcookies{$balancer}) {
23202:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
23203:             }
23204:         }
23205:         if (ref($currtargets{$balancer}) eq 'HASH') {
23206:             foreach my $sparetype (@sparestypes) {
23207:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
23208:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
23209:                     if (@targetdiffs > 0) {
23210:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23211:                     }
23212:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23213:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23214:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23215:                     }
23216:                 }
23217:             }
23218:         } else {
23219:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
23220:                 foreach my $sparetype (@sparestypes) {
23221:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23222:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23223:                             $changes{'curr'}{$balancer}{'targets'} = 1;
23224:                         }
23225:                     }
23226:                 }
23227:             }
23228:         }
23229:         my $ishomedom;
23230:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
23231:             $ishomedom = 1;
23232:         }
23233:         if (ref($alltypes) eq 'ARRAY') {
23234:             foreach my $type (@{$alltypes}) {
23235:                 my $rule;
23236:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
23237:                          (!$ishomedom)) {
23238:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
23239:                 }
23240:                 if ($rule eq 'specific') {
23241:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
23242:                     if (exists($servers{$specifiedhost})) {
23243:                         $rule = $specifiedhost;
23244:                     }
23245:                 }
23246:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
23247:                 if (ref($currrules{$balancer}) eq 'HASH') {
23248:                     if ($rule ne $currrules{$balancer}{$type}) {
23249:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23250:                     }
23251:                 } elsif ($rule ne '') {
23252:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23253:                 }
23254:             }
23255:         }
23256:     }
23257:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
23258:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
23259:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
23260:             $defaultshash{'loadbalancing'} = {};
23261:         }
23262:         my $putresult = &Apache::lonnet::put_dom('configuration',
23263:                                                  \%defaultshash,$dom);
23264:         if ($putresult eq 'ok') {
23265:             if (keys(%changes) > 0) {
23266:                 my %toupdate;
23267:                 if (ref($changes{'delete'}) eq 'ARRAY') {
23268:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
23269:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
23270:                         $toupdate{$balancer} = 1;
23271:                     }
23272:                 }
23273:                 if (ref($changes{'add'}) eq 'ARRAY') {
23274:                     foreach my $balancer (sort(@{$changes{'add'}})) {
23275:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
23276:                         $toupdate{$balancer} = 1;
23277:                     }
23278:                 }
23279:                 if (ref($changes{'curr'}) eq 'HASH') {
23280:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
23281:                         $toupdate{$balancer} = 1;
23282:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
23283:                             if ($changes{'curr'}{$balancer}{'targets'}) {
23284:                                 my %offloadstr;
23285:                                 foreach my $sparetype (@sparestypes) {
23286:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23287:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23288:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23289:                                         }
23290:                                     }
23291:                                 }
23292:                                 if (keys(%offloadstr) == 0) {
23293:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
23294:                                 } else {
23295:                                     my $showoffload;
23296:                                     foreach my $sparetype (@sparestypes) {
23297:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
23298:                                         if (defined($offloadstr{$sparetype})) {
23299:                                             $showoffload .= $offloadstr{$sparetype};
23300:                                         } else {
23301:                                             $showoffload .= &mt('None');
23302:                                         }
23303:                                         $showoffload .= ('&nbsp;'x3);
23304:                                     }
23305:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
23306:                                 }
23307:                             }
23308:                         }
23309:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
23310:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
23311:                                 foreach my $type (@{$alltypes}) {
23312:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
23313:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23314:                                         my $balancetext;
23315:                                         if ($rule eq '') {
23316:                                             $balancetext =  $ruletitles{'default'};
23317:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
23318:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23319:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23320:                                                 foreach my $sparetype (@sparestypes) {
23321:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23322:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23323:                                                     }
23324:                                                 }
23325:                                                 foreach my $item (@{$alltypes}) {
23326:                                                     next if ($item =~  /^_LC_ipchange/);
23327:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
23328:                                                     if ($hasrule eq 'homeserver') {
23329:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
23330:                                                     } else {
23331:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
23332:                                                             if ($servers{$hasrule}) {
23333:                                                                 $toupdate{$hasrule} = 1;
23334:                                                             }
23335:                                                         }
23336:                                                     }
23337:                                                 }
23338:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
23339:                                                     $balancetext =  $ruletitles{$rule};
23340:                                                 } else {
23341:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23342:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
23343:                                                     if ($receiver) {
23344:                                                         $toupdate{$receiver};
23345:                                                     }
23346:                                                 }
23347:                                             } else {
23348:                                                 $balancetext =  $ruletitles{$rule};
23349:                                             }
23350:                                         } else {
23351:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
23352:                                         }
23353:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
23354:                                     }
23355:                                 }
23356:                             }
23357:                         }
23358:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
23359:                             if ($currcookies{$balancer}) {
23360:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
23361:                                                           $balancer).'</li>';
23362:                             } else {
23363:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
23364:                                                           $balancer).'</li>';
23365:                             }
23366:                         }
23367:                     }
23368:                 }
23369:                 if (keys(%toupdate)) {
23370:                     my %thismachine;
23371:                     my $updatedhere;
23372:                     my $cachetime = 60*60*24;
23373:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23374:                     foreach my $lonhost (keys(%toupdate)) {
23375:                         if ($thismachine{$lonhost}) {
23376:                             unless ($updatedhere) {
23377:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
23378:                                                               $defaultshash{'loadbalancing'},
23379:                                                               $cachetime);
23380:                                 $updatedhere = 1;
23381:                             }
23382:                         } else {
23383:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
23384:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
23385:                         }
23386:                     }
23387:                 }
23388:                 if ($resulttext ne '') {
23389:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
23390:                 } else {
23391:                     $resulttext = $nochgmsg;
23392:                 }
23393:             } else {
23394:                 $resulttext = $nochgmsg;
23395:             }
23396:         } else {
23397:             $resulttext = '<span class="LC_error">'.
23398:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23399:         }
23400:     } else {
23401:         $resulttext = $nochgmsg;
23402:     }
23403:     return $resulttext;
23404: }
23405: 
23406: sub recurse_check {
23407:     my ($chkcats,$categories,$depth,$name) = @_;
23408:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
23409:         my $chg = 0;
23410:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
23411:             my $category = $chkcats->[$depth]{$name}[$j];
23412:             my $item;
23413:             if ($category eq '') {
23414:                 $chg ++;
23415:             } else {
23416:                 my $deeper = $depth + 1;
23417:                 $item = &escape($category).':'.&escape($name).':'.$depth;
23418:                 if ($chg) {
23419:                     $categories->{$item} -= $chg;
23420:                 }
23421:                 &recurse_check($chkcats,$categories,$deeper,$category);
23422:                 $deeper --;
23423:             }
23424:         }
23425:     }
23426:     return;
23427: }
23428: 
23429: sub recurse_cat_deletes {
23430:     my ($item,$coursecategories,$deletions) = @_;
23431:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
23432:     my $subdepth = $depth + 1;
23433:     if (ref($coursecategories) eq 'HASH') {
23434:         foreach my $subitem (keys(%{$coursecategories})) {
23435:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
23436:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
23437:                 delete($coursecategories->{$subitem});
23438:                 $deletions->{$subitem} = 1;
23439:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
23440:             }
23441:         }
23442:     }
23443:     return;
23444: }
23445: 
23446: sub active_dc_picker {
23447:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
23448:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
23449:     my @domcoord = keys(%domcoords);
23450:     if (keys(%currhash)) {
23451:         foreach my $dc (keys(%currhash)) {
23452:             unless (exists($domcoords{$dc})) {
23453:                 push(@domcoord,$dc);
23454:             }
23455:         }
23456:     }
23457:     @domcoord = sort(@domcoord);
23458:     my $numdcs = scalar(@domcoord);
23459:     my $rows = 0;
23460:     my $table;
23461:     if ($numdcs > 1) {
23462:         $table = '<table>';
23463:         for (my $i=0; $i<@domcoord; $i++) {
23464:             my $rem = $i%($numinrow);
23465:             if ($rem == 0) {
23466:                 if ($i > 0) {
23467:                     $table .= '</tr>';
23468:                 }
23469:                 $table .= '<tr>';
23470:                 $rows ++;
23471:             }
23472:             my $check = '';
23473:             if ($inputtype eq 'radio') {
23474:                 if (keys(%currhash) == 0) {
23475:                     if (!$i) {
23476:                         $check = ' checked="checked"';
23477:                     }
23478:                 } elsif (exists($currhash{$domcoord[$i]})) {
23479:                     $check = ' checked="checked"';
23480:                 }
23481:             } else {
23482:                 if (exists($currhash{$domcoord[$i]})) {
23483:                     $check = ' checked="checked"';
23484:                 }
23485:             }
23486:             if ($i == @domcoord - 1) {
23487:                 my $colsleft = $numinrow - $rem;
23488:                 if ($colsleft > 1) {
23489:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
23490:                 } else {
23491:                     $table .= '<td class="LC_left_item">';
23492:                 }
23493:             } else {
23494:                 $table .= '<td class="LC_left_item">';
23495:             }
23496:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
23497:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23498:             $table .= '<span class="LC_nobreak"><label>'.
23499:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
23500:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
23501:             if ($user ne $dcname.':'.$dcdom) {
23502:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23503:             }
23504:             $table .= '</label></span></td>';
23505:         }
23506:         $table .= '</tr></table>';
23507:     } elsif ($numdcs == 1) {
23508:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
23509:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23510:         if ($inputtype eq 'radio') {
23511:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
23512:             if ($user ne $dcname.':'.$dcdom) {
23513:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23514:             }
23515:         } else {
23516:             my $check;
23517:             if (exists($currhash{$domcoord[0]})) {
23518:                 $check = ' checked="checked"';
23519:             }
23520:             $table = '<span class="LC_nobreak"><label>'.
23521:                      '<input type="checkbox" name="'.$name.'" '.
23522:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
23523:             if ($user ne $dcname.':'.$dcdom) {
23524:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23525:             }
23526:             $table .= '</label></span>';
23527:             $rows ++;
23528:         }
23529:     }
23530:     return ($numdcs,$table,$rows);
23531: }
23532: 
23533: sub usersession_titles {
23534:     return &Apache::lonlocal::texthash(
23535:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
23536:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
23537:                spares => 'Servers offloaded to, when busy',
23538:                version => 'LON-CAPA version requirement',
23539:                excludedomain => 'Allow all, but exclude specific domains',
23540:                includedomain => 'Deny all, but include specific domains',
23541:                primary => 'Primary (checked first)',
23542:                default => 'Default',
23543:            );
23544: }
23545: 
23546: sub id_for_thisdom {
23547:     my (%servers) = @_;
23548:     my %altids;
23549:     foreach my $server (keys(%servers)) {
23550:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
23551:         if ($serverhome ne $server) {
23552:             $altids{$serverhome} = $server;
23553:         }
23554:     }
23555:     return %altids;
23556: }
23557: 
23558: sub count_servers {
23559:     my ($currbalancer,%servers) = @_;
23560:     my (@spares,$numspares);
23561:     foreach my $lonhost (sort(keys(%servers))) {
23562:         next if ($currbalancer eq $lonhost);
23563:         push(@spares,$lonhost);
23564:     }
23565:     if ($currbalancer) {
23566:         $numspares = scalar(@spares);
23567:     } else {
23568:         $numspares = scalar(@spares) - 1;
23569:     }
23570:     return ($numspares,@spares);
23571: }
23572: 
23573: sub lonbalance_targets_js {
23574:     my ($dom,$types,$servers,$settings) = @_;
23575:     my $select = &mt('Select');
23576:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
23577:     if (ref($servers) eq 'HASH') {
23578:         $alltargets = join("','",sort(keys(%{$servers})));
23579:         my @homedoms;
23580:         foreach my $server (sort(keys(%{$servers}))) {
23581:             if (&Apache::lonnet::host_domain($server) eq $dom) {
23582:                 push(@homedoms,'1');
23583:             } else {
23584:                 push(@homedoms,'0');
23585:             }
23586:         }
23587:         $allishome = join("','",@homedoms);
23588:     }
23589:     if (ref($types) eq 'ARRAY') {
23590:         if (@{$types} > 0) {
23591:             @alltypes = @{$types};
23592:         }
23593:     }
23594:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
23595:     $allinsttypes = join("','",@alltypes);
23596:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23597:     if (ref($settings) eq 'HASH') {
23598:         %existing = %{$settings};
23599:     }
23600:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
23601:                               \%currtargets,\%currrules,\%currcookies);
23602:     my $balancers = join("','",sort(keys(%currbalancer)));
23603:     return <<"END";
23604: 
23605: <script type="text/javascript">
23606: // <![CDATA[
23607: 
23608: currBalancers = new Array('$balancers');
23609: 
23610: function toggleTargets(balnum) {
23611:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23612:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
23613:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
23614:     var prevbalancer = prevhostitem.value;
23615:     var baltotal = document.getElementById('loadbalancing_total').value;
23616:     prevhostitem.value = balancer;
23617:     if (prevbalancer != '') {
23618:         var prevIdx = currBalancers.indexOf(prevbalancer);
23619:         if (prevIdx != -1) {
23620:             currBalancers.splice(prevIdx,1);
23621:         }
23622:     }
23623:     if (balancer == '') {
23624:         hideSpares(balnum);
23625:     } else {
23626:         var currIdx = currBalancers.indexOf(balancer);
23627:         if (currIdx == -1) {
23628:             currBalancers.push(balancer);
23629:         }
23630:         var homedoms = new Array('$allishome');
23631:         var ishomedom = homedoms[lonhostitem.selectedIndex];
23632:         showSpares(balancer,ishomedom,balnum);
23633:     }
23634:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
23635:     return;
23636: }
23637: 
23638: function showSpares(balancer,ishomedom,balnum) {
23639:     var alltargets = new Array('$alltargets');
23640:     var insttypes = new Array('$allinsttypes');
23641:     var offloadtypes = new Array('primary','default');
23642: 
23643:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
23644:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
23645:  
23646:     for (var i=0; i<offloadtypes.length; i++) {
23647:         var count = 0;
23648:         for (var j=0; j<alltargets.length; j++) {
23649:             if (alltargets[j] != balancer) {
23650:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
23651:                 item.value = alltargets[j];
23652:                 item.style.textAlign='left';
23653:                 item.style.textFace='normal';
23654:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
23655:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
23656:                     item.disabled = '';
23657:                 } else {
23658:                     item.disabled = 'disabled';
23659:                     item.checked = false;
23660:                 }
23661:                 count ++;
23662:             }
23663:         }
23664:     }
23665:     for (var k=0; k<insttypes.length; k++) {
23666:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
23667:             if (ishomedom == 1) {
23668:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23669:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23670:             } else {
23671:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23672:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23673:             }
23674:         } else {
23675:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23676:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23677:         }
23678:         if ((insttypes[k] != '_LC_external') && 
23679:             ((insttypes[k] != '_LC_internetdom') ||
23680:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
23681:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
23682:             item.options.length = 0;
23683:             item.options[0] = new Option("","",true,true);
23684:             var idx = 0;
23685:             for (var m=0; m<alltargets.length; m++) {
23686:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
23687:                     idx ++;
23688:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23689:                 }
23690:             }
23691:         }
23692:     }
23693:     return;
23694: }
23695: 
23696: function hideSpares(balnum) {
23697:     var alltargets = new Array('$alltargets');
23698:     var insttypes = new Array('$allinsttypes');
23699:     var offloadtypes = new Array('primary','default');
23700: 
23701:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
23702:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
23703: 
23704:     var total = alltargets.length - 1;
23705:     for (var i=0; i<offloadtypes; i++) {
23706:         for (var j=0; j<total; j++) {
23707:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
23708:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
23709:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
23710:         }
23711:     }
23712:     for (var k=0; k<insttypes.length; k++) {
23713:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23714:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23715:         if (insttypes[k] != '_LC_external') {
23716:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
23717:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
23718:         }
23719:     }
23720:     return;
23721: }
23722: 
23723: function checkOffloads(item,balnum,type) {
23724:     var alltargets = new Array('$alltargets');
23725:     var offloadtypes = new Array('primary','default');
23726:     if (item.checked) {
23727:         var total = alltargets.length - 1;
23728:         var other;
23729:         if (type == offloadtypes[0]) {
23730:             other = offloadtypes[1];
23731:         } else {
23732:             other = offloadtypes[0];
23733:         }
23734:         for (var i=0; i<total; i++) {
23735:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
23736:             if (server == item.value) {
23737:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
23738:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
23739:                 }
23740:             }
23741:         }
23742:     }
23743:     return;
23744: }
23745: 
23746: function singleServerToggle(balnum,type) {
23747:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
23748:     if (offloadtoSelIdx == 0) {
23749:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
23750:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23751: 
23752:     } else {
23753:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
23754:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23755:     }
23756:     return;
23757: }
23758: 
23759: function balanceruleChange(formname,balnum,type) {
23760:     if (type == '_LC_external') {
23761:         return;
23762:     }
23763:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
23764:     for (var i=0; i<typesRules.length; i++) {
23765:         if (formname.elements[typesRules[i]].checked) {
23766:             if (formname.elements[typesRules[i]].value != 'specific') {
23767:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
23768:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23769:             } else {
23770:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23771:             }
23772:         }
23773:     }
23774:     return;
23775: }
23776: 
23777: function balancerDeleteChange(balnum) {
23778:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23779:     var baltotal = document.getElementById('loadbalancing_total').value;
23780:     var addtarget;
23781:     var removetarget;
23782:     var action = 'delete';
23783:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
23784:         var lonhost = hostitem.value;
23785:         var currIdx = currBalancers.indexOf(lonhost);
23786:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
23787:             if (currIdx != -1) {
23788:                 currBalancers.splice(currIdx,1);
23789:             }
23790:             addtarget = lonhost;
23791:         } else {
23792:             if (currIdx == -1) {
23793:                 currBalancers.push(lonhost);
23794:             }
23795:             removetarget = lonhost;
23796:             action = 'undelete';
23797:         }
23798:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
23799:     }
23800:     return;
23801: }
23802: 
23803: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
23804:     if (baltotal > 1) {
23805:         var offloadtypes = new Array('primary','default');
23806:         var alltargets = new Array('$alltargets');
23807:         var insttypes = new Array('$allinsttypes');
23808:         for (var i=0; i<baltotal; i++) {
23809:             if (i != balnum) {
23810:                 for (var j=0; j<offloadtypes.length; j++) {
23811:                     var total = alltargets.length - 1;
23812:                     for (var k=0; k<total; k++) {
23813:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
23814:                         var server = serveritem.value;
23815:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
23816:                             if (server == addtarget) {
23817:                                 serveritem.disabled = '';
23818:                             }
23819:                         }
23820:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23821:                             if (server == removetarget) {
23822:                                 serveritem.disabled = 'disabled';
23823:                                 serveritem.checked = false;
23824:                             }
23825:                         }
23826:                     }
23827:                 }
23828:                 for (var j=0; j<insttypes.length; j++) {
23829:                     if (insttypes[j] != '_LC_external') {
23830:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
23831:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
23832:                             var currSel = singleserver.selectedIndex;
23833:                             var currVal = singleserver.options[currSel].value;
23834:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
23835:                                 var numoptions = singleserver.options.length;
23836:                                 var needsnew = 1;
23837:                                 for (var k=0; k<numoptions; k++) {
23838:                                     if (singleserver.options[k] == addtarget) {
23839:                                         needsnew = 0;
23840:                                         break;
23841:                                     }
23842:                                 }
23843:                                 if (needsnew == 1) {
23844:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
23845:                                 }
23846:                             }
23847:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23848:                                 singleserver.options.length = 0;
23849:                                 if ((currVal) && (currVal != removetarget)) {
23850:                                     singleserver.options[0] = new Option("","",false,false);
23851:                                 } else {
23852:                                     singleserver.options[0] = new Option("","",true,true);
23853:                                 }
23854:                                 var idx = 0;
23855:                                 for (var m=0; m<alltargets.length; m++) {
23856:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
23857:                                         idx ++;
23858:                                         if (currVal == alltargets[m]) {
23859:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
23860:                                         } else {
23861:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23862:                                         }
23863:                                     }
23864:                                 }
23865:                             }
23866:                         }
23867:                     }
23868:                 }
23869:             }
23870:         }
23871:     }
23872:     return;
23873: }
23874: 
23875: // ]]>
23876: </script>
23877: 
23878: END
23879: }
23880: 
23881: sub new_spares_js {
23882:     my @sparestypes = ('primary','default');
23883:     my $types = join("','",@sparestypes);
23884:     my $select = &mt('Select');
23885:     return <<"END";
23886: 
23887: <script type="text/javascript">
23888: // <![CDATA[
23889: 
23890: function updateNewSpares(formname,lonhost) {
23891:     var types = new Array('$types');
23892:     var include = new Array();
23893:     var exclude = new Array();
23894:     for (var i=0; i<types.length; i++) {
23895:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23896:         for (var j=0; j<spareboxes.length; j++) {
23897:             if (formname.elements[spareboxes[j]].checked) {
23898:                 exclude.push(formname.elements[spareboxes[j]].value);
23899:             } else {
23900:                 include.push(formname.elements[spareboxes[j]].value);
23901:             }
23902:         }
23903:     }
23904:     for (var i=0; i<types.length; i++) {
23905:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23906:         var selIdx = newSpare.selectedIndex;
23907:         var currnew = newSpare.options[selIdx].value;
23908:         var okSpares = new Array();
23909:         for (var j=0; j<newSpare.options.length; j++) {
23910:             var possible = newSpare.options[j].value;
23911:             if (possible != '') {
23912:                 if (exclude.indexOf(possible) == -1) {
23913:                     okSpares.push(possible);
23914:                 } else {
23915:                     if (currnew == possible) {
23916:                         selIdx = 0;
23917:                     }
23918:                 }
23919:             }
23920:         }
23921:         for (var k=0; k<include.length; k++) {
23922:             if (okSpares.indexOf(include[k]) == -1) {
23923:                 okSpares.push(include[k]);
23924:             }
23925:         }
23926:         okSpares.sort();
23927:         newSpare.options.length = 0;
23928:         if (selIdx == 0) {
23929:             newSpare.options[0] = new Option("$select","",true,true);
23930:         } else {
23931:             newSpare.options[0] = new Option("$select","",false,false);
23932:         }
23933:         for (var m=0; m<okSpares.length; m++) {
23934:             var idx = m+1;
23935:             var selThis = 0;
23936:             if (selIdx != 0) {
23937:                 if (okSpares[m] == currnew) {
23938:                     selThis = 1;
23939:                 }
23940:             }
23941:             if (selThis == 1) {
23942:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23943:             } else {
23944:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23945:             }
23946:         }
23947:     }
23948:     return;
23949: }
23950: 
23951: function checkNewSpares(lonhost,type) {
23952:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23953:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23954:     if (chosen != '') {
23955:         var othertype;
23956:         var othernewSpare;
23957:         if (type == 'primary') {
23958:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23959:         }
23960:         if (type == 'default') {
23961:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23962:         }
23963:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23964:             othernewSpare.selectedIndex = 0;
23965:         }
23966:     }
23967:     return;
23968: }
23969: 
23970: // ]]>
23971: </script>
23972: 
23973: END
23974: 
23975: }
23976: 
23977: sub common_domprefs_js {
23978:     return <<"END";
23979: 
23980: <script type="text/javascript">
23981: // <![CDATA[
23982: 
23983: function getIndicesByName(formname,item) {
23984:     var group = new Array();
23985:     for (var i=0;i<formname.elements.length;i++) {
23986:         if (formname.elements[i].name == item) {
23987:             group.push(formname.elements[i].id);
23988:         }
23989:     }
23990:     return group;
23991: }
23992: 
23993: // ]]>
23994: </script>
23995: 
23996: END
23997: 
23998: }
23999: 
24000: sub recaptcha_js {
24001:     my %lt = &captcha_phrases();
24002:     return <<"END";
24003: 
24004: <script type="text/javascript">
24005: // <![CDATA[
24006: 
24007: function updateCaptcha(caller,context) {
24008:     var privitem;
24009:     var pubitem;
24010:     var privtext;
24011:     var pubtext;
24012:     var versionitem;
24013:     var versiontext;
24014:     if (document.getElementById(context+'_recaptchapub')) {
24015:         pubitem = document.getElementById(context+'_recaptchapub');
24016:     } else {
24017:         return;
24018:     }
24019:     if (document.getElementById(context+'_recaptchapriv')) {
24020:         privitem = document.getElementById(context+'_recaptchapriv');
24021:     } else {
24022:         return;
24023:     }
24024:     if (document.getElementById(context+'_recaptchapubtxt')) {
24025:         pubtext = document.getElementById(context+'_recaptchapubtxt');
24026:     } else {
24027:         return;
24028:     }
24029:     if (document.getElementById(context+'_recaptchaprivtxt')) {
24030:         privtext = document.getElementById(context+'_recaptchaprivtxt');
24031:     } else {
24032:         return;
24033:     }
24034:     if (document.getElementById(context+'_recaptchaversion')) {
24035:         versionitem = document.getElementById(context+'_recaptchaversion');
24036:     } else {
24037:         return;
24038:     }
24039:     if (document.getElementById(context+'_recaptchavertxt')) {
24040:         versiontext = document.getElementById(context+'_recaptchavertxt');
24041:     } else {
24042:         return;
24043:     }
24044:     if (caller.checked) {
24045:         if (caller.value == 'recaptcha') {
24046:             pubitem.type = 'text';
24047:             privitem.type = 'text';
24048:             pubitem.size = '40';
24049:             privitem.size = '40';
24050:             pubtext.innerHTML = "$lt{'pub'}";
24051:             privtext.innerHTML = "$lt{'priv'}";
24052:             versionitem.type = 'text';
24053:             versionitem.size = '3';
24054:             versiontext.innerHTML = "$lt{'ver'}";
24055:         } else {
24056:             pubitem.type = 'hidden';
24057:             privitem.type = 'hidden';
24058:             versionitem.type = 'hidden';
24059:             pubtext.innerHTML = '';
24060:             privtext.innerHTML = '';
24061:             versiontext.innerHTML = '';
24062:         }
24063:     }
24064:     return;
24065: }
24066: 
24067: // ]]>
24068: </script>
24069: 
24070: END
24071: 
24072: }
24073: 
24074: sub toggle_display_js {
24075:     return <<"END";
24076: 
24077: <script type="text/javascript">
24078: // <![CDATA[
24079: 
24080: function toggleDisplay(domForm,caller) {
24081:     if (document.getElementById(caller)) {
24082:         var divitem = document.getElementById(caller);
24083:         var optionsElement = domForm.coursecredits;
24084:         var checkval = 1;
24085:         var dispval = 'block';
24086:         var selfcreateRegExp = /^cancreate_emailverified/;
24087:         if (caller == 'emailoptions') {
24088:             optionsElement = domForm.cancreate_email;
24089:         }
24090:         if (caller == 'studentsubmission') {
24091:             optionsElement = domForm.postsubmit;
24092:         }
24093:         if (caller == 'cloneinstcode') {
24094:             optionsElement = domForm.canclone;
24095:             checkval = 'instcode';
24096:         }
24097:         if (selfcreateRegExp.test(caller)) {
24098:             optionsElement = domForm.elements[caller];
24099:             checkval = 'other';
24100:             dispval = 'inline'
24101:         }
24102:         if (optionsElement.length) {
24103:             var currval;
24104:             for (var i=0; i<optionsElement.length; i++) {
24105:                 if (optionsElement[i].checked) {
24106:                    currval = optionsElement[i].value;
24107:                 }
24108:             }
24109:             if (currval == checkval) {
24110:                 divitem.style.display = dispval;
24111:             } else {
24112:                 divitem.style.display = 'none';
24113:             }
24114:         }
24115:     }
24116:     return;
24117: }
24118: 
24119: // ]]>
24120: </script>
24121: 
24122: END
24123: 
24124: }
24125: 
24126: sub captcha_phrases {
24127:     return &Apache::lonlocal::texthash (
24128:                  priv => 'Private key',
24129:                  pub  => 'Public key',
24130:                  original  => 'original (CAPTCHA)',
24131:                  recaptcha => 'successor (ReCAPTCHA)',
24132:                  notused   => 'unused',
24133:                  ver => 'ReCAPTCHA version (1 or 2)',
24134:     );
24135: }
24136: 
24137: sub devalidate_remote_domconfs {
24138:     my ($dom,$cachekeys) = @_;
24139:     return unless (ref($cachekeys) eq 'HASH');
24140:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
24141:     my %thismachine;
24142:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
24143:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
24144:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
24145:                       'ipaccess','trust');
24146:     my %cache_by_lonhost;
24147:     if (exists($cachekeys->{'samllanding'})) {
24148:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
24149:             my %landing = %{$cachekeys->{'samllanding'}};
24150:             my %domservers = &Apache::lonnet::get_servers($dom);
24151:             if (keys(%domservers)) {
24152:                 foreach my $server (keys(%domservers)) {
24153:                     my @cached;
24154:                     next if ($thismachine{$server});
24155:                     if ($landing{$server}) {
24156:                         push(@cached,&escape('samllanding').':'.&escape($server));
24157:                     }
24158:                     if (@cached) {
24159:                         $cache_by_lonhost{$server} = \@cached;
24160:                     }
24161:                 }
24162:             }
24163:         }
24164:     }
24165:     if (keys(%servers)) {
24166:         foreach my $server (keys(%servers)) {
24167:             next if ($thismachine{$server});
24168:             my @cached;
24169:             foreach my $name (@posscached) {
24170:                 if ($cachekeys->{$name}) {
24171:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
24172:                         if (ref($cachekeys->{$name}) eq 'HASH') {
24173:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
24174:                                 push(@cached,&escape($name).':'.&escape($key));
24175:                             }
24176:                         }
24177:                     } else {
24178:                         push(@cached,&escape($name).':'.&escape($dom));
24179:                     }
24180:                 }
24181:             }
24182:             if ((exists($cache_by_lonhost{$server})) &&
24183:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
24184:                 push(@cached,@{$cache_by_lonhost{$server}});
24185:             }
24186:             if (@cached) {
24187:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
24188:             }
24189:         }
24190:     }
24191:     return;
24192: }
24193: 
24194: 1;

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