File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.430: download - view: text, annotated - select for diffs
Fri Nov 3 01:12:15 2023 UTC (6 months, 2 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Authoring Space defaults defaults which domain coordinator can override.
  - Available Editors, WebDAV access, Disk Quota for Authoring Space
    can be set for specific users by DC via "Add/modify user"
- Bug 5273 Co-authors in "group authoring" accounts can manage co-authors
  - Existing co-author(s) can be assigned rights to add/revoke co-author
    roles. Can be set by DC via "Add/modify user" or by author via "People"
- Co-authors and Assistant co-authors can view a list of co-authors for
  the Authoring Space. Availability set by Author or Co-author "manager".
  Listing can be set to "opt-in".

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.430 2023/11/03 01:12:15 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 => 'Portfilo 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',
  673:                               col2 => 'Settings'},
  674:                              {col1 => 'Consumers',
  675:                               col2 => 'Settings'},],
  676:                   print => \&print_lti,
  677:                   modify => \&modify_lti,
  678:                  },
  679:         'ipaccess' =>
  680:                        {text => 'IP-based access control',
  681:                         help => 'Domain_Configuration_IP_Access',
  682:                         header => [{col1 => 'Setting',
  683:                                     col2 => 'Value'},],
  684:                         print  => \&print_ipaccess,
  685:                         modify => \&modify_ipaccess,
  686:                        },
  687:         'authordefaults' => 
  688:                             {text => 'Authoring Space defaults',
  689:                              help => 'Domain_Configuration_Author_Defaults',
  690:                              header => [{col1 => 'Defaults which can be overridden by Author',
  691:                                          col2 => 'Settings',},
  692:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  693:                                          col2 => 'Settings',},],
  694:                              print => \&print_authordefaults,
  695:                              modify => \&modify_authordefaults,
  696:                             },
  697:     );
  698:     if (keys(%servers) > 1) {
  699:         $prefs{'login'}  = { text   => 'Log-in page options',
  700:                              help   => 'Domain_Configuration_Login_Page',
  701:                             header => [{col1 => 'Log-in Service',
  702:                                         col2 => 'Server Setting',},
  703:                                        {col1 => 'Log-in Page Items',
  704:                                         col2 => 'Settings'},
  705:                                        {col1 => 'Log-in Help',
  706:                                         col2 => 'Value'},
  707:                                        {col1 => 'Custom HTML in document head',
  708:                                         col2 => 'Value'},
  709:                                        {col1 => 'SSO',
  710:                                         col2 => 'Dual login: SSO and non-SSO options'},
  711:                                       ],
  712:                             print => \&print_login,
  713:                             modify => \&modify_login,
  714:                            };
  715:     }
  716: 
  717:     my @roles = ('student','coordinator','author','admin');
  718:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  719:     &Apache::lonhtmlcommon::add_breadcrumb
  720:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  721:       text=>"Settings to display/modify"});
  722:     my $confname = $dom.'-domainconfig';
  723: 
  724:     if ($phase eq 'process') {
  725:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  726:                                                               \%prefs,\%domconfig,$confname,\@roles);
  727:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  728:             $r->rflush();
  729:             &devalidate_remote_domconfs($dom,$result);
  730:         }
  731:     } elsif ($phase eq 'display') {
  732:         my $js = &recaptcha_js().
  733:                  &toggle_display_js();
  734:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  735:             my ($othertitle,$usertypes,$types) =
  736:                 &Apache::loncommon::sorted_inst_types($dom);
  737:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  738:                                           $domconfig{'loadbalancing'}).
  739:                    &new_spares_js().
  740:                    &common_domprefs_js().
  741:                    &Apache::loncommon::javascript_array_indexof();
  742:         }
  743:         if (grep(/^requestcourses$/,@actions)) {
  744:             my $javascript_validations;
  745:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  746:             $js .= <<END;
  747: <script type="text/javascript">
  748: $javascript_validations
  749: </script>
  750: $coursebrowserjs
  751: END
  752:         } elsif (grep(/^ipaccess$/,@actions)) {
  753:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  754:         }
  755:         if (grep(/^selfcreation$/,@actions)) {
  756:             $js .= &selfcreate_javascript();
  757:         }
  758:         if (grep(/^contacts$/,@actions)) {
  759:             $js .= &contacts_javascript();
  760:         }
  761:         if (grep(/^scantron$/,@actions)) {
  762:             $js .= &scantron_javascript();
  763:         }
  764:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  765:     } else {
  766: # check if domconfig user exists for the domain.
  767:         my $servadm = $r->dir_config('lonAdmEMail');
  768:         my ($configuserok,$author_ok,$switchserver) =
  769:             &config_check($dom,$confname,$servadm);
  770:         unless ($configuserok eq 'ok') {
  771:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  772:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  773:                           $confname).
  774:                       '<br />'
  775:             );
  776:             if ($switchserver) {
  777:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  778:                           '<br />'.
  779:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  780:                           '<br />'.
  781:                           &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).
  782:                           '<br />'.
  783:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  784:                 );
  785:             } else {
  786:                 $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.').
  787:                           '<br />'.
  788:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  789:                 );
  790:             }
  791:             $r->print(&Apache::loncommon::end_page());
  792:             return OK;
  793:         }
  794:         if (keys(%domconfig) == 0) {
  795:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  796:             my @ids=&Apache::lonnet::current_machine_ids();
  797:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  798:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  799:                 my @loginimages = ('img','logo','domlogo','login');
  800:                 my $custom_img_count = 0;
  801:                 foreach my $img (@loginimages) {
  802:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  803:                         $custom_img_count ++;
  804:                     }
  805:                 }
  806:                 foreach my $role (@roles) {
  807:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  808:                         $custom_img_count ++;
  809:                     }
  810:                 }
  811:                 if ($custom_img_count > 0) {
  812:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  813:                     my $switch_server = &check_switchserver($dom,$confname);
  814:                     $r->print(
  815:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  816:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  817:     &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 />'.
  818:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  819:                     if ($switch_server) {
  820:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  821:                     }
  822:                     $r->print(&Apache::loncommon::end_page());
  823:                     return OK;
  824:                 }
  825:             }
  826:         }
  827:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  828:     }
  829:     return OK;
  830: }
  831: 
  832: sub process_changes {
  833:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  834:     my %domconfig;
  835:     if (ref($values) eq 'HASH') {
  836:         %domconfig = %{$values};
  837:     }
  838:     my $output;
  839:     if ($action eq 'login') {
  840:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  841:     } elsif ($action eq 'rolecolors') {
  842:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  843:                                      $lastactref,%domconfig);
  844:     } elsif ($action eq 'quotas') {
  845:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  846:     } elsif ($action eq 'autoenroll') {
  847:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  848:     } elsif ($action eq 'autoupdate') {
  849:         $output = &modify_autoupdate($dom,%domconfig);
  850:     } elsif ($action eq 'autocreate') {
  851:         $output = &modify_autocreate($dom,%domconfig);
  852:     } elsif ($action eq 'directorysrch') {
  853:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  854:     } elsif ($action eq 'usercreation') {
  855:         $output = &modify_usercreation($dom,%domconfig);
  856:     } elsif ($action eq 'selfcreation') {
  857:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  858:     } elsif ($action eq 'usermodification') {
  859:         $output = &modify_usermodification($dom,%domconfig);
  860:     } elsif ($action eq 'contacts') {
  861:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  862:     } elsif ($action eq 'defaults') {
  863:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  864:     } elsif ($action eq 'scantron') {
  865:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  866:     } elsif ($action eq 'coursecategories') {
  867:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  868:     } elsif ($action eq 'serverstatuses') {
  869:         $output = &modify_serverstatuses($dom,%domconfig);
  870:     } elsif ($action eq 'requestcourses') {
  871:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  872:     } elsif ($action eq 'requestauthor') {
  873:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  874:     } elsif ($action eq 'helpsettings') {
  875:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  876:     } elsif ($action eq 'coursedefaults') {
  877:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  878:     } elsif ($action eq 'selfenrollment') {
  879:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  880:     } elsif ($action eq 'usersessions') {
  881:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  882:     } elsif ($action eq 'loadbalancing') {
  883:         $output = &modify_loadbalancing($dom,%domconfig);
  884:     } elsif ($action eq 'ltitools') {
  885:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  886:     } elsif ($action eq 'proctoring') {
  887:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  888:     } elsif ($action eq 'ssl') {
  889:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  890:     } elsif ($action eq 'trust') {
  891:         $output = &modify_trust($dom,$lastactref,%domconfig);
  892:     } elsif ($action eq 'lti') {
  893:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  894:     } elsif ($action eq 'privacy') {
  895:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  896:     } elsif ($action eq 'passwords') {
  897:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  898:     } elsif ($action eq 'wafproxy') {
  899:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  900:     } elsif ($action eq 'ipaccess') {
  901:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  902:     } elsif ($action eq 'authordefaults') {
  903:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  904:     }
  905:     return $output;
  906: }
  907: 
  908: sub print_config_box {
  909:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  910:     my $rowtotal = 0;
  911:     my $output;
  912:     if ($action eq 'coursecategories') {
  913:         $output = &coursecategories_javascript($settings);
  914:     } elsif ($action eq 'defaults') {
  915:         $output = &defaults_javascript($settings); 
  916:     } elsif ($action eq 'passwords') {
  917:         $output = &passwords_javascript($action);
  918:     } elsif ($action eq 'helpsettings') {
  919:         my (%privs,%levelscurrent);
  920:         my %full=();
  921:         my %levels=(
  922:                      course => {},
  923:                      domain => {},
  924:                      system => {},
  925:                    );
  926:         my $context = 'domain';
  927:         my $crstype = 'Course';
  928:         my $formname = 'display';
  929:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  930:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  931:         $output =
  932:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  933:                                                       \@templateroles);
  934:     } elsif ($action eq 'ltitools') {
  935:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  936:     } elsif ($action eq 'lti') {
  937:         $output .= &passwords_javascript('ltisecrets')."\n".
  938:                    &lti_javascript($dom,$settings);
  939:     } elsif ($action eq 'proctoring') {
  940:         $output .= &proctoring_javascript($settings);
  941:     } elsif ($action eq 'wafproxy') {
  942:         $output .= &wafproxy_javascript($dom);
  943:     } elsif ($action eq 'autoupdate') {
  944:         $output .= &autoupdate_javascript();
  945:     } elsif ($action eq 'autoenroll') {
  946:         $output .= &autoenroll_javascript();
  947:     } elsif ($action eq 'login') {
  948:         $output .= &saml_javascript();
  949:     } elsif ($action eq 'ipaccess') {
  950:         $output .= &ipaccess_javascript($settings);
  951:     } elsif ($action eq 'authordefaults') {
  952:         $output .= &authordefaults_javascript();
  953:     }
  954:     $output .=
  955:          '<table class="LC_nested_outer">
  956:           <tr>
  957:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  958:            &mt($item->{text}).'&nbsp;'.
  959:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  960:           '</tr>';
  961:     $rowtotal ++;
  962:     my $numheaders = 1;
  963:     if (ref($item->{'header'}) eq 'ARRAY') {
  964:         $numheaders = scalar(@{$item->{'header'}});
  965:     }
  966:     if ($numheaders > 1) {
  967:         my $colspan = '';
  968:         my $rightcolspan = '';
  969:         my $leftnobr = '';
  970:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  971:             ($action eq 'directorysrch') ||
  972:             (($action eq 'login') && ($numheaders < 5))) {
  973:             $colspan = ' colspan="2"';
  974:         }
  975:         if ($action eq 'usersessions') {
  976:             $rightcolspan = ' colspan="3"'; 
  977:         }
  978:         if ($action eq 'passwords') {
  979:             $leftnobr = ' LC_nobreak';
  980:         }
  981:         $output .= '
  982:           <tr>
  983:            <td>
  984:             <table class="LC_nested">
  985:              <tr class="LC_info_row">
  986:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  987:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  988:              </tr>';
  989:         $rowtotal ++;
  990:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  991:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  992:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  993:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  994:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  995:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
  996:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  997:         } elsif ($action eq 'passwords') {
  998:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  999:         } elsif ($action eq 'coursecategories') {
 1000:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
 1001:         } elsif ($action eq 'scantron') {
 1002:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'login') {
 1004:             if ($numheaders == 5) {
 1005:                 $colspan = ' colspan="2"';
 1006:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
 1007:             } else {
 1008:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
 1009:             }
 1010:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 1011:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1012:         } elsif ($action eq 'rolecolors') {
 1013:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1014:         }
 1015:         $output .= '
 1016:            </table>
 1017:           </td>
 1018:          </tr>
 1019:          <tr>
 1020:            <td>
 1021:             <table class="LC_nested">
 1022:              <tr class="LC_info_row">
 1023:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1024:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1025:              </tr>';
 1026:             $rowtotal ++;
 1027:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1028:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1029:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1030:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1031:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1032:             ($action eq 'ltitools')) {
 1033:             if ($action eq 'coursecategories') {
 1034:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1035:                 $colspan = ' colspan="2"';
 1036:             } elsif ($action eq 'trust') {
 1037:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1038:             } elsif ($action eq 'passwords') {
 1039:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1040:             } else {
 1041:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1042:             }
 1043:             if ($action eq 'trust') {
 1044:                 $output .= '
 1045:             </table>
 1046:           </td>
 1047:          </tr>';
 1048:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1049:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1050:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1051:                     $output .= '
 1052:          <tr>
 1053:            <td>
 1054:             <table class="LC_nested">
 1055:              <tr class="LC_info_row">
 1056:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1057:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1058:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1059:             </table>
 1060:           </td>
 1061:          </tr>';
 1062:                 }
 1063:                 $output .= '
 1064:          <tr>
 1065:            <td>
 1066:             <table class="LC_nested">
 1067:              <tr class="LC_info_row">
 1068:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1069:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1070:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1071:             } else {
 1072:                 $output .= '
 1073:            </table>
 1074:           </td>
 1075:          </tr>
 1076:          <tr>
 1077:            <td>
 1078:             <table class="LC_nested">
 1079:              <tr class="LC_info_row">
 1080:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1081:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1082:              </tr>'."\n";
 1083:                 if ($action eq 'coursecategories') {
 1084:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1085:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1086:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1087:                     if ($action eq 'passwords') {
 1088:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1089:                     } else {
 1090:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1091:                     }
 1092:                     $output .= '
 1093:              </tr>
 1094:             </table>
 1095:            </td>
 1096:           </tr>
 1097:           <tr>
 1098:            <td>
 1099:             <table class="LC_nested">
 1100:              <tr class="LC_info_row">
 1101:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1102:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1103:                     if ($action eq 'passwords') {
 1104:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1105:                     } else {
 1106:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1107:                     }
 1108:                     $output .= '
 1109:             </table>
 1110:           </td>
 1111:          </tr>
 1112:          <tr>';
 1113:                 } else {
 1114:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1115:                 }
 1116:             }
 1117:             $rowtotal ++;
 1118:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1119:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1120:                  ($action eq 'wafproxy') || ($action eq 'authordefaults')) {
 1121:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1122:         } elsif ($action eq 'scantron') {
 1123:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1124:         } elsif ($action eq 'ssl') {
 1125:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1126:             </table>
 1127:           </td>
 1128:          </tr>
 1129:          <tr>
 1130:            <td>
 1131:             <table class="LC_nested">
 1132:              <tr class="LC_info_row">
 1133:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1134:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1135:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1136:             </table>
 1137:           </td>
 1138:          </tr>
 1139:          <tr>
 1140:            <td>
 1141:             <table class="LC_nested">
 1142:              <tr class="LC_info_row">
 1143:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1144:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1145:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1146:         } elsif ($action eq 'login') {
 1147:             if ($numheaders == 5) {
 1148:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1149:            </table>
 1150:           </td>
 1151:          </tr>
 1152:          <tr>
 1153:            <td>
 1154:             <table class="LC_nested">
 1155:              <tr class="LC_info_row">
 1156:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1157:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1158:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1159:                 $rowtotal ++;
 1160:             } else {
 1161:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1162:             }
 1163:             $output .= '
 1164:            </table>
 1165:           </td>
 1166:          </tr>
 1167:          <tr>
 1168:            <td>
 1169:             <table class="LC_nested">
 1170:              <tr class="LC_info_row">';
 1171:             if ($numheaders == 5) {
 1172:                 $output .= '
 1173:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1174:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1175:              </tr>';
 1176:             } else {
 1177:                 $output .= '
 1178:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1179:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1180:              </tr>';
 1181:             }
 1182:             $rowtotal ++;
 1183:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 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'}->[4]->{'col1'}).'</td>
 1194:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1195:              </tr>';
 1196:             } else {
 1197:                 $output .= '
 1198:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1199:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1200:              </tr>';
 1201:             }
 1202:             $rowtotal ++;
 1203:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1204:         } elsif ($action eq 'requestcourses') {
 1205:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1206:             $rowtotal ++;
 1207:             $output .= &print_studentcode($settings,\$rowtotal).'
 1208:            </table>
 1209:           </td>
 1210:          </tr>
 1211:          <tr>
 1212:            <td>
 1213:             <table class="LC_nested">
 1214:              <tr class="LC_info_row">
 1215:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1216:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1217:                        &textbookcourses_javascript($settings).
 1218:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1219:             </table>
 1220:            </td>
 1221:           </tr>
 1222:          <tr>
 1223:            <td>
 1224:             <table class="LC_nested">
 1225:              <tr class="LC_info_row">
 1226:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1227:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1228:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1229:             </table>
 1230:            </td>
 1231:           </tr>
 1232:           <tr>
 1233:            <td>
 1234:             <table class="LC_nested">
 1235:              <tr class="LC_info_row">
 1236:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1237:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1238:              </tr>'.
 1239:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1240:         } elsif ($action eq 'requestauthor') {
 1241:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1242:             $rowtotal ++;
 1243:         } elsif ($action eq 'rolecolors') {
 1244:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1245:            </table>
 1246:           </td>
 1247:          </tr>
 1248:          <tr>
 1249:            <td>
 1250:             <table class="LC_nested">
 1251:              <tr class="LC_info_row">
 1252:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1253:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1254:               <td class="LC_right_item" style="vertical-align: top">'.
 1255:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1256:              </tr>'.
 1257:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1258:            </table>
 1259:           </td>
 1260:          </tr>
 1261:          <tr>
 1262:            <td>
 1263:             <table class="LC_nested">
 1264:              <tr class="LC_info_row">
 1265:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1266:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1267:              </tr>'.
 1268:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1269:             $rowtotal += 2;
 1270:         }
 1271:     } else {
 1272:         $output .= '
 1273:           <tr>
 1274:            <td>
 1275:             <table class="LC_nested">
 1276:              <tr class="LC_info_row">';
 1277:         if ($action eq 'login') {
 1278:             $output .= '  
 1279:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1280:         } elsif ($action eq 'serverstatuses') {
 1281:             $output .= '
 1282:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1283:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1284: 
 1285:         } else {
 1286:             $output .= '
 1287:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1288:         }
 1289:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1290:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1291:                        &mt($item->{'header'}->[0]->{'col2'});
 1292:             if ($action eq 'serverstatuses') {
 1293:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1294:             } 
 1295:         } else {
 1296:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1297:                        &mt($item->{'header'}->[0]->{'col2'});
 1298:         }
 1299:         $output .= '</td>';
 1300:         if ($item->{'header'}->[0]->{'col3'}) {
 1301:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1302:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1303:                             &mt($item->{'header'}->[0]->{'col3'});
 1304:             } else {
 1305:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1306:                            &mt($item->{'header'}->[0]->{'col3'});
 1307:             }
 1308:             if ($action eq 'serverstatuses') {
 1309:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1310:             }
 1311:             $output .= '</td>';
 1312:         }
 1313:         if ($item->{'header'}->[0]->{'col4'}) {
 1314:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1315:                        &mt($item->{'header'}->[0]->{'col4'});
 1316:         }
 1317:         $output .= '</tr>';
 1318:         $rowtotal ++;
 1319:         if ($action eq 'quotas') {
 1320:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1321:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1322:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1323:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1324:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1325:         }
 1326:     }
 1327:     $output .= '
 1328:    </table>
 1329:   </td>
 1330:  </tr>
 1331: </table><br />';
 1332:     return ($output,$rowtotal);
 1333: }
 1334: 
 1335: sub print_login {
 1336:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1337:     my ($css_class,$datatable,$switchserver,%lt);
 1338:     my %choices = &login_choices();
 1339:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1340:         %lt = &login_file_options();
 1341:         $switchserver = &check_switchserver($dom,$confname);
 1342:     }
 1343:     if ($caller eq 'service') {
 1344:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1345:         my $choice = $choices{'disallowlogin'};
 1346:         $css_class = ' class="LC_odd_row"';
 1347:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1348:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1349:                       '<th>'.$choices{'server'}.'</th>'.
 1350:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1351:                       '<th>'.$choices{'custompath'}.'</th>'.
 1352:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1353:         my %disallowed;
 1354:         if (ref($settings) eq 'HASH') {
 1355:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1356:                %disallowed = %{$settings->{'loginvia'}};
 1357:             }
 1358:         }
 1359:         foreach my $lonhost (sort(keys(%servers))) {
 1360:             my $direct = 'selected="selected"';
 1361:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1362:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1363:                     $direct = '';
 1364:                 }
 1365:             }
 1366:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1367:                           '<td><select name="'.$lonhost.'_server">'.
 1368:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1369:                           '</option>';
 1370:             foreach my $hostid (sort(keys(%servers))) {
 1371:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1372:                 my $selected = '';
 1373:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1374:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1375:                         $selected = 'selected="selected"';
 1376:                     }
 1377:                 }
 1378:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1379:                               $servers{$hostid}.'</option>';
 1380:             }
 1381:             $datatable .= '</select></td>'.
 1382:                           '<td><select name="'.$lonhost.'_serverpath">';
 1383:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1384:                 my $pathname = $path;
 1385:                 if ($path eq 'custom') {
 1386:                     $pathname = &mt('Custom Path').' ->';
 1387:                 }
 1388:                 my $selected = '';
 1389:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1390:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1391:                         $selected = 'selected="selected"';
 1392:                     }
 1393:                 } elsif ($path eq '') {
 1394:                     $selected = 'selected="selected"';
 1395:                 }
 1396:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1397:             }
 1398:             $datatable .= '</select></td>';
 1399:             my ($custom,$exempt);
 1400:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1401:                 $custom = $disallowed{$lonhost}{'custompath'};
 1402:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1403:             }
 1404:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1405:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1406:                           '</tr>';
 1407:         }
 1408:         $datatable .= '</table></td></tr>';
 1409:         return $datatable;
 1410:     } elsif ($caller eq 'page') {
 1411:         my %defaultchecked = ( 
 1412:                                'coursecatalog' => 'on',
 1413:                                'helpdesk'      => 'on',
 1414:                                'adminmail'     => 'off',
 1415:                                'newuser'       => 'off',
 1416:                              );
 1417:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1418:         my (%checkedon,%checkedoff);
 1419:         foreach my $item (@toggles) {
 1420:             if ($defaultchecked{$item} eq 'on') { 
 1421:                 $checkedon{$item} = ' checked="checked" ';
 1422:                 $checkedoff{$item} = ' ';
 1423:             } elsif ($defaultchecked{$item} eq 'off') {
 1424:                 $checkedoff{$item} = ' checked="checked" ';
 1425:                 $checkedon{$item} = ' ';
 1426:             }
 1427:         }
 1428:         my @images = ('img','logo','domlogo','login');
 1429:         my @alttext = ('img','logo','domlogo');
 1430:         my @logintext = ('textcol','bgcol');
 1431:         my @bgs = ('pgbg','mainbg','sidebg');
 1432:         my @links = ('link','alink','vlink');
 1433:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1434:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1435:         my (%is_custom,%designs);
 1436:         my %defaults = (
 1437:                        font => $defaultdesign{'login.font'},
 1438:                        );
 1439:         foreach my $item (@images) {
 1440:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1441:             $defaults{'showlogo'}{$item} = 1;
 1442:         }
 1443:         foreach my $item (@bgs) {
 1444:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1445:         }
 1446:         foreach my $item (@logintext) {
 1447:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1448:         }
 1449:         foreach my $item (@links) {
 1450:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1451:         }
 1452:         if (ref($settings) eq 'HASH') {
 1453:             foreach my $item (@toggles) {
 1454:                 if ($settings->{$item} eq '1') {
 1455:                     $checkedon{$item} =  ' checked="checked" ';
 1456:                     $checkedoff{$item} = ' ';
 1457:                 } elsif ($settings->{$item} eq '0') {
 1458:                     $checkedoff{$item} =  ' checked="checked" ';
 1459:                     $checkedon{$item} = ' ';
 1460:                 }
 1461:             }
 1462:             foreach my $item (@images) {
 1463:                 if (defined($settings->{$item})) {
 1464:                     $designs{$item} = $settings->{$item};
 1465:                     $is_custom{$item} = 1;
 1466:                 }
 1467:                 if (defined($settings->{'showlogo'}{$item})) {
 1468:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1469:                 }
 1470:             }
 1471:             foreach my $item (@alttext) {
 1472:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1473:                     if ($settings->{'alttext'}->{$item} ne '') {
 1474:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1475:                     }
 1476:                 }
 1477:             }
 1478:             foreach my $item (@logintext) {
 1479:                 if ($settings->{$item} ne '') {
 1480:                     $designs{'logintext'}{$item} = $settings->{$item};
 1481:                     $is_custom{$item} = 1;
 1482:                 }
 1483:             }
 1484:             if ($settings->{'font'} ne '') {
 1485:                 $designs{'font'} = $settings->{'font'};
 1486:                 $is_custom{'font'} = 1;
 1487:             }
 1488:             foreach my $item (@bgs) {
 1489:                 if ($settings->{$item} ne '') {
 1490:                     $designs{'bgs'}{$item} = $settings->{$item};
 1491:                     $is_custom{$item} = 1;
 1492:                 }
 1493:             }
 1494:             foreach my $item (@links) {
 1495:                 if ($settings->{$item} ne '') {
 1496:                     $designs{'links'}{$item} = $settings->{$item};
 1497:                     $is_custom{$item} = 1;
 1498:                 }
 1499:             }
 1500:         } else {
 1501:             if ($designhash{$dom.'.login.font'} ne '') {
 1502:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1503:                 $is_custom{'font'} = 1;
 1504:             }
 1505:             foreach my $item (@images) {
 1506:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1507:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1508:                     $is_custom{$item} = 1;
 1509:                 }
 1510:             }
 1511:             foreach my $item (@bgs) {
 1512:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1513:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1514:                     $is_custom{$item} = 1;
 1515:                 }
 1516:             }
 1517:             foreach my $item (@links) {
 1518:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1519:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1520:                     $is_custom{$item} = 1;
 1521:                 }
 1522:             }
 1523:         }
 1524:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1525:                                                       logo => 'Institution Logo',
 1526:                                                       domlogo => 'Domain Logo',
 1527:                                                       login => 'Login box');
 1528:         my $itemcount = 1;
 1529:         foreach my $item (@toggles) {
 1530:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1531:             $datatable .=  
 1532:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1533:                 '</td><td>'.
 1534:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1535:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1536:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1537:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1538:                 '</tr>';
 1539:             $itemcount ++;
 1540:         }
 1541:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1542:         $datatable .= '</tr></table></td></tr>';
 1543:     } elsif ($caller eq 'help') {
 1544:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1545:         my $itemcount = 1;
 1546:         $defaulturl = '/adm/loginproblems.html';
 1547:         $defaulttype = 'default';
 1548:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1549:         my @currlangs;
 1550:         if (ref($settings) eq 'HASH') {
 1551:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1552:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1553:                     next if ($settings->{'helpurl'}{$key} eq '');
 1554:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1555:                     $type{$key} = 'custom';
 1556:                     unless ($key eq 'nolang') {
 1557:                         push(@currlangs,$key);
 1558:                     }
 1559:                 }
 1560:             } elsif ($settings->{'helpurl'} ne '') {
 1561:                 $type{'nolang'} = 'custom';
 1562:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1563:             }
 1564:         }
 1565:         foreach my $lang ('nolang',sort(@currlangs)) {
 1566:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1567:             $datatable .= '<tr'.$css_class.'>';
 1568:             if ($url{$lang} eq '') {
 1569:                 $url{$lang} = $defaulturl;
 1570:             }
 1571:             if ($type{$lang} eq '') {
 1572:                 $type{$lang} = $defaulttype;
 1573:             }
 1574:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1575:             if ($lang eq 'nolang') {
 1576:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1577:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1578:             } else {
 1579:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1580:                                   $langchoices{$lang},
 1581:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1582:             }
 1583:             $datatable .= '</span></td>'."\n".
 1584:                           '<td class="LC_left_item">';
 1585:             if ($type{$lang} eq 'custom') {
 1586:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1587:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1588:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1589:             } else {
 1590:                 $datatable .= $lt{'upl'};
 1591:             }
 1592:             $datatable .='<br />';
 1593:             if ($switchserver) {
 1594:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1595:             } else {
 1596:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1597:             }
 1598:             $datatable .= '</td></tr>';
 1599:             $itemcount ++;
 1600:         }
 1601:         my @addlangs;
 1602:         foreach my $lang (sort(keys(%langchoices))) {
 1603:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1604:             push(@addlangs,$lang);
 1605:         }
 1606:         if (@addlangs > 0) {
 1607:             my %toadd;
 1608:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1609:             $toadd{''} = &mt('Select');
 1610:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1611:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1612:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1613:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1614:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1615:             if ($switchserver) {
 1616:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1617:             } else {
 1618:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1619:             }
 1620:             $datatable .= '</td></tr>';
 1621:             $itemcount ++;
 1622:         }
 1623:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1624:     } elsif ($caller eq 'headtag') {
 1625:         my %domservers = &Apache::lonnet::get_servers($dom);
 1626:         my $choice = $choices{'headtag'};
 1627:         $css_class = ' class="LC_odd_row"';
 1628:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1629:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1630:                       '<th>'.$choices{'current'}.'</th>'.
 1631:                       '<th>'.$choices{'action'}.'</th>'.
 1632:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1633:         my (%currurls,%currexempt);
 1634:         if (ref($settings) eq 'HASH') {
 1635:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1636:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1637:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1638:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1639:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1640:                     }
 1641:                 }
 1642:             }
 1643:         }
 1644:         foreach my $lonhost (sort(keys(%domservers))) {
 1645:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1646:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1647:             if ($currurls{$lonhost}) {
 1648:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1649:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1650:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1651:                               '">'.$lt{'curr'}.'</a></td>'.
 1652:                               '<td><span class="LC_nobreak"><label>'.
 1653:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1654:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1655:             } else {
 1656:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1657:             }
 1658:             $datatable .='<br />';
 1659:             if ($switchserver) {
 1660:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1661:             } else {
 1662:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1663:             }
 1664:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1665:         }
 1666:         $datatable .= '</table></td></tr>';
 1667:     } elsif ($caller eq 'saml') {
 1668:         my %domservers = &Apache::lonnet::get_servers($dom);
 1669:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1670:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1671:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1672:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1673:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1674:         foreach my $lonhost (keys(%domservers)) {
 1675:             $samlurl{$lonhost} = '/adm/sso';
 1676:             $styleon{$lonhost} = 'display:none';
 1677:             $styleoff{$lonhost} = '';
 1678:         }
 1679:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1680:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1681:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1682:                     $saml{$lonhost} = 1;
 1683:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1684:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1685:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1686:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1687:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1688:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1689:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1690:                     $styleon{$lonhost} = '';
 1691:                     $styleoff{$lonhost} = 'display:none';
 1692:                 } else {
 1693:                     $styleon{$lonhost} = 'display:none';
 1694:                     $styleoff{$lonhost} = '';
 1695:                 }
 1696:             }
 1697:         }
 1698:         my $itemcount = 1;
 1699:         foreach my $lonhost (sort(keys(%domservers))) {
 1700:             my $samlon = ' ';
 1701:             my $samloff = ' checked="checked" ';
 1702:             if ($saml{$lonhost}) {
 1703:                 $samlon = $samloff;
 1704:                 $samloff = ' ';
 1705:             }
 1706:             my $samlwinon = '';
 1707:             my $samlwinoff = ' checked="checked"';
 1708:             if ($samlwindow{$lonhost}) {
 1709:                 $samlwinon = $samlwinoff;
 1710:                 $samlwinoff = '';
 1711:             }
 1712:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1713:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1714:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1715:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1716:                           &mt('No').'</label>'.('&nbsp;'x2).
 1717:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1718:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1719:                           &mt('Yes').'</label></span></td>'.
 1720:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1721:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1722:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1723:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1724:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1725:                           $samltext{$lonhost}.'" /></td><td>';
 1726:             if ($samlimg{$lonhost}) {
 1727:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1728:                               '<span class="LC_nobreak"><label>'.
 1729:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1730:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1731:             } else {
 1732:                 $datatable .= $lt{'upl'};
 1733:             }
 1734:             $datatable .='<br />';
 1735:             if ($switchserver) {
 1736:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1737:             } else {
 1738:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1739:             }
 1740:             $datatable .= '</td>'.
 1741:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1742:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1743:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1744:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1745:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1746:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1747:                           '<tr'.$css_class.'>'.
 1748:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1749:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1750:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1751:                           $samltitle{$lonhost}.'</textarea></td>'.
 1752:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1753:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1754:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1755:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1756:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1757:                           '</table></td>'.
 1758:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1759:            $itemcount ++;
 1760:         }
 1761:         $datatable .= '</table></td></tr>';
 1762:     }
 1763:     return $datatable;
 1764: }
 1765: 
 1766: sub login_choices {
 1767:     my %choices =
 1768:         &Apache::lonlocal::texthash (
 1769:             coursecatalog => 'Display Course/Community Catalog link?',
 1770:             adminmail     => "Display Administrator's E-mail Address?",
 1771:             helpdesk      => 'Display "Contact Helpdesk" link',
 1772:             disallowlogin => "Login page requests redirected",
 1773:             hostid        => "Server",
 1774:             server        => "Redirect to:",
 1775:             serverpath    => "Path",
 1776:             custompath    => "Custom", 
 1777:             exempt        => "Exempt IP(s)",
 1778:             directlogin   => "No redirect",
 1779:             newuser       => "Link to create a user account",
 1780:             img           => "Header",
 1781:             logo          => "Main Logo",
 1782:             domlogo       => "Domain Logo",
 1783:             login         => "Log-in Header", 
 1784:             textcol       => "Text color",
 1785:             bgcol         => "Box color",
 1786:             bgs           => "Background colors",
 1787:             links         => "Link colors",
 1788:             font          => "Font color",
 1789:             pgbg          => "Header",
 1790:             mainbg        => "Page",
 1791:             sidebg        => "Login box",
 1792:             link          => "Link",
 1793:             alink         => "Active link",
 1794:             vlink         => "Visited link",
 1795:             headtag       => "Custom markup",
 1796:             action        => "Action",
 1797:             current       => "Current",
 1798:             samllanding   => "Dual login?",
 1799:             samloptions   => "Options",
 1800:             alttext       => "Alt text",
 1801:         );
 1802:     return %choices;
 1803: }
 1804: 
 1805: sub login_file_options {
 1806:       return &Apache::lonlocal::texthash(
 1807:                                            del     => 'Delete?',
 1808:                                            rep     => 'Replace:',
 1809:                                            upl     => 'Upload:',
 1810:                                            curr    => 'View contents',
 1811:                                            default => 'Default',
 1812:                                            custom  => 'Custom',
 1813:                                            none    => 'None',
 1814:       );
 1815: }
 1816: 
 1817: sub print_ipaccess {
 1818:     my ($dom,$settings,$rowtotal) = @_;
 1819:     my $css_class;
 1820:     my $itemcount = 0;
 1821:     my $datatable;
 1822:     my %ordered;
 1823:     if (ref($settings) eq 'HASH') {
 1824:         foreach my $item (keys(%{$settings})) {
 1825:             if (ref($settings->{$item}) eq 'HASH') {
 1826:                 my $num = $settings->{$item}{'order'};
 1827:                 if ($num eq '') {
 1828:                     $num = scalar(keys(%{$settings}));
 1829:                 }
 1830:                 $ordered{$num} = $item;
 1831:             }
 1832:         }
 1833:     }
 1834:     my $maxnum = scalar(keys(%ordered));
 1835:     if (keys(%ordered)) {
 1836:         my @items = sort { $a <=> $b } keys(%ordered);
 1837:         for (my $i=0; $i<@items; $i++) {
 1838:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1839:             my $item = $ordered{$items[$i]};
 1840:             my ($name,$ipranges,%commblocks,%courses);
 1841:             if (ref($settings->{$item}) eq 'HASH') {
 1842:                 $name = $settings->{$item}->{'name'};
 1843:                 $ipranges = $settings->{$item}->{'ip'};
 1844:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1845:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1846:                 }
 1847:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1848:                     %courses = %{$settings->{$item}->{'courses'}};
 1849:                 }
 1850:             }
 1851:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1852:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1853:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1854:             for (my $k=0; $k<=$maxnum; $k++) {
 1855:                 my $vpos = $k+1;
 1856:                 my $selstr;
 1857:                 if ($k == $i) {
 1858:                     $selstr = ' selected="selected" ';
 1859:                 }
 1860:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1861:             }
 1862:             $datatable .= '</select>'.('&nbsp;'x2).
 1863:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1864:                 &mt('Delete?').'</label></span></td>'.
 1865:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1866:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1867:                 '</td></tr>';
 1868:             $itemcount ++;
 1869:         }
 1870:     }
 1871:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1872:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1873:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1874:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1875:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1876:     for (my $k=0; $k<$maxnum+1; $k++) {
 1877:         my $vpos = $k+1;
 1878:         my $selstr;
 1879:         if ($k == $maxnum) {
 1880:             $selstr = ' selected="selected" ';
 1881:         }
 1882:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1883:     }
 1884:     $datatable .= '</select>&nbsp;'."\n".
 1885:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1886:                   '<td colspan="2">'.
 1887:                   &ipaccess_options('add',$itemcount,$dom).
 1888:                   '</td>'."\n".
 1889:                   '</tr>'."\n";
 1890:     $$rowtotal ++;
 1891:     return $datatable;
 1892: }
 1893: 
 1894: sub ipaccess_options {
 1895:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1896:     my (%currblocks,%currcourses,$output);
 1897:     if (ref($blocksref) eq 'HASH') {
 1898:         %currblocks = %{$blocksref};
 1899:     }
 1900:     if (ref($coursesref) eq 'HASH') {
 1901:         %currcourses = %{$coursesref};
 1902:     }
 1903:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1904:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1905:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1906:               '</span></fieldset>'.
 1907:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1908:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1909:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1910:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1911:               $ipranges.'</textarea></fieldset>'.
 1912:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1913:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1914:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1915:               '<table>';
 1916:     foreach my $cid (sort(keys(%currcourses))) {
 1917:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1918:         $output .= '<tr><td><span class="LC_nobreak">'.
 1919:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1920:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1921:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1922:     }
 1923:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1924:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1925:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1926:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1927:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1928:                '</span></td></tr></table>'."\n".
 1929:                '</fieldset>';
 1930:     return $output;
 1931: }
 1932: 
 1933: sub blocker_checkboxes {
 1934:     my ($num,$blocks) = @_;
 1935:     my ($typeorder,$types) = &commblocktype_text();
 1936:     my $numinrow = 6;
 1937:     my $output = '<table>';
 1938:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1939:         my $block = $typeorder->[$i];
 1940:         my $blockstatus;
 1941:         if (ref($blocks) eq 'HASH') {
 1942:             if ($blocks->{$block} eq 'on') {
 1943:                 $blockstatus = 'checked="checked"';
 1944:             }
 1945:         }
 1946:         my $rem = $i%($numinrow);
 1947:         if ($rem == 0) {
 1948:             if ($i > 0) {
 1949:                 $output .= '</tr>';
 1950:             }
 1951:             $output .= '<tr>';
 1952:         }
 1953:         if ($i == scalar(@{$typeorder})-1) {
 1954:             my $colsleft = $numinrow-$rem;
 1955:             if ($colsleft > 1) {
 1956:                 $output .= '<td colspan="'.$colsleft.'">';
 1957:             } else {
 1958:                 $output .= '<td>';
 1959:             }
 1960:         } else {
 1961:             $output .= '<td>';
 1962:         }
 1963:         my $item = 'ipaccess_block_'.$num;
 1964:         if ($blockstatus) {
 1965:             $blockstatus = ' '.$blockstatus;
 1966:         }
 1967:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1968:                    '<input type="checkbox" name="'.$item.'"'.
 1969:                    $blockstatus.' value="'.$block.'"'.' />'.
 1970:                    $types->{$block}.'</label></span>'."\n".
 1971:                    '<br /></td>';
 1972:     }
 1973:     $output .= '</tr></table>';
 1974:     return $output;
 1975: }
 1976: 
 1977: sub commblocktype_text {
 1978:     my %types = &Apache::lonlocal::texthash(
 1979:         'com' => 'Messaging',
 1980:         'chat' => 'Chat Room',
 1981:         'boards' => 'Discussion',
 1982:         'port' => 'Portfolio',
 1983:         'groups' => 'Groups',
 1984:         'blogs' => 'Blogs',
 1985:         'about' => 'User Information',
 1986:         'printout' => 'Printouts',
 1987:         'passwd' => 'Change Password',
 1988:         'grades' => 'Gradebook',
 1989:         'search' => 'Course search',
 1990:         'wishlist' => 'Stored links',
 1991:         'annotate' => 'Annotations',
 1992:     );
 1993:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1994:     return ($typeorder,\%types);
 1995: }
 1996: 
 1997: sub print_rolecolors {
 1998:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1999:     my %choices = &color_font_choices();
 2000:     my @bgs = ('pgbg','tabbg','sidebg');
 2001:     my @links = ('link','alink','vlink');
 2002:     my @images = ('img');
 2003:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 2004:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 2005:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2006:     my (%is_custom,%designs);
 2007:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 2008:     if (ref($settings) eq 'HASH') {
 2009:         if (ref($settings->{$role}) eq 'HASH') {
 2010:             if ($settings->{$role}->{'img'} ne '') {
 2011:                 $designs{'img'} = $settings->{$role}->{'img'};
 2012:                 $is_custom{'img'} = 1;
 2013:             }
 2014:             if ($settings->{$role}->{'font'} ne '') {
 2015:                 $designs{'font'} = $settings->{$role}->{'font'};
 2016:                 $is_custom{'font'} = 1;
 2017:             }
 2018:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2019:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2020:                 $is_custom{'fontmenu'} = 1;
 2021:             }
 2022:             foreach my $item (@bgs) {
 2023:                 if ($settings->{$role}->{$item} ne '') {
 2024:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2025:                     $is_custom{$item} = 1;
 2026:                 }
 2027:             }
 2028:             foreach my $item (@links) {
 2029:                 if ($settings->{$role}->{$item} ne '') {
 2030:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2031:                     $is_custom{$item} = 1;
 2032:                 }
 2033:             }
 2034:         }
 2035:     } else {
 2036:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 2037:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 2038:             $is_custom{'img'} = 1;
 2039:         }
 2040:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2041:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2042:             $is_custom{'fontmenu'} = 1; 
 2043:         }
 2044:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2045:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2046:             $is_custom{'font'} = 1;
 2047:         }
 2048:         foreach my $item (@bgs) {
 2049:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2050:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2051:                 $is_custom{$item} = 1;
 2052:             
 2053:             }
 2054:         }
 2055:         foreach my $item (@links) {
 2056:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2057:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2058:                 $is_custom{$item} = 1;
 2059:             }
 2060:         }
 2061:     }
 2062:     my $itemcount = 1;
 2063:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2064:     $datatable .= '</tr></table></td></tr>';
 2065:     return $datatable;
 2066: }
 2067: 
 2068: sub role_defaults {
 2069:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2070:     my %defaults;
 2071:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2072:         return %defaults;
 2073:     }
 2074:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2075:     if ($role eq 'login') {
 2076:         %defaults = (
 2077:                        font => $defaultdesign{$role.'.font'},
 2078:                     );
 2079:         if (ref($logintext) eq 'ARRAY') {
 2080:             foreach my $item (@{$logintext}) {
 2081:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2082:             }
 2083:         }
 2084:         foreach my $item (@{$images}) {
 2085:             $defaults{'showlogo'}{$item} = 1;
 2086:         }
 2087:     } else {
 2088:         %defaults = (
 2089:                        img => $defaultdesign{$role.'.img'},
 2090:                        font => $defaultdesign{$role.'.font'},
 2091:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2092:                     );
 2093:     }
 2094:     foreach my $item (@{$bgs}) {
 2095:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2096:     }
 2097:     foreach my $item (@{$links}) {
 2098:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2099:     }
 2100:     foreach my $item (@{$images}) {
 2101:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2102:     }
 2103:     return %defaults;
 2104: }
 2105: 
 2106: sub display_color_options {
 2107:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2108:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2109:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2110:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2111:     my $datatable = '<tr'.$css_class.'>'.
 2112:         '<td>'.$choices->{'font'}.'</td>';
 2113:     if (!$is_custom->{'font'}) {
 2114:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2115:     } else {
 2116:         $datatable .= '<td>&nbsp;</td>';
 2117:     }
 2118:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2119: 
 2120:     $datatable .= '<td><span class="LC_nobreak">'.
 2121:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2122:                   ' value="'.$current_color.'" />&nbsp;'.
 2123:                   '&nbsp;</span></td></tr>';
 2124:     unless ($role eq 'login') { 
 2125:         $datatable .= '<tr'.$css_class.'>'.
 2126:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2127:         if (!$is_custom->{'fontmenu'}) {
 2128:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2129:         } else {
 2130:             $datatable .= '<td>&nbsp;</td>';
 2131:         }
 2132: 	$current_color = $designs->{'fontmenu'} ?
 2133: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2134:         $datatable .= '<td><span class="LC_nobreak">'.
 2135:                       '<input class="colorchooser" type="text" size="10" name="'
 2136: 		      .$role.'_fontmenu"'.
 2137:                       ' value="'.$current_color.'" />&nbsp;'.
 2138:                       '&nbsp;</span></td></tr>';
 2139:     }
 2140:     my $switchserver = &check_switchserver($dom,$confname);
 2141:     foreach my $img (@{$images}) {
 2142: 	$itemcount ++;
 2143:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2144:         $datatable .= '<tr'.$css_class.'>'.
 2145:                       '<td>'.$choices->{$img};
 2146:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2147:         if ($role eq 'login') {
 2148:             if ($img eq 'login') {
 2149:                 $login_hdr_pick =
 2150:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2151:                 $logincolors =
 2152:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2153:                                        $designs,$defaults);
 2154:             } else {
 2155:                 if ($img ne 'domlogo') {
 2156:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2157:                 }
 2158:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2159:                     $alttext = $designs->{'alttext'}{$img};
 2160:                 }
 2161:             }
 2162:         }
 2163:         $datatable .= '</td>';
 2164:         if ($designs->{$img} ne '') {
 2165:             $imgfile = $designs->{$img};
 2166: 	    $img_import = ($imgfile =~ m{^/adm/});
 2167:         } else {
 2168:             $imgfile = $defaults->{$img};
 2169:         }
 2170:         if ($imgfile) {
 2171:             my ($showfile,$fullsize);
 2172:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2173:                 my $urldir = $1;
 2174:                 my $filename = $2;
 2175:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2176:                 if (@info) {
 2177:                     my $thumbfile = 'tn-'.$filename;
 2178:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2179:                     if (@thumb) {
 2180:                         $showfile = $urldir.'/'.$thumbfile;
 2181:                     } else {
 2182:                         $showfile = $imgfile;
 2183:                     }
 2184:                 } else {
 2185:                     $showfile = '';
 2186:                 }
 2187:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2188:                 $showfile = $imgfile;
 2189:                 my $imgdir = $1;
 2190:                 my $filename = $2;
 2191:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2192:                     $showfile = "/$imgdir/tn-".$filename;
 2193:                 } else {
 2194:                     my $input = $londocroot.$imgfile;
 2195:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2196:                     if (!-e $output) {
 2197:                         my ($width,$height) = &thumb_dimensions();
 2198:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2199:                         if ($fullwidth ne '' && $fullheight ne '') {
 2200:                             if ($fullwidth > $width && $fullheight > $height) { 
 2201:                                 my $size = $width.'x'.$height;
 2202:                                 my @args = ('convert','-sample',$size,$input,$output);
 2203:                                 system({$args[0]} @args);
 2204:                                 $showfile = "/$imgdir/tn-".$filename;
 2205:                             }
 2206:                         }
 2207:                     }
 2208:                 }
 2209:             }
 2210:             if ($showfile) {
 2211:                 if ($showfile =~ m{^/(adm|res)/}) {
 2212:                     if ($showfile =~ m{^/res/}) {
 2213:                         my $local_showfile =
 2214:                             &Apache::lonnet::filelocation('',$showfile);
 2215:                         &Apache::lonnet::repcopy($local_showfile);
 2216:                     }
 2217:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2218:                 }
 2219:                 if ($imgfile) {
 2220:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2221:                         if ($imgfile =~ m{^/res/}) {
 2222:                             my $local_imgfile =
 2223:                                 &Apache::lonnet::filelocation('',$imgfile);
 2224:                             &Apache::lonnet::repcopy($local_imgfile);
 2225:                         }
 2226:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2227:                     } else {
 2228:                         $fullsize = $imgfile;
 2229:                     }
 2230:                 }
 2231:                 $datatable .= '<td>';
 2232:                 if ($img eq 'login') {
 2233:                     $datatable .= $login_hdr_pick;
 2234:                 } 
 2235:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2236:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2237:             } else {
 2238:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2239:                               &mt('Upload:').'<br />';
 2240:             }
 2241:         } else {
 2242:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2243:                           &mt('Upload:').'<br />';
 2244:         }
 2245:         if ($switchserver) {
 2246:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2247:         } else {
 2248:             if ($img ne 'login') { # suppress file selection for Log-in header
 2249:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2250:             }
 2251:         }
 2252:         if (($role eq 'login') && ($img ne 'login')) {
 2253:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2254:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2255:                           '</label></span>';
 2256:         }
 2257:         $datatable .= '</td></tr>';
 2258:     }
 2259:     $itemcount ++;
 2260:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2261:     $datatable .= '<tr'.$css_class.'>'.
 2262:                   '<td>'.$choices->{'bgs'}.'</td>';
 2263:     my $bgs_def;
 2264:     foreach my $item (@{$bgs}) {
 2265:         if (!$is_custom->{$item}) {
 2266:             $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>';
 2267:         }
 2268:     }
 2269:     if ($bgs_def) {
 2270:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2271:     } else {
 2272:         $datatable .= '<td>&nbsp;</td>';
 2273:     }
 2274:     $datatable .= '<td class="LC_right_item">'.
 2275:                   '<table border="0"><tr>';
 2276: 
 2277:     foreach my $item (@{$bgs}) {
 2278:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2279: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2280:         if ($designs->{'bgs'}{$item}) {
 2281:             $datatable .= '&nbsp;';
 2282:         }
 2283:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2284:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2285:     }
 2286:     $datatable .= '</tr></table></td></tr>';
 2287:     $itemcount ++;
 2288:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2289:     $datatable .= '<tr'.$css_class.'>'.
 2290:                   '<td>'.$choices->{'links'}.'</td>';
 2291:     my $links_def;
 2292:     foreach my $item (@{$links}) {
 2293:         if (!$is_custom->{$item}) {
 2294:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2295:         }
 2296:     }
 2297:     if ($links_def) {
 2298:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2299:     } else {
 2300:         $datatable .= '<td>&nbsp;</td>';
 2301:     }
 2302:     $datatable .= '<td class="LC_right_item">'.
 2303:                   '<table border="0"><tr>';
 2304:     foreach my $item (@{$links}) {
 2305: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2306:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2307:         if ($designs->{'links'}{$item}) {
 2308:             $datatable.='&nbsp;';
 2309:         }
 2310:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2311:                       '" /></td>';
 2312:     }
 2313:     $$rowtotal += $itemcount;
 2314:     return $datatable;
 2315: }
 2316: 
 2317: sub logo_display_options {
 2318:     my ($img,$defaults,$designs) = @_;
 2319:     my $checkedon;
 2320:     if (ref($defaults) eq 'HASH') {
 2321:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2322:             if ($defaults->{'showlogo'}{$img}) {
 2323:                 $checkedon = 'checked="checked" ';     
 2324:             }
 2325:         } 
 2326:     }
 2327:     if (ref($designs) eq 'HASH') {
 2328:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2329:             if (defined($designs->{'showlogo'}{$img})) {
 2330:                 if ($designs->{'showlogo'}{$img} == 0) {
 2331:                     $checkedon = '';
 2332:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2333:                     $checkedon = 'checked="checked" ';
 2334:                 }
 2335:             }
 2336:         }
 2337:     }
 2338:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2339:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2340:            &mt('show').'</label>'."\n";
 2341: }
 2342: 
 2343: sub login_header_options  {
 2344:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2345:     my $output = '';
 2346:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2347:         $output .= &mt('Text default(s):').'<br />';
 2348:         if (!$is_custom->{'textcol'}) {
 2349:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2350:                        '&nbsp;&nbsp;&nbsp;';
 2351:         }
 2352:         if (!$is_custom->{'bgcol'}) {
 2353:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2354:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2355:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2356:         }
 2357:         $output .= '<br />';
 2358:     }
 2359:     $output .='<br />';
 2360:     return $output;
 2361: }
 2362: 
 2363: sub login_text_colors {
 2364:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2365:     my $color_menu = '<table border="0"><tr>';
 2366:     foreach my $item (@{$logintext}) {
 2367:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2368:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2369:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2370:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2371:     }
 2372:     $color_menu .= '</tr></table><br />';
 2373:     return $color_menu;
 2374: }
 2375: 
 2376: sub image_changes {
 2377:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2378:     my $output;
 2379:     if ($img eq 'login') {
 2380:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2381:     } elsif (!$is_custom) {
 2382:         if ($img ne 'domlogo') {
 2383:             $output = &mt('Default image:').'<br />';
 2384:         } else {
 2385:             $output = &mt('Default in use:').'<br />';
 2386:         }
 2387:     }
 2388:     if ($img ne 'login') {
 2389:         if ($img_import) {
 2390:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2391:         }
 2392:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2393:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2394:         if ($is_custom) {
 2395:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2396:                        '<input type="checkbox" name="'.
 2397:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2398:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2399:         } else {
 2400:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2401:         }
 2402:     }
 2403:     return $output;
 2404: }
 2405: 
 2406: sub print_quotas {
 2407:     my ($dom,$settings,$rowtotal,$action) = @_;
 2408:     my $context;
 2409:     if ($action eq 'quotas') {
 2410:         $context = 'tools';
 2411:     } else {
 2412:         $context = $action;
 2413:     }
 2414:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2415:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2416:     my $typecount = 0;
 2417:     my ($css_class,%titles);
 2418:     if ($context eq 'requestcourses') {
 2419:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2420:         @options =('norequest','approval','validate','autolimit');
 2421:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2422:         %titles = &courserequest_titles();
 2423:     } elsif ($context eq 'requestauthor') {
 2424:         @usertools = ('author');
 2425:         @options = ('norequest','approval','automatic');
 2426:         %titles = &authorrequest_titles();
 2427:     } else {
 2428:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
 2429:         %titles = &tool_titles();
 2430:     }
 2431:     if (ref($types) eq 'ARRAY') {
 2432:         foreach my $type (@{$types}) {
 2433:             my $currdefquota;
 2434:             unless (($context eq 'requestcourses') ||
 2435:                     ($context eq 'requestauthor')) {
 2436:                 if (ref($settings) eq 'HASH') {
 2437:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2438:                         $currdefquota = $settings->{defaultquota}->{$type};
 2439:                     } else {
 2440:                         $currdefquota = $settings->{$type};
 2441:                     }
 2442:                 }
 2443:             }
 2444:             if (defined($usertypes->{$type})) {
 2445:                 $typecount ++;
 2446:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2447:                 $datatable .= '<tr'.$css_class.'>'.
 2448:                               '<td>'.$usertypes->{$type}.'</td>'.
 2449:                               '<td class="LC_left_item">';
 2450:                 if ($context eq 'requestcourses') {
 2451:                     $datatable .= '<table><tr>';
 2452:                 }
 2453:                 my %cell;  
 2454:                 foreach my $item (@usertools) {
 2455:                     if ($context eq 'requestcourses') {
 2456:                         my ($curroption,$currlimit);
 2457:                         if (ref($settings) eq 'HASH') {
 2458:                             if (ref($settings->{$item}) eq 'HASH') {
 2459:                                 $curroption = $settings->{$item}->{$type};
 2460:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2461:                                     $currlimit = $1; 
 2462:                                 }
 2463:                             }
 2464:                         }
 2465:                         if (!$curroption) {
 2466:                             $curroption = 'norequest';
 2467:                         }
 2468:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2469:                         foreach my $option (@options) {
 2470:                             my $val = $option;
 2471:                             if ($option eq 'norequest') {
 2472:                                 $val = 0;  
 2473:                             }
 2474:                             if ($option eq 'validate') {
 2475:                                 my $canvalidate = 0;
 2476:                                 if (ref($validations{$item}) eq 'HASH') { 
 2477:                                     if ($validations{$item}{$type}) {
 2478:                                         $canvalidate = 1;
 2479:                                     }
 2480:                                 }
 2481:                                 next if (!$canvalidate);
 2482:                             }
 2483:                             my $checked = '';
 2484:                             if ($option eq $curroption) {
 2485:                                 $checked = ' checked="checked"';
 2486:                             } elsif ($option eq 'autolimit') {
 2487:                                 if ($curroption =~ /^autolimit/) {
 2488:                                     $checked = ' checked="checked"';
 2489:                                 }                       
 2490:                             } 
 2491:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2492:                                   '<input type="radio" name="crsreq_'.$item.
 2493:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2494:                                   $titles{$option}.'</label>';
 2495:                             if ($option eq 'autolimit') {
 2496:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2497:                                                 $item.'_limit_'.$type.'" size="1" '.
 2498:                                                 'value="'.$currlimit.'" />';
 2499:                             }
 2500:                             $cell{$item} .= '</span> ';
 2501:                             if ($option eq 'autolimit') {
 2502:                                 $cell{$item} .= $titles{'unlimited'};
 2503:                             }
 2504:                         }
 2505:                     } elsif ($context eq 'requestauthor') {
 2506:                         my $curroption;
 2507:                         if (ref($settings) eq 'HASH') {
 2508:                             $curroption = $settings->{$type};
 2509:                         }
 2510:                         if (!$curroption) {
 2511:                             $curroption = 'norequest';
 2512:                         }
 2513:                         foreach my $option (@options) {
 2514:                             my $val = $option;
 2515:                             if ($option eq 'norequest') {
 2516:                                 $val = 0;
 2517:                             }
 2518:                             my $checked = '';
 2519:                             if ($option eq $curroption) {
 2520:                                 $checked = ' checked="checked"';
 2521:                             }
 2522:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2523:                                   '<input type="radio" name="authorreq_'.$type.
 2524:                                   '" value="'.$val.'"'.$checked.' />'.
 2525:                                   $titles{$option}.'</label></span>&nbsp; ';
 2526:                         }
 2527:                     } else {
 2528:                         my $checked = 'checked="checked" ';
 2529:                         if ($item eq 'timezone') {
 2530:                             $checked = '';
 2531:                         }
 2532:                         if (ref($settings) eq 'HASH') {
 2533:                             if (ref($settings->{$item}) eq 'HASH') {
 2534:                                 if (!$settings->{$item}->{$type}) {
 2535:                                     $checked = '';
 2536:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2537:                                     $checked =  'checked="checked" ';
 2538:                                 }
 2539:                             }
 2540:                         }
 2541:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2542:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2543:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2544:                                       '</label></span>&nbsp; ';
 2545:                     }
 2546:                 }
 2547:                 if ($context eq 'requestcourses') {
 2548:                     $datatable .= '</tr><tr>';
 2549:                     foreach my $item (@usertools) {
 2550:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2551:                     }
 2552:                     $datatable .= '</tr></table>';
 2553:                 }
 2554:                 $datatable .= '</td>';
 2555:                 unless (($context eq 'requestcourses') ||
 2556:                         ($context eq 'requestauthor')) {
 2557:                     $datatable .= 
 2558:                               '<td class="LC_right_item">'.
 2559:                               '<span class="LC_nobreak">'.
 2560:                               '<input type="text" name="quota_'.$type.
 2561:                               '" value="'.$currdefquota.
 2562:                               '" size="5" /></span></td>';
 2563:                 }
 2564:                 $datatable .= '</tr>';
 2565:             }
 2566:         }
 2567:     }
 2568:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2569:         $defaultquota = '20';
 2570:         if (ref($settings) eq 'HASH') {
 2571:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2572:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2573:             } elsif (defined($settings->{'default'})) {
 2574:                 $defaultquota = $settings->{'default'};
 2575:             }
 2576:         }
 2577:     }
 2578:     $typecount ++;
 2579:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2580:     $datatable .= '<tr'.$css_class.'>'.
 2581:                   '<td>'.$othertitle.'</td>'.
 2582:                   '<td class="LC_left_item">';
 2583:     if ($context eq 'requestcourses') {
 2584:         $datatable .= '<table><tr>';
 2585:     }
 2586:     my %defcell;
 2587:     foreach my $item (@usertools) {
 2588:         if ($context eq 'requestcourses') {
 2589:             my ($curroption,$currlimit);
 2590:             if (ref($settings) eq 'HASH') {
 2591:                 if (ref($settings->{$item}) eq 'HASH') {
 2592:                     $curroption = $settings->{$item}->{'default'};
 2593:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2594:                         $currlimit = $1;
 2595:                     }
 2596:                 }
 2597:             }
 2598:             if (!$curroption) {
 2599:                 $curroption = 'norequest';
 2600:             }
 2601:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2602:             foreach my $option (@options) {
 2603:                 my $val = $option;
 2604:                 if ($option eq 'norequest') {
 2605:                     $val = 0;
 2606:                 }
 2607:                 if ($option eq 'validate') {
 2608:                     my $canvalidate = 0;
 2609:                     if (ref($validations{$item}) eq 'HASH') {
 2610:                         if ($validations{$item}{'default'}) {
 2611:                             $canvalidate = 1;
 2612:                         }
 2613:                     }
 2614:                     next if (!$canvalidate);
 2615:                 }
 2616:                 my $checked = '';
 2617:                 if ($option eq $curroption) {
 2618:                     $checked = ' checked="checked"';
 2619:                 } elsif ($option eq 'autolimit') {
 2620:                     if ($curroption =~ /^autolimit/) {
 2621:                         $checked = ' checked="checked"';
 2622:                     }
 2623:                 }
 2624:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2625:                                   '<input type="radio" name="crsreq_'.$item.
 2626:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2627:                                   $titles{$option}.'</label>';
 2628:                 if ($option eq 'autolimit') {
 2629:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2630:                                        $item.'_limit_default" size="1" '.
 2631:                                        'value="'.$currlimit.'" />';
 2632:                 }
 2633:                 $defcell{$item} .= '</span> ';
 2634:                 if ($option eq 'autolimit') {
 2635:                     $defcell{$item} .= $titles{'unlimited'};
 2636:                 }
 2637:             }
 2638:         } elsif ($context eq 'requestauthor') {
 2639:             my $curroption;
 2640:             if (ref($settings) eq 'HASH') {
 2641:                 $curroption = $settings->{'default'};
 2642:             }
 2643:             if (!$curroption) {
 2644:                 $curroption = 'norequest';
 2645:             }
 2646:             foreach my $option (@options) {
 2647:                 my $val = $option;
 2648:                 if ($option eq 'norequest') {
 2649:                     $val = 0;
 2650:                 }
 2651:                 my $checked = '';
 2652:                 if ($option eq $curroption) {
 2653:                     $checked = ' checked="checked"';
 2654:                 }
 2655:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2656:                               '<input type="radio" name="authorreq_default"'.
 2657:                               ' value="'.$val.'"'.$checked.' />'.
 2658:                               $titles{$option}.'</label></span>&nbsp; ';
 2659:             }
 2660:         } else {
 2661:             my $checked = 'checked="checked" ';
 2662:             if (ref($settings) eq 'HASH') {
 2663:                 if (ref($settings->{$item}) eq 'HASH') {
 2664:                     if ($settings->{$item}->{'default'} == 0) {
 2665:                         $checked = '';
 2666:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2667:                         $checked = 'checked="checked" ';
 2668:                     }
 2669:                 }
 2670:             }
 2671:             $datatable .= '<span class="LC_nobreak"><label>'.
 2672:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2673:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2674:                           '</label></span>&nbsp; ';
 2675:         }
 2676:     }
 2677:     if ($context eq 'requestcourses') {
 2678:         $datatable .= '</tr><tr>';
 2679:         foreach my $item (@usertools) {
 2680:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2681:         }
 2682:         $datatable .= '</tr></table>';
 2683:     }
 2684:     $datatable .= '</td>';
 2685:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2686:         $datatable .= '<td class="LC_right_item">'.
 2687:                       '<span class="LC_nobreak">'.
 2688:                       '<input type="text" name="defaultquota" value="'.
 2689:                       $defaultquota.'" size="5" /></span></td>';
 2690:     }
 2691:     $datatable .= '</tr>';
 2692:     $typecount ++;
 2693:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2694:     $datatable .= '<tr'.$css_class.'>'.
 2695:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2696:     if ($context eq 'requestcourses') {
 2697:         $datatable .= &mt('(overrides affiliation, if set)').
 2698:                       '</td>'.
 2699:                       '<td class="LC_left_item">'.
 2700:                       '<table><tr>';
 2701:     } else {
 2702:         $datatable .= &mt('(overrides affiliation, if checked)').
 2703:                       '</td>'.
 2704:                       '<td class="LC_left_item" colspan="2">'.
 2705:                       '<br />';
 2706:     }
 2707:     my %advcell;
 2708:     foreach my $item (@usertools) {
 2709:         if ($context eq 'requestcourses') {
 2710:             my ($curroption,$currlimit);
 2711:             if (ref($settings) eq 'HASH') {
 2712:                 if (ref($settings->{$item}) eq 'HASH') {
 2713:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2714:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2715:                         $currlimit = $1;
 2716:                     }
 2717:                 }
 2718:             }
 2719:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2720:             my $checked = '';
 2721:             if ($curroption eq '') {
 2722:                 $checked = ' checked="checked"';
 2723:             }
 2724:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2725:                                '<input type="radio" name="crsreq_'.$item.
 2726:                                '__LC_adv" value=""'.$checked.' />'.
 2727:                                &mt('No override set').'</label></span>&nbsp; ';
 2728:             foreach my $option (@options) {
 2729:                 my $val = $option;
 2730:                 if ($option eq 'norequest') {
 2731:                     $val = 0;
 2732:                 }
 2733:                 if ($option eq 'validate') {
 2734:                     my $canvalidate = 0;
 2735:                     if (ref($validations{$item}) eq 'HASH') {
 2736:                         if ($validations{$item}{'_LC_adv'}) {
 2737:                             $canvalidate = 1;
 2738:                         }
 2739:                     }
 2740:                     next if (!$canvalidate);
 2741:                 }
 2742:                 my $checked = '';
 2743:                 if ($val eq $curroption) {
 2744:                     $checked = ' checked="checked"';
 2745:                 } elsif ($option eq 'autolimit') {
 2746:                     if ($curroption =~ /^autolimit/) {
 2747:                         $checked = ' checked="checked"';
 2748:                     }
 2749:                 }
 2750:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2751:                                   '<input type="radio" name="crsreq_'.$item.
 2752:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2753:                                   $titles{$option}.'</label>';
 2754:                 if ($option eq 'autolimit') {
 2755:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2756:                                        $item.'_limit__LC_adv" size="1" '.
 2757:                                        'value="'.$currlimit.'" />';
 2758:                 }
 2759:                 $advcell{$item} .= '</span> ';
 2760:                 if ($option eq 'autolimit') {
 2761:                     $advcell{$item} .= $titles{'unlimited'};
 2762:                 }
 2763:             }
 2764:         } elsif ($context eq 'requestauthor') {
 2765:             my $curroption;
 2766:             if (ref($settings) eq 'HASH') {
 2767:                 $curroption = $settings->{'_LC_adv'};
 2768:             }
 2769:             my $checked = '';
 2770:             if ($curroption eq '') {
 2771:                 $checked = ' checked="checked"';
 2772:             }
 2773:             $datatable .= '<span class="LC_nobreak"><label>'.
 2774:                           '<input type="radio" name="authorreq__LC_adv"'.
 2775:                           ' value=""'.$checked.' />'.
 2776:                           &mt('No override set').'</label></span>&nbsp; ';
 2777:             foreach my $option (@options) {
 2778:                 my $val = $option;
 2779:                 if ($option eq 'norequest') {
 2780:                     $val = 0;
 2781:                 }
 2782:                 my $checked = '';
 2783:                 if ($val eq $curroption) {
 2784:                     $checked = ' checked="checked"';
 2785:                 }
 2786:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2787:                               '<input type="radio" name="authorreq__LC_adv"'.
 2788:                               ' value="'.$val.'"'.$checked.' />'.
 2789:                               $titles{$option}.'</label></span>&nbsp; ';
 2790:             }
 2791:         } else {
 2792:             my $checked = 'checked="checked" ';
 2793:             if (ref($settings) eq 'HASH') {
 2794:                 if (ref($settings->{$item}) eq 'HASH') {
 2795:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2796:                         $checked = '';
 2797:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2798:                         $checked = 'checked="checked" ';
 2799:                     }
 2800:                 }
 2801:             }
 2802:             $datatable .= '<span class="LC_nobreak"><label>'.
 2803:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2804:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2805:                           '</label></span>&nbsp; ';
 2806:         }
 2807:     }
 2808:     if ($context eq 'requestcourses') {
 2809:         $datatable .= '</tr><tr>';
 2810:         foreach my $item (@usertools) {
 2811:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2812:         }
 2813:         $datatable .= '</tr></table>';
 2814:     }
 2815:     $datatable .= '</td></tr>';
 2816:     $$rowtotal += $typecount;
 2817:     return $datatable;
 2818: }
 2819: 
 2820: sub print_requestmail {
 2821:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2822:     my ($now,$datatable,%currapp);
 2823:     $now = time;
 2824:     if (ref($settings) eq 'HASH') {
 2825:         if (ref($settings->{'notify'}) eq 'HASH') {
 2826:             if ($settings->{'notify'}{'approval'} ne '') {
 2827:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2828:             }
 2829:         }
 2830:     }
 2831:     my $numinrow = 2;
 2832:     my $css_class;
 2833:     if ($$rowtotal%2) {
 2834:         $css_class = 'LC_odd_row';
 2835:     }
 2836:     if ($customcss) {
 2837:         $css_class .= " $customcss";
 2838:     }
 2839:     $css_class =~ s/^\s+//;
 2840:     if ($css_class) {
 2841:         $css_class = ' class="'.$css_class.'"';
 2842:     }
 2843:     if ($rowstyle) {
 2844:         $css_class .= ' style="'.$rowstyle.'"';
 2845:     }
 2846:     my $text;
 2847:     if ($action eq 'requestcourses') {
 2848:         $text = &mt('Receive notification of course requests requiring approval');
 2849:     } elsif ($action eq 'requestauthor') {
 2850:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2851:     } else {
 2852:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2853:     }
 2854:     $datatable = '<tr'.$css_class.'>'.
 2855:                  ' <td>'.$text.'</td>'.
 2856:                  ' <td class="LC_left_item">';
 2857:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2858:                                                  $action.'notifyapproval',%currapp);
 2859:     if ($numdc > 0) {
 2860:         $datatable .= $table;
 2861:     } else {
 2862:         $datatable .= &mt('There are no active Domain Coordinators');
 2863:     }
 2864:     $datatable .='</td></tr>';
 2865:     return $datatable;
 2866: }
 2867: 
 2868: sub print_studentcode {
 2869:     my ($settings,$rowtotal) = @_;
 2870:     my $rownum = 0; 
 2871:     my ($output,%current);
 2872:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2873:     if (ref($settings) eq 'HASH') {
 2874:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2875:             foreach my $type (@crstypes) {
 2876:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2877:             }
 2878:         }
 2879:     }
 2880:     $output .= '<tr>'.
 2881:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2882:                '<td class="LC_left_item">';
 2883:     foreach my $type (@crstypes) {
 2884:         my $check = ' ';
 2885:         if ($current{$type}) {
 2886:             $check = ' checked="checked" ';
 2887:         }
 2888:         $output .= '<span class="LC_nobreak"><label>'.
 2889:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2890:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2891:     }
 2892:     $output .= '</td></tr>';
 2893:     $$rowtotal ++;
 2894:     return $output;
 2895: }
 2896: 
 2897: sub print_textbookcourses {
 2898:     my ($dom,$type,$settings,$rowtotal) = @_;
 2899:     my $rownum = 0;
 2900:     my $css_class;
 2901:     my $itemcount = 1;
 2902:     my $maxnum = 0;
 2903:     my $bookshash;
 2904:     if (ref($settings) eq 'HASH') {
 2905:         $bookshash = $settings->{$type};
 2906:     }
 2907:     my %ordered;
 2908:     if (ref($bookshash) eq 'HASH') {
 2909:         foreach my $item (keys(%{$bookshash})) {
 2910:             if (ref($bookshash->{$item}) eq 'HASH') {
 2911:                 my $num = $bookshash->{$item}{'order'};
 2912:                 $ordered{$num} = $item;
 2913:             }
 2914:         }
 2915:     }
 2916:     my $confname = $dom.'-domainconfig';
 2917:     my $switchserver = &check_switchserver($dom,$confname);
 2918:     my $maxnum = scalar(keys(%ordered));
 2919:     my $datatable;
 2920:     if (keys(%ordered)) {
 2921:         my @items = sort { $a <=> $b } keys(%ordered);
 2922:         for (my $i=0; $i<@items; $i++) {
 2923:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2924:             my $key = $ordered{$items[$i]};
 2925:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2926:             my $coursetitle = $coursehash{'description'};
 2927:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2928:             if (ref($bookshash->{$key}) eq 'HASH') {
 2929:                 $subject = $bookshash->{$key}->{'subject'};
 2930:                 $title = $bookshash->{$key}->{'title'};
 2931:                 if ($type eq 'textbooks') {
 2932:                     $publisher = $bookshash->{$key}->{'publisher'};
 2933:                     $author = $bookshash->{$key}->{'author'};
 2934:                     $image = $bookshash->{$key}->{'image'};
 2935:                     if ($image ne '') {
 2936:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2937:                         my $imagethumb = "$path/tn-".$imagefile;
 2938:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2939:                     }
 2940:                 }
 2941:             }
 2942:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2943:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2944:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2945:             for (my $k=0; $k<=$maxnum; $k++) {
 2946:                 my $vpos = $k+1;
 2947:                 my $selstr;
 2948:                 if ($k == $i) {
 2949:                     $selstr = ' selected="selected" ';
 2950:                 }
 2951:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2952:             }
 2953:             $datatable .= '</select>'.('&nbsp;'x2).
 2954:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2955:                 &mt('Delete?').'</label></span></td>'.
 2956:                 '<td colspan="2">'.
 2957:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2958:                 ('&nbsp;'x2).
 2959:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2960:             if ($type eq 'textbooks') {
 2961:                 $datatable .= ('&nbsp;'x2).
 2962:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2963:                               ('&nbsp;'x2).
 2964:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2965:                               ('&nbsp;'x2).
 2966:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2967:                 if ($image) {
 2968:                     $datatable .= $imgsrc.
 2969:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2970:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2971:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2972:                 }
 2973:                 if ($switchserver) {
 2974:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2975:                 } else {
 2976:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2977:                 }
 2978:             }
 2979:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2980:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2981:                           $coursetitle.'</span></td></tr>'."\n";
 2982:             $itemcount ++;
 2983:         }
 2984:     }
 2985:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2986:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2987:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2988:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2989:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2990:     for (my $k=0; $k<$maxnum+1; $k++) {
 2991:         my $vpos = $k+1;
 2992:         my $selstr;
 2993:         if ($k == $maxnum) {
 2994:             $selstr = ' selected="selected" ';
 2995:         }
 2996:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2997:     }
 2998:     $datatable .= '</select>&nbsp;'."\n".
 2999:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3000:                   '<td colspan="2">'.
 3001:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3002:                   ('&nbsp;'x2).
 3003:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3004:                   ('&nbsp;'x2);
 3005:     if ($type eq 'textbooks') {
 3006:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3007:                       ('&nbsp;'x2).
 3008:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3009:                       ('&nbsp;'x2).
 3010:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3011:         if ($switchserver) {
 3012:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3013:         } else {
 3014:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3015:         }
 3016:         $datatable .= '</span>'."\n";
 3017:     }
 3018:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3019:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3020:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3021:                   &Apache::loncommon::selectcourse_link
 3022:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3023:                   '</span></td>'."\n".
 3024:                   '</tr>'."\n";
 3025:     $itemcount ++;
 3026:     return $datatable;
 3027: }
 3028: 
 3029: sub textbookcourses_javascript {
 3030:     my ($settings) = @_;
 3031:     return unless(ref($settings) eq 'HASH');
 3032:     my (%ordered,%total,%jstext);
 3033:     foreach my $type ('textbooks','templates') {
 3034:         $total{$type} = 0;
 3035:         if (ref($settings->{$type}) eq 'HASH') {
 3036:             foreach my $item (keys(%{$settings->{$type}})) {
 3037:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3038:                     my $num = $settings->{$type}->{$item}{'order'};
 3039:                     $ordered{$type}{$num} = $item;
 3040:                 }
 3041:             }
 3042:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3043:         }
 3044:         my @jsarray = ();
 3045:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3046:             push(@jsarray,$ordered{$type}{$item});
 3047:         }
 3048:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3049:     }
 3050:     return <<"ENDSCRIPT";
 3051: <script type="text/javascript">
 3052: // <![CDATA[
 3053: function reorderBooks(form,item,caller) {
 3054:     var changedVal;
 3055: $jstext{'textbooks'};
 3056: $jstext{'templates'};
 3057:     var newpos;
 3058:     var maxh;
 3059:     if (caller == 'textbooks') {  
 3060:         newpos = 'textbooks_addbook_pos';
 3061:         maxh = 1 + $total{'textbooks'};
 3062:     } else {
 3063:         newpos = 'templates_addbook_pos';
 3064:         maxh = 1 + $total{'templates'};
 3065:     }
 3066:     var current = new Array;
 3067:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3068:     if (item == newpos) {
 3069:         changedVal = newitemVal;
 3070:     } else {
 3071:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3072:         current[newitemVal] = newpos;
 3073:     }
 3074:     if (caller == 'textbooks') {
 3075:         for (var i=0; i<textbooks.length; i++) {
 3076:             var elementName = 'textbooks_'+textbooks[i];
 3077:             if (elementName != item) {
 3078:                 if (form.elements[elementName]) {
 3079:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3080:                     current[currVal] = elementName;
 3081:                 }
 3082:             }
 3083:         }
 3084:     }
 3085:     if (caller == 'templates') {
 3086:         for (var i=0; i<templates.length; i++) {
 3087:             var elementName = 'templates_'+templates[i];
 3088:             if (elementName != item) {
 3089:                 if (form.elements[elementName]) {
 3090:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3091:                     current[currVal] = elementName;
 3092:                 }
 3093:             }
 3094:         }
 3095:     }
 3096:     var oldVal;
 3097:     for (var j=0; j<maxh; j++) {
 3098:         if (current[j] == undefined) {
 3099:             oldVal = j;
 3100:         }
 3101:     }
 3102:     if (oldVal < changedVal) {
 3103:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3104:            var elementName = current[k];
 3105:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3106:         }
 3107:     } else {
 3108:         for (var k=changedVal; k<oldVal; k++) {
 3109:             var elementName = current[k];
 3110:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3111:         }
 3112:     }
 3113:     return;
 3114: }
 3115: 
 3116: // ]]>
 3117: </script>
 3118: 
 3119: ENDSCRIPT
 3120: }
 3121: 
 3122: sub ltitools_javascript {
 3123:     my ($settings) = @_;
 3124:     my $togglejs = &ltitools_toggle_js();
 3125:     unless (ref($settings) eq 'HASH') {
 3126:         return $togglejs;
 3127:     }
 3128:     my (%ordered,$total,%jstext);
 3129:     $total = 0;
 3130:     foreach my $item (keys(%{$settings})) {
 3131:         if (ref($settings->{$item}) eq 'HASH') {
 3132:             my $num = $settings->{$item}{'order'};
 3133:             $ordered{$num} = $item;
 3134:         }
 3135:     }
 3136:     $total = scalar(keys(%{$settings}));
 3137:     my @jsarray = ();
 3138:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3139:         push(@jsarray,$ordered{$item});
 3140:     }
 3141:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3142:     return <<"ENDSCRIPT";
 3143: <script type="text/javascript">
 3144: // <![CDATA[
 3145: function reorderLTITools(form,item) {
 3146:     var changedVal;
 3147: $jstext
 3148:     var newpos = 'ltitools_add_pos';
 3149:     var maxh = 1 + $total;
 3150:     var current = new Array;
 3151:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3152:     if (item == newpos) {
 3153:         changedVal = newitemVal;
 3154:     } else {
 3155:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3156:         current[newitemVal] = newpos;
 3157:     }
 3158:     for (var i=0; i<ltitools.length; i++) {
 3159:         var elementName = 'ltitools_'+ltitools[i];
 3160:         if (elementName != item) {
 3161:             if (form.elements[elementName]) {
 3162:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3163:                 current[currVal] = elementName;
 3164:             }
 3165:         }
 3166:     }
 3167:     var oldVal;
 3168:     for (var j=0; j<maxh; j++) {
 3169:         if (current[j] == undefined) {
 3170:             oldVal = j;
 3171:         }
 3172:     }
 3173:     if (oldVal < changedVal) {
 3174:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3175:            var elementName = current[k];
 3176:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3177:         }
 3178:     } else {
 3179:         for (var k=changedVal; k<oldVal; k++) {
 3180:             var elementName = current[k];
 3181:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3182:         }
 3183:     }
 3184:     return;
 3185: }
 3186: 
 3187: // ]]>
 3188: </script>
 3189: 
 3190: $togglejs
 3191: 
 3192: ENDSCRIPT
 3193: }
 3194: 
 3195: sub ltitools_toggle_js {
 3196:     return <<"ENDSCRIPT";
 3197: <script type="text/javascript">
 3198: // <![CDATA[
 3199: 
 3200: function toggleLTITools(form,setting,item) {
 3201:     var radioname = '';
 3202:     var divid = '';
 3203:     if ((setting == 'passback') || (setting == 'roster')) {
 3204:         radioname = 'ltitools_'+setting+'_'+item;
 3205:         divid = 'ltitools_'+setting+'time_'+item;
 3206:         var num = form.elements[radioname].length;
 3207:         if (num) {
 3208:             var setvis = '';
 3209:             for (var i=0; i<num; i++) {
 3210:                 if (form.elements[radioname][i].checked) {
 3211:                     if (form.elements[radioname][i].value == '1') {
 3212:                         if (document.getElementById(divid)) {
 3213:                             document.getElementById(divid).style.display = 'inline-block';
 3214:                         }
 3215:                         setvis = 1;
 3216:                     }
 3217:                     break;
 3218:                 }
 3219:             }
 3220:         }
 3221:         if (!setvis) {
 3222:             if (document.getElementById(divid)) {
 3223:                 document.getElementById(divid).style.display = 'none';
 3224:             }
 3225:         }
 3226:     }
 3227:     if (setting == 'user') {
 3228:         divid = 'ltitools_'+setting+'_div_'+item;
 3229:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3230:         if (document.getElementById(divid)) {
 3231:             if (document.getElementById(checkid)) {
 3232:                 if (document.getElementById(checkid).checked) {
 3233:                     document.getElementById(divid).style.display = 'inline-block';
 3234:                 } else {
 3235:                     document.getElementById(divid).style.display = 'none';
 3236:                 }
 3237:             }
 3238:         }
 3239:     }
 3240:     return;
 3241: }
 3242: // ]]>
 3243: </script>
 3244: 
 3245: ENDSCRIPT
 3246: }
 3247: 
 3248: sub wafproxy_javascript {
 3249:     my ($dom) = @_;
 3250:     return <<"ENDSCRIPT";
 3251: <script type="text/javascript">
 3252: // <![CDATA[
 3253: function updateWAF() {
 3254:     if (document.getElementById('wafproxy_remoteip')) {
 3255:         var wafremote = 0;
 3256:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3257:             wafremote = 1;
 3258:         }
 3259:         var fields = new Array('header','trust');
 3260:         for (var i=0; i<fields.length; i++) {
 3261:             if (document.getElementById('wafproxy_'+fields[i])) {
 3262:                 if (wafremote == 1) {
 3263:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3264:                 }
 3265:                 else {
 3266:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3267:                 }
 3268:             }
 3269:         }
 3270:         if (document.getElementById('wafproxyranges_$dom')) {
 3271:             if (wafremote == 1) {
 3272:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3273:             } else {
 3274:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3275:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3276:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3277:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3278:                         }
 3279:                     }
 3280:                 }
 3281:             }
 3282:         }
 3283:     }
 3284:     return;
 3285: }
 3286: 
 3287: function checkWAF() {
 3288:     if (document.getElementById('wafproxy_remoteip')) {
 3289:         var wafvpn = 0;
 3290:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3291:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3292:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3293:                     wafvpn = 1;
 3294:                 }
 3295:                 break;
 3296:             }
 3297:         }
 3298:         var vpn = new Array('vpnint','vpnext');
 3299:         for (var i=0; i<vpn.length; i++) {
 3300:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3301:                 if (wafvpn == 1) {
 3302:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3303:                 }
 3304:                 else {
 3305:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3306:                 }
 3307:             }
 3308:         }
 3309:         if (document.getElementById('wafproxyranges_$dom')) {
 3310:             if (wafvpn == 1) {
 3311:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3312:             }
 3313:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3314:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3315:             }
 3316:         }
 3317:     }
 3318:     return;
 3319: }
 3320: 
 3321: function toggleWAF() {
 3322:     if (document.getElementById('wafproxy_table')) {
 3323:         var wafproxy = 0;
 3324:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3325:              if (document.display.wafproxy_${dom}[i].checked) {
 3326:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3327:                      wafproxy = 1;
 3328:                      break;
 3329:                 }
 3330:             }
 3331:         }
 3332:         if (wafproxy == 1) {
 3333:             document.getElementById('wafproxy_table').style.display='inline';
 3334:         }
 3335:         else {
 3336:            document.getElementById('wafproxy_table').style.display='none';
 3337:         }
 3338:         if (document.getElementById('wafproxyrow_${dom}')) {
 3339:             if (wafproxy == 1) {
 3340:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3341:             }
 3342:             else {
 3343:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3344:             }
 3345:         }
 3346:         if (document.getElementById('nowafproxyrow_$dom')) {
 3347:             if (wafproxy == 1) {
 3348:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3349:             }
 3350:             else {
 3351:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3352:             }
 3353:         }
 3354:     }
 3355:     return;
 3356: }
 3357: // ]]>
 3358: </script>
 3359: 
 3360: ENDSCRIPT
 3361: }
 3362: 
 3363: sub proctoring_javascript {
 3364:     my ($settings) = @_;
 3365:     my (%ordered,$total,%jstext);
 3366:     $total = 0;
 3367:     if (ref($settings) eq 'HASH') {
 3368:         foreach my $item (keys(%{$settings})) {
 3369:             if (ref($settings->{$item}) eq 'HASH') {
 3370:                 my $num = $settings->{$item}{'order'};
 3371:                 $ordered{$num} = $item;
 3372:             }
 3373:         }
 3374:         $total = scalar(keys(%{$settings}));
 3375:     } else {
 3376:         %ordered = (
 3377:                        0 => 'proctorio',
 3378:                        1 => 'examity',
 3379:                    );
 3380:         $total = 2; 
 3381:     }
 3382:     my @jsarray = ();
 3383:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3384:         push(@jsarray,$ordered{$item});
 3385:     }
 3386:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3387:     return <<"ENDSCRIPT";
 3388: <script type="text/javascript">
 3389: // <![CDATA[
 3390: function reorderProctoring(form,item) {
 3391:     var changedVal;
 3392: $jstext
 3393:     var maxh = $total;
 3394:     var current = new Array;
 3395:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3396:     for (var i=0; i<proctors.length; i++) {
 3397:         var elementName = 'proctoring_pos_'+proctors[i];
 3398:         if (elementName != item) {
 3399:             if (form.elements[elementName]) {
 3400:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3401:                 current[currVal] = elementName;
 3402:             }
 3403:         }
 3404:     }
 3405:     var oldVal;
 3406:     for (var j=0; j<maxh; j++) {
 3407:         if (current[j] == undefined) {
 3408:             oldVal = j;
 3409:         }
 3410:     }
 3411:     if (oldVal < changedVal) {
 3412:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3413:            var elementName = current[k];
 3414:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3415:         }
 3416:     } else {
 3417:         for (var k=changedVal; k<oldVal; k++) {
 3418:             var elementName = current[k];
 3419:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3420:         }
 3421:     }
 3422:     return;
 3423: }
 3424: 
 3425: function toggleProctoring(form,item) {
 3426:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3427:     if (fieldsets.length) {
 3428:         var radioname = 'proctoring_available_'+item;
 3429:         var num = form.elements[radioname].length;
 3430:         if (num) {
 3431:             var setvis = '';
 3432:             for (var i=0; i<num; i++) {
 3433:                 if (form.elements[radioname][i].checked) {
 3434:                     if (form.elements[radioname][i].value == '1') {
 3435:                        setvis = 1;
 3436:                        break;
 3437:                     }
 3438:                 }
 3439:             }
 3440:             for (var j=0; j<fieldsets.length; j++) {
 3441:                 if (setvis) {
 3442:                     fieldsets[j].style.display = 'block';
 3443:                 } else {
 3444:                     fieldsets[j].style.display = 'none';
 3445:                 }
 3446:             }
 3447:         }
 3448:     }
 3449:     return;
 3450: }
 3451: 
 3452: // ]]>
 3453: </script>
 3454: 
 3455: ENDSCRIPT
 3456: }
 3457: 
 3458: 
 3459: sub lti_javascript {
 3460:     my ($dom,$settings) = @_;
 3461:     my $togglejs = &lti_toggle_js($dom);
 3462:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3463:     unless (ref($settings) eq 'HASH') {
 3464:         return $togglejs.'
 3465: <script type="text/javascript">
 3466: // <![CDATA[
 3467: 
 3468: '.$linkprot_js.'
 3469: 
 3470: // ]]>
 3471: </script>
 3472: ';
 3473:     }
 3474:     my (%ordered,$total,%jstext);
 3475:     $total = scalar(keys(%{$settings}));
 3476:     foreach my $item (keys(%{$settings})) {
 3477:         if (ref($settings->{$item}) eq 'HASH') {
 3478:             my $num = $settings->{$item}{'order'};
 3479:             if ($num eq '') {
 3480:                 $num = $total - 1;
 3481:             }
 3482:             $ordered{$num} = $item;
 3483:         }
 3484:     }
 3485:     my @jsarray = ();
 3486:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3487:         push(@jsarray,$ordered{$item});
 3488:     }
 3489:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3490:     return <<"ENDSCRIPT";
 3491: <script type="text/javascript">
 3492: // <![CDATA[
 3493: function reorderLTI(form,item) {
 3494:     var changedVal;
 3495: $jstext
 3496:     var newpos = 'lti_pos_add';
 3497:     var maxh = 1 + $total;
 3498:     var current = new Array;
 3499:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3500:     if (item == newpos) {
 3501:         changedVal = newitemVal;
 3502:     } else {
 3503:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3504:         current[newitemVal] = newpos;
 3505:     }
 3506:     for (var i=0; i<lti.length; i++) {
 3507:         var elementName = 'lti_pos_'+lti[i];
 3508:         if (elementName != item) {
 3509:             if (form.elements[elementName]) {
 3510:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3511:                 current[currVal] = elementName;
 3512:             }
 3513:         }
 3514:     }
 3515:     var oldVal;
 3516:     for (var j=0; j<maxh; j++) {
 3517:         if (current[j] == undefined) {
 3518:             oldVal = j;
 3519:         }
 3520:     }
 3521:     if (oldVal < changedVal) {
 3522:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3523:            var elementName = current[k];
 3524:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3525:         }
 3526:     } else {
 3527:         for (var k=changedVal; k<oldVal; k++) {
 3528:             var elementName = current[k];
 3529:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3530:         }
 3531:     }
 3532:     return;
 3533: }
 3534: 
 3535: $linkprot_js
 3536: 
 3537: // ]]>
 3538: </script>
 3539: 
 3540: $togglejs
 3541: 
 3542: ENDSCRIPT
 3543: }
 3544: 
 3545: sub lti_toggle_js {
 3546:     my ($dom) = @_;
 3547:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3548:                             localauth => 'Local auth argument',
 3549:                             krb       => 'Kerberos domain',
 3550:                          );
 3551:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3552:     &js_escape(\$crsincalert);
 3553:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3554:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3555:     my $course_servers = "'".join("','",keys(%servers))."'";
 3556:     return <<"ENDSCRIPT";
 3557: <script type="text/javascript">
 3558: // <![CDATA[
 3559: 
 3560: function toggleLTI(form,setting,item) {
 3561:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3562:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3563:         var setvis = '';
 3564:         var radioname = 'lti_requser_'+item;
 3565:         var num = form.elements[radioname].length;
 3566:         if (num) {
 3567:             for (var i=0; i<num; i++) {
 3568:                 if (form.elements[radioname][i].checked) {
 3569:                     if (form.elements[radioname][i].value == '1') {
 3570:                         setvis = 1;
 3571:                         break;
 3572:                     }
 3573:                 }
 3574:             }
 3575:         }
 3576:         if (usrfieldsets.length) {
 3577:             for (var j=0; j<usrfieldsets.length; j++) {
 3578:                 if (setvis) {
 3579:                     usrfieldsets[j].style.display = 'block';
 3580:                 } else {
 3581:                     usrfieldsets[j].style.display = 'none';
 3582:                 }
 3583:             }
 3584:         }
 3585:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3586:         if (crsfieldsets.length) {
 3587:             radioname = 'lti_crsinc_'+item;
 3588:             var num = form.elements[radioname].length;
 3589:             if (num) {
 3590:                 var crsvis = '';
 3591:                 for (var i=0; i<num; i++) {
 3592:                     if (form.elements[radioname][i].checked) {
 3593:                         if (form.elements[radioname][i].value == '1') {
 3594:                             if (setvis == '') {
 3595:                                 if (setting == 'crsinc'){
 3596:                                     alert("$crsincalert");
 3597:                                     form.elements[radioname][0].checked = true;
 3598:                                 }
 3599:                             } else {
 3600:                                 crsvis = 1;
 3601:                             }
 3602:                             break;
 3603:                         }
 3604:                     }
 3605:                 }
 3606:                 setvis = crsvis;
 3607:             }
 3608:             for (var j=0; j<crsfieldsets.length; j++) {
 3609:                 if (setvis) {
 3610:                     crsfieldsets[j].style.display = 'block';
 3611:                 } else {
 3612:                     crsfieldsets[j].style.display = 'none';
 3613:                 }
 3614:             }
 3615:         }
 3616:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3617:         var radioname = '';
 3618:         var divid = '';
 3619:         if (setting == 'user') {
 3620:             radioname = 'lti_mapuser_'+item;
 3621:             divid = 'lti_userfield_'+item;
 3622:         } else if (setting == 'crs') {
 3623:             radioname = 'lti_mapcrs_'+item;
 3624:             divid = 'lti_crsfield_'+item;
 3625:         } else if (setting == 'callback') {
 3626:             radioname = 'lti_callback_'+item;
 3627:             divid = 'lti_callbackfield_'+item;
 3628:         } else {
 3629:             radioname = 'lti_passback_'+item;
 3630:             divid =  'lti_passback_'+item;
 3631:         }
 3632:         var num = form.elements[radioname].length;
 3633:         if (num) {
 3634:             var setvis = '';
 3635:             for (var i=0; i<num; i++) {
 3636:                if (form.elements[radioname][i].checked) {
 3637:                    if ((setting == 'passback') || (setting == 'callback')) {
 3638:                        if (form.elements[radioname][i].value == '1') {
 3639:                            if (document.getElementById(divid)) {
 3640:                                document.getElementById(divid).style.display = 'inline-block';
 3641:                            }
 3642:                            setvis = 1;
 3643:                            break;
 3644:                        }
 3645:                    } else {
 3646:                        if (form.elements[radioname][i].value == 'other') {
 3647:                            if (document.getElementById(divid)) {
 3648:                                document.getElementById(divid).style.display = 'inline-block';
 3649:                            }
 3650:                            setvis = 1;
 3651:                            break;
 3652:                        }
 3653:                    }
 3654:                }
 3655:             }
 3656:             if (!setvis) {
 3657:                 if (document.getElementById(divid)) {
 3658:                     document.getElementById(divid).style.display = 'none';
 3659:                 }
 3660:             }
 3661:         }
 3662:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3663:         var numsec = form.elements['lti_crssec_'+item].length;
 3664:         if (numsec) {
 3665:             var setvis = '';
 3666:             for (var i=0; i<numsec; i++) {
 3667:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3668:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3669:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3670:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3671:                             setvis = 1;
 3672:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3673:                             if (numsrcsec) {
 3674:                                 var setsrcvis = '';
 3675:                                 for (var j=0; j<numsrcsec; j++) {
 3676:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3677:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3678:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3679:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3680:                                                 setsrcvis = 1;
 3681:                                             }
 3682:                                         }
 3683:                                     }
 3684:                                 }
 3685:                                 if (!setsrcvis) {
 3686:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3687:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3688:                                     }
 3689:                                 }
 3690:                             }
 3691:                         }
 3692:                     }
 3693:                 }
 3694:             }
 3695:             if (!setvis) {
 3696:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3697:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3698:                 }
 3699:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3700:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3701:                 }
 3702:             }
 3703:         }
 3704:     } else if (setting == 'lcauth') {
 3705:         var numauth = form.elements['lti_lcauth_'+item].length;
 3706:         if (numauth) {
 3707:             for (var i=0; i<numauth; i++) {
 3708:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3709:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3710:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3711:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3712:                         } else {
 3713:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3714:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3715:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3716:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3717:                                 } else {
 3718:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3719:                                 }
 3720:                             }
 3721:                         }
 3722:                     }
 3723:                 }
 3724:             }
 3725:         }
 3726:     } else if (setting == 'lcmenu') {
 3727:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3728:         var divid = 'lti_menufield_'+item;
 3729:         var setvis = '';
 3730:         for (var i=0; i<menus.length; i++) {
 3731:             var radioname = menus[i];
 3732:             var num = form.elements[radioname].length;
 3733:             if (num) {
 3734:                 for (var j=0; j<num; j++) {
 3735:                     if (form.elements[radioname][j].checked) {
 3736:                         if (form.elements[radioname][j].value == '1') {
 3737:                             if (document.getElementById(divid)) {
 3738:                                 document.getElementById(divid).style.display = 'inline-block';
 3739:                             }
 3740:                             setvis = 1;
 3741:                             break;
 3742:                         }
 3743:                     }
 3744:                 }
 3745:             }
 3746:             if (setvis == 1) {
 3747:                 break;
 3748:             }
 3749:         }
 3750:         if (!setvis) {
 3751:             if (document.getElementById(divid)) {
 3752:                 document.getElementById(divid).style.display = 'none';
 3753:             }
 3754:         }
 3755:     }
 3756:     return;
 3757: }
 3758: 
 3759: // ]]>
 3760: </script>
 3761: 
 3762: ENDSCRIPT
 3763: }
 3764: 
 3765: sub autoupdate_javascript {
 3766:     return <<"ENDSCRIPT";
 3767: <script type="text/javascript">
 3768: // <![CDATA[
 3769: function toggleLastActiveDays(form) {
 3770:     var radioname = 'lastactive';
 3771:     var divid = 'lastactive_div';
 3772:     var num = form.elements[radioname].length;
 3773:     if (num) {
 3774:         var setvis = '';
 3775:         for (var i=0; i<num; i++) {
 3776:             if (form.elements[radioname][i].checked) {
 3777:                 if (form.elements[radioname][i].value == '1') {
 3778:                     if (document.getElementById(divid)) {
 3779:                         document.getElementById(divid).style.display = 'inline-block';
 3780:                     }
 3781:                     setvis = 1;
 3782:                 }
 3783:                 break;
 3784:             }
 3785:         }
 3786:         if (!setvis) {
 3787:             if (document.getElementById(divid)) {
 3788:                 document.getElementById(divid).style.display = 'none';
 3789:             }
 3790:         }
 3791:     }
 3792:     return;
 3793: }
 3794: // ]]>
 3795: </script>
 3796: 
 3797: ENDSCRIPT
 3798: }
 3799: 
 3800: sub autoenroll_javascript {
 3801:     return <<"ENDSCRIPT";
 3802: <script type="text/javascript">
 3803: // <![CDATA[
 3804: function toggleFailsafe(form) {
 3805:     var radioname = 'autoenroll_failsafe';
 3806:     var divid = 'autoenroll_failsafe_div';
 3807:     var num = form.elements[radioname].length;
 3808:     if (num) {
 3809:         var setvis = '';
 3810:         for (var i=0; i<num; i++) {
 3811:             if (form.elements[radioname][i].checked) {
 3812:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3813:                     if (document.getElementById(divid)) {
 3814:                         document.getElementById(divid).style.display = 'inline-block';
 3815:                     }
 3816:                     setvis = 1;
 3817:                 }
 3818:                 break;
 3819:             }
 3820:         }
 3821:         if (!setvis) {
 3822:             if (document.getElementById(divid)) {
 3823:                 document.getElementById(divid).style.display = 'none';
 3824:             }
 3825:         }
 3826:     }
 3827:     return;
 3828: }
 3829: // ]]>
 3830: </script>
 3831: 
 3832: ENDSCRIPT
 3833: }
 3834: 
 3835: sub saml_javascript {
 3836:     return <<"ENDSCRIPT";
 3837: <script type="text/javascript">
 3838: // <![CDATA[
 3839: function toggleSamlOptions(form,hostid) {
 3840:     var radioname = 'saml_'+hostid;
 3841:     var tablecellon = 'samloptionson_'+hostid;
 3842:     var tablecelloff = 'samloptionsoff_'+hostid;
 3843:     var num = form.elements[radioname].length;
 3844:     if (num) {
 3845:         var setvis = '';
 3846:         for (var i=0; i<num; i++) {
 3847:             if (form.elements[radioname][i].checked) {
 3848:                 if (form.elements[radioname][i].value == '1') {
 3849:                     if (document.getElementById(tablecellon)) {
 3850:                         document.getElementById(tablecellon).style.display='';
 3851:                     }
 3852:                     if (document.getElementById(tablecelloff)) {
 3853:                         document.getElementById(tablecelloff).style.display='none';
 3854:                     }
 3855:                     setvis = 1;
 3856:                 }
 3857:                 break;
 3858:             }
 3859:         }
 3860:         if (!setvis) {
 3861:             if (document.getElementById(tablecellon)) {
 3862:                 document.getElementById(tablecellon).style.display='none';
 3863:             }
 3864:             if (document.getElementById(tablecelloff)) {
 3865:                 document.getElementById(tablecelloff).style.display='';
 3866:             }
 3867:         }
 3868:     }
 3869:     return;
 3870: }
 3871: // ]]>
 3872: </script>
 3873: 
 3874: ENDSCRIPT
 3875: }
 3876: 
 3877: sub ipaccess_javascript {
 3878:     my ($settings) = @_;
 3879:     my (%ordered,$total,%jstext);
 3880:     $total = 0;
 3881:     if (ref($settings) eq 'HASH') {
 3882:         foreach my $item (keys(%{$settings})) {
 3883:             if (ref($settings->{$item}) eq 'HASH') {
 3884:                 my $num = $settings->{$item}{'order'};
 3885:                 $ordered{$num} = $item;
 3886:             }
 3887:         }
 3888:         $total = scalar(keys(%{$settings}));
 3889:     }
 3890:     my @jsarray = ();
 3891:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3892:         push(@jsarray,$ordered{$item});
 3893:     }
 3894:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3895:     return <<"ENDSCRIPT";
 3896: <script type="text/javascript">
 3897: // <![CDATA[
 3898: function reorderIPaccess(form,item) {
 3899:     var changedVal;
 3900: $jstext
 3901:     var newpos = 'ipaccess_pos_add';
 3902:     var maxh = 1 + $total;
 3903:     var current = new Array;
 3904:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3905:     if (item == newpos) {
 3906:         changedVal = newitemVal;
 3907:     } else {
 3908:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3909:         current[newitemVal] = newpos;
 3910:     }
 3911:     for (var i=0; i<ipaccess.length; i++) {
 3912:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3913:         if (elementName != item) {
 3914:             if (form.elements[elementName]) {
 3915:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3916:                 current[currVal] = elementName;
 3917:             }
 3918:         }
 3919:     }
 3920:     var oldVal;
 3921:     for (var j=0; j<maxh; j++) {
 3922:         if (current[j] == undefined) {
 3923:             oldVal = j;
 3924:         }
 3925:     }
 3926:     if (oldVal < changedVal) {
 3927:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3928:            var elementName = current[k];
 3929:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3930:         }
 3931:     } else {
 3932:         for (var k=changedVal; k<oldVal; k++) {
 3933:             var elementName = current[k];
 3934:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3935:         }
 3936:     }
 3937:     return;
 3938: }
 3939: // ]]>
 3940: </script>
 3941: 
 3942: ENDSCRIPT
 3943: }
 3944: 
 3945: sub authordefaults_javascript {
 3946:     my %alert = &Apache::lonlocal::texthash (
 3947:                     reqd => 'Warning: at least one editor needs to be available.',
 3948:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3949:     );
 3950:     &js_escape(\%alert);
 3951:     return <<"ENDSCRIPT";
 3952: <script type="text/javascript">
 3953: // <![CDATA[
 3954: 
 3955: function checkEditors(form,checkbox,current) {
 3956:     if (form.elements[checkbox].length != undefined) {
 3957:         var count = 0;
 3958:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3959:             if (form.elements[checkbox][i].checked) {
 3960:                 count ++;
 3961:             }
 3962:         }
 3963:         if (count == 0) {
 3964:             if (current.type =='radio') {
 3965:                 current.checked = true;
 3966:                 alert('$alert{reqd}\\n$alert{rest}');
 3967:             }
 3968:         }
 3969:     }
 3970:     return;
 3971: }
 3972: // ]]>
 3973: </script>
 3974: 
 3975: ENDSCRIPT
 3976: }
 3977: 
 3978: sub print_autoenroll {
 3979:     my ($dom,$settings,$rowtotal) = @_;
 3980:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3981:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3982:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3983:     $failsafesty = 'none';
 3984:     %failsafechecked = (
 3985:         off => ' checked="checked"',
 3986:     );
 3987:     if (ref($settings) eq 'HASH') {
 3988:         if (exists($settings->{'run'})) {
 3989:             if ($settings->{'run'} eq '0') {
 3990:                 $runoff = ' checked="checked" ';
 3991:                 $runon = ' ';
 3992:             } else {
 3993:                 $runon = ' checked="checked" ';
 3994:                 $runoff = ' ';
 3995:             }
 3996:         } else {
 3997:             if ($autorun) {
 3998:                 $runon = ' checked="checked" ';
 3999:                 $runoff = ' ';
 4000:             } else {
 4001:                 $runoff = ' checked="checked" ';
 4002:                 $runon = ' ';
 4003:             }
 4004:         }
 4005:         if (exists($settings->{'co-owners'})) {
 4006:             if ($settings->{'co-owners'} eq '0') {
 4007:                 $coownersoff = ' checked="checked" ';
 4008:                 $coownerson = ' ';
 4009:             } else {
 4010:                 $coownerson = ' checked="checked" ';
 4011:                 $coownersoff = ' ';
 4012:             }
 4013:         } else {
 4014:             $coownersoff = ' checked="checked" ';
 4015:             $coownerson = ' ';
 4016:         }
 4017:         if (exists($settings->{'sender_domain'})) {
 4018:             $defdom = $settings->{'sender_domain'};
 4019:         }
 4020:         if (exists($settings->{'failsafe'})) {
 4021:             $failsafe = $settings->{'failsafe'};
 4022:             if ($failsafe eq 'zero') {
 4023:                 $failsafechecked{'zero'} = ' checked="checked"';
 4024:                 $failsafechecked{'off'} = '';
 4025:                 $failsafesty = 'inline-block';
 4026:             } elsif ($failsafe eq 'any') {
 4027:                 $failsafechecked{'any'} = ' checked="checked"';
 4028:                 $failsafechecked{'off'} = '';
 4029:             }
 4030:             $autofailsafe = $settings->{'autofailsafe'};
 4031:         } elsif (exists($settings->{'autofailsafe'})) {
 4032:             $autofailsafe = $settings->{'autofailsafe'};
 4033:             if ($autofailsafe ne '') {
 4034:                 $failsafechecked{'zero'} = ' checked="checked"';
 4035:                 $failsafe = 'zero';
 4036:                 $failsafechecked{'off'} = '';
 4037:             }
 4038:         }
 4039:     } else {
 4040:         if ($autorun) {
 4041:             $runon = ' checked="checked" ';
 4042:             $runoff = ' ';
 4043:         } else {
 4044:             $runoff = ' checked="checked" ';
 4045:             $runon = ' ';
 4046:         }
 4047:     }
 4048:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4049:     my $notif_sender;
 4050:     if (ref($settings) eq 'HASH') {
 4051:         $notif_sender = $settings->{'sender_uname'};
 4052:     }
 4053:     my $datatable='<tr class="LC_odd_row">'.
 4054:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4055:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4056:                   '<input type="radio" name="autoenroll_run"'.
 4057:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4058:                   '<label><input type="radio" name="autoenroll_run"'.
 4059:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4060:                   '</tr><tr>'.
 4061:                   '<td>'.&mt('Notification messages - sender').
 4062:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4063:                   &mt('username').':&nbsp;'.
 4064:                   '<input type="text" name="sender_uname" value="'.
 4065:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4066:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4067:                   '<tr class="LC_odd_row">'.
 4068:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4069:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4070:                   '<input type="radio" name="autoassign_coowners"'.
 4071:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4072:                   '<label><input type="radio" name="autoassign_coowners"'.
 4073:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4074:                   '</tr><tr>'.
 4075:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4076:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4077:                   '<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; '.
 4078:                   '<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 />'.
 4079:                   '<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>'.
 4080:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4081:                   '<span class="LC_nobreak">'.
 4082:                   &mt('Threshold for number of students in section to drop: [_1]',
 4083:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4084:                   '</span></div></td></tr>';
 4085:     $$rowtotal += 4;
 4086:     return $datatable;
 4087: }
 4088: 
 4089: sub print_autoupdate {
 4090:     my ($position,$dom,$settings,$rowtotal) = @_;
 4091:     my ($enable,$datatable);
 4092:     if ($position eq 'top') {
 4093:         my %choices = &Apache::lonlocal::texthash (
 4094:                           run        => 'Auto-update active?',
 4095:                           classlists => 'Update information in classlists?',
 4096:                           unexpired  => 'Skip updates for users without active or future roles?',
 4097:                           lastactive => 'Skip updates for inactive users?',
 4098:         );
 4099:         my $itemcount = 0;
 4100:         my $updateon = ' ';
 4101:         my $updateoff = ' checked="checked" ';
 4102:         if (ref($settings) eq 'HASH') {
 4103:             if ($settings->{'run'} eq '1') {
 4104:                 $updateon = $updateoff;
 4105:                 $updateoff = ' ';
 4106:             }
 4107:         }
 4108:         $enable = '<tr class="LC_odd_row">'.
 4109:                   '<td>'.$choices{'run'}.'</td>'.
 4110:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4111:                   '<input type="radio" name="autoupdate_run"'.
 4112:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4113:                   '<label><input type="radio" name="autoupdate_run"'.
 4114:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4115:                   '</tr>';
 4116:         my @toggles = ('classlists','unexpired');
 4117:         my %defaultchecked = ('classlists' => 'off',
 4118:                               'unexpired'  => 'off'
 4119:                               );
 4120:         $$rowtotal ++;
 4121:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4122:                                                      \%choices,$itemcount,'','','left','no');
 4123:         $datatable = $enable.$datatable;
 4124:         $$rowtotal += $itemcount;
 4125:         my $lastactiveon = ' ';
 4126:         my $lastactiveoff = ' checked="checked" ';
 4127:         my $lastactivestyle = 'none';
 4128:         my $lastactivedays;
 4129:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4130:         if (ref($settings) eq 'HASH') {
 4131:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4132:                 $lastactiveon = $lastactiveoff;
 4133:                 $lastactiveoff = ' ';
 4134:                 $lastactivestyle = 'inline-block';
 4135:                 $lastactivedays = $settings->{'lastactive'};
 4136:             }
 4137:         }
 4138:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4139:         $datatable .= '<tr'.$css_class.'>'.
 4140:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4141:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4142:                       '<input type="radio" name="lastactive"'.
 4143:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4144:                       '&nbsp;<label>'.
 4145:                       '<input type="radio" name="lastactive"'.
 4146:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4147:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4148:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4149:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4150:                           $lastactivedays.'" />').
 4151:                       '</span></td>'.
 4152:                       '</tr>';
 4153:         $$rowtotal ++;
 4154:     } elsif ($position eq 'middle') {
 4155:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4156:         my $numinrow = 3;
 4157:         my $locknamesettings;
 4158:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4159:                                      $dom,$numinrow,$othertitle,
 4160:                                     'lockablenames',$rowtotal);
 4161:         $$rowtotal ++;
 4162:     } else {
 4163:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4164:         my @fields = ('lastname','firstname','middlename','generation',
 4165:                       'permanentemail','id');
 4166:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4167:         my $numrows = 0;
 4168:         if (ref($types) eq 'ARRAY') {
 4169:             if (@{$types} > 0) {
 4170:                 $datatable = 
 4171:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4172:                                          \@fields,$types,\$numrows);
 4173:                     $$rowtotal += @{$types}; 
 4174:             }
 4175:         }
 4176:         $datatable .= 
 4177:             &usertype_update_row($settings,{'default' => $othertitle},
 4178:                                  \%fieldtitles,\@fields,['default'],
 4179:                                  \$numrows);
 4180:         $$rowtotal ++;     
 4181:     }
 4182:     return $datatable;
 4183: }
 4184: 
 4185: sub print_autocreate {
 4186:     my ($dom,$settings,$rowtotal) = @_;
 4187:     my (%createon,%createoff,%currhash);
 4188:     my @types = ('xml','req');
 4189:     if (ref($settings) eq 'HASH') {
 4190:         foreach my $item (@types) {
 4191:             $createoff{$item} = ' checked="checked" ';
 4192:             $createon{$item} = ' ';
 4193:             if (exists($settings->{$item})) {
 4194:                 if ($settings->{$item}) {
 4195:                     $createon{$item} = ' checked="checked" ';
 4196:                     $createoff{$item} = ' ';
 4197:                 }
 4198:             }
 4199:         }
 4200:         if ($settings->{'xmldc'} ne '') {
 4201:             $currhash{$settings->{'xmldc'}} = 1;
 4202:         }
 4203:     } else {
 4204:         foreach my $item (@types) {
 4205:             $createoff{$item} = ' checked="checked" ';
 4206:             $createon{$item} = ' ';
 4207:         }
 4208:     }
 4209:     $$rowtotal += 2;
 4210:     my $numinrow = 2;
 4211:     my $datatable='<tr class="LC_odd_row">'.
 4212:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4213:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4214:                   '<input type="radio" name="autocreate_xml"'.
 4215:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4216:                   '<label><input type="radio" name="autocreate_xml"'.
 4217:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4218:                   '</td></tr><tr>'.
 4219:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4220:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4221:                   '<input type="radio" name="autocreate_req"'.
 4222:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4223:                   '<label><input type="radio" name="autocreate_req"'.
 4224:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4225:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4226:                                                    'autocreate_xmldc',%currhash);
 4227:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4228:     if ($numdc > 1) {
 4229:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4230:                       '</td><td class="LC_left_item">';
 4231:     } else {
 4232:         $datatable .= &mt('Course creation processed as:').
 4233:                       '</td><td class="LC_right_item">';
 4234:     }
 4235:     $datatable .= $dctable.'</td></tr>';
 4236:     $$rowtotal += $rows;
 4237:     return $datatable;
 4238: }
 4239: 
 4240: sub print_directorysrch {
 4241:     my ($position,$dom,$settings,$rowtotal) = @_;
 4242:     my $datatable;
 4243:     if ($position eq 'top') {
 4244:         my $instsrchon = ' ';
 4245:         my $instsrchoff = ' checked="checked" ';
 4246:         my ($exacton,$containson,$beginson);
 4247:         my $instlocalon = ' ';
 4248:         my $instlocaloff = ' checked="checked" ';
 4249:         if (ref($settings) eq 'HASH') {
 4250:             if ($settings->{'available'} eq '1') {
 4251:                 $instsrchon = $instsrchoff;
 4252:                 $instsrchoff = ' ';
 4253:             }
 4254:             if ($settings->{'localonly'} eq '1') {
 4255:                 $instlocalon = $instlocaloff;
 4256:                 $instlocaloff = ' ';
 4257:             }
 4258:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4259:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4260:                     if ($type eq 'exact') {
 4261:                         $exacton = ' checked="checked" ';
 4262:                     } elsif ($type eq 'contains') {
 4263:                         $containson = ' checked="checked" ';
 4264:                     } elsif ($type eq 'begins') {
 4265:                         $beginson = ' checked="checked" ';
 4266:                     }
 4267:                 }
 4268:             } else {
 4269:                 if ($settings->{'searchtypes'} eq 'exact') {
 4270:                     $exacton = ' checked="checked" ';
 4271:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4272:                     $containson = ' checked="checked" ';
 4273:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4274:                     $exacton = ' checked="checked" ';
 4275:                     $containson = ' checked="checked" ';
 4276:                 }
 4277:             }
 4278:         }
 4279:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4280:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4281: 
 4282:         my $numinrow = 4;
 4283:         my $cansrchrow = 0;
 4284:         $datatable='<tr class="LC_odd_row">'.
 4285:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4286:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4287:                    '<input type="radio" name="dirsrch_available"'.
 4288:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4289:                    '<label><input type="radio" name="dirsrch_available"'.
 4290:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4291:                    '</tr><tr>'.
 4292:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4293:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4294:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4295:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4296:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4297:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4298:                    '</tr>';
 4299:         $$rowtotal += 2;
 4300:         if (ref($usertypes) eq 'HASH') {
 4301:             if (keys(%{$usertypes}) > 0) {
 4302:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4303:                                              $numinrow,$othertitle,'cansearch',
 4304:                                              $rowtotal);
 4305:                 $cansrchrow = 1;
 4306:             }
 4307:         }
 4308:         if ($cansrchrow) {
 4309:             $$rowtotal ++;
 4310:             $datatable .= '<tr>';
 4311:         } else {
 4312:             $datatable .= '<tr class="LC_odd_row">';
 4313:         }
 4314:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4315:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4316:         foreach my $title (@{$titleorder}) {
 4317:             if (defined($searchtitles->{$title})) {
 4318:                 my $check = ' ';
 4319:                 if (ref($settings) eq 'HASH') {
 4320:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4321:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4322:                             $check = ' checked="checked" ';
 4323:                         }
 4324:                     }
 4325:                 }
 4326:                 $datatable .= '<td class="LC_left_item">'.
 4327:                               '<span class="LC_nobreak"><label>'.
 4328:                               '<input type="checkbox" name="searchby" '.
 4329:                               'value="'.$title.'"'.$check.'/>'.
 4330:                               $searchtitles->{$title}.'</label></span></td>';
 4331:             }
 4332:         }
 4333:         $datatable .= '</tr></table></td></tr>';
 4334:         $$rowtotal ++;
 4335:         if ($cansrchrow) {
 4336:             $datatable .= '<tr class="LC_odd_row">';
 4337:         } else {
 4338:             $datatable .= '<tr>';
 4339:         }
 4340:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4341:                       '<td class="LC_left_item" colspan="2">'.
 4342:                       '<span class="LC_nobreak"><label>'.
 4343:                       '<input type="checkbox" name="searchtypes" '.
 4344:                       $exacton.' value="exact" />'.&mt('Exact match').
 4345:                       '</label>&nbsp;'.
 4346:                       '<label><input type="checkbox" name="searchtypes" '.
 4347:                       $beginson.' value="begins" />'.&mt('Begins with').
 4348:                       '</label>&nbsp;'.
 4349:                       '<label><input type="checkbox" name="searchtypes" '.
 4350:                       $containson.' value="contains" />'.&mt('Contains').
 4351:                       '</label></span></td></tr>';
 4352:         $$rowtotal ++;
 4353:     } else {
 4354:         my $domsrchon = ' checked="checked" ';
 4355:         my $domsrchoff = ' ';
 4356:         my $domlocalon = ' ';
 4357:         my $domlocaloff = ' checked="checked" ';
 4358:         if (ref($settings) eq 'HASH') {
 4359:             if ($settings->{'lclocalonly'} eq '1') {
 4360:                 $domlocalon = $domlocaloff;
 4361:                 $domlocaloff = ' ';
 4362:             }
 4363:             if ($settings->{'lcavailable'} eq '0') {
 4364:                 $domsrchoff = $domsrchon;
 4365:                 $domsrchon = ' ';
 4366:             }
 4367:         }
 4368:         $datatable='<tr class="LC_odd_row">'.
 4369:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4370:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4371:                       '<input type="radio" name="dirsrch_domavailable"'.
 4372:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4373:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4374:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4375:                       '</tr><tr>'.
 4376:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4377:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4378:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4379:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4380:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4381:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4382:                       '</tr>';
 4383:         $$rowtotal += 2;
 4384:     }
 4385:     return $datatable;
 4386: }
 4387: 
 4388: sub print_contacts {
 4389:     my ($position,$dom,$settings,$rowtotal) = @_;
 4390:     my $datatable;
 4391:     my @contacts = ('adminemail','supportemail');
 4392:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4393:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4394:     if ($position eq 'top') {
 4395:         if (ref($settings) eq 'HASH') {
 4396:             foreach my $item (@contacts) {
 4397:                 if (exists($settings->{$item})) {
 4398:                     $to{$item} = $settings->{$item};
 4399:                 }
 4400:             }
 4401:         }
 4402:     } elsif ($position eq 'middle') {
 4403:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4404:                      'updatesmail','idconflictsmail','hostipmail');
 4405:         foreach my $type (@mailings) {
 4406:             $otheremails{$type} = '';
 4407:         }
 4408:     } elsif ($position eq 'lower') {
 4409:         if (ref($settings) eq 'HASH') {
 4410:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4411:                 %lonstatus = %{$settings->{'lonstatus'}};
 4412:             }
 4413:         }
 4414:     } else {
 4415:         @mailings = ('helpdeskmail','otherdomsmail');
 4416:         foreach my $type (@mailings) {
 4417:             $otheremails{$type} = '';
 4418:         }
 4419:         $bccemails{'helpdeskmail'} = '';
 4420:         $bccemails{'otherdomsmail'} = '';
 4421:         $includestr{'helpdeskmail'} = '';
 4422:         $includestr{'otherdomsmail'} = '';
 4423:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4424:     }
 4425:     if (ref($settings) eq 'HASH') {
 4426:         unless (($position eq 'top') || ($position eq 'lower')) {
 4427:             foreach my $type (@mailings) {
 4428:                 if (exists($settings->{$type})) {
 4429:                     if (ref($settings->{$type}) eq 'HASH') {
 4430:                         foreach my $item (@contacts) {
 4431:                             if ($settings->{$type}{$item}) {
 4432:                                 $checked{$type}{$item} = ' checked="checked" ';
 4433:                             }
 4434:                         }
 4435:                         $otheremails{$type} = $settings->{$type}{'others'};
 4436:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4437:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4438:                             if ($settings->{$type}{'include'} ne '') {
 4439:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4440:                                 $includestr{$type} = &unescape($includestr{$type});
 4441:                             }
 4442:                         }
 4443:                     }
 4444:                 } elsif ($type eq 'lonstatusmail') {
 4445:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4446:                 }
 4447:             }
 4448:         }
 4449:         if ($position eq 'bottom') {
 4450:             foreach my $type (@mailings) {
 4451:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4452:                 if ($settings->{$type}{'include'} ne '') {
 4453:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4454:                     $includestr{$type} = &unescape($includestr{$type});
 4455:                 }
 4456:             }
 4457:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4458:                 if (ref($fields) eq 'ARRAY') {
 4459:                     foreach my $field (@{$fields}) {
 4460:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4461:                     }
 4462:                 }
 4463:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4464:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4465:                 } else {
 4466:                     $maxsize = '1.0';
 4467:                 }
 4468:             } else {
 4469:                 if (ref($fields) eq 'ARRAY') {
 4470:                     foreach my $field (@{$fields}) {
 4471:                         $currfield{$field} = 'yes';
 4472:                     }
 4473:                 }
 4474:                 $maxsize = '1.0';
 4475:             }
 4476:         }
 4477:     } else {
 4478:         if ($position eq 'top') {
 4479:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4480:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4481:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4482:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4483:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4484:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4485:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4486:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4487:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4488:         } elsif ($position eq 'bottom') {
 4489:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4490:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4491:             if (ref($fields) eq 'ARRAY') {
 4492:                 foreach my $field (@{$fields}) {
 4493:                     $currfield{$field} = 'yes';
 4494:                 }
 4495:             }
 4496:             $maxsize = '1.0';
 4497:         }
 4498:     }
 4499:     my ($titles,$short_titles) = &contact_titles();
 4500:     my $rownum = 0;
 4501:     my $css_class;
 4502:     if ($position eq 'top') {
 4503:         foreach my $item (@contacts) {
 4504:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4505:             $datatable .= '<tr'.$css_class.'>'. 
 4506:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4507:                           '</span></td><td class="LC_right_item">'.
 4508:                           '<input type="text" name="'.$item.'" value="'.
 4509:                           $to{$item}.'" /></td></tr>';
 4510:             $rownum ++;
 4511:         }
 4512:     } elsif ($position eq 'bottom') {
 4513:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4514:         $datatable .= '<tr'.$css_class.'>'.
 4515:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4516:                       &mt('(e-mail, subject, and description always shown)').
 4517:                       '</td><td class="LC_left_item">';
 4518:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4519:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4520:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4521:             foreach my $field (@{$fields}) {
 4522:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4523:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4524:                     $datatable .= ' '.&mt('(logged-in users)');
 4525:                 }
 4526:                 $datatable .='</td><td>';
 4527:                 my $clickaction;
 4528:                 if ($field eq 'screenshot') {
 4529:                     $clickaction = ' onclick="screenshotSize(this);"';
 4530:                 }
 4531:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4532:                     foreach my $option (@{$possoptions->{$field}}) {
 4533:                         my $checked;
 4534:                         if ($currfield{$field} eq $option) {
 4535:                             $checked = ' checked="checked"';
 4536:                         }
 4537:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4538:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4539:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4540:                                       '</label></span>'.('&nbsp;'x2);
 4541:                     }
 4542:                 }
 4543:                 if ($field eq 'screenshot') {
 4544:                     my $display;
 4545:                     if ($currfield{$field} eq 'no') {
 4546:                         $display = ' style="display:none"';
 4547:                     }
 4548:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4549:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4550:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4551:                 }
 4552:                 $datatable .= '</td></tr>';
 4553:             }
 4554:             $datatable .= '</table>';
 4555:         }
 4556:         $datatable .= '</td></tr>'."\n";
 4557:         $rownum ++;
 4558:     }
 4559:     unless (($position eq 'top') || ($position eq 'lower')) {
 4560:         foreach my $type (@mailings) {
 4561:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4562:             $datatable .= '<tr'.$css_class.'>'.
 4563:                           '<td><span class="LC_nobreak">'.
 4564:                           $titles->{$type}.': </span></td>'.
 4565:                           '<td class="LC_left_item">';
 4566:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4567:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4568:             }
 4569:             $datatable .= '<span class="LC_nobreak">';
 4570:             foreach my $item (@contacts) {
 4571:                 $datatable .= '<label>'.
 4572:                               '<input type="checkbox" name="'.$type.'"'.
 4573:                               $checked{$type}{$item}.
 4574:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4575:                               '</label>&nbsp;';
 4576:             }
 4577:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4578:                           '<input type="text" name="'.$type.'_others" '.
 4579:                           'value="'.$otheremails{$type}.'"  />';
 4580:             my %locchecked;
 4581:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4582:                 foreach my $loc ('s','b') {
 4583:                     if ($includeloc{$type} eq $loc) {
 4584:                         $locchecked{$loc} = ' checked="checked"';
 4585:                         last;
 4586:                     }
 4587:                 }
 4588:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4589:                               '<input type="text" name="'.$type.'_bcc" '.
 4590:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4591:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4592:                               &mt('Text automatically added to e-mail:').' '.
 4593:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4594:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4595:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4596:                               ('&nbsp;'x2).
 4597:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4598:                               '</span></fieldset>';
 4599:             }
 4600:             $datatable .= '</td></tr>'."\n";
 4601:             $rownum ++;
 4602:         }
 4603:     }
 4604:     if ($position eq 'middle') {
 4605:         my %choices;
 4606:         my $corelink = &core_link_msu();
 4607:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4608:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4609:                                         $corelink);
 4610:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4611:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4612:         my %defaultchecked = ('reporterrors'  => 'on',
 4613:                               'reportupdates' => 'on',
 4614:                               'reportstatus'  => 'on');
 4615:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4616:                                                    \%choices,$rownum);
 4617:         $datatable .= $reports;
 4618:     } elsif ($position eq 'lower') {
 4619:         my (%current,%excluded,%weights);
 4620:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4621:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4622:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4623:         } else {
 4624:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4625:         }
 4626:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4627:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4628:         } else {
 4629:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4630:         }
 4631:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4632:             foreach my $type ('E','W','N','U') {
 4633:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4634:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4635:                 } else {
 4636:                     $weights{$type} = $defaults->{$type};
 4637:                 }
 4638:             }
 4639:         } else {
 4640:             foreach my $type ('E','W','N','U') {
 4641:                 $weights{$type} = $defaults->{$type};
 4642:             }
 4643:         }
 4644:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4645:             if (@{$lonstatus{'excluded'}} > 0) {
 4646:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4647:             }
 4648:         }
 4649:         foreach my $item ('errorthreshold','errorsysmail') {
 4650:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4651:             $datatable .= '<tr'.$css_class.'>'.
 4652:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4653:                           $titles->{$item}.
 4654:                           '</span></td><td class="LC_left_item">'.
 4655:                           '<input type="text" name="'.$item.'" value="'.
 4656:                           $current{$item}.'" size="5" /></td></tr>';
 4657:             $rownum ++;
 4658:         }
 4659:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4660:         $datatable .= '<tr'.$css_class.'>'.
 4661:                       '<td class="LC_left_item">'.
 4662:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4663:                       '</span></td><td class="LC_left_item"><table><tr>';
 4664:         foreach my $type ('E','W','N','U') {
 4665:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4666:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4667:                           $weights{$type}.'" size="5" /></td>';
 4668:         }
 4669:         $datatable .= '</tr></table></tr>';
 4670:         $rownum ++;
 4671:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4672:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4673:                       $titles->{'errorexcluded'}.'</td>'.
 4674:                       '<td class="LC_left_item"><table>';
 4675:         my $numinrow = 4;
 4676:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4677:         for (my $i=0; $i<@ids; $i++) {
 4678:             my $rem = $i%($numinrow);
 4679:             if ($rem == 0) {
 4680:                 if ($i > 0) {
 4681:                     $datatable .= '</tr>';
 4682:                 }
 4683:                 $datatable .= '<tr>';
 4684:             }
 4685:             my $check;
 4686:             if ($excluded{$ids[$i]}) {
 4687:                 $check = ' checked="checked" ';
 4688:             }
 4689:             $datatable .= '<td class="LC_left_item">'.
 4690:                           '<span class="LC_nobreak"><label>'.
 4691:                           '<input type="checkbox" name="errorexcluded" '.
 4692:                           'value="'.$ids[$i].'"'.$check.' />'.
 4693:                           $ids[$i].'</label></span></td>';
 4694:         }
 4695:         my $colsleft = $numinrow - @ids%($numinrow);
 4696:         if ($colsleft > 1 ) {
 4697:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4698:                           '&nbsp;</td>';
 4699:         } elsif ($colsleft == 1) {
 4700:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4701:         }
 4702:         $datatable .= '</tr></table></td></tr>';
 4703:         $rownum ++;
 4704:     } elsif ($position eq 'bottom') {
 4705:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4706:         my (@posstypes,%usertypeshash);
 4707:         if (ref($types) eq 'ARRAY') {
 4708:             @posstypes = @{$types};
 4709:         }
 4710:         if (@posstypes) {
 4711:             if (ref($usertypes) eq 'HASH') {
 4712:                 %usertypeshash = %{$usertypes};
 4713:             }
 4714:             my @overridden;
 4715:             my $numinrow = 4;
 4716:             if (ref($settings) eq 'HASH') {
 4717:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4718:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4719:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4720:                             push(@overridden,$key);
 4721:                             foreach my $item (@contacts) {
 4722:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4723:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4724:                                 }
 4725:                             }
 4726:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4727:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4728:                             $includeloc{'override_'.$key} = '';
 4729:                             $includestr{'override_'.$key} = '';
 4730:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4731:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4732:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4733:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4734:                             }
 4735:                         }
 4736:                     }
 4737:                 }
 4738:             }
 4739:             my $customclass = 'LC_helpdesk_override';
 4740:             my $optionsprefix = 'LC_options_helpdesk_';
 4741: 
 4742:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4743:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4744:                                          $numinrow,$othertitle,'overrides',
 4745:                                          \$rownum,$onclicktypes,$customclass);
 4746:             $rownum ++;
 4747:             $usertypeshash{'default'} = $othertitle;
 4748:             foreach my $status (@posstypes) {
 4749:                 my $css_class;
 4750:                 if ($rownum%2) {
 4751:                     $css_class = 'LC_odd_row ';
 4752:                 }
 4753:                 $css_class .= $customclass;
 4754:                 my $rowid = $optionsprefix.$status;
 4755:                 my $hidden = 1;
 4756:                 my $currstyle = 'display:none';
 4757:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4758:                     $currstyle = 'display:table-row';
 4759:                     $hidden = 0;
 4760:                 }
 4761:                 my $key = 'override_'.$status;
 4762:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4763:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4764:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4765:                                                   \@contacts,$short_titles);
 4766:                 unless ($hidden) {
 4767:                     $rownum ++;
 4768:                 }
 4769:             }
 4770:         }
 4771:     }
 4772:     $$rowtotal += $rownum;
 4773:     return $datatable;
 4774: }
 4775: 
 4776: sub core_link_msu {
 4777:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4778:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4779: }
 4780: 
 4781: sub overridden_helpdesk {
 4782:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4783:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4784:     my $class = 'LC_left_item';
 4785:     if ($css_class) {
 4786:         $css_class = ' class="'.$css_class.'"';
 4787:     }
 4788:     if ($rowid) {
 4789:         $rowid = ' id="'.$rowid.'"';
 4790:     }
 4791:     if ($rowstyle) {
 4792:         $rowstyle = ' style="'.$rowstyle.'"';
 4793:     }
 4794:     my ($output,$description);
 4795:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4796:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4797:               "<td>$description</td>\n".
 4798:               '<td class="'.$class.'" colspan="2">'.
 4799:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4800:               '<span class="LC_nobreak">';
 4801:     if (ref($contacts) eq 'ARRAY') {
 4802:         foreach my $item (@{$contacts}) {
 4803:             my $check;
 4804:             if (ref($checked) eq 'HASH') {
 4805:                $check = $checked->{$item};
 4806:             }
 4807:             my $title;
 4808:             if (ref($short_titles) eq 'HASH') {
 4809:                 $title = $short_titles->{$item};
 4810:             }
 4811:             $output .= '<label>'.
 4812:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4813:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4814:         }
 4815:     }
 4816:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4817:                '<input type="text" name="override_'.$type.'_others" '.
 4818:                'value="'.$otheremails.'"  />';
 4819:     my %locchecked;
 4820:     foreach my $loc ('s','b') {
 4821:         if ($includeloc eq $loc) {
 4822:             $locchecked{$loc} = ' checked="checked"';
 4823:             last;
 4824:         }
 4825:     }
 4826:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4827:                '<input type="text" name="override_'.$type.'_bcc" '.
 4828:                'value="'.$bccemails.'"  /></fieldset>'.
 4829:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4830:                &mt('Text automatically added to e-mail:').' '.
 4831:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4832:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4833:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4834:                ('&nbsp;'x2).
 4835:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4836:                '</span></fieldset>'.
 4837:                '</td></tr>'."\n";
 4838:     return $output;
 4839: }
 4840: 
 4841: sub contacts_javascript {
 4842:     return <<"ENDSCRIPT";
 4843: 
 4844: <script type="text/javascript">
 4845: // <![CDATA[
 4846: 
 4847: function screenshotSize(field) {
 4848:     if (document.getElementById('help_screenshotsize')) {
 4849:         if (field.value == 'no') {
 4850:             document.getElementById('help_screenshotsize').style.display="none";
 4851:         } else {
 4852:             document.getElementById('help_screenshotsize').style.display="";
 4853:         }
 4854:     }
 4855:     return;
 4856: }
 4857: 
 4858: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4859:     if (form.elements[checkbox].length != undefined) {
 4860:         var count = 0;
 4861:         if (docount) {
 4862:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4863:                 if (form.elements[checkbox][i].checked) {
 4864:                     count ++;
 4865:                 }
 4866:             }
 4867:         }
 4868:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4869:             var type = form.elements[checkbox][i].value;
 4870:             if (document.getElementById(prefix+type)) {
 4871:                 if (form.elements[checkbox][i].checked) {
 4872:                     document.getElementById(prefix+type).style.display = 'table-row';
 4873:                     if (count % 2 == 1) {
 4874:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4875:                     } else {
 4876:                         document.getElementById(prefix+type).className = target;
 4877:                     }
 4878:                     count ++;
 4879:                 } else {
 4880:                     document.getElementById(prefix+type).style.display = 'none';
 4881:                 }
 4882:             }
 4883:         }
 4884:     }
 4885:     return;
 4886: }
 4887: 
 4888: // ]]>
 4889: </script>
 4890: 
 4891: ENDSCRIPT
 4892: }
 4893: 
 4894: sub print_helpsettings {
 4895:     my ($position,$dom,$settings,$rowtotal) = @_;
 4896:     my $confname = $dom.'-domainconfig';
 4897:     my $formname = 'display';
 4898:     my ($datatable,$itemcount);
 4899:     if ($position eq 'top') {
 4900:         $itemcount = 1;
 4901:         my (%choices,%defaultchecked,@toggles);
 4902:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4903:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4904:                                      &mt('LON-CAPA bug tracker'),600,500));
 4905:         %defaultchecked = ('submitbugs' => 'on');
 4906:         @toggles = ('submitbugs');
 4907:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4908:                                                      \%choices,$itemcount);
 4909:         $$rowtotal ++;
 4910:     } else {
 4911:         my $css_class;
 4912:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4913:         my (%customroles,%ordered,%current);
 4914:         if (ref($settings) eq 'HASH') {
 4915:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4916:                 %current = %{$settings->{'adhoc'}};
 4917:             }
 4918:         }
 4919:         my $count = 0;
 4920:         foreach my $key (sort(keys(%existing))) {
 4921:             if ($key=~/^rolesdef\_(\w+)$/) {
 4922:                 my $rolename = $1;
 4923:                 my (%privs,$order);
 4924:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4925:                 $customroles{$rolename} = \%privs;
 4926:                 if (ref($current{$rolename}) eq 'HASH') {
 4927:                     $order = $current{$rolename}{'order'};
 4928:                 }
 4929:                 if ($order eq '') {
 4930:                     $order = $count;
 4931:                 }
 4932:                 $ordered{$order} = $rolename;
 4933:                 $count++;
 4934:             }
 4935:         }
 4936:         my $maxnum = scalar(keys(%ordered));
 4937:         my @roles_by_num = ();
 4938:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4939:             push(@roles_by_num,$item);
 4940:         }
 4941:         my $context = 'domprefs';
 4942:         my $crstype = 'Course';
 4943:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4944:         my @accesstypes = ('all','dh','da','none');
 4945:         my ($numstatustypes,@jsarray);
 4946:         if (ref($types) eq 'ARRAY') {
 4947:             if (@{$types} > 0) {
 4948:                 $numstatustypes = scalar(@{$types});
 4949:                 push(@accesstypes,'status');
 4950:                 @jsarray = ('bystatus');
 4951:             }
 4952:         }
 4953:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4954:         if (keys(%domhelpdesk)) {
 4955:             push(@accesstypes,('inc','exc'));
 4956:             push(@jsarray,('notinc','notexc'));
 4957:         }
 4958:         my $hiddenstr = join("','",@jsarray);
 4959:         my $context = 'domprefs';
 4960:         my $crstype = 'Course';
 4961:         my $prefix = 'helproles_';
 4962:         my $add_class = 'LC_hidden';
 4963:         foreach my $num (@roles_by_num) {
 4964:             my $role = $ordered{$num};
 4965:             my ($desc,$access,@statuses);
 4966:             if (ref($current{$role}) eq 'HASH') {
 4967:                 $desc = $current{$role}{'desc'};
 4968:                 $access = $current{$role}{'access'};
 4969:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4970:                     @statuses = @{$current{$role}{'insttypes'}};
 4971:                 }
 4972:             }
 4973:             if ($desc eq '') {
 4974:                 $desc = $role;
 4975:             }
 4976:             my $identifier = 'custhelp'.$num;
 4977:             my %full=();
 4978:             my %levels= (
 4979:                          course => {},
 4980:                          domain => {},
 4981:                          system => {},
 4982:                         );
 4983:             my %levelscurrent=(
 4984:                                course => {},
 4985:                                domain => {},
 4986:                                system => {},
 4987:                               );
 4988:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4989:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4990:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4991:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4992:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4993:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4994:             for (my $k=0; $k<=$maxnum; $k++) {
 4995:                 my $vpos = $k+1;
 4996:                 my $selstr;
 4997:                 if ($k == $num) {
 4998:                     $selstr = ' selected="selected" ';
 4999:                 }
 5000:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5001:             }
 5002:             $datatable .= '</select>'.('&nbsp;'x2).
 5003:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 5004:                           '</td>'.
 5005:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5006:                           &mt('Name shown to users:').
 5007:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 5008:                           '</fieldset>'.
 5009:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 5010:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 5011:                           '<fieldset>'.
 5012:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 5013:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 5014:                                                                    \%levelscurrent,$identifier,
 5015:                                                                    'LC_hidden',$prefix.$num.'_privs').
 5016:                           '</fieldset></td>';
 5017:             $itemcount ++;
 5018:         }
 5019:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5020:         my $newcust = 'custhelp'.$count;
 5021:         my (%privs,%levelscurrent);
 5022:         my %full=();
 5023:         my %levels= (
 5024:                      course => {},
 5025:                      domain => {},
 5026:                      system => {},
 5027:                     );
 5028:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 5029:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5030:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 5031:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 5032:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5033:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5034:         for (my $k=0; $k<$maxnum+1; $k++) {
 5035:             my $vpos = $k+1;
 5036:             my $selstr;
 5037:             if ($k == $maxnum) {
 5038:                 $selstr = ' selected="selected" ';
 5039:             }
 5040:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5041:         }
 5042:         $datatable .= '</select>&nbsp;'."\n".
 5043:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5044:                       '</label></span></td>'.
 5045:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5046:                       '<span class="LC_nobreak">'.
 5047:                       &mt('Internal name:').
 5048:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5049:                       '</span>'.('&nbsp;'x4).
 5050:                       '<span class="LC_nobreak">'.
 5051:                       &mt('Name shown to users:').
 5052:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5053:                       '</span></fieldset>'.
 5054:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5055:                                              $usertypes,$types,\%domhelpdesk).
 5056:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5057:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5058:                                                                 \@templateroles,$newcust).
 5059:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5060:                                                                \%levelscurrent,$newcust).
 5061:                       '</fieldset>'.
 5062:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5063:                       '</td></tr>';
 5064:         $count ++;
 5065:         $$rowtotal += $count;
 5066:     }
 5067:     return $datatable;
 5068: }
 5069: 
 5070: sub adhocbutton {
 5071:     my ($prefix,$num,$field,$visibility) = @_;
 5072:     my %lt = &Apache::lonlocal::texthash(
 5073:                                           show => 'Show details',
 5074:                                           hide => 'Hide details',
 5075:                                         );
 5076:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5077:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5078:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5079:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5080: }
 5081: 
 5082: sub helpsettings_javascript {
 5083:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5084:     return unless(ref($roles_by_num) eq 'ARRAY');
 5085:     my %html_js_lt = &Apache::lonlocal::texthash(
 5086:                                           show => 'Show details',
 5087:                                           hide => 'Hide details',
 5088:                                         );
 5089:     &html_escape(\%html_js_lt);
 5090:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5091:     return <<"ENDSCRIPT";
 5092: <script type="text/javascript">
 5093: // <![CDATA[
 5094: 
 5095: function reorderHelpRoles(form,item) {
 5096:     var changedVal;
 5097: $jstext
 5098:     var newpos = 'helproles_${total}_pos';
 5099:     var maxh = 1 + $total;
 5100:     var current = new Array();
 5101:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5102:     if (item == newpos) {
 5103:         changedVal = newitemVal;
 5104:     } else {
 5105:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5106:         current[newitemVal] = newpos;
 5107:     }
 5108:     for (var i=0; i<helproles.length; i++) {
 5109:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5110:         if (elementName != item) {
 5111:             if (form.elements[elementName]) {
 5112:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5113:                 current[currVal] = elementName;
 5114:             }
 5115:         }
 5116:     }
 5117:     var oldVal;
 5118:     for (var j=0; j<maxh; j++) {
 5119:         if (current[j] == undefined) {
 5120:             oldVal = j;
 5121:         }
 5122:     }
 5123:     if (oldVal < changedVal) {
 5124:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5125:            var elementName = current[k];
 5126:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5127:         }
 5128:     } else {
 5129:         for (var k=changedVal; k<oldVal; k++) {
 5130:             var elementName = current[k];
 5131:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5132:         }
 5133:     }
 5134:     return;
 5135: }
 5136: 
 5137: function helpdeskAccess(num) {
 5138:     var curraccess = null;
 5139:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5140:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5141:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5142:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5143:             }
 5144:         }
 5145:     }
 5146:     var shown = Array();
 5147:     var hidden = Array();
 5148:     if (curraccess == 'none') {
 5149:         hidden = Array('$hiddenstr');
 5150:     } else {
 5151:         if (curraccess == 'status') {
 5152:             shown = Array('bystatus');
 5153:             hidden = Array('notinc','notexc');
 5154:         } else {
 5155:             if (curraccess == 'exc') {
 5156:                 shown = Array('notexc');
 5157:                 hidden = Array('notinc','bystatus');
 5158:             }
 5159:             if (curraccess == 'inc') {
 5160:                 shown = Array('notinc');
 5161:                 hidden = Array('notexc','bystatus');
 5162:             }
 5163:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5164:                 hidden = Array('notinc','notexc','bystatus');
 5165:             }
 5166:         }
 5167:     }
 5168:     if (hidden.length > 0) {
 5169:         for (var i=0; i<hidden.length; i++) {
 5170:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5171:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5172:             }
 5173:         }
 5174:     }
 5175:     if (shown.length > 0) {
 5176:         for (var i=0; i<shown.length; i++) {
 5177:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5178:                 if (shown[i] == 'privs') {
 5179:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5180:                 } else {
 5181:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5182:                 }
 5183:             }
 5184:         }
 5185:     }
 5186:     return;
 5187: }
 5188: 
 5189: function toggleHelpdeskItem(num,field) {
 5190:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5191:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5192:             document.getElementById('helproles_'+num+'_'+field).className =
 5193:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5194:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5195:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5196:             }
 5197:         } else {
 5198:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5199:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5200:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5201:             }
 5202:         }
 5203:     }
 5204:     return;
 5205: }
 5206: 
 5207: // ]]>
 5208: </script>
 5209: 
 5210: ENDSCRIPT
 5211: }
 5212: 
 5213: sub helpdeskroles_access {
 5214:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5215:         $usertypes,$types,$domhelpdesk) = @_;
 5216:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5217:     my %lt = &Apache::lonlocal::texthash(
 5218:                     'rou'    => 'Role usage',
 5219:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5220:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5221:                     'dh'     => 'All with domain helpdesk role',
 5222:                     'da'     => 'All with domain helpdesk assistant role',
 5223:                     'none'   => 'None',
 5224:                     'status' => 'Determined based on institutional status',
 5225:                     'inc'    => 'Include all, but exclude specific personnel',
 5226:                     'exc'    => 'Exclude all, but include specific personnel',
 5227:                   );
 5228:     my %usecheck = (
 5229:                      all => ' checked="checked"',
 5230:                    );
 5231:     my %displaydiv = (
 5232:                       status => 'none',
 5233:                       inc    => 'none',
 5234:                       exc    => 'none',
 5235:                       priv   => 'block',
 5236:                      );
 5237:     my $output;
 5238:     if (ref($current) eq 'HASH') {
 5239:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5240:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5241:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5242:                 delete($usecheck{'all'});
 5243:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5244:                     my $access = $1;
 5245:                     $displaydiv{$access} = 'inline';
 5246:                 } elsif ($current->{access} eq 'none') {
 5247:                     $displaydiv{'priv'} = 'none';
 5248:                 }
 5249:             }
 5250:         }
 5251:     }
 5252:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5253:               '<p>'.$lt{'whi'}.'</p>';
 5254:     foreach my $access (@{$accesstypes}) {
 5255:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5256:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5257:                    $lt{$access}.'</label>';
 5258:         if ($access eq 'status') {
 5259:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5260:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5261:                                                                  $othertitle,$usertypes,$types).
 5262:                        '</div>';
 5263:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5264:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5265:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5266:                        '</div>';
 5267:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5268:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5269:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5270:                        '</div>';
 5271:         }
 5272:         $output .= '</p>';
 5273:     }
 5274:     $output .= '</fieldset>';
 5275:     return $output;
 5276: }
 5277: 
 5278: sub radiobutton_prefs {
 5279:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5280:         $additional,$align,$firstval) = @_;
 5281:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5282:                    (ref($choices) eq 'HASH'));
 5283: 
 5284:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5285: 
 5286:     foreach my $item (@{$toggles}) {
 5287:         if ($defaultchecked->{$item} eq 'on') {
 5288:             $checkedon{$item} = ' checked="checked" ';
 5289:             $checkedoff{$item} = ' ';
 5290:         } elsif ($defaultchecked->{$item} eq 'off') {
 5291:             $checkedoff{$item} = ' checked="checked" ';
 5292:             $checkedon{$item} = ' ';
 5293:         }
 5294:     }
 5295:     if (ref($settings) eq 'HASH') {
 5296:         foreach my $item (@{$toggles}) {
 5297:             if ($settings->{$item} eq '1') {
 5298:                 $checkedon{$item} =  ' checked="checked" ';
 5299:                 $checkedoff{$item} = ' ';
 5300:             } elsif ($settings->{$item} eq '0') {
 5301:                 $checkedoff{$item} =  ' checked="checked" ';
 5302:                 $checkedon{$item} = ' ';
 5303:             }
 5304:         }
 5305:     }
 5306:     if ($onclick) {
 5307:         $onclick = ' onclick="'.$onclick.'"';
 5308:     }
 5309:     foreach my $item (@{$toggles}) {
 5310:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5311:         $datatable .=
 5312:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5313:             '<span class="LC_nobreak">'.$choices->{$item}.
 5314:             '</span></td>';
 5315:         if ($align eq 'left') {
 5316:             $datatable .= '<td class="LC_left_item">';
 5317:         } else {
 5318:             $datatable .= '<td class="LC_right_item">';
 5319:         }
 5320:         $datatable .= '<span class="LC_nobreak">';
 5321:         if ($firstval eq 'no') {
 5322:             $datatable .=
 5323:                 '<label><input type="radio" name="'.
 5324:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5325:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5326:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5327:         } else {
 5328:             $datatable .=
 5329:                 '<label><input type="radio" name="'.
 5330:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5331:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5332:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5333:         }
 5334:         $datatable .= '</span>'.$additional.'</td></tr>';
 5335:         $itemcount ++;
 5336:     }
 5337:     return ($datatable,$itemcount);
 5338: }
 5339: 
 5340: sub print_ltitools {
 5341:     my ($position,$dom,$settings,$rowtotal) = @_;
 5342:     my (%rules,%encrypt,%privkeys,%linkprot);
 5343:     if (ref($settings) eq 'HASH') {
 5344:         if ($position eq 'top') {
 5345:             if (exists($settings->{'encrypt'})) {
 5346:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5347:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5348:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5349:                     }
 5350:                 }
 5351:             }
 5352:             if (exists($settings->{'private'})) {
 5353:                 if (ref($settings->{'private'}) eq 'HASH') {
 5354:                     if (ref($settings->{'private'}) eq 'HASH') {
 5355:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5356:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5357:                         }
 5358:                     }
 5359:                 }
 5360:             }
 5361:         } elsif ($position eq 'middle') {
 5362:             if (exists($settings->{'rules'})) {
 5363:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5364:                     %rules = %{$settings->{'rules'}};
 5365:                 }
 5366:             }
 5367:         } else {
 5368:             foreach my $key ('encrypt','private','rules') {
 5369:                 if (exists($settings->{$key})) {
 5370:                     delete($settings->{$key});
 5371:                 }
 5372:             }
 5373:         }
 5374:     }
 5375:     my $datatable;
 5376:     my $itemcount = 1;
 5377:     if ($position eq 'top') {
 5378:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5379:     } elsif ($position eq 'middle') {
 5380:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5381:         $$rowtotal += $itemcount;
 5382:     } else {
 5383:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5384:     }
 5385:     return $datatable;
 5386: }
 5387: 
 5388: sub ltitools_names {
 5389:     my %lt = &Apache::lonlocal::texthash(
 5390:                                           'title'          => 'Title',
 5391:                                           'version'        => 'Version',
 5392:                                           'msgtype'        => 'Message Type',
 5393:                                           'sigmethod'      => 'Signature Method',
 5394:                                           'url'            => 'URL',
 5395:                                           'key'            => 'Key',
 5396:                                           'lifetime'       => 'Nonce lifetime (s)',
 5397:                                           'secret'         => 'Secret',
 5398:                                           'icon'           => 'Icon',
 5399:                                           'user'           => 'User',
 5400:                                           'fullname'       => 'Full Name',
 5401:                                           'firstname'      => 'First Name',
 5402:                                           'lastname'       => 'Last Name',
 5403:                                           'email'          => 'E-mail',
 5404:                                           'roles'          => 'Role',
 5405:                                           'window'         => 'Window',
 5406:                                           'tab'            => 'Tab',
 5407:                                           'iframe'         => 'iFrame',
 5408:                                           'height'         => 'Height',
 5409:                                           'width'          => 'Width',
 5410:                                           'linktext'       => 'Default Link Text',
 5411:                                           'explanation'    => 'Default Explanation',
 5412:                                           'passback'       => 'Tool can return grades:',
 5413:                                           'roster'         => 'Tool can retrieve roster:',
 5414:                                           'crstarget'      => 'Display target',
 5415:                                           'crslabel'       => 'Course label',
 5416:                                           'crstitle'       => 'Course title',
 5417:                                           'crslinktext'    => 'Link Text',
 5418:                                           'crsexplanation' => 'Explanation',
 5419:                                           'crsappend'      => 'Provider URL',
 5420:                                         );
 5421:     return %lt;
 5422: }
 5423: 
 5424: sub secrets_form {
 5425:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5426:     my @ids=&Apache::lonnet::current_machine_ids();
 5427:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5428:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5429:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5430:     $itemcount = 0;
 5431:     foreach my $hostid (sort(keys(%servers))) {
 5432:         my ($showextra,$divsty,$switch);
 5433:         if ($hostid eq $primary) {
 5434:             if ($context eq 'ltisec') {
 5435:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5436:                     $showextra = 1;
 5437:                 }
 5438:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5439:                     $showextra = 1;
 5440:                 }
 5441:             } else {
 5442:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5443:                     $showextra = 1;
 5444:                 }
 5445:             }
 5446:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5447:                 $switch = 1;
 5448:             }
 5449:             if ($showextra) {
 5450:                 $numshown ++;
 5451:                 $divsty = 'display:inline-block';
 5452:             } else {
 5453:                 $divsty = 'display:none';
 5454:             }
 5455:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5456:                       '<legend>'.$hostid.'</legend>';
 5457:             if ($switch) {
 5458:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5459:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5460:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5461:                 if (exists($privkeys->{$hostid})) {
 5462:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5463:                               '<span class="LC_nobreak">'.
 5464:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5465:                               '<span class="LC_nobreak">'.&mt('Change?').
 5466:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5467:                               ('&nbsp;'x2).
 5468:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5469:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5470:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5471:                               '</span></div>';
 5472:                 } else {
 5473:                     $extra .= '<span class="LC_nobreak">'.
 5474:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5475:                               '</span>'."\n";
 5476:                 }
 5477:             } elsif (exists($privkeys->{$hostid})) {
 5478:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5479:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5480:                           '<span class="LC_nobreak">'.&mt('Change?').
 5481:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5482:                           ('&nbsp;'x2).
 5483:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5484:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5485:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5486:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5487:                           '<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>'.
 5488:                           '</span></div>';
 5489:             } else {
 5490:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5491:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5492:                           '<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>';
 5493:             }
 5494:             $extra .= '</fieldset>';
 5495:         }
 5496:     }
 5497:     my (%choices,@toggles,%defaultchecked);
 5498:     if ($context eq 'ltisec') {
 5499:         %choices = &Apache::lonlocal::texthash (
 5500:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5501:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5502:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5503:                                                );
 5504:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5505:         %defaultchecked = (
 5506:                            'ltisec_crslinkprot' => 'off',
 5507:                            'ltisec_domlinkprot' => 'off',
 5508:                            'ltisec_consumers'   => 'off',
 5509:                           );
 5510:     } else {
 5511:         %choices = &Apache::lonlocal::texthash (
 5512:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5513:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5514:                                                );
 5515:         @toggles = qw(toolsec_crs toolsec_dom);
 5516:         %defaultchecked = (
 5517:                            'toolsec_crs' => 'off',
 5518:                            'toolsec_dom' => 'off',
 5519:                           );
 5520:     }
 5521:     my ($onclick,$itemcount);
 5522:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5523:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5524:                                               \%choices,$itemcount,$onclick,'','left','no');
 5525: 
 5526:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5527:     my $noprivkeysty = 'display:inline-block';
 5528:     if ($numshown) {
 5529:         $noprivkeysty = 'display:none';
 5530:     }
 5531:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5532:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5533:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5534:                $extra.
 5535:                '</td></tr>';
 5536:     $itemcount ++;
 5537:     $$rowtotal += $itemcount;
 5538:     return $output;
 5539: }
 5540: 
 5541: sub print_proctoring {
 5542:     my ($dom,$settings,$rowtotal) = @_;
 5543:     my $itemcount = 1;
 5544:     my (%ordered,%providernames,%current,%currentdef);
 5545:     my $confname = $dom.'-domainconfig';
 5546:     my $switchserver = &check_switchserver($dom,$confname);
 5547:     if (ref($settings) eq 'HASH') {
 5548:         foreach my $item (keys(%{$settings})) {
 5549:             if (ref($settings->{$item}) eq 'HASH') {
 5550:                 my $num = $settings->{$item}{'order'};
 5551:                 $ordered{$num} = $item;
 5552:             }
 5553:         }
 5554:     } else {
 5555:         %ordered = (
 5556:                      1 => 'proctorio',
 5557:                      2 => 'examity',
 5558:                    );
 5559:     }
 5560:     %providernames = &proctoring_providernames();
 5561:     my $maxnum = scalar(keys(%ordered));
 5562:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5563:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5564:     if (ref($requref) eq 'HASH') {
 5565:         %requserfields = %{$requref};
 5566:     }
 5567:     if (ref($opturef) eq 'HASH') {
 5568:         %optuserfields = %{$opturef};
 5569:     }
 5570:     if (ref($defref) eq 'HASH') {
 5571:         %defaults = %{$defref};
 5572:     }
 5573:     if (ref($extref) eq 'HASH') {
 5574:         %extended = %{$extref};
 5575:     }
 5576:     if (ref($crsref) eq 'HASH') {
 5577:         %crsconf = %{$crsref};
 5578:     }
 5579:     if (ref($rolesref) eq 'ARRAY') {
 5580:         @courseroles = @{$rolesref};
 5581:     }
 5582:     if (ref($ltiref) eq 'ARRAY') {
 5583:         @ltiroles = @{$ltiref};
 5584:     }
 5585:     my $datatable;
 5586:     my $css_class;
 5587:     if (keys(%ordered)) {
 5588:         my @items = sort { $a <=> $b } keys(%ordered);
 5589:         for (my $i=0; $i<@items; $i++) {
 5590:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5591:             my $provider = $ordered{$items[$i]};
 5592:             my $optionsty = 'none';
 5593:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5594:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5595:             if (ref($settings) eq 'HASH') {
 5596:                 if (ref($settings->{$provider}) eq 'HASH') {
 5597:                     %current = %{$settings->{$provider}};
 5598:                     if ($current{'available'}) {
 5599:                         $optionsty = 'block';
 5600:                         $available = 1;
 5601:                     }
 5602:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5603:                         $lifetime = $current{'lifetime'};
 5604:                     }
 5605:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5606:                         $version = $current{'version'};
 5607:                     }
 5608:                     if ($current{'image'} ne '') {
 5609:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5610:                     }
 5611:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5612:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5613:                     }
 5614:                     $userincdom = $current{'incdom'};
 5615:                     if (ref($current{'roles'}) eq 'HASH') {
 5616:                         %rolemaps = %{$current{'roles'}};
 5617:                         $checkedfields{'roles'} = 1;
 5618:                     }
 5619:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5620:                         foreach my $val (@{$current{'defaults'}}) {
 5621:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5622:                                 $inuse{$val} = 1;
 5623:                             } else {
 5624:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5625:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5626:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5627:                                             $inuse{$poss} = $val;
 5628:                                             last;
 5629:                                         }
 5630:                                     }
 5631:                                 }
 5632:                             }
 5633:                         }
 5634:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5635:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5636:                             my $currval = $current{'defaults'}{$key}; 
 5637:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5638:                                 $inuse{$key} = 1;
 5639:                             } else {
 5640:                                 my $match;
 5641:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5642:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5643:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5644:                                             $inuse{$poss} = $key;
 5645:                                             last;
 5646:                                         }
 5647:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5648:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5649:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5650:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5651:                                                     $currentdef{$inner} = $currval;
 5652:                                                     $match = 1;
 5653:                                                     last;
 5654:                                                 }
 5655:                                             } elsif ($inner eq $key) {
 5656:                                                 $currentdef{$key} = $currval;
 5657:                                                 $match = 1;
 5658:                                                 last;
 5659:                                             }
 5660:                                         }
 5661:                                     }
 5662:                                     last if ($match);
 5663:                                 }
 5664:                             }
 5665:                         }
 5666:                     }
 5667:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5668:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5669:                     }
 5670:                 }
 5671:             }
 5672:             my %lt = &proctoring_titles($provider);
 5673:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5674:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5675:             my %checkedavailable = (
 5676:                                    yes => '',
 5677:                                    no  => ' checked="checked"',
 5678:                                  );
 5679:             if ($available) {
 5680:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5681:                 $checkedavailable{'no'} = '';
 5682:             }
 5683:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5684:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5685:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5686:             for (my $k=0; $k<$maxnum; $k++) {
 5687:                 my $vpos = $k+1;
 5688:                 my $selstr;
 5689:                 if ($k == $i) {
 5690:                     $selstr = ' selected="selected" ';
 5691:                 }
 5692:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5693:             }
 5694:             if ($version eq '') {
 5695:                 if ($provider eq 'proctorio') {
 5696:                     $version = '1.0';
 5697:                 } elsif ($provider eq 'examity') {
 5698:                     $version = '1.1';
 5699:                 }
 5700:             }
 5701:             if ($lifetime eq '') {
 5702:                 $lifetime = '300';
 5703:             }
 5704:             $datatable .=
 5705:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5706:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5707:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5708:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5709:                 '</td>'.
 5710:                 '<td colspan="2">'.
 5711:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5712:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5713:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5714:                 ('&nbsp;'x2).
 5715:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5716:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5717:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5718:                 ('&nbsp;'x2).
 5719:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5720:                 '<br />'.
 5721:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5722:                 '<br />'.
 5723:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5724:                 ('&nbsp;'x2).
 5725:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5726:                 '<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";
 5727:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5728:             if ($imgsrc) {
 5729:                 $datatable .= $imgsrc.
 5730:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5731:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5732:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5733:             }
 5734:             $datatable .= '&nbsp;';
 5735:             if ($switchserver) {
 5736:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5737:             } else {
 5738:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5739:             }
 5740:             unless ($imgsrc) {
 5741:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5742:             }
 5743:             $datatable .= '</fieldset>'."\n";
 5744:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5745:                 if (@{$requserfields{$provider}} > 0) {
 5746:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5747:                     foreach my $field (@{$requserfields{$provider}}) {
 5748:                         $datatable .= '<span class="LC_nobreak">'.
 5749:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5750:                                       $lt{$field}.'</label>';
 5751:                         if ($field eq 'user') {
 5752:                             my $seluserdom = '';
 5753:                             my $unseluserdom = ' selected="selected"';
 5754:                             if ($userincdom) {
 5755:                                 $seluserdom = $unseluserdom;
 5756:                                 $unseluserdom = '';
 5757:                             }
 5758:                             $datatable .= ':&nbsp;'.
 5759:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5760:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5761:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5762:                                 '</select>&nbsp;';
 5763:                         } else {
 5764:                             $datatable .= '&nbsp;';
 5765:                             if ($field eq 'roles') {
 5766:                                 $showroles = 1; 
 5767:                             } 
 5768:                         }
 5769:                         $datatable .= '</span> ';
 5770:                     }
 5771:                 }
 5772:                 $datatable .= '</fieldset>'."\n";
 5773:             }
 5774:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5775:                 if (@{$optuserfields{$provider}} > 0) {
 5776:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5777:                     foreach my $field (@{$optuserfields{$provider}}) {
 5778:                         my $checked;
 5779:                         if ($checkedfields{$field}) {
 5780:                             $checked = ' checked="checked"';
 5781:                         }
 5782:                         $datatable .= '<span class="LC_nobreak">'.
 5783:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5784:                     }
 5785:                     $datatable .= '</fieldset>'."\n";
 5786:                 }
 5787:             }
 5788:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5789:                 if (@{$defaults{$provider}}) {
 5790:                     my (%options,@selectboxes);
 5791:                     if (ref($extended{$provider}) eq 'HASH') {
 5792:                         %options = %{$extended{$provider}};
 5793:                     }
 5794:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5795:                     my ($rem,$numinrow,$dropdowns);
 5796:                     if ($provider eq 'proctorio') { 
 5797:                         $datatable .= '<table>';
 5798:                         $numinrow = 4;
 5799:                     }
 5800:                     my $i = 0;
 5801:                     foreach my $field (@{$defaults{$provider}}) {
 5802:                         my $checked;
 5803:                         if ($inuse{$field}) {
 5804:                             $checked = ' checked="checked"';
 5805:                         }
 5806:                         if ($provider eq 'examity') {
 5807:                             if ($field eq 'display') {
 5808:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5809:                                 foreach my $option ('iframe','tab','window') {
 5810:                                     my $checkdisp;
 5811:                                     if ($currentdef{'target'} eq $option) {
 5812:                                         $checkdisp = ' checked="checked"';
 5813:                                     }
 5814:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5815:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5816:                                 }
 5817:                                 $datatable .= ('&nbsp;'x4);
 5818:                                 foreach my $dimen ('width','height') {
 5819:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5820:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5821:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5822:                                                   ('&nbsp;'x2);
 5823:                                 }
 5824:                                 $datatable .= '</span><br />'.
 5825:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5826:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5827:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5828:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5829:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5830:                                               $currentdef{'explanation'}.
 5831:                                               '</textarea></div><div style=""></div><br />';
 5832:                             }
 5833:                         } else {
 5834:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5835:                                 my ($output,$selnone);
 5836:                                 unless ($checked) {
 5837:                                     $selnone = ' selected="selected"';
 5838:                                 }
 5839:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5840:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5841:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5842:                                 foreach my $option (@{$options{$field}}) {
 5843:                                     my $sel; 
 5844:                                     if ($inuse{$field} eq $option) {
 5845:                                         $sel = ' selected="selected"';
 5846:                                     }
 5847:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5848:                                 }
 5849:                                 $output .= '</select></span>';
 5850:                                 push(@selectboxes,$output);
 5851:                             } else {
 5852:                                 $rem = $i%($numinrow);
 5853:                                 if ($rem == 0) {
 5854:                                     if ($i > 0) {
 5855:                                         $datatable .= '</tr>';
 5856:                                     }
 5857:                                     $datatable .= '<tr>';
 5858:                                 }
 5859:                                 $datatable .= '<td class="LC_left_item">'.
 5860:                                               '<span class="LC_nobreak">'.
 5861:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5862:                                               $fieldtitles{$field}.'</label></span></td>';
 5863:                                 $i++;
 5864:                             }
 5865:                         }
 5866:                     }
 5867:                     if ($provider eq 'proctorio') {
 5868:                         if ($numinrow) {
 5869:                             $rem = $i%$numinrow;
 5870:                         }
 5871:                         my $colsleft = $numinrow - $rem;
 5872:                         if ($colsleft > 1) {
 5873:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5874:                         } else {
 5875:                             $datatable .= '<td class="LC_left_item">';
 5876:                         }
 5877:                         $datatable .= '&nbsp;'.
 5878:                                       '</td></tr></table>';
 5879:                         if (@selectboxes) {
 5880:                             $datatable .= '<hr /><table>';
 5881:                             $numinrow = 2;
 5882:                             for (my $i=0; $i<@selectboxes; $i++) {
 5883:                                 $rem = $i%($numinrow);
 5884:                                 if ($rem == 0) {
 5885:                                     if ($i > 0) {
 5886:                                         $datatable .= '</tr>';
 5887:                                     }
 5888:                                     $datatable .= '<tr>';
 5889:                                 }
 5890:                                 $datatable .= '<td class="LC_left_item">'.
 5891:                                               $selectboxes[$i].'</td>';
 5892:                             }
 5893:                             if ($numinrow) {
 5894:                                 $rem = $i%$numinrow;
 5895:                             }
 5896:                             $colsleft = $numinrow - $rem;
 5897:                             if ($colsleft > 1) {
 5898:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5899:                             } else {
 5900:                                 $datatable .= '<td class="LC_left_item">';
 5901:                             }
 5902:                             $datatable .= '&nbsp;'.
 5903:                                           '</td></tr></table>';
 5904:                         }
 5905:                     }
 5906:                     $datatable .= '</fieldset>';
 5907:                 }
 5908:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5909:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5910:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5911:                     my ($rem,$numinrow);
 5912:                     if ($provider eq 'proctorio') {
 5913:                         $datatable .= '<table>';
 5914:                         $numinrow = 4;
 5915:                     }
 5916:                     my $i = 0;
 5917:                     foreach my $item (@{$crsconf{$provider}}) {
 5918:                         my $name;
 5919:                         if ($provider eq 'examity') {
 5920:                             $name = $lt{'crs'.$item};
 5921:                         } elsif ($provider eq 'proctorio') {
 5922:                             $name = $fieldtitles{$item};
 5923:                             $rem = $i%($numinrow);
 5924:                             if ($rem == 0) {
 5925:                                 if ($i > 0) {
 5926:                                     $datatable .= '</tr>';
 5927:                                 }
 5928:                                 $datatable .= '<tr>';
 5929:                             }
 5930:                             $datatable .= '<td class="LC_left_item>';
 5931:                         }
 5932:                         my $checked;
 5933:                         if ($crsconfig{$item}) {
 5934:                             $checked = ' checked="checked"';
 5935:                         }
 5936:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5937:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5938:                                       $name.'</label></span>';
 5939:                         if ($provider eq 'examity') {
 5940:                             $datatable .= '&nbsp; ';
 5941:                         }
 5942:                         $datatable .= "\n";
 5943:                         $i++;
 5944:                     }
 5945:                     if ($provider eq 'proctorio') {
 5946:                         if ($numinrow) {
 5947:                             $rem = $i%$numinrow;
 5948:                         }
 5949:                         my $colsleft = $numinrow - $rem;
 5950:                         if ($colsleft > 1) {
 5951:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5952:                         } else {
 5953:                             $datatable .= '<td class="LC_left_item">';
 5954:                         }
 5955:                         $datatable .= '&nbsp;'.
 5956:                                       '</td></tr></table>';
 5957:                     }
 5958:                     $datatable .= '</fieldset>';
 5959:                 }
 5960:                 if ($showroles) {
 5961:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5962:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5963:                     foreach my $role (@courseroles) {
 5964:                         my ($selected,$selectnone);
 5965:                         if (!$rolemaps{$role}) {
 5966:                             $selectnone = ' selected="selected"';
 5967:                         }
 5968:                         $datatable .= '<td style="text-align: center">'.
 5969:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5970:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5971:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5972:                         foreach my $ltirole (@ltiroles) {
 5973:                             unless ($selectnone) {
 5974:                                 if ($rolemaps{$role} eq $ltirole) {
 5975:                                     $selected = ' selected="selected"';
 5976:                                 } else {
 5977:                                     $selected = '';
 5978:                                 }
 5979:                             }
 5980:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5981:                         }
 5982:                         $datatable .= '</select></td>';
 5983:                     }
 5984:                     $datatable .= '</tr></table></fieldset>'.
 5985:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5986:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5987:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5988:                                   '<tr><td></td><td>lms</td>'.
 5989:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5990:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5991:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5992:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5993:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5994:                         if (keys(%custom) > 0) {
 5995:                             foreach my $key (sort(keys(%custom))) {
 5996:                                 next if ($key eq 'lms');
 5997:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5998:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5999:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6000:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6001:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6002:                             }
 6003:                         }
 6004:                     }
 6005:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6006:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6007:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6008:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6009:                                   '</table></fieldset></td></tr>'."\n";
 6010:                 }
 6011:                 $datatable .= '</td></tr>';
 6012:             }
 6013:             $itemcount ++;
 6014:         }
 6015:     }
 6016:     return $datatable;
 6017: }
 6018: 
 6019: sub proctoring_data {
 6020:     my $requserfields = {
 6021:                       proctorio => ['user'],
 6022:                       examity   => ['roles','user'],
 6023:                      };
 6024:     my $optuserfields = {
 6025:                         proctorio => ['fullname'],
 6026:                         examity   => ['fullname','firstname','lastname','email'],
 6027:                      };
 6028:     my $defaults = {
 6029:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6030:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6031:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6032:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6033:                                 'reentry','calculator','whiteboard'],
 6034:                   examity   => ['display'],
 6035:                 };
 6036:     my $extended = { 
 6037:                   proctorio => {
 6038:                                  verifyid => ['verifyidauto','verifyidlive'],
 6039:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6040:                                  tabslinks => ['notabs','linksonly'],
 6041:                                  reentry => ['noreentry','agentreentry'],
 6042:                                  calculator => ['calculatorbasic','calculatorsci'],
 6043:                                },
 6044:                   examity => {
 6045:                                display => {
 6046:                                            target      => ['iframe','tab','window'],
 6047:                                            width       => '',
 6048:                                            height      => '',
 6049:                                            linktext    => '',
 6050:                                            explanation => '',
 6051:                                           },
 6052:                              },
 6053:                 };
 6054:     my $crsconf = {
 6055:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6056:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6057:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6058:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6059:                                'reentry','calculator','whiteboard'],
 6060:                  examity => ['label','title','target','linktext','explanation','append'],
 6061:                };
 6062:     my $courseroles = ['cc','in','ta','ep','st'];
 6063:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6064:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6065: }
 6066: 
 6067: sub proctoring_titles {
 6068:     my ($item) = @_;
 6069:     my (%common_lt,%custom_lt);
 6070:     %common_lt = &Apache::lonlocal::texthash (
 6071:                      'avai'      => 'Available?',
 6072:                      'base'      => 'Basic Settings',
 6073:                      'requ'      => 'User data required to be sent on launch',
 6074:                      'optu'      => 'User data optionally sent on launch',
 6075:                      'udsl'      => 'User data sent on launch',
 6076:                      'defa'      => 'Defaults for items configurable in course',
 6077:                      'sigmethod' => 'Signature Method',
 6078:                      'key'       => 'Key',
 6079:                      'lifetime'  => 'Nonce lifetime (s)',
 6080:                      'secret'    => 'Secret',
 6081:                      'icon'      => 'Icon',
 6082:                      'fullname'  => 'Full Name',
 6083:                      'visible'   => 'Visible input',
 6084:                      'username'  => 'username',
 6085:                      'user'      => 'User',
 6086:                  );
 6087:     if ($item eq 'proctorio') {
 6088:         %custom_lt = &Apache::lonlocal::texthash (
 6089:                          'version'        => 'OAuth version',
 6090:                          'url'            => 'API URL',
 6091:                          'uname:dom'      => 'username-domain',
 6092:         );
 6093:     } elsif ($item eq 'examity') {
 6094:         %custom_lt = &Apache::lonlocal::texthash (
 6095:                          'version'        => 'LTI Version',
 6096:                          'url'            => 'URL',
 6097:                          'uname:dom'      => 'username:domain',
 6098:                          'msgtype'        => 'Message Type',
 6099:                          'firstname'      => 'First Name',
 6100:                          'lastname'       => 'Last Name',
 6101:                          'email'          => 'E-mail',
 6102:                          'roles'          => 'Role',
 6103:                          'crstarget'      => 'Display target',
 6104:                          'crslabel'       => 'Course label',
 6105:                          'crstitle'       => 'Course title', 
 6106:                          'crslinktext'    => 'Link Text',
 6107:                          'crsexplanation' => 'Explanation',
 6108:                          'crsappend'      => 'Provider URL',
 6109:         );
 6110:     }
 6111:     my %lt = (%common_lt,%custom_lt);
 6112:     return %lt;
 6113: }
 6114: 
 6115: sub proctoring_fieldtitles {
 6116:     my ($item) = @_;
 6117:     if ($item eq 'proctorio') {
 6118:         return &Apache::lonlocal::texthash (
 6119:                   'recordvideo' => 'Record video',
 6120:                   'recordaudio' => 'Record audio',
 6121:                   'recordscreen' => 'Record screen',
 6122:                   'recordwebtraffic' => 'Record web traffic',
 6123:                   'recordroomstart' => 'Record room scan',
 6124:                   'verifyvideo' => 'Verify webcam',
 6125:                   'verifyaudio' => 'Verify microphone',
 6126:                   'verifydesktop' => 'Verify desktop recording',
 6127:                   'verifyid' => 'Photo ID verification',
 6128:                   'verifysignature' => 'Require signature',
 6129:                   'fullscreen' => 'Fullscreen',
 6130:                   'clipboard' => 'Disable copy/paste',
 6131:                   'tabslinks' => 'New tabs/windows',
 6132:                   'closetabs' => 'Close other tabs',
 6133:                   'onescreen' => 'Limit to single screen',
 6134:                   'print' => 'Disable Printing',
 6135:                   'downloads' => 'Disable Downloads',
 6136:                   'cache' => 'Empty cache after exam',
 6137:                   'rightclick' => 'Disable right click',
 6138:                   'reentry' => 'Re-entry to exam',
 6139:                   'calculator' => 'Onscreen calculator',
 6140:                   'whiteboard' => 'Onscreen whiteboard',
 6141:                   'verifyidauto' => 'Automated verification',
 6142:                   'verifyidlive' => 'Live agent verification',
 6143:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6144:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6145:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6146:                   'notabs' => 'Disaallowed',
 6147:                   'linksonly' => 'Allowed from links in exam',
 6148:                   'noreentry' => 'Disallowed',
 6149:                   'agentreentry' => 'Agent required for re-entry',
 6150:                   'calculatorbasic' => 'Basic',
 6151:                   'calculatorsci' => 'Scientific',
 6152:         );
 6153:     } elsif ($item eq 'examity') {
 6154:         return &Apache::lonlocal::texthash (
 6155:                 'target'         => 'Display target',   
 6156:                 'window'         => 'Window',
 6157:                 'tab'            => 'Tab',
 6158:                 'iframe'         => 'iFrame',
 6159:                 'height'         => 'Height (pixels)',
 6160:                 'width'          => 'Width (pixels)',
 6161:                 'linktext'       => 'Default Link Text',
 6162:                 'explanation'    => 'Default Explanation',
 6163:                 'append'         => 'Provider URL',
 6164:         );
 6165:     }
 6166: }
 6167: 
 6168: sub proctoring_providernames {
 6169:     return (
 6170:              proctorio => 'Proctorio',
 6171:              examity   => 'Examity',
 6172:            );
 6173: }
 6174: 
 6175: sub print_lti {
 6176:     my ($position,$dom,$settings,$rowtotal) = @_;
 6177:     my $itemcount = 1;
 6178:     my ($datatable,$css_class);
 6179:     my (%rules,%encrypt,%privkeys,%linkprot);
 6180:     if (ref($settings) eq 'HASH') {
 6181:         if ($position eq 'top') {
 6182:             if (exists($settings->{'encrypt'})) {
 6183:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6184:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6185:                         if ($key eq 'consumers') {
 6186:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6187:                         } else {
 6188:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6189:                         }
 6190:                     }
 6191:                 }
 6192:             }
 6193:             if (exists($settings->{'private'})) {
 6194:                 if (ref($settings->{'private'}) eq 'HASH') {
 6195:                     if (ref($settings->{'private'}) eq 'HASH') {
 6196:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6197:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6198:                         }
 6199:                     }
 6200:                 }
 6201:             }
 6202:         } elsif ($position eq 'middle') {
 6203:             if (exists($settings->{'rules'})) {
 6204:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6205:                     %rules = %{$settings->{'rules'}};
 6206:                 }
 6207:             }
 6208:         } elsif ($position eq 'lower') {
 6209:             if (exists($settings->{'linkprot'})) {
 6210:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6211:                     %linkprot = %{$settings->{'linkprot'}};
 6212:                     if ($linkprot{'lock'}) {
 6213:                         delete($linkprot{'lock'});
 6214:                     }
 6215:                 }
 6216:             }
 6217:         } else {
 6218:             foreach my $key ('encrypt','private','rules','linkprot') {
 6219:                 if (exists($settings->{$key})) {
 6220:                     delete($settings->{$key});
 6221:                 }
 6222:             }
 6223:         }
 6224:     }
 6225:     if ($position eq 'top') {
 6226:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6227:     } elsif ($position eq 'middle') {
 6228:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6229:         $$rowtotal += $itemcount;
 6230:     } elsif ($position eq 'lower') {
 6231:          $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6232:     } else {
 6233:         my ($switchserver,$switchmessage);
 6234:         $switchserver = &check_switchserver($dom);
 6235:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6236:         my $maxnum = 0;
 6237:         my %ordered;
 6238:         if (ref($settings) eq 'HASH') {
 6239:             foreach my $item (keys(%{$settings})) {
 6240:                 if (ref($settings->{$item}) eq 'HASH') {
 6241:                     my $num = $settings->{$item}{'order'};
 6242:                     if ($num eq '') {
 6243:                         $num = scalar(keys(%{$settings}));
 6244:                     }
 6245:                     $ordered{$num} = $item;
 6246:                 }
 6247:             }
 6248:         }
 6249:         $maxnum = scalar(keys(%ordered));
 6250:         my %lt = &lti_names();
 6251:         if (keys(%ordered)) {
 6252:             my @items = sort { $a <=> $b } keys(%ordered);
 6253:             for (my $i=0; $i<@items; $i++) {
 6254:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6255:                 my $item = $ordered{$items[$i]};
 6256:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6257:                 if (ref($settings->{$item}) eq 'HASH') {
 6258:                     $key = $settings->{$item}->{'key'};
 6259:                     $usable = $settings->{$item}->{'usable'};
 6260:                     $lifetime = $settings->{$item}->{'lifetime'};
 6261:                     $consumer = $settings->{$item}->{'consumer'};
 6262:                     $requser = $settings->{$item}->{'requser'};
 6263:                     $crsinc = $settings->{$item}->{'crsinc'};
 6264:                     $current = $settings->{$item};
 6265:                 }
 6266:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6267:                 my %checkedrequser = (
 6268:                                        yes => ' checked="checked"',
 6269:                                        no  => '',
 6270:                                      );
 6271:                 if (!$requser) {
 6272:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6273:                     $checkedrequser{'yes'} = '';
 6274:                 }
 6275:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6276:                 my %checkedcrsinc = (
 6277:                                       yes => ' checked="checked"',
 6278:                                       no  => '',
 6279:                                     );
 6280:                 if (!$crsinc) {
 6281:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6282:                     $checkedcrsinc{'yes'} = '';
 6283:                 }
 6284:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6285:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6286:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6287:                 for (my $k=0; $k<=$maxnum; $k++) {
 6288:                     my $vpos = $k+1;
 6289:                     my $selstr;
 6290:                     if ($k == $i) {
 6291:                         $selstr = ' selected="selected" ';
 6292:                     }
 6293:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6294:                 }
 6295:                 $datatable .= '</select>'.('&nbsp;'x2).
 6296:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6297:                     &mt('Delete?').'</label></span></td>'.
 6298:                     '<td colspan="2">'.
 6299:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6300:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6301:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6302:                     ('&nbsp;'x2).
 6303:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6304:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6305:                     ('&nbsp;'x2).
 6306:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6307:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6308:                 if ($key ne '') {
 6309:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6310:                     if ($switchserver) {
 6311:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6312:                     } else {
 6313:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6314:                     }
 6315:                     $datatable .= '</span> '.('&nbsp;'x2);
 6316:                 } elsif (!$switchserver) {
 6317:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6318:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6319:                                   '</span> '.('&nbsp;'x2);
 6320:                 }
 6321:                 if ($switchserver) {
 6322:                     if ($usable ne '') {
 6323:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6324:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6325:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6326:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6327:                                       ('&nbsp;'x2).
 6328:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6329:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6330:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6331:                                       '</div>';
 6332:                     } elsif ($key eq '') {
 6333:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6334:                     } else {
 6335:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6336:                     }
 6337:                 } else {
 6338:                     if ($usable ne '') {
 6339:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6340:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6341:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6342:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6343:                                       ('&nbsp;'x2).
 6344:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6345:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6346:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6347:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6348:                                       '<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>';
 6349:                     } else {
 6350:                         $datatable .=
 6351:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6352:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6353:                             '<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>';
 6354:                     }
 6355:                 }
 6356:                 $datatable .= '<br /><br />'.
 6357:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6358:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6359:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6360:                     '<br /><br />'.
 6361:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6362:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6363:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6364:                     ('&nbsp;'x4).
 6365:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6366:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6367:                 $itemcount ++;
 6368:             }
 6369:         }
 6370:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6371:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6372:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6373:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6374:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6375:         for (my $k=0; $k<$maxnum+1; $k++) {
 6376:             my $vpos = $k+1;
 6377:             my $selstr;
 6378:             if ($k == $maxnum) {
 6379:                 $selstr = ' selected="selected" ';
 6380:             }
 6381:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6382:         }
 6383:         $datatable .= '</select>&nbsp;'."\n".
 6384:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6385:                       '<td colspan="2">'.
 6386:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6387:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6388:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6389:                       ('&nbsp;'x2).
 6390:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6391:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6392:                       ('&nbsp;'x2).
 6393:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6394:         if ($switchserver) {
 6395:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6396:         } else {
 6397:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6398:                           ('&nbsp;'x2).
 6399:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6400:                           '<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";
 6401:         }
 6402:         $datatable .= '<br /><br />'.
 6403:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6404:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6405:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6406:                       '<br /><br />'.
 6407:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6408:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6409:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6410:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6411:                       '</td>'."\n".
 6412:                       '</tr>'."\n";
 6413:         $itemcount ++;
 6414:     }
 6415:     $$rowtotal += $itemcount;
 6416:     return $datatable;
 6417: }
 6418: 
 6419: sub lti_names {
 6420:     my %lt = &Apache::lonlocal::texthash(
 6421:                                           'version'   => 'LTI Version',
 6422:                                           'url'       => 'URL',
 6423:                                           'key'       => 'Key',
 6424:                                           'lifetime'  => 'Nonce lifetime (s)',
 6425:                                           'consumer'  => 'Consumer',
 6426:                                           'secret'    => 'Secret',
 6427:                                           'requser'   => "User's identity sent",
 6428:                                           'crsinc'    => "Course's identity sent",
 6429:                                           'email'     => 'Email address',
 6430:                                           'sourcedid' => 'User ID',
 6431:                                           'other'     => 'Other',
 6432:                                           'passback'  => 'Can return grades to Consumer:',
 6433:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6434:                                           'topmenu'   => 'Display LON-CAPA page header',
 6435:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6436:                                         );
 6437:     return %lt;
 6438: }
 6439: 
 6440: sub lti_options {
 6441:     my ($num,$current,$itemcount,%lt) = @_;
 6442:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6443:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6444:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6445:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6446:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6447:     $checked{'mapcrstype'} = {};
 6448:     $checked{'makeuser'} = {};
 6449:     $checked{'selfenroll'} = {};
 6450:     $checked{'crssec'} = {};
 6451:     $checked{'crssecsrc'} = {};
 6452:     $checked{'lcauth'} = {};
 6453:     $checked{'menuitem'} = {};
 6454:     if ($num eq 'add') {
 6455:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6456:     }
 6457:     my $userfieldsty = 'none';
 6458:     my $crsfieldsty = 'none';
 6459:     my $crssecfieldsty = 'none';
 6460:     my $secsrcfieldsty = 'none';
 6461:     my $callbacksty = 'none';
 6462:     my $passbacksty = 'none';
 6463:     my $optionsty = 'block';
 6464:     my $crssty = 'block';
 6465:     my $lcauthparm;
 6466:     my $lcauthparmstyle = 'display:none';
 6467:     my $lcauthparmtext;
 6468:     my $menusty;
 6469:     my $numinrow = 4;
 6470:     my %menutitles = &ltimenu_titles();
 6471: 
 6472:     if (ref($current) eq 'HASH') {
 6473:         if (!$current->{'requser'}) {
 6474:             $optionsty = 'none';
 6475:             $crssty = 'none';
 6476:         } elsif (!$current->{'crsinc'}) {
 6477:             $crssty = 'none';
 6478:         }
 6479:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6480:             $checked{'mapuser'}{'sourcedid'} = '';
 6481:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6482:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6483:             } else {
 6484:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6485:                 $userfield = $current->{'mapuser'};
 6486:                 $userfieldsty = 'inline-block';
 6487:             }
 6488:         }
 6489:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6490:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6491:             if ($current->{'mapcrs'} eq 'context_id') {
 6492:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6493:             } else {
 6494:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6495:                 $cidfield = $current->{'mapcrs'};
 6496:                 $crsfieldsty = 'inline-block';
 6497:             }
 6498:         }
 6499:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6500:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6501:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6502:             }
 6503:         }
 6504:         if (!$current->{'storecrs'}) {
 6505:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6506:             $checked{'storecrs'}{'Y'} = '';
 6507:         }
 6508:         if ($current->{'makecrs'}) {
 6509:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6510:         }
 6511:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6512:             foreach my $role (@{$current->{'makeuser'}}) {
 6513:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6514:             }
 6515:         }
 6516:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6517:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6518:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6519:                 $lcauthparm = $current->{'lcauthparm'};
 6520:                 $lcauthparmstyle = 'display:table-row';
 6521:                 if ($current->{'lcauth'} eq 'localauth') {
 6522:                     $lcauthparmtext = &mt('Local auth argument');
 6523:                 } else {
 6524:                     $lcauthparmtext = &mt('Kerberos domain');
 6525:                 }
 6526:             }
 6527:         }
 6528:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6529:             foreach my $role (@{$current->{'selfenroll'}}) {
 6530:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6531:             }
 6532:         }
 6533:         if (ref($current->{'maproles'}) eq 'HASH') {
 6534:             %rolemaps = %{$current->{'maproles'}};
 6535:         }
 6536:         if ($current->{'section'} ne '') {
 6537:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6538:             $crssecfieldsty = 'inline-block';
 6539:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6540:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6541:             } else {
 6542:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6543:                 $crssecsrc = $current->{'section'};
 6544:                 $secsrcfieldsty = 'inline-block';
 6545:             }
 6546:         } else {
 6547:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6548:         }
 6549:         if ($current->{'callback'} ne '') {
 6550:             $callback = $current->{'callback'};
 6551:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6552:             $callbacksty = 'inline-block';
 6553:         } else {
 6554:             $checked{'callback'}{'N'} = ' checked="checked"';
 6555:         }
 6556:         if ($current->{'topmenu'}) {
 6557:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6558:         } else {
 6559:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6560:         }
 6561:         if ($current->{'inlinemenu'}) {
 6562:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6563:         } else {
 6564:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6565:         }
 6566:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6567:             $menusty = 'inline-block';
 6568:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6569:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6570:                     if (exists($menutitles{$item})) {
 6571:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6572:                     }
 6573:                 }
 6574:             }
 6575:         } else {
 6576:             $menusty = 'none';
 6577:         }
 6578:     } else {
 6579:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6580:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6581:         $checked{'callback'}{'N'} = ' checked="checked"';
 6582:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6583:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6584:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6585:         $menusty = 'inline-block';
 6586:     }
 6587:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6588:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6589:                                official   => 'Official',
 6590:                                unofficial => 'Unofficial',
 6591:                                community  => 'Community',
 6592:                                textbook   => 'Textbook',
 6593:                                placement  => 'Placement Test',
 6594:                                lti        => 'LTI Provider',
 6595:     );
 6596:     my @authtypes = ('internal','krb4','krb5','localauth');
 6597:     my %shortauth = (
 6598:                      internal => 'int',
 6599:                      krb4 => 'krb4',
 6600:                      krb5 => 'krb5',
 6601:                      localauth  => 'loc'
 6602:                     );
 6603:     my %authnames = &authtype_names();
 6604:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6605:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6606:     my @courseroles = ('cc','in','ta','ep','st');
 6607:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6608:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6609:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6610:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6611:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6612:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6613:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6614:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6615:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6616:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6617:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6618:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6619:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6620:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6621:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6622:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6623:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6624:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6625:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6626:     foreach my $option ('sourcedid','email','other') {
 6627:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6628:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6629:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6630:     }
 6631:     $output .= '</span></div>'.
 6632:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6633:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6634:                'value="'.$userfield.'" /></div></fieldset>'.
 6635:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6636:     foreach my $ltirole (@ltiroles) {
 6637:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6638:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6639:     }
 6640:     $output .= '</fieldset>'.
 6641:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6642:                '<table>'.
 6643:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6644:                '</table>'.
 6645:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6646:                '<td class="LC_left_item">';
 6647:     foreach my $auth ('lti',@authtypes) {
 6648:         my $authtext;
 6649:         if ($auth eq 'lti') {
 6650:             $authtext = &mt('None');
 6651:         } else {
 6652:             $authtext = $authnames{$shortauth{$auth}};
 6653:         }
 6654:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6655:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6656:                    $authtext.'</label></span> &nbsp;';
 6657:     }
 6658:     $output .= '</td></tr>'.
 6659:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6660:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6661:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6662:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6663:                '</table></fieldset>'.
 6664:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6665:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6666:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6667:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6668:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6669:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6670:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6671:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6672:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6673:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6674:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6675:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6676:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6677:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6678:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6679:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6680:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6681:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6682:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6683:                    ('&nbsp;'x2);
 6684:     }
 6685:     $output .= '</span></div></fieldset>'.
 6686:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6687:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6688:                &mt('Unique course identifier').':&nbsp;';
 6689:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6690:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6691:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6692:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6693:     }
 6694:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6695:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6696:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6697:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6698:     foreach my $type (@coursetypes) {
 6699:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6700:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6701:                    ('&nbsp;'x2);
 6702:     }
 6703:     $output .= '</span><br /><br />'.
 6704:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6705:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6706:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6707:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6708:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6709:                '</fieldset>'.
 6710:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6711:     foreach my $ltirole (@lticourseroles) {
 6712:         my ($selected,$selectnone);
 6713:         if ($rolemaps{$ltirole} eq '') {
 6714:             $selectnone = ' selected="selected"';
 6715:         }
 6716:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6717:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6718:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6719:         foreach my $role (@courseroles) {
 6720:             unless ($selectnone) {
 6721:                 if ($rolemaps{$ltirole} eq $role) {
 6722:                     $selected = ' selected="selected"';
 6723:                 } else {
 6724:                     $selected = '';
 6725:                 }
 6726:             }
 6727:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6728:                        &Apache::lonnet::plaintext($role,'Course').
 6729:                        '</option>';
 6730:         }
 6731:         $output .= '</select></td>';
 6732:     }
 6733:     $output .= '</tr></table></fieldset>'.
 6734:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6735:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6736:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6737:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6738:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6739:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6740:                '</fieldset>'.
 6741:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6742:     foreach my $lticrsrole (@lticourseroles) {
 6743:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6744:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6745:     }
 6746:     $output .= '</fieldset>'.
 6747:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6748:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6749:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6750:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6751:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6752:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6753:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6754:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6755:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6756:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6757:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6758:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6759:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6760:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6761:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6762:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6763:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6764:     foreach my $extra ('roster','passback') {
 6765:         my $checkedon = '';
 6766:         my $checkedoff = ' checked="checked"';
 6767:         if ($extra eq 'passback') {
 6768:             $pb1p1chk = ' checked="checked"';
 6769:             $pb1p0chk = '';
 6770:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6771:         } else {
 6772:             $onclickpb = '';
 6773:         }
 6774:         if (ref($current) eq 'HASH') {
 6775:             if (($current->{$extra})) {
 6776:                 $checkedon = $checkedoff;
 6777:                 $checkedoff = '';
 6778:                 if ($extra eq 'passback') {
 6779:                     $passbacksty = 'inline-block';
 6780:                 }
 6781:                 if ($current->{'passbackformat'} eq '1.0') {
 6782:                     $pb1p0chk =  ' checked="checked"';
 6783:                     $pb1p1chk = '';
 6784:                 }
 6785:             }
 6786:         }
 6787:         $output .= $lt{$extra}.'&nbsp;'.
 6788:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6789:                    &mt('No').'</label>'.('&nbsp;'x2).
 6790:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6791:                    &mt('Yes').'</label><br />';
 6792:     }
 6793:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6794:                '<span class="LC_nobreak">'.&mt('Grade format').
 6795:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6796:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6797:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6798:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6799:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6800:     $output .= '</span></div></fieldset>';
 6801: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6802: #
 6803: #    $output .= '</fieldset>'.
 6804: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6805:     return $output;
 6806: }
 6807: 
 6808: sub ltimenu_titles {
 6809:     return &Apache::lonlocal::texthash(
 6810:                                         fullname    => 'Full name',
 6811:                                         coursetitle => 'Course title',
 6812:                                         role        => 'Role',
 6813:                                         logout      => 'Logout',
 6814:                                         grades      => 'Grades',
 6815:     );
 6816: }
 6817: 
 6818: sub print_coursedefaults {
 6819:     my ($position,$dom,$settings,$rowtotal) = @_;
 6820:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6821:     my $itemcount = 1;
 6822:     my %choices =  &Apache::lonlocal::texthash (
 6823:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6824:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6825:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course',
 6826:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6827:         coursecredits        => 'Credits can be specified for courses',
 6828:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6829:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6830:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6831:         texengine            => 'Default method to display mathematics',
 6832:         postsubmit           => 'Disable submit button/keypress following student submission',
 6833:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6834:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6835:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6836:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6837:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6838:     );
 6839:     my %staticdefaults = (
 6840:                            anonsurvey_threshold => 10,
 6841:                            uploadquota          => 500,
 6842:                            coursequota          => 20,
 6843:                            postsubmit           => 60,
 6844:                            mysqltables          => 172800,
 6845:                            domexttool           => 1,
 6846:                            exttool              => 0,
 6847:                          );
 6848:     if ($position eq 'top') {
 6849:         %defaultchecked = (
 6850:                             'canuse_pdfforms' => 'off',
 6851:                             'uselcmath'       => 'on',
 6852:                             'usejsme'         => 'on',
 6853:                             'inline_chem'     => 'on',
 6854:                             'canclone'        => 'none',
 6855:                           );
 6856:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6857:         my $deftex = $Apache::lonnet::deftex;
 6858:         if (ref($settings) eq 'HASH') {
 6859:             if ($settings->{'texengine'}) {
 6860:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6861:                     $deftex = $settings->{'texengine'};
 6862:                 }
 6863:             }
 6864:         }
 6865:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6866:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6867:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6868:                        '</span></td><td class="LC_right_item">'.
 6869:                        '<select name="texengine">'."\n";
 6870:         my %texoptions = (
 6871:                             MathJax  => 'MathJax',
 6872:                             mimetex  => &mt('Convert to Images'),
 6873:                             tth      => &mt('TeX to HTML'),
 6874:                          );
 6875:         foreach my $renderer ('MathJax','mimetex','tth') {
 6876:             my $selected = '';
 6877:             if ($renderer eq $deftex) {
 6878:                 $selected = ' selected="selected"';
 6879:             }
 6880:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6881:         }
 6882:         $mathdisp .= '</select></td></tr>'."\n";
 6883:         $itemcount ++;
 6884:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6885:                                                      \%choices,$itemcount);
 6886:         $datatable = $mathdisp.$datatable;
 6887:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6888:         $datatable .=
 6889:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6890:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6891:             '</span></td><td class="LC_left_item">';
 6892:         my $currcanclone = 'none';
 6893:         my $onclick;
 6894:         my @cloneoptions = ('none','domain');
 6895:         my %clonetitles = &Apache::lonlocal::texthash (
 6896:                              none     => 'No additional course requesters',
 6897:                              domain   => "Any course requester in course's domain",
 6898:                              instcode => 'Course requests for official courses ...',
 6899:                           );
 6900:         my (%codedefaults,@code_order,@posscodes);
 6901:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6902:                                                     \@code_order) eq 'ok') {
 6903:             if (@code_order > 0) {
 6904:                 push(@cloneoptions,'instcode');
 6905:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6906:             }
 6907:         }
 6908:         if (ref($settings) eq 'HASH') {
 6909:             if ($settings->{'canclone'}) {
 6910:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6911:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6912:                         if (@code_order > 0) {
 6913:                             $currcanclone = 'instcode';
 6914:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6915:                         }
 6916:                     }
 6917:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6918:                     $currcanclone = $settings->{'canclone'};
 6919:                 }
 6920:             }
 6921:         }
 6922:         foreach my $option (@cloneoptions) {
 6923:             my ($checked,$additional);
 6924:             if ($currcanclone eq $option) {
 6925:                 $checked = ' checked="checked"';
 6926:             }
 6927:             if ($option eq 'instcode') {
 6928:                 if (@code_order) {
 6929:                     my $show = 'none';
 6930:                     if ($checked) {
 6931:                         $show = 'block';
 6932:                     }
 6933:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6934:                                   &mt('Institutional codes for new and cloned course have identical:').
 6935:                                   '<br />';
 6936:                     foreach my $item (@code_order) {
 6937:                         my $codechk;
 6938:                         if ($checked) {
 6939:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6940:                                 $codechk = ' checked="checked"';
 6941:                             }
 6942:                         }
 6943:                         $additional .= '<label>'.
 6944:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6945:                                        $item.'</label>';
 6946:                     }
 6947:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6948:                 }
 6949:             }
 6950:             $datatable .=
 6951:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6952:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6953:                 '</label>&nbsp;'.$additional.'</span><br />';
 6954:         }
 6955:         $datatable .= '</td>'.
 6956:                       '</tr>';
 6957:         $itemcount ++;
 6958:     } else {
 6959:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6960:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 6961:             %deftimeout,%currmysql);
 6962:         my $currusecredits = 0;
 6963:         my $postsubmitclient = 1;
 6964:         my $ltiauth = 0;
 6965:         my %domexttool;
 6966:         my %exttool;
 6967:         my @types = ('official','unofficial','community','textbook','placement');
 6968:         if (ref($settings) eq 'HASH') {
 6969:             if ($settings->{'ltiauth'}) {
 6970:                 $ltiauth = 1;
 6971:             }
 6972:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 6973:                 foreach my $type (@types) {
 6974:                     if ($settings->{'domexttool'}->{$type}) {
 6975:                         $domexttool{$type} = ' checked="checked"';
 6976:                     }
 6977:                 }
 6978:             } else {
 6979:                 foreach my $type (@types) {
 6980:                     if ($staticdefaults{'domexttool'}) {
 6981:                         $domexttool{$type} = ' checked="checked"';
 6982:                     }
 6983:                 }
 6984:             }
 6985:             if (ref($settings->{'exttool'}) eq 'HASH') {
 6986:                 foreach my $type (@types) {
 6987:                     if ($settings->{'exttool'}->{$type}) {
 6988:                         $exttool{$type} = ' checked="checked"';
 6989:                     }
 6990:                 }
 6991:             }
 6992:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6993:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6994:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6995:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6996:                 }
 6997:             }
 6998:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 6999:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 7000:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 7001:                 }
 7002:             }
 7003:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 7004:                 foreach my $type (@types) {
 7005:                     next if ($type eq 'community');
 7006:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 7007:                     if ($defcredits{$type} ne '') {
 7008:                         $currusecredits = 1;
 7009:                     }
 7010:                 }
 7011:             }
 7012:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 7013:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 7014:                     $postsubmitclient = 0;
 7015:                     foreach my $type (@types) {
 7016:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7017:                     }
 7018:                 } else {
 7019:                     foreach my $type (@types) {
 7020:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 7021:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 7022:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 7023:                             } else {
 7024:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7025:                             }
 7026:                         } else {
 7027:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7028:                         }
 7029:                     }
 7030:                 }
 7031:             } else {
 7032:                 foreach my $type (@types) {
 7033:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7034:                 }
 7035:             }
 7036:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 7037:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 7038:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 7039:                 }
 7040:             } else {
 7041:                 foreach my $type (@types) {
 7042:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7043:                 }
 7044:             }
 7045:         } else {
 7046:             foreach my $type (@types) {
 7047:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7048:                 if ($staticdefaults{'domexttool'}) {
 7049:                     $domexttool{$type} = ' checked="checked"';
 7050:                 }
 7051:             }
 7052:         }
 7053:         if (!$currdefresponder) {
 7054:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7055:         } elsif ($currdefresponder < 1) {
 7056:             $currdefresponder = 1;
 7057:         }
 7058:         foreach my $type (@types) {
 7059:             if ($curruploadquota{$type} eq '') {
 7060:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7061:             }
 7062:             if ($currcoursequota{$type} eq '') {
 7063:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 7064:             }
 7065:         }
 7066:         $datatable .=
 7067:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7068:                 $choices{'anonsurvey_threshold'}.
 7069:                 '</span></td>'.
 7070:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7071:                 '<input type="text" name="anonsurvey_threshold"'.
 7072:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7073:                 '</td></tr>'."\n";
 7074:         $itemcount ++;
 7075:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7076:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7077:                       $choices{'uploadquota'}.
 7078:                       '</span></td>'.
 7079:                       '<td style="text-align: right" class="LC_right_item">'.
 7080:                       '<table><tr>';
 7081:         foreach my $type (@types) {
 7082:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7083:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7084:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7085:         }
 7086:         $datatable .= '</tr></table></td></tr>'."\n";
 7087:         $itemcount ++;
 7088:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7089:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7090:                       $choices{'coursequota'}.
 7091:                       '</span></td>'.
 7092:                       '<td style="text-align: right" class="LC_right_item">'.
 7093:                       '<table><tr>';
 7094:         foreach my $type (@types) {
 7095:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7096:                            '<input type="text" name="coursequota_'.$type.'"'.
 7097:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 7098:         }
 7099:         $datatable .= '</tr></table></td></tr>'."\n";
 7100:         $itemcount ++;
 7101:         my $onclick = "toggleDisplay(this.form,'credits');";
 7102:         my $display = 'none';
 7103:         if ($currusecredits) {
 7104:             $display = 'block';
 7105:         }
 7106:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7107:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7108:         foreach my $type (@types) {
 7109:             next if ($type eq 'community');
 7110:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7111:                            '<input type="text" name="'.$type.'_credits"'.
 7112:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7113:         }
 7114:         $additional .= '</tr></table></div>'."\n";
 7115:         %defaultchecked = ('coursecredits' => 'off');
 7116:         @toggles = ('coursecredits');
 7117:         my $current = {
 7118:                         'coursecredits' => $currusecredits,
 7119:                       };
 7120:         (my $table,$itemcount) =
 7121:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7122:                                \%choices,$itemcount,$onclick,$additional,'left');
 7123:         $datatable .= $table;
 7124:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7125:         my $display = 'none';
 7126:         if ($postsubmitclient) {
 7127:             $display = 'block';
 7128:         }
 7129:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7130:                       &mt('Number of seconds submit is disabled').'<br />'.
 7131:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7132:                       '<table><tr>';
 7133:         foreach my $type (@types) {
 7134:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7135:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7136:                            $deftimeout{$type}.'" size="5" /></td>';
 7137:         }
 7138:         $additional .= '</tr></table></div>'."\n";
 7139:         %defaultchecked = ('postsubmit' => 'on');
 7140:         @toggles = ('postsubmit');
 7141:         $current = {
 7142:                        'postsubmit' => $postsubmitclient,
 7143:                    };
 7144:         ($table,$itemcount) =
 7145:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7146:                                \%choices,$itemcount,$onclick,$additional,'left');
 7147:         $datatable .= $table;
 7148:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7149:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7150:                       $choices{'mysqltables'}.
 7151:                       '</span></td>'.
 7152:                       '<td style="text-align: right" class="LC_right_item">'.
 7153:                       '<table><tr>';
 7154:         foreach my $type (@types) {
 7155:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7156:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7157:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7158:         }
 7159:         $datatable .= '</tr></table></td></tr>'."\n";
 7160:         $itemcount ++;
 7161:         %defaultchecked = ('ltiauth' => 'off');
 7162:         @toggles = ('ltiauth');
 7163:         $current = {
 7164:                        'ltiauth' => $ltiauth,
 7165:                    };
 7166:         ($table,$itemcount) =
 7167:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7168:                                \%choices,$itemcount,undef,undef,'left');
 7169:         $datatable .= $table;
 7170:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7171:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7172:                       $choices{'domexttool'}.
 7173:                       '</span></td>'.
 7174:                       '<td style="text-align: right" class="LC_right_item">'.
 7175:                       '<table><tr>';
 7176:         foreach my $type (@types) {
 7177:             $datatable .= '<td style="text-align: left">'.
 7178:                           '<span class="LC_nobreak">'.
 7179:                           '<input type="checkbox" name="domexttool"'.
 7180:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7181:                           &mt($type).'</span></td>'."\n";
 7182:         }
 7183:         $datatable .= '</tr></table></td></tr>'."\n";
 7184:         $itemcount ++;
 7185:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7186:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7187:                       $choices{'exttool'}.
 7188:                       '</span></td>'.
 7189:                       '<td style="text-align: right" class="LC_right_item">'.
 7190:                       '<table><tr>';
 7191:         foreach my $type (@types) {
 7192:             $datatable .= '<td style="text-align: left">'.
 7193:                           '<span class="LC_nobreak">'.
 7194:                           '<input type="checkbox" name="exttool"'.
 7195:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7196:                           &mt($type).'</span></td>'."\n";
 7197:         }
 7198:         $datatable .= '</tr></table></td></tr>'."\n";
 7199:     }
 7200:     $$rowtotal += $itemcount;
 7201:     return $datatable;
 7202: }
 7203: 
 7204: sub print_authordefaults {
 7205:     my ($position,$dom,$settings,$rowtotal) = @_;
 7206:     my ($css_class,$datatable,%checkedon,%checkedoff);
 7207:     my $itemcount = 1;
 7208:     my %titles = &authordefaults_titles();
 7209:     if ($position eq 'top') {
 7210:         my %defaultchecked = (
 7211:                             'nocodemirror' => 'off',
 7212:                             'domcoordacc'  => 'on',
 7213:                           );
 7214:         my @toggles = ('nocodemirror','domcoordacc');
 7215:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 7216:                                                      \%titles,$itemcount);
 7217:         my %staticdefaults = (
 7218:                                 'copyright'    => 'default',
 7219:                                 'sourceavail'  => 'closed',
 7220:                              );
 7221:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7222:         my %currrights;
 7223:         foreach my $item ('copyright','sourceavail') {
 7224:             $currrights{$item} = $staticdefaults{$item};
 7225:             if (ref($settings) eq 'HASH') {
 7226:                 if (exists($settings->{$item})) {
 7227:                     $currrights{$item} = $settings->{$item};
 7228:                 }
 7229:             }
 7230:         }
 7231:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7232:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 7233:                       '</span></td><td class="LC_right_item">'.
 7234:                       &selectbox('copyright',$currrights{'copyright'},'',
 7235:                                  \&Apache::loncommon::copyrightdescription,
 7236:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 7237:                       '</td></tr>'."\n";
 7238:         $itemcount ++;
 7239:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7240:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7241:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 7242:                       '</span></td><td class="LC_right_item">'.
 7243:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 7244:                                  \&Apache::loncommon::source_copyrightdescription,
 7245:                                  (&Apache::loncommon::source_copyrightids)).
 7246:                       '</td></tr>'."\n";        
 7247:     } else {
 7248:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7249:         my $curreditors;
 7250:         my %staticdefaults = (
 7251:                                 editors => ['edit','xml'],
 7252:                                 authorquota => 500,
 7253:                                 webdav => 0,
 7254:                              );
 7255:         my $curreditors = $staticdefaults{'editors'};
 7256:         if ((ref($settings) eq 'HASH') &&
 7257:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 7258:             $curreditors = $settings->{'editors'};
 7259:         } else {
 7260:             $curreditors = $staticdefaults{'editors'};
 7261:         }
 7262:         my @editors = ('edit','xml','daxe');
 7263:         $datatable = '<tr'.$css_class.'>'."\n".
 7264:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 7265:                      '<td class="LC_left_item">'."\n".
 7266:                      '<span class="LC_nobreak">';
 7267:         foreach my $editor (@editors) {
 7268:             my $checked;
 7269:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 7270:                 $checked = ' checked="checked"';
 7271:             }
 7272:             $datatable .= '<label>'.
 7273:                           '<input type="checkbox" name="author_editors" '.
 7274:                           $checked.' value="'.$editor.'" '.
 7275:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 7276:                           $titles{$editor}.'</label>&nbsp;';
 7277:         }
 7278:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 7279:         $itemcount ++;
 7280:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7281:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7282:         my @insttypes;
 7283:         if (ref($types) eq 'ARRAY') {
 7284:             @insttypes = @{$types};
 7285:         }
 7286:         my $typecount = 0;
 7287:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 7288:         my @items = ('webdav','authorquota');
 7289:         my %quotas;
 7290:         if (ref($domconf{'quotas'}) eq 'HASH') {
 7291:             %quotas = %{$domconf{'quotas'}};
 7292:             foreach my $item (@items) {
 7293:                 if (ref($quotas{$item}) eq 'HASH') {
 7294:                     foreach my $type (@insttypes,'default') {
 7295:                         if ($item eq 'authorquota') {
 7296:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 7297:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7298:                             }
 7299:                         } elsif ($item eq 'webdav') {
 7300:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 7301:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7302:                             }
 7303:                         }
 7304:                     }
 7305:                 } else {
 7306:                     foreach my $type (@insttypes,'default') {
 7307:                         $quotas{$item}{$type} = $staticdefaults{$item};
 7308:                     }
 7309:                 }
 7310:             }
 7311:         } else {
 7312:             foreach my $item (@items) {
 7313:                 foreach my $type (@insttypes,'default') {
 7314:                     $quotas{$item}{$type} = $staticdefaults{$item};
 7315:                 }
 7316:             }
 7317:         }
 7318:         if (ref($usertypes) eq 'HASH') {
 7319:             my $numinrow = 4;
 7320:             my $onclick = '';
 7321:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7322:                                          $numinrow,$othertitle,'authorquota',
 7323:                                          \$itemcount,$onclick);
 7324:             $itemcount ++;
 7325:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7326:                                          $numinrow,$othertitle,'webdav',
 7327:                                          \$itemcount);
 7328:             $itemcount ++;
 7329:         }
 7330:         my $checkedno = ' checked="checked"';
 7331:         my ($checkedon,$checkedoff);
 7332:         if (ref($quotas{'webdav'}) eq 'HASH') {
 7333:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) { 
 7334:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 7335:                     $checkedon = $checkedno;
 7336:                 } else {
 7337:                     $checkedoff = $checkedno;
 7338:                 }
 7339:                 undef($checkedno);
 7340:             }
 7341:         }
 7342:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7343:         $datatable .= '<tr'.$css_class.'>'.
 7344:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 7345:                              $titles{'webdav_LC_adv_over'}.
 7346:                       '</td>'.
 7347:                       '<td class="LC_left_item">';
 7348:         foreach my $option ('none','off','on') {
 7349:             my ($text,$val,$checked);
 7350:             if ($option eq 'none') {
 7351:                 $text = $titles{'none'};
 7352:                 $val = '';
 7353:                 $checked = $checkedno;
 7354:             } elsif ($option eq 'off') {
 7355:                 $text = $titles{'overoff'};
 7356:                 $val = 0;
 7357:                 $checked = $checkedoff;
 7358:             } elsif ($option eq 'on') {
 7359:                 $text = $titles{'overon'};
 7360:                 $val = 1;
 7361:                 $checked = $checkedon;
 7362:             } 
 7363:             $datatable .= '<span class="LC_nobreak"><label>'.
 7364:                           '<input type="radio" name="webdav_LC_adv"'.
 7365:                           ' value="'.$val.'"'.$checked.' />'.
 7366:                           $text.'</label></span>&nbsp; ';
 7367:         }
 7368:         $datatable .= '</td></tr>';
 7369:         $itemcount ++;
 7370:     }
 7371:     $$rowtotal += $itemcount;
 7372:     return $datatable;
 7373: }
 7374: 
 7375: sub authordefaults_titles {
 7376:     return &Apache::lonlocal::texthash(
 7377:                copyright => 'Copyright/Distribution',
 7378:                sourceavail => ' Source Available',
 7379:                editors => 'Available Editors',
 7380:                webdav => 'WebDAV',
 7381:                authorquota => 'Authoring Space quotas (MB)',
 7382:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 7383:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 7384:                edit  => 'Standard editor (Edit)',
 7385:                xml   => 'Text editor (EditXML)',
 7386:                daxe  => 'Daxe editor (Daxe)',
 7387:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 7388:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 7389:                none => 'No override set',
 7390:                overon => 'Override -- webDAV on',
 7391:                overoff => 'Override -- webDAV off', 
 7392:     );
 7393: }
 7394: 
 7395: sub selectbox {
 7396:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 7397:     my $selout = '<select name="'.$name.'">';
 7398:     foreach my $id (@idlist) {
 7399:         $selout.='<option value="'.$id.'"';
 7400:         if ($id eq $value) {
 7401:             $selout.=' selected="selected"';
 7402:         }
 7403:         if ($readonly) {
 7404:             $selout .= ' disabled="disabled"';
 7405:         }
 7406:         $selout.='>'.&{$functionref}($id).'</option>';
 7407:     }
 7408:     $selout.='</select>';
 7409:     return $selout;
 7410: }
 7411: 
 7412: sub print_selfenrollment {
 7413:     my ($position,$dom,$settings,$rowtotal) = @_;
 7414:     my ($css_class,$datatable);
 7415:     my $itemcount = 1;
 7416:     my @types = ('official','unofficial','community','textbook','placement');
 7417:     if (($position eq 'top') || ($position eq 'middle')) {
 7418:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7419:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7420:         my @rows;
 7421:         my $key;
 7422:         if ($position eq 'top') {
 7423:             $key = 'admin'; 
 7424:             if (ref($rowsref) eq 'ARRAY') {
 7425:                 @rows = @{$rowsref};
 7426:             }
 7427:         } elsif ($position eq 'middle') {
 7428:             $key = 'default';
 7429:             @rows = ('types','registered','approval','limit');
 7430:         }
 7431:         foreach my $row (@rows) {
 7432:             if (defined($titlesref->{$row})) {
 7433:                 $itemcount ++;
 7434:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7435:                 $datatable .= '<tr'.$css_class.'>'.
 7436:                               '<td>'.$titlesref->{$row}.'</td>'.
 7437:                               '<td class="LC_left_item">'.
 7438:                               '<table><tr>';
 7439:                 my (%current,%currentcap);
 7440:                 if (ref($settings) eq 'HASH') {
 7441:                     if (ref($settings->{$key}) eq 'HASH') {
 7442:                         foreach my $type (@types) {
 7443:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7444:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7445:                             }
 7446:                             if (($row eq 'limit') && ($key eq 'default')) {
 7447:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7448:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7449:                                 }
 7450:                             }
 7451:                         }
 7452:                     }
 7453:                 }
 7454:                 my %roles = (
 7455:                              '0' => &Apache::lonnet::plaintext('dc'),
 7456:                             ); 
 7457:             
 7458:                 foreach my $type (@types) {
 7459:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7460:                         $datatable .= '<th>'.&mt($type).'</th>';
 7461:                     }
 7462:                 }
 7463:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7464:                     $datatable .= '</tr><tr>';
 7465:                 }
 7466:                 foreach my $type (@types) {
 7467:                     if ($type eq 'community') {
 7468:                         $roles{'1'} = &mt('Community personnel');
 7469:                     } else {
 7470:                         $roles{'1'} = &mt('Course personnel');
 7471:                     }
 7472:                     $datatable .= '<td style="vertical-align: top">';
 7473:                     if ($position eq 'top') {
 7474:                         my %checked;
 7475:                         if ($current{$type} eq '0') {
 7476:                             $checked{'0'} = ' checked="checked"';
 7477:                         } else {
 7478:                             $checked{'1'} = ' checked="checked"';
 7479:                         }
 7480:                         foreach my $role ('1','0') {
 7481:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7482:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7483:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7484:                                           $roles{$role}.'</label></span> ';
 7485:                         }
 7486:                     } else {
 7487:                         if ($row eq 'types') {
 7488:                             my %checked;
 7489:                             if ($current{$type} =~ /^(all|dom)$/) {
 7490:                                 $checked{$1} = ' checked="checked"';
 7491:                             } else {
 7492:                                 $checked{''} = ' checked="checked"';
 7493:                             }
 7494:                             foreach my $val ('','dom','all') {
 7495:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7496:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7497:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7498:                             }
 7499:                         } elsif ($row eq 'registered') {
 7500:                             my %checked;
 7501:                             if ($current{$type} eq '1') {
 7502:                                 $checked{'1'} = ' checked="checked"';
 7503:                             } else {
 7504:                                 $checked{'0'} = ' checked="checked"';
 7505:                             }
 7506:                             foreach my $val ('0','1') {
 7507:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7508:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7509:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7510:                             }
 7511:                         } elsif ($row eq 'approval') {
 7512:                             my %checked;
 7513:                             if ($current{$type} =~ /^([12])$/) {
 7514:                                 $checked{$1} = ' checked="checked"';
 7515:                             } else {
 7516:                                 $checked{'0'} = ' checked="checked"';
 7517:                             }
 7518:                             for my $val (0..2) {
 7519:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7520:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7521:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7522:                             }
 7523:                         } elsif ($row eq 'limit') {
 7524:                             my %checked;
 7525:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7526:                                 $checked{$1} = ' checked="checked"';
 7527:                             } else {
 7528:                                 $checked{'none'} = ' checked="checked"';
 7529:                             }
 7530:                             my $cap;
 7531:                             if ($currentcap{$type} =~ /^\d+$/) {
 7532:                                 $cap = $currentcap{$type};
 7533:                             }
 7534:                             foreach my $val ('none','allstudents','selfenrolled') {
 7535:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7536:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7537:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7538:                             }
 7539:                             $datatable .= '<br />'.
 7540:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7541:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7542:                                           '</span>'; 
 7543:                         }
 7544:                     }
 7545:                     $datatable .= '</td>';
 7546:                 }
 7547:                 $datatable .= '</tr>';
 7548:             }
 7549:             $datatable .= '</table></td></tr>';
 7550:         }
 7551:     } elsif ($position eq 'bottom') {
 7552:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7553:     }
 7554:     $$rowtotal += $itemcount;
 7555:     return $datatable;
 7556: }
 7557: 
 7558: sub print_validation_rows {
 7559:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7560:     my ($itemsref,$namesref,$fieldsref);
 7561:     if ($caller eq 'selfenroll') { 
 7562:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7563:     } elsif ($caller eq 'requestcourses') {
 7564:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7565:     }
 7566:     my %currvalidation;
 7567:     if (ref($settings) eq 'HASH') {
 7568:         if (ref($settings->{'validation'}) eq 'HASH') {
 7569:             %currvalidation = %{$settings->{'validation'}};
 7570:         }
 7571:     }
 7572:     my $datatable;
 7573:     my $itemcount = 0;
 7574:     foreach my $item (@{$itemsref}) {
 7575:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7576:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7577:                       $namesref->{$item}.
 7578:                       '</span></td>'.
 7579:                       '<td class="LC_left_item">';
 7580:         if (($item eq 'url') || ($item eq 'button')) {
 7581:             $datatable .= '<span class="LC_nobreak">'.
 7582:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7583:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7584:         } elsif ($item eq 'fields') {
 7585:             my @currfields;
 7586:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7587:                 @currfields = @{$currvalidation{$item}};
 7588:             }
 7589:             foreach my $field (@{$fieldsref}) {
 7590:                 my $check = '';
 7591:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7592:                     $check = ' checked="checked"';
 7593:                 }
 7594:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7595:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7596:                               ' value="'.$field.'"'.$check.' />'.$field.
 7597:                               '</label></span> ';
 7598:             }
 7599:         } elsif ($item eq 'markup') {
 7600:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7601:                            $currvalidation{$item}.
 7602:                               '</textarea>';
 7603:         }
 7604:         $datatable .= '</td></tr>'."\n";
 7605:         if (ref($rowtotal)) {
 7606:             $itemcount ++;
 7607:         }
 7608:     }
 7609:     if ($caller eq 'requestcourses') {
 7610:         my %currhash;
 7611:         if (ref($settings) eq 'HASH') {
 7612:             if (ref($settings->{'validation'}) eq 'HASH') {
 7613:                 if ($settings->{'validation'}{'dc'} ne '') {
 7614:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7615:                 }
 7616:             }
 7617:         }
 7618:         my $numinrow = 2;
 7619:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7620:                                                        'validationdc',%currhash);
 7621:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7622:         $datatable .= '<tr'.$css_class.'><td>';
 7623:         if ($numdc > 1) {
 7624:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7625:         } else {
 7626:             $datatable .=  &mt('Course creation processed as: ');
 7627:         }
 7628:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7629:         $itemcount ++;
 7630:     }
 7631:     if (ref($rowtotal)) {
 7632:         $$rowtotal += $itemcount;
 7633:     }
 7634:     return $datatable;
 7635: }
 7636: 
 7637: sub print_privacy {
 7638:     my ($position,$dom,$settings,$rowtotal) = @_;
 7639:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7640:     my $itemcount = 0;
 7641:     if ($position eq 'top') {
 7642:         $numinrow = 2;
 7643:     } else {
 7644:         @items = ('domain','author','course','community');
 7645:         %names = &Apache::lonlocal::texthash (
 7646:                      domain => 'Assigned domain role(s)',
 7647:                      author => 'Assigned co-author role(s)',
 7648:                      course => 'Assigned course role(s)',
 7649:                      community => 'Assigned community role(s)',
 7650:                  );
 7651:         $numinrow = 4;
 7652:         ($othertitle,$usertypes,$types) =
 7653:             &Apache::loncommon::sorted_inst_types($dom);
 7654:     }
 7655:     if (($position eq 'top') || ($position eq 'middle')) {
 7656:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7657:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7658:         if ($position eq 'top') {
 7659:             my %curr;
 7660:             my @options = ('none','user','domain','auto');
 7661:             my %titles = &Apache::lonlocal::texthash (
 7662:                 none   => 'Not allowed',
 7663:                 user   => 'User authorizes',
 7664:                 domain => 'DC authorizes',
 7665:                 auto   => 'Unrestricted',
 7666:                 instdom => 'Other domain shares institution/provider',
 7667:                 extdom => 'Other domain has different institution/provider',
 7668:                 notify => 'Notify when role needs authorization',
 7669:             );
 7670:             my %names = &Apache::lonlocal::texthash (
 7671:                 domain => 'Domain role',
 7672:                 author => 'Co-author role',
 7673:                 course => 'Course role',
 7674:                 community => 'Community role',
 7675:             );
 7676:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7677:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7678:             foreach my $domtype ('instdom','extdom') {
 7679:                 my (%checked,$skip);
 7680:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7681:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7682:                               '<td class="LC_left_item">';
 7683:                 if ($domtype eq 'instdom') {
 7684:                     unless (@instdoms > 1) {
 7685:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7686:                         $skip = 1;
 7687:                     }
 7688:                 } elsif ($domtype eq 'extdom') {
 7689:                     if (keys(%by_location) == 0) {
 7690:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7691:                         $skip = 1;
 7692:                     }
 7693:                 }
 7694:                 unless ($skip) {
 7695:                     foreach my $roletype ('domain','author','course','community') {
 7696:                         $checked{'auto'} = ' checked="checked"';
 7697:                         if (ref($settings) eq 'HASH') {
 7698:                             if (ref($settings->{approval}) eq 'HASH') {
 7699:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7700:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7701:                                         $checked{$1} = ' checked="checked"';
 7702:                                         $checked{'auto'} = '';
 7703:                                     }
 7704:                                 }
 7705:                             }
 7706:                         }
 7707:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7708:                         foreach my $option (@options) {
 7709:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7710:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7711:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7712:                                           '</label></span>&nbsp; ';
 7713:                         }
 7714:                         $datatable .= '</fieldset>';
 7715:                     }
 7716:                 }
 7717:                 $datatable .= '</td></tr>';
 7718:                 $itemcount ++;
 7719:             }
 7720:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7721:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7722:                           '<td class="LC_left_item">';
 7723:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7724:                 my %curr;
 7725:                 if (ref($settings) eq 'HASH') {
 7726:                     if ($settings->{'notify'} ne '') {
 7727:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7728:                     }
 7729:                 }
 7730:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7731:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7732:                                                              'privacy_notify',%curr);
 7733:                 if ($numdc > 0) {
 7734:                     $datatable .= $table;
 7735:                 } else {
 7736:                     $datatable .= &mt('There are no active Domain Coordinators');
 7737:                 }
 7738:             } else {
 7739:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7740:             }
 7741:             $datatable .='</td></tr>';
 7742:         } elsif ($position eq 'middle') {
 7743:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7744:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7745:                     foreach my $item (@{$types}) {
 7746:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7747:                                                                $numinrow,$itemcount,'','','','','',
 7748:                                                                '',$usertypes->{$item});
 7749:                         $itemcount ++;
 7750:                     }
 7751:                 }
 7752:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7753:                                                        $numinrow,$itemcount,'','','','','',
 7754:                                                        '',$othertitle);
 7755:                 $itemcount ++;
 7756:             } else {
 7757:                 my (@insttypes,%insttitles);
 7758:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7759:                     @insttypes = @{$types};
 7760:                     %insttitles = %{$usertypes};
 7761:                 }
 7762:                 foreach my $item (@insttypes,'default') {
 7763:                     my $title;
 7764:                     if ($item eq 'default') {
 7765:                         $title = $othertitle;
 7766:                     } else {
 7767:                         $title = $insttitles{$item};
 7768:                     }
 7769:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7770:                     $datatable .= '<tr'.$css_class.'>'.
 7771:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7772:                                   '<td class="LC_left_item">'.
 7773:                                   &mt('Nothing to set here, as there are no other domains').
 7774:                                   '</td></tr>';
 7775:                     $itemcount ++;
 7776:                 }
 7777:             }
 7778:         }
 7779:     } else {
 7780:         my $prefix;
 7781:         if ($position eq 'lower') {
 7782:             $prefix = 'priv';
 7783:         } else {
 7784:             $prefix = 'unpriv';
 7785:         }
 7786:         foreach my $item (@items) {
 7787:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7788:                                                    $numinrow,$itemcount,'','','','','',
 7789:                                                    '',$names{$item});
 7790:             $itemcount ++;
 7791:         }
 7792:     }
 7793:     if (ref($rowtotal)) {
 7794:         $$rowtotal += $itemcount;
 7795:     }
 7796:     return $datatable;
 7797: }
 7798: 
 7799: sub print_passwords {
 7800:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7801:     my ($datatable,$css_class);
 7802:     my $itemcount = 0;
 7803:     my %titles = &Apache::lonlocal::texthash (
 7804:         captcha        => '"Forgot Password" CAPTCHA validation',
 7805:         link           => 'Reset link expiration (hours)',
 7806:         case           => 'Case-sensitive usernames/e-mail',
 7807:         prelink        => 'Information required (form 1)',
 7808:         postlink       => 'Information required (form 2)',
 7809:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7810:         customtext     => 'Domain specific text (HTML)',
 7811:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7812:         intauth_check  => 'Check bcrypt cost if authenticated',
 7813:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7814:         permanent      => 'Permanent e-mail address',
 7815:         critical       => 'Critical notification address',
 7816:         notify         => 'Notification address',
 7817:         min            => 'Minimum password length',
 7818:         max            => 'Maximum password length',
 7819:         chars          => 'Required characters',
 7820:         expire         => 'Password expiration (days)',
 7821:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7822:     );
 7823:     if ($position eq 'top') {
 7824:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7825:         my $shownlinklife = 2;
 7826:         my $prelink = 'both';
 7827:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7828:         if (ref($settings) eq 'HASH') {
 7829:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7830:                 $shownlinklife = $settings->{resetlink};
 7831:             }
 7832:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7833:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7834:             }
 7835:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7836:                 $prelink = $settings->{resetprelink};
 7837:             }
 7838:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7839:                 %postlink = %{$settings->{resetpostlink}};
 7840:             }
 7841:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7842:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7843:             }
 7844:             if ($settings->{resetremove}) {
 7845:                 $nostdtext = 1;
 7846:             }
 7847:             if ($settings->{resetcustom}) {
 7848:                 $customurl = $settings->{resetcustom};
 7849:             }
 7850:         } else {
 7851:             if (ref($types) eq 'ARRAY') {
 7852:                 foreach my $item (@{$types}) {
 7853:                     $casesens{$item} = 1;
 7854:                     $postlink{$item} = ['username','email'];
 7855:                 }
 7856:             }
 7857:             $casesens{'default'} = 1;
 7858:             $postlink{'default'} = ['username','email'];
 7859:             $prelink = 'both';
 7860:             %emailsrc = (
 7861:                           permanent => 1,
 7862:                           critical  => 1,
 7863:                           notify    => 1,
 7864:             );
 7865:         }
 7866:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7867:         $itemcount ++;
 7868:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7869:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7870:                       '<td class="LC_left_item">'.
 7871:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7872:                       'name="passwords_link" size="3" /></td></tr>';
 7873:         $itemcount ++;
 7874:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7875:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7876:                       '<td class="LC_left_item">';
 7877:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7878:             foreach my $item (@{$types}) {
 7879:                 my $checkedcase;
 7880:                 if ($casesens{$item}) {
 7881:                     $checkedcase = ' checked="checked"';
 7882:                 }
 7883:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7884:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7885:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7886:                               '</span>&nbsp;&nbsp; ';
 7887:             }
 7888:         }
 7889:         my $checkedcase;
 7890:         if ($casesens{'default'}) {
 7891:             $checkedcase = ' checked="checked"';
 7892:         }
 7893:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7894:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7895:                       $othertitle.'</label></span></td>';
 7896:         $itemcount ++;
 7897:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7898:         my %checkedpre = (
 7899:                              both => ' checked="checked"',
 7900:                              either => '',
 7901:                          );
 7902:         if ($prelink eq 'either') {
 7903:             $checkedpre{either} = ' checked="checked"';
 7904:             $checkedpre{both} = '';
 7905:         }
 7906:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7907:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7908:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7909:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7910:                       '<span class="LC_nobreak"><label>'.
 7911:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7912:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7913:         $itemcount ++;
 7914:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7915:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7916:                       '<td class="LC_left_item">';
 7917:         my %postlinked;
 7918:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7919:             foreach my $item (@{$types}) {
 7920:                 undef(%postlinked);
 7921:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7922:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7923:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7924:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7925:                 }
 7926:                 foreach my $field ('email','username') {
 7927:                     my $checked;
 7928:                     if ($postlinked{$field}) {
 7929:                         $checked = ' checked="checked"';
 7930:                     }
 7931:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7932:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7933:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7934:                                   '<span>&nbsp;&nbsp; ';
 7935:                 }
 7936:                 $datatable .= '</fieldset>';
 7937:             }
 7938:         }
 7939:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7940:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7941:         }
 7942:         $datatable .= '<fieldset style="display: inline-block;">'.
 7943:                       '<legend>'.$othertitle.'</legend>';
 7944:         foreach my $field ('email','username') {
 7945:             my $checked;
 7946:             if ($postlinked{$field}) {
 7947:                 $checked = ' checked="checked"';
 7948:             }
 7949:             $datatable .= '<span class="LC_nobreak"><label>'.
 7950:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7951:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7952:                           '<span>&nbsp;&nbsp; ';
 7953:         }
 7954:         $datatable .= '</fieldset></td></tr>';
 7955:         $itemcount ++;
 7956:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7957:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7958:                       '<td class="LC_left_item">';
 7959:         foreach my $type ('permanent','critical','notify') {
 7960:             my $checkedemail;
 7961:             if ($emailsrc{$type}) {
 7962:                 $checkedemail = ' checked="checked"';
 7963:             }
 7964:             $datatable .= '<span class="LC_nobreak"><label>'.
 7965:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7966:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7967:                           '<span>&nbsp;&nbsp; ';
 7968:         }
 7969:         $datatable .= '</td></tr>';
 7970:         $itemcount ++;
 7971:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7972:         my $switchserver = &check_switchserver($dom,$confname);
 7973:         my ($showstd,$noshowstd);
 7974:         if ($nostdtext) {
 7975:             $noshowstd = ' checked="checked"';
 7976:         } else {
 7977:             $showstd = ' checked="checked"';
 7978:         }
 7979:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7980:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7981:                       &mt('Retain standard text:').
 7982:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7983:                       &mt('Yes').'</label>'.'&nbsp;'.
 7984:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7985:                       &mt('No').'</label></span><br />'.
 7986:                       '<span class="LC_fontsize_small">'.
 7987:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7988:                       &mt('Include custom text:');
 7989:         if ($customurl) {
 7990:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7991:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7992:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7993:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7994:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7995:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7996:         }
 7997:         if ($switchserver) {
 7998:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7999:         } else {
 8000:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8001:                          '<input type="file" name="passwords_customfile" /></span>';
 8002:         }
 8003:         $datatable .= '</td></tr>';
 8004:     } elsif ($position eq 'middle') {
 8005:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 8006:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 8007:         my %defaults;
 8008:         if (ref($domconf{'defaults'}) eq 'HASH') {
 8009:             %defaults = %{$domconf{'defaults'}};
 8010:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 8011:                 $defaults{'intauth_cost'} = 10;
 8012:             }
 8013:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 8014:                 $defaults{'intauth_check'} = 0;
 8015:             }
 8016:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 8017:                 $defaults{'intauth_switch'} = 0;
 8018:             }
 8019:         } else {
 8020:             %defaults = (
 8021:                           'intauth_cost'   => 10,
 8022:                           'intauth_check'  => 0,
 8023:                           'intauth_switch' => 0,
 8024:                         );
 8025:         }
 8026:         foreach my $item (@items) {
 8027:             if ($itemcount%2) {
 8028:                 $css_class = '';
 8029:             } else {
 8030:                 $css_class = ' class="LC_odd_row" ';
 8031:             }
 8032:             $datatable .= '<tr'.$css_class.'>'.
 8033:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 8034:                           '</span></td><td class="LC_left_item" colspan="3">';
 8035:             if ($item eq 'intauth_switch') {
 8036:                 my @options = (0,1,2);
 8037:                 my %optiondesc = &Apache::lonlocal::texthash (
 8038:                                    0 => 'No',
 8039:                                    1 => 'Yes',
 8040:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 8041:                                  );
 8042:                 $datatable .= '<table width="100%">';
 8043:                 foreach my $option (@options) {
 8044:                     my $checked = ' ';
 8045:                     if ($defaults{$item} eq $option) {
 8046:                         $checked = ' checked="checked"';
 8047:                     }
 8048:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8049:                                   '<label><input type="radio" name="'.$item.
 8050:                                   '" value="'.$option.'"'.$checked.' />'.
 8051:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8052:                 }
 8053:                 $datatable .= '</table>';
 8054:             } elsif ($item eq 'intauth_check') {
 8055:                 my @options = (0,1,2);
 8056:                 my %optiondesc = &Apache::lonlocal::texthash (
 8057:                                    0 => 'No',
 8058:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 8059:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 8060:                                  );
 8061:                 $datatable .= '<table width="100%">';
 8062:                 foreach my $option (@options) {
 8063:                     my $checked = ' ';
 8064:                     my $onclick;
 8065:                     if ($defaults{$item} eq $option) {
 8066:                         $checked = ' checked="checked"';
 8067:                     }
 8068:                     if ($option == 2) {
 8069:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 8070:                     }
 8071:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8072:                                   '<label><input type="radio" name="'.$item.
 8073:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 8074:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8075:                 }
 8076:                 $datatable .= '</table>';
 8077:             } else {
 8078:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8079:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 8080:             }
 8081:             $datatable .= '</td></tr>';
 8082:             $itemcount ++;
 8083:         }
 8084:     } elsif ($position eq 'lower') {
 8085:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 8086:     } else {
 8087:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8088:         my %ownerchg = (
 8089:                           by  => {},
 8090:                           for => {},
 8091:                        );
 8092:         my %ownertitles = &Apache::lonlocal::texthash (
 8093:                             by  => 'Course owner status(es) allowed',
 8094:                             for => 'Student status(es) allowed',
 8095:                           );
 8096:         if (ref($settings) eq 'HASH') {
 8097:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 8098:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 8099:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 8100:                 }
 8101:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 8102:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 8103:                 }
 8104:             }
 8105:         }
 8106:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8107:         $datatable .= '<tr '.$css_class.'>'.
 8108:                       '<td>'.
 8109:                       &mt('Requirements').'<ul>'.
 8110:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 8111:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 8112:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 8113:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 8114:                       '</ul>'.
 8115:                       '</td>'.
 8116:                       '<td class="LC_left_item">';
 8117:         foreach my $item ('by','for') {
 8118:             $datatable .= '<fieldset style="display: inline-block;">'.
 8119:                           '<legend>'.$ownertitles{$item}.'</legend>';
 8120:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8121:                 foreach my $type (@{$types}) {
 8122:                     my $checked;
 8123:                     if ($ownerchg{$item}{$type}) {
 8124:                         $checked = ' checked="checked"';
 8125:                     }
 8126:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8127:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 8128:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 8129:                                   '</span>&nbsp;&nbsp; ';
 8130:                 }
 8131:             }
 8132:             my $checked;
 8133:             if ($ownerchg{$item}{'default'}) {
 8134:                 $checked = ' checked="checked"';
 8135:             }
 8136:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8137:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 8138:                           $othertitle.'</label></span></fieldset>';
 8139:         }
 8140:         $datatable .= '</td></tr>';
 8141:     }
 8142:     return $datatable;
 8143: }
 8144: 
 8145: sub password_rules {
 8146:     my ($prefix,$itemcountref,$settings) = @_;
 8147:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 8148:     my %titles;
 8149:     if ($prefix eq 'passwords') {
 8150:         %titles = &Apache::lonlocal::texthash (
 8151:             min            => 'Minimum password length',
 8152:             max            => 'Maximum password length',
 8153:             chars          => 'Required characters',
 8154:         );
 8155:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 8156:         %titles = &Apache::lonlocal::texthash (
 8157:             min            => 'Minimum secret length',
 8158:             max            => 'Maximum secret length',
 8159:             chars          => 'Required characters',
 8160:         );
 8161:     }
 8162:     $min = $Apache::lonnet::passwdmin;
 8163:     my $datatable;
 8164:     my $itemcount;
 8165:     if (ref($itemcountref)) {
 8166:         $itemcount = $$itemcountref;
 8167:     }
 8168:     if (ref($settings) eq 'HASH') {
 8169:         if ($settings->{min}) {
 8170:             $min = $settings->{min};
 8171:         }
 8172:         if ($settings->{max}) {
 8173:             $max = $settings->{max};
 8174:         }
 8175:         if (ref($settings->{chars}) eq 'ARRAY') {
 8176:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 8177:         }
 8178:         if ($prefix eq 'passwords') {
 8179:             if ($settings->{expire}) {
 8180:                 $expire = $settings->{expire};
 8181:             }
 8182:             if ($settings->{numsaved}) {
 8183:                 $numsaved = $settings->{numsaved};
 8184:             }
 8185:         }
 8186:     }
 8187:     my %rulenames = &Apache::lonlocal::texthash(
 8188:                                                  uc => 'At least one upper case letter',
 8189:                                                  lc => 'At least one lower case letter',
 8190:                                                  num => 'At least one number',
 8191:                                                  spec => 'At least one non-alphanumeric',
 8192:                                                );
 8193:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8194:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 8195:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8196:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 8197:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8198:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 8199:                   '</span></td></tr>';
 8200:     $itemcount ++;
 8201:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8202:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 8203:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8204:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 8205:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8206:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 8207:                   '</span></td></tr>';
 8208:     $itemcount ++;
 8209:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8210:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 8211:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 8212:                   '</span></td>';
 8213:     my $numinrow = 2;
 8214:     my @possrules = ('uc','lc','num','spec');
 8215:     $datatable .= '<td class="LC_left_item"><table>';
 8216:     for (my $i=0; $i<@possrules; $i++) {
 8217:         my ($rem,$checked);
 8218:         if ($chars{$possrules[$i]}) {
 8219:             $checked = ' checked="checked"';
 8220:         }
 8221:         $rem = $i%($numinrow);
 8222:         if ($rem == 0) {
 8223:             if ($i > 0) {
 8224:                 $datatable .= '</tr>';
 8225:             }
 8226:             $datatable .= '<tr>';
 8227:         }
 8228:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 8229:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 8230:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 8231:     }
 8232:     my $rem = @possrules%($numinrow);
 8233:     my $colsleft = $numinrow - $rem;
 8234:     if ($colsleft > 1 ) {
 8235:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8236:                       '&nbsp;</td>';
 8237:     } elsif ($colsleft == 1) {
 8238:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8239:     }
 8240:     $datatable .='</table></td></tr>';
 8241:     $itemcount ++;
 8242:     if ($prefix eq 'passwords') {
 8243:         $titles{'expire'} = &mt('Password expiration (days)');
 8244:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 8245:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8246:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 8247:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8248:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 8249:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8250:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 8251:                       '</span></td></tr>';
 8252:         $itemcount ++;
 8253:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8254:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 8255:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8256:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 8257:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8258:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 8259:                       '</span></td></tr>';
 8260:         $itemcount ++;
 8261:     }
 8262:     if (ref($itemcountref)) {
 8263:         $$itemcountref += $itemcount;
 8264:     }
 8265:     return $datatable;
 8266: }
 8267: 
 8268: sub print_wafproxy {
 8269:     my ($position,$dom,$settings,$rowtotal) = @_;
 8270:     my $css_class;
 8271:     my $itemcount = 0;
 8272:     my $datatable;
 8273:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8274:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8275:     my %lt = &wafproxy_titles();
 8276:     foreach my $server (sort(keys(%servers))) {
 8277:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8278:         next if ($serverhome eq '');
 8279:         my $serverdom;
 8280:         if ($serverhome ne $server) {
 8281:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8282:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8283:                 $othercontrol{$server} = $serverdom;
 8284:             }
 8285:         } else {
 8286:             $serverdom = &Apache::lonnet::host_domain($server);
 8287:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8288:             if ($serverdom ne $dom) {
 8289:                 $othercontrol{$server} = $serverdom;
 8290:             } else {
 8291:                 $setdom = 1;
 8292:                 if (ref($settings) eq 'HASH') {
 8293:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8294:                         $aliases{$dom} = $settings->{'alias'};
 8295:                         if ($aliases{$dom} ne '') {
 8296:                             $showdom = 1;
 8297:                         }
 8298:                     }
 8299:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8300:                         $saml{$dom} = $settings->{'saml'};
 8301:                     }
 8302:                 }
 8303:             }
 8304:         }
 8305:     }
 8306:     if ($setdom) {
 8307:         %{$values{$dom}} = ();
 8308:         if (ref($settings) eq 'HASH') {
 8309:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8310:                 $values{$dom}{$item} = $settings->{$item};
 8311:             }
 8312:         }
 8313:     }
 8314:     if (keys(%othercontrol)) {
 8315:         %otherdoms = reverse(%othercontrol);
 8316:         foreach my $domain (keys(%otherdoms)) {
 8317:             %{$values{$domain}} = ();
 8318:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8319:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8320:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8321:                 if (exists($config{'wafproxy'}{'saml'})) {
 8322:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8323:                 }
 8324:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8325:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8326:                 }
 8327:             }
 8328:         }
 8329:     }
 8330:     if ($position eq 'top') {
 8331:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8332:         my %aliasinfo;
 8333:         foreach my $server (sort(keys(%servers))) {
 8334:             $itemcount ++;
 8335:             my $dom_in_effect;
 8336:             my $aliasrows = '<tr>'.
 8337:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8338:                             &mt('Hostname').':&nbsp;'.
 8339:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 8340:             if ($othercontrol{$server}) {
 8341:                 $dom_in_effect = $othercontrol{$server};
 8342:                 my ($current,$forsaml);
 8343:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8344:                     $current = $aliases{$dom_in_effect}{$server};
 8345:                 }
 8346:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8347:                     if ($saml{$dom_in_effect}{$server}) {
 8348:                         $forsaml = 1;
 8349:                     }
 8350:                 }
 8351:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8352:                               &mt('Alias').':&nbsp';
 8353:                 if ($current) {
 8354:                     $aliasrows .= $current;
 8355:                     if ($forsaml) {
 8356:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8357:                     }
 8358:                 } else {
 8359:                     $aliasrows .= &mt('None');
 8360:                 }
 8361:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8362:                               &mt('controlled by domain: [_1]',
 8363:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8364:             } else {
 8365:                 $dom_in_effect = $dom;
 8366:                 my ($current,$samlon,$samloff);
 8367:                 $samloff = ' checked="checked"';
 8368:                 if (ref($aliases{$dom}) eq 'HASH') {
 8369:                     if ($aliases{$dom}{$server}) {
 8370:                         $current = $aliases{$dom}{$server};
 8371:                     }
 8372:                 }
 8373:                 if (ref($saml{$dom}) eq 'HASH') {
 8374:                     if ($saml{$dom}{$server}) {
 8375:                         $samlon = $samloff;
 8376:                         undef($samloff);
 8377:                     }
 8378:                 }
 8379:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8380:                               &mt('Alias').':&nbsp;'.
 8381:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8382:                               'value="'.$current.'" size="30" />'.
 8383:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8384:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8385:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8386:                               &mt('No').'</label>&nbsp;<label>'.
 8387:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8388:                               &mt('Yes').'</label></span>'.
 8389:                               '</td>';
 8390:             }
 8391:             $aliasrows .= '</tr>';
 8392:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8393:         }
 8394:         if ($aliasinfo{$dom}) {
 8395:             my ($onclick,$wafon,$wafoff,$showtable);
 8396:             $onclick = ' onclick="javascript:toggleWAF();"';
 8397:             $wafoff = ' checked="checked"';
 8398:             $showtable = ' style="display:none";';
 8399:             if ($showdom) {
 8400:                 $wafon = $wafoff;
 8401:                 $wafoff = '';
 8402:                 $showtable = ' style="display:inline;"';
 8403:             }
 8404:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8405:             $datatable = '<tr'.$css_class.'>'.
 8406:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8407:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8408:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8409:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8410:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8411:                          &mt('No').'</label></span></td>'.
 8412:                          '<td class="LC_left_item">'.
 8413:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8414:                          '</table></td></tr>';
 8415:             $itemcount++;
 8416:         }
 8417:         if (keys(%otherdoms)) {
 8418:             foreach my $key (sort(keys(%otherdoms))) {
 8419:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8420:                 $datatable .= '<tr'.$css_class.'>'.
 8421:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8422:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8423:                               '</table></td></tr>';
 8424:                 $itemcount++;
 8425:             }
 8426:         }
 8427:     } else {
 8428:         my %ip_methods = &remoteip_methods();
 8429:         if ($setdom) {
 8430:             $itemcount ++;
 8431:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8432:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8433:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8434:             $wafstyle = ' style="display:none;"';
 8435:             $nowafstyle = ' style="display:table-row;"';
 8436:             $currwafdisplay = ' style="display: none"';
 8437:             $wafrangestyle = ' style="display: none"';
 8438:             $curr_remotip = 'n';
 8439:             $ssltossl = ' checked="checked"';
 8440:             if ($showdom) {
 8441:                 $wafstyle = ' style="display:table-row;"';
 8442:                 $nowafstyle =  ' style="display:none;"';
 8443:                 if (keys(%{$values{$dom}})) {
 8444:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8445:                         $curr_remotip = $values{$dom}{remoteip};
 8446:                     }
 8447:                     if ($curr_remotip eq 'h') {
 8448:                         $currwafdisplay = ' style="display:table-row"';
 8449:                         $wafrangestyle = ' style="display:inline-block;"';
 8450:                     }
 8451:                     if ($values{$dom}{'sslopt'}) {
 8452:                         $alltossl = ' checked="checked"';
 8453:                         $ssltossl = '';
 8454:                     }
 8455:                 }
 8456:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8457:                     $vpndircheck = ' checked="checked"';
 8458:                     $currwafvpn = ' style="display:table-row;"';
 8459:                     $wafrangestyle = ' style="display:inline-block;"';
 8460:                 } else {
 8461:                     $vpnaliascheck = ' checked="checked"';
 8462:                     $currwafvpn = ' style="display:none;"';
 8463:                 }
 8464:             }
 8465:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8466:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8467:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8468:                           '</tr>'.
 8469:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8470:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8471:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8472:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8473:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8474:                           '<td class="LC_left_item"><table>'.
 8475:                           '<tr>'.
 8476:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8477:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8478:             foreach my $option ('m','h','n') {
 8479:                 my $sel;
 8480:                 if ($option eq $curr_remotip) {
 8481:                    $sel = ' selected="selected"';
 8482:                 }
 8483:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8484:                               $ip_methods{$option}.'</option>';
 8485:             }
 8486:             $datatable .= '</select></td></tr>'."\n".
 8487:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8488:                           $lt{'ipheader'}.':&nbsp;'.
 8489:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8490:                           'name="wafproxy_ipheader" />'.
 8491:                           '</td></tr>'."\n".
 8492:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8493:                           $lt{'trusted'}.':<br />'.
 8494:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8495:                           $values{$dom}{'trusted'}.'</textarea>'.
 8496:                           '</td></tr>'."\n".
 8497:                           '<tr><td><hr /></td></tr>'."\n".
 8498:                           '<tr>'.
 8499:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8500:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8501:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8502:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8503:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8504:             foreach my $item ('vpnint','vpnext') {
 8505:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8506:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8507:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8508:                               $values{$dom}{$item}.'</textarea>'.
 8509:                               '</td></tr>'."\n";
 8510:             }
 8511:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8512:                           '<tr>'.
 8513:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8514:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8515:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8516:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8517:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8518:                           '</table></td></tr>';
 8519:         }
 8520:         if (keys(%otherdoms)) {
 8521:             foreach my $domain (sort(keys(%otherdoms))) {
 8522:                 $itemcount ++;
 8523:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8524:                 $datatable .= '<tr'.$css_class.'>'.
 8525:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8526:                               '<td class="LC_left_item"><table>';
 8527:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8528:                     my $showval = &mt('None');
 8529:                     if ($item eq 'ssl') {
 8530:                         $showval = $lt{'ssltossl'};
 8531:                     }
 8532:                     if ($values{$domain}{$item}) {
 8533:                         $showval = $values{$domain}{$item};
 8534:                         if ($item eq 'ssl') {
 8535:                             $showval = $lt{'alltossl'};
 8536:                         } elsif ($item eq 'remoteip') {
 8537:                             $showval = $ip_methods{$values{$domain}{$item}};
 8538:                         }
 8539:                     }
 8540:                     $datatable .= '<tr>'.
 8541:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8542:                 }
 8543:                 $datatable .= '</table></td></tr>';
 8544:             }
 8545:         }
 8546:     }
 8547:     $$rowtotal += $itemcount;
 8548:     return $datatable;
 8549: }
 8550: 
 8551: sub wafproxy_titles {
 8552:     return &Apache::lonlocal::texthash(
 8553:                remoteip   => "Method for determining user's IP",
 8554:                ipheader   => 'Request header containing remote IP',
 8555:                trusted    => 'Trusted IP range(s)',
 8556:                vpnaccess  => 'Access from institutional VPN',
 8557:                vpndirect  => 'via regular hostname (no WAF)',
 8558:                vpnaliased => 'via aliased hostname (WAF)',
 8559:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8560:                vpnext     => 'IP Range(s) for backend WAF connections',
 8561:                sslopt     => 'Forwarding http/https',
 8562:                alltossl   => 'WAF forwards both http and https requests to https',
 8563:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8564:            );
 8565: }
 8566: 
 8567: sub remoteip_methods {
 8568:     return &Apache::lonlocal::texthash(
 8569:               m => 'Use Apache mod_remoteip',
 8570:               h => 'Use headers parsed by LON-CAPA',
 8571:               n => 'Not in use',
 8572:            );
 8573: }
 8574: 
 8575: sub print_usersessions {
 8576:     my ($position,$dom,$settings,$rowtotal) = @_;
 8577:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8578:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8579:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8580: 
 8581:     my @alldoms = &Apache::lonnet::all_domains();
 8582:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8583:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8584:     my %altids = &id_for_thisdom(%servers);
 8585:     if ($position eq 'top') {
 8586:         if (keys(%serverhomes) > 1) {
 8587:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8588:             my ($curroffloadnow,$curroffloadoth);
 8589:             if (ref($settings) eq 'HASH') {
 8590:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8591:                     $curroffloadnow = $settings->{'offloadnow'};
 8592:                 }
 8593:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8594:                     $curroffloadoth = $settings->{'offloadoth'};
 8595:                 }
 8596:             }
 8597:             my $other_insts = scalar(keys(%by_location));
 8598:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8599:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8600:         } else {
 8601:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8602:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8603:                           '</td></tr>';
 8604:         }
 8605:     } else {
 8606:         my %titles = &usersession_titles();
 8607:         my ($prefix,@types);
 8608:         if ($position eq 'bottom') {
 8609:             $prefix = 'remote';
 8610:             @types = ('version','excludedomain','includedomain');
 8611:         } else {
 8612:             $prefix = 'hosted';
 8613:             @types = ('excludedomain','includedomain');
 8614:         }
 8615:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8616:     }
 8617:     $$rowtotal += $itemcount;
 8618:     return $datatable;
 8619: }
 8620: 
 8621: sub rules_by_location {
 8622:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8623:     my ($datatable,$itemcount,$css_class);
 8624:     if (keys(%{$by_location}) == 0) {
 8625:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8626:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8627:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8628:                      '</td></tr>';
 8629:         $itemcount = 1;
 8630:     } else {
 8631:         $itemcount = 0;
 8632:         my $numinrow = 5;
 8633:         my (%current,%checkedon,%checkedoff);
 8634:         my @locations = sort(keys(%{$by_location}));
 8635:         foreach my $type (@{$types}) {
 8636:             $checkedon{$type} = '';
 8637:             $checkedoff{$type} = ' checked="checked"';
 8638:         }
 8639:         if (ref($settings) eq 'HASH') {
 8640:             if (ref($settings->{$prefix}) eq 'HASH') {
 8641:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8642:                     $current{$key} = $settings->{$prefix}{$key};
 8643:                     if ($key eq 'version') {
 8644:                         if ($current{$key} ne '') {
 8645:                             $checkedon{$key} = ' checked="checked"';
 8646:                             $checkedoff{$key} = '';
 8647:                         }
 8648:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8649:                         $checkedon{$key} = ' checked="checked"';
 8650:                         $checkedoff{$key} = '';
 8651:                     }
 8652:                 }
 8653:             }
 8654:         }
 8655:         foreach my $type (@{$types}) {
 8656:             next if ($type ne 'version' && !@locations);
 8657:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8658:             $datatable .= '<tr'.$css_class.'>
 8659:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8660:                            <span class="LC_nobreak">&nbsp;
 8661:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8662:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8663:             if ($type eq 'version') {
 8664:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8665:                 my $selector = '<select name="'.$prefix.'_version">';
 8666:                 foreach my $version (@lcversions) {
 8667:                     my $selected = '';
 8668:                     if ($current{'version'} eq $version) {
 8669:                         $selected = ' selected="selected"';
 8670:                     }
 8671:                     $selector .= ' <option value="'.$version.'"'.
 8672:                                  $selected.'>'.$version.'</option>';
 8673:                 }
 8674:                 $selector .= '</select> ';
 8675:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8676:             } else {
 8677:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8678:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8679:                              ' />'.('&nbsp;'x2).
 8680:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8681:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8682:                              "\n".
 8683:                              '</div><div><table>';
 8684:                 my $rem;
 8685:                 for (my $i=0; $i<@locations; $i++) {
 8686:                     my ($showloc,$value,$checkedtype);
 8687:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8688:                         my $ip = $by_location->{$locations[$i]}->[0];
 8689:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8690:                             $value = join(':',@{$by_ip->{$ip}});
 8691:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8692:                             if (ref($current{$type}) eq 'ARRAY') {
 8693:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8694:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8695:                                         $checkedtype = ' checked="checked"';
 8696:                                         last;
 8697:                                     }
 8698:                                 }
 8699:                             }
 8700:                         }
 8701:                     }
 8702:                     $rem = $i%($numinrow);
 8703:                     if ($rem == 0) {
 8704:                         if ($i > 0) {
 8705:                             $datatable .= '</tr>';
 8706:                         }
 8707:                         $datatable .= '<tr>';
 8708:                     }
 8709:                     $datatable .= '<td class="LC_left_item">'.
 8710:                                   '<span class="LC_nobreak"><label>'.
 8711:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8712:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8713:                                   '</label></span></td>';
 8714:                 }
 8715:                 $rem = @locations%($numinrow);
 8716:                 my $colsleft = $numinrow - $rem;
 8717:                 if ($colsleft > 1 ) {
 8718:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8719:                                   '&nbsp;</td>';
 8720:                 } elsif ($colsleft == 1) {
 8721:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8722:                 }
 8723:                 $datatable .= '</tr></table>';
 8724:             }
 8725:             $datatable .= '</td></tr>';
 8726:             $itemcount ++;
 8727:         }
 8728:     }
 8729:     return ($datatable,$itemcount);
 8730: }
 8731: 
 8732: sub print_ssl {
 8733:     my ($position,$dom,$settings,$rowtotal) = @_;
 8734:     my ($css_class,$datatable);
 8735:     my $itemcount = 1;
 8736:     if ($position eq 'top') {
 8737:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8738:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8739:         my $same_institution;
 8740:         if ($intdom ne '') {
 8741:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8742:             if (ref($internet_names) eq 'ARRAY') {
 8743:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8744:                     $same_institution = 1;
 8745:                 }
 8746:             }
 8747:         }
 8748:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8749:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8750:         if ($same_institution) {
 8751:             my %domservers = &Apache::lonnet::get_servers($dom);
 8752:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8753:         } else {
 8754:             $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.");
 8755:         }
 8756:         $datatable .= '</td></tr>';
 8757:         $itemcount ++;
 8758:     } else {
 8759:         my %titles = &ssl_titles();
 8760:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8761:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8762:         my @alldoms = &Apache::lonnet::all_domains();
 8763:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8764:         my @domservers = &Apache::lonnet::get_servers($dom);
 8765:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8766:         my %altids = &id_for_thisdom(%servers);
 8767:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8768:             my $legacy;
 8769:             unless (ref($settings) eq 'HASH') {
 8770:                 my $name;
 8771:                 if ($position eq 'connto') {
 8772:                     $name = 'loncAllowInsecure';
 8773:                 } else {
 8774:                     $name = 'londAllowInsecure';
 8775:                 }
 8776:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8777:                 my @ids=&Apache::lonnet::current_machine_ids();
 8778:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8779:                     my %what = (
 8780:                                    $name => 1,
 8781:                                );
 8782:                     my ($result,$returnhash) =
 8783:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8784:                     if ($result eq 'ok') {
 8785:                         if (ref($returnhash) eq 'HASH') {
 8786:                             $legacy = $returnhash->{$name};
 8787:                         }
 8788:                     }
 8789:                 } else {
 8790:                     $legacy = $Apache::lonnet::perlvar{$name};
 8791:                 }
 8792:             }
 8793:             foreach my $type ('dom','intdom','other') {
 8794:                 my %checked;
 8795:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8796:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8797:                               '<td class="LC_right_item">';
 8798:                 my $skip; 
 8799:                 if ($type eq 'dom') {
 8800:                     unless (keys(%servers) > 1) {
 8801:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8802:                         $skip = 1;
 8803:                     }
 8804:                 }
 8805:                 if ($type eq 'intdom') {
 8806:                     unless (@instdoms > 1) {
 8807:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8808:                         $skip = 1;
 8809:                     } 
 8810:                 } elsif ($type eq 'other') {
 8811:                     if (keys(%by_location) == 0) {
 8812:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8813:                         $skip = 1;
 8814:                     }
 8815:                 }
 8816:                 unless ($skip) {
 8817:                     $checked{'yes'} = ' checked="checked"'; 
 8818:                     if (ref($settings) eq 'HASH') {
 8819:                         if (ref($settings->{$position}) eq 'HASH') {
 8820:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8821:                                 $checked{$1} = $checked{'yes'};
 8822:                                 delete($checked{'yes'}); 
 8823:                             }
 8824:                         }
 8825:                     } else {
 8826:                         if ($legacy == 0) {
 8827:                             $checked{'req'} = $checked{'yes'};
 8828:                             delete($checked{'yes'});    
 8829:                         }
 8830:                     }
 8831:                     foreach my $option ('no','yes','req') {
 8832:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8833:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8834:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8835:                                       '</label></span>'.('&nbsp;'x2);
 8836:                     }
 8837:                 }
 8838:                 $datatable .= '</td></tr>';
 8839:                 $itemcount ++; 
 8840:             }
 8841:         } else {
 8842:             my $prefix = 'replication';
 8843:             my @types = ('certreq','nocertreq');
 8844:             if (keys(%by_location) == 0) {
 8845:                 $datatable .= '<tr'.$css_class.'><td>'.
 8846:                               &mt('Nothing to set here, as there are no other institutions').
 8847:                               '</td></tr>';
 8848:                 $itemcount ++;
 8849:             } else {
 8850:                 ($datatable,$itemcount) = 
 8851:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8852:             }
 8853:         }
 8854:     }
 8855:     $$rowtotal += $itemcount;
 8856:     return $datatable;
 8857: }
 8858: 
 8859: sub ssl_titles {
 8860:     return &Apache::lonlocal::texthash (
 8861:                dom           => 'LON-CAPA servers/VMs from same domain',
 8862:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8863:                other         => 'External LON-CAPA servers/VMs',
 8864:                connto        => 'Connections to other servers',
 8865:                connfrom      => 'Connections from other servers',
 8866:                replication   => 'Replicating content to other institutions',
 8867:                certreq       => 'Client certificate required, but specific domains exempt',
 8868:                nocertreq     => 'No client certificate required, except for specific domains',
 8869:                no            => 'SSL not used',
 8870:                yes           => 'SSL Optional (used if available)',
 8871:                req           => 'SSL Required',
 8872:     );
 8873: }
 8874: 
 8875: sub print_trust {
 8876:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8877:     my ($css_class,$datatable,%checked,%choices);
 8878:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8879:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8880:     my $itemcount = 1;
 8881:     my %titles = &trust_titles();
 8882:     my @types = ('exc','inc');
 8883:     if ($prefix eq 'top') {
 8884:         $prefix = 'content';
 8885:     } elsif ($prefix eq 'bottom') {
 8886:         $prefix = 'msg';
 8887:     }
 8888:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8889:     $$rowtotal += $itemcount;
 8890:     return $datatable;
 8891: }
 8892: 
 8893: sub trust_titles {
 8894:     return &Apache::lonlocal::texthash(
 8895:                content  => "Access to this domain's content by others",
 8896:                shared   => "Access to other domain's content by this domain",
 8897:                enroll   => "Enrollment in this domain's courses by others", 
 8898:                othcoau  => "Co-author roles in this domain for others",
 8899:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8900:                domroles => "Domain roles in this domain assignable to others",
 8901:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8902:                reqcrs   => "Requests for creation of courses in this domain by others",
 8903:                msg      => "Users in other domains can send messages to this domain",
 8904:                exc      => "Allow all, but exclude specific domains",
 8905:                inc      => "Deny all, but include specific domains",
 8906:            );
 8907: } 
 8908: 
 8909: sub build_location_hashes {
 8910:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8911:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8912:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8913:     my %iphost = &Apache::lonnet::get_iphost();
 8914:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8915:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8916:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8917:         foreach my $id (@{$iphost{$primary_ip}}) {
 8918:             my $intdom = &Apache::lonnet::internet_dom($id);
 8919:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8920:                 push(@{$intdoms},$intdom);
 8921:             }
 8922:         }
 8923:     }
 8924:     foreach my $ip (keys(%iphost)) {
 8925:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8926:             foreach my $id (@{$iphost{$ip}}) {
 8927:                 my $location = &Apache::lonnet::internet_dom($id);
 8928:                 if ($location) {
 8929:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8930:                         my $dom = &Apache::lonnet::host_domain($id);
 8931:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8932:                             push(@{$instdoms},$dom);
 8933:                         }
 8934:                         next;
 8935:                     }
 8936:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8937:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8938:                             push(@{$by_ip->{$ip}},$location);
 8939:                         }
 8940:                     } else {
 8941:                         $by_ip->{$ip} = [$location];
 8942:                     }
 8943:                 }
 8944:             }
 8945:         }
 8946:     }
 8947:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8948:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8949:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8950:             my $first = $by_ip->{$ip}->[0];
 8951:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8952:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8953:                     push(@{$by_location->{$first}},$ip);
 8954:                 }
 8955:             } else {
 8956:                 $by_location->{$first} = [$ip];
 8957:             }
 8958:         }
 8959:     }
 8960:     return;
 8961: }
 8962: 
 8963: sub current_offloads_to {
 8964:     my ($dom,$settings,$servers) = @_;
 8965:     my (%spareid,%otherdomconfigs);
 8966:     if (ref($servers) eq 'HASH') {
 8967:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8968:             my $gotspares;
 8969:             if (ref($settings) eq 'HASH') {
 8970:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8971:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8972:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8973:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8974:                         $gotspares = 1;
 8975:                     }
 8976:                 }
 8977:             }
 8978:             unless ($gotspares) {
 8979:                 my $gotspares;
 8980:                 my $serverhomeID =
 8981:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8982:                 my $serverhomedom =
 8983:                     &Apache::lonnet::host_domain($serverhomeID);
 8984:                 if ($serverhomedom ne $dom) {
 8985:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8986:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8987:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8988:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8989:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8990:                                 $gotspares = 1;
 8991:                             }
 8992:                         }
 8993:                     } else {
 8994:                         $otherdomconfigs{$serverhomedom} =
 8995:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8996:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8997:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8998:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8999:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 9000:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9001:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9002:                                         $gotspares = 1;
 9003:                                     }
 9004:                                 }
 9005:                             }
 9006:                         }
 9007:                     }
 9008:                 }
 9009:             }
 9010:             unless ($gotspares) {
 9011:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9012:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9013:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9014:                } else {
 9015:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 9016:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 9017:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9018:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9019:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9020:                     } else {
 9021:                         my %what = (
 9022:                              spareid => 1,
 9023:                         );
 9024:                         my ($result,$returnhash) = 
 9025:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 9026:                         if ($result eq 'ok') { 
 9027:                             if (ref($returnhash) eq 'HASH') {
 9028:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 9029:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 9030:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 9031:                                 }
 9032:                             }
 9033:                         }
 9034:                     }
 9035:                 }
 9036:             }
 9037:         }
 9038:     }
 9039:     return %spareid;
 9040: }
 9041: 
 9042: sub spares_row {
 9043:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 9044:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 9045:     my $css_class;
 9046:     my $numinrow = 4;
 9047:     my $itemcount = 1;
 9048:     my $datatable;
 9049:     my %typetitles = &sparestype_titles();
 9050:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9051:         foreach my $server (sort(keys(%{$servers}))) {
 9052:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 9053:             my ($othercontrol,$serverdom);
 9054:             if ($serverhome ne $server) {
 9055:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 9056:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9057:             } else {
 9058:                 $serverdom = &Apache::lonnet::host_domain($server);
 9059:                 if ($serverdom ne $dom) {
 9060:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9061:                 }
 9062:             }
 9063:             next unless (ref($spareid->{$server}) eq 'HASH');
 9064:             my ($checkednow,$checkedoth);
 9065:             if (ref($curroffloadnow) eq 'HASH') {
 9066:                 if ($curroffloadnow->{$server}) {
 9067:                     $checkednow = ' checked="checked"';
 9068:                 }
 9069:             }
 9070:             if (ref($curroffloadoth) eq 'HASH') {
 9071:                 if ($curroffloadoth->{$server}) {
 9072:                     $checkedoth = ' checked="checked"';
 9073:                 }
 9074:             }
 9075:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 9076:             $datatable .= '<tr'.$css_class.'>
 9077:                            <td rowspan="2">
 9078:                             <span class="LC_nobreak">'.
 9079:                           &mt('[_1] when busy, offloads to:'
 9080:                               ,'<b>'.$server.'</b>').'</span><br />'.
 9081:                           '<span class="LC_nobreak">'."\n".
 9082:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 9083:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 9084:                           "\n";
 9085:             if ($other_insts) {
 9086:                 $datatable .= '<br />'.
 9087:                               '<span class="LC_nobreak">'."\n".
 9088:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 9089:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 9090:                           "\n";
 9091:             }
 9092:             my (%current,%canselect);
 9093:             my @choices = 
 9094:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 9095:             foreach my $type ('primary','default') {
 9096:                 if (ref($spareid->{$server}) eq 'HASH') {
 9097:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 9098:                         my @spares = @{$spareid->{$server}{$type}};
 9099:                         if (@spares > 0) {
 9100:                             if ($othercontrol) {
 9101:                                 $current{$type} = join(', ',@spares);
 9102:                             } else {
 9103:                                 $current{$type} .= '<table>';
 9104:                                 my $numspares = scalar(@spares);
 9105:                                 for (my $i=0;  $i<@spares; $i++) {
 9106:                                     my $rem = $i%($numinrow);
 9107:                                     if ($rem == 0) {
 9108:                                         if ($i > 0) {
 9109:                                             $current{$type} .= '</tr>';
 9110:                                         }
 9111:                                         $current{$type} .= '<tr>';
 9112:                                     }
 9113:                                     $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;'.
 9114:                                                        $spareid->{$server}{$type}[$i].
 9115:                                                        '</label></td>'."\n";
 9116:                                 }
 9117:                                 my $rem = @spares%($numinrow);
 9118:                                 my $colsleft = $numinrow - $rem;
 9119:                                 if ($colsleft > 1 ) {
 9120:                                     $current{$type} .= '<td colspan="'.$colsleft.
 9121:                                                        '" class="LC_left_item">'.
 9122:                                                        '&nbsp;</td>';
 9123:                                 } elsif ($colsleft == 1) {
 9124:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 9125:                                 }
 9126:                                 $current{$type} .= '</tr></table>';
 9127:                             }
 9128:                         }
 9129:                     }
 9130:                     if ($current{$type} eq '') {
 9131:                         $current{$type} = &mt('None specified');
 9132:                     }
 9133:                     if ($othercontrol) {
 9134:                         if ($type eq 'primary') {
 9135:                             $canselect{$type} = $othercontrol;
 9136:                         }
 9137:                     } else {
 9138:                         $canselect{$type} = 
 9139:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 9140:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 9141:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 9142:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 9143:                         if (@choices > 0) {
 9144:                             foreach my $lonhost (@choices) {
 9145:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 9146:                             }
 9147:                         }
 9148:                         $canselect{$type} .= '</select>'."\n";
 9149:                     }
 9150:                 } else {
 9151:                     $current{$type} = &mt('Could not be determined');
 9152:                     if ($type eq 'primary') {
 9153:                         $canselect{$type} =  $othercontrol;
 9154:                     }
 9155:                 }
 9156:                 if ($type eq 'default') {
 9157:                     $datatable .= '<tr'.$css_class.'>';
 9158:                 }
 9159:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 9160:                               '<td>'.$current{$type}.'</td>'."\n".
 9161:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 9162:             }
 9163:             $itemcount ++;
 9164:         }
 9165:     }
 9166:     $$rowtotal += $itemcount;
 9167:     return $datatable;
 9168: }
 9169: 
 9170: sub possible_newspares {
 9171:     my ($server,$currspares,$serverhomes,$altids) = @_;
 9172:     my $serverhostname = &Apache::lonnet::hostname($server);
 9173:     my %excluded;
 9174:     if ($serverhostname ne '') {
 9175:         %excluded = (
 9176:                        $serverhostname => 1,
 9177:                     );
 9178:     }
 9179:     if (ref($currspares) eq 'HASH') {
 9180:         foreach my $type (keys(%{$currspares})) {
 9181:             if (ref($currspares->{$type}) eq 'ARRAY') {
 9182:                 if (@{$currspares->{$type}} > 0) {
 9183:                     foreach my $curr (@{$currspares->{$type}}) {
 9184:                         my $hostname = &Apache::lonnet::hostname($curr);
 9185:                         $excluded{$hostname} = 1;
 9186:                     }
 9187:                 }
 9188:             }
 9189:         }
 9190:     }
 9191:     my @choices;
 9192:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9193:         if (keys(%{$serverhomes}) > 1) {
 9194:             foreach my $name (sort(keys(%{$serverhomes}))) {
 9195:                 unless ($excluded{$name}) {
 9196:                     if (exists($altids->{$serverhomes->{$name}})) {
 9197:                         push(@choices,$altids->{$serverhomes->{$name}});
 9198:                     } else {
 9199:                         push(@choices,$serverhomes->{$name});
 9200:                     }
 9201:                 }
 9202:             }
 9203:         }
 9204:     }
 9205:     return sort(@choices);
 9206: }
 9207: 
 9208: sub print_loadbalancing {
 9209:     my ($dom,$settings,$rowtotal) = @_;
 9210:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9211:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9212:     my $numinrow = 1;
 9213:     my $datatable;
 9214:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9215:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 9216:     if (ref($settings) eq 'HASH') {
 9217:         %existing = %{$settings};
 9218:     }
 9219:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 9220:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9221:                                   \%currtargets,\%currrules,\%currcookies);
 9222:     } else {
 9223:         return;
 9224:     }
 9225:     my ($othertitle,$usertypes,$types) =
 9226:         &Apache::loncommon::sorted_inst_types($dom);
 9227:     my $rownum = 8;
 9228:     if (ref($types) eq 'ARRAY') {
 9229:         $rownum += scalar(@{$types});
 9230:     }
 9231:     my @css_class = ('LC_odd_row','LC_even_row');
 9232:     my $balnum = 0;
 9233:     my $islast;
 9234:     my (@toshow,$disabledtext);
 9235:     if (keys(%currbalancer) > 0) {
 9236:         @toshow = sort(keys(%currbalancer));
 9237:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 9238:             push(@toshow,'');
 9239:         }
 9240:     } else {
 9241:         @toshow = ('');
 9242:         $disabledtext = &mt('No existing load balancer');
 9243:     }
 9244:     foreach my $lonhost (@toshow) {
 9245:         if ($balnum == scalar(@toshow)-1) {
 9246:             $islast = 1;
 9247:         } else {
 9248:             $islast = 0;
 9249:         }
 9250:         my $cssidx = $balnum%2;
 9251:         my $targets_div_style = 'display: none';
 9252:         my $disabled_div_style = 'display: block';
 9253:         my $homedom_div_style = 'display: none';
 9254:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 9255:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9256:                       '<p>';
 9257:         if ($lonhost eq '') {
 9258:             $datatable .= '<span class="LC_nobreak">';
 9259:             if (keys(%currbalancer) > 0) {
 9260:                 $datatable .= &mt('Add balancer:');
 9261:             } else {
 9262:                 $datatable .= &mt('Enable balancer:');
 9263:             }
 9264:             $datatable .= '&nbsp;'.
 9265:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9266:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9267:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9268:                           '<option value="" selected="selected">'.&mt('None').
 9269:                           '</option>'."\n";
 9270:             foreach my $server (sort(keys(%servers))) {
 9271:                 next if ($currbalancer{$server});
 9272:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9273:             }
 9274:             $datatable .=
 9275:                 '</select>'."\n".
 9276:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9277:         } else {
 9278:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9279:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9280:                            &mt('Stop balancing').'</label>'.
 9281:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9282:             $targets_div_style = 'display: block';
 9283:             $disabled_div_style = 'display: none';
 9284:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9285:                 $homedom_div_style = 'display: block';
 9286:             }
 9287:         }
 9288:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9289:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9290:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9291:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9292:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9293:         my @sparestypes = ('primary','default');
 9294:         my %typetitles = &sparestype_titles();
 9295:         my %hostherechecked = (
 9296:                                   no => ' checked="checked"',
 9297:                               );
 9298:         my %balcookiechecked = (
 9299:                                   no => ' checked="checked"',
 9300:                                );
 9301:         foreach my $sparetype (@sparestypes) {
 9302:             my $targettable;
 9303:             for (my $i=0; $i<$numspares; $i++) {
 9304:                 my $checked;
 9305:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9306:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9307:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9308:                             $checked = ' checked="checked"';
 9309:                         }
 9310:                     }
 9311:                 }
 9312:                 my ($chkboxval,$disabled);
 9313:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9314:                     $chkboxval = $spares[$i];
 9315:                 }
 9316:                 if (exists($currbalancer{$spares[$i]})) {
 9317:                     $disabled = ' disabled="disabled"';
 9318:                 }
 9319:                 $targettable .=
 9320:                     '<td><span class="LC_nobreak"><label>'.
 9321:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9322:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9323:                     '</span></label></span></td>';
 9324:                 my $rem = $i%($numinrow);
 9325:                 if ($rem == 0) {
 9326:                     if (($i > 0) && ($i < $numspares-1)) {
 9327:                         $targettable .= '</tr>';
 9328:                     }
 9329:                     if ($i < $numspares-1) {
 9330:                         $targettable .= '<tr>';
 9331:                     }
 9332:                 }
 9333:             }
 9334:             if ($targettable ne '') {
 9335:                 my $rem = $numspares%($numinrow);
 9336:                 my $colsleft = $numinrow - $rem;
 9337:                 if ($colsleft > 1 ) {
 9338:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9339:                                     '&nbsp;</td>';
 9340:                 } elsif ($colsleft == 1) {
 9341:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9342:                 }
 9343:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9344:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9345:             }
 9346:             $hostherechecked{$sparetype} = '';
 9347:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9348:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9349:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9350:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9351:                         $hostherechecked{'no'} = '';
 9352:                     }
 9353:                 }
 9354:             }
 9355:         }
 9356:         if ($currcookies{$lonhost}) {
 9357:             %balcookiechecked = (
 9358:                                     yes => ' checked="checked"',
 9359:                                 );
 9360:         }
 9361:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9362:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9363:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9364:         foreach my $sparetype (@sparestypes) {
 9365:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9366:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9367:                           '</i></label><br />';
 9368:         }
 9369:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9370:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9371:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9372:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9373:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9374:                       '</div></td></tr>'.
 9375:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9376:                                            $othertitle,$usertypes,$types,\%servers,
 9377:                                            \%currbalancer,$lonhost,
 9378:                                            $targets_div_style,$homedom_div_style,
 9379:                                            $css_class[$cssidx],$balnum,$islast);
 9380:         $$rowtotal += $rownum;
 9381:         $balnum ++;
 9382:     }
 9383:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9384:     return $datatable;
 9385: }
 9386: 
 9387: sub get_loadbalancers_config {
 9388:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9389:     return unless ((ref($servers) eq 'HASH') &&
 9390:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9391:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9392:                    (ref($currcookies) eq 'HASH'));
 9393:     if (keys(%{$existing}) > 0) {
 9394:         my $oldlonhost;
 9395:         foreach my $key (sort(keys(%{$existing}))) {
 9396:             if ($key eq 'lonhost') {
 9397:                 $oldlonhost = $existing->{'lonhost'};
 9398:                 $currbalancer->{$oldlonhost} = 1;
 9399:             } elsif ($key eq 'targets') {
 9400:                 if ($oldlonhost) {
 9401:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9402:                 }
 9403:             } elsif ($key eq 'rules') {
 9404:                 if ($oldlonhost) {
 9405:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9406:                 }
 9407:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9408:                 $currbalancer->{$key} = 1;
 9409:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9410:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9411:                 if ($existing->{$key}{'cookie'}) {
 9412:                     $currcookies->{$key} = 1;
 9413:                 }
 9414:             }
 9415:         }
 9416:     } else {
 9417:         my ($balancerref,$targetsref) =
 9418:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9419:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9420:             foreach my $server (sort(keys(%{$balancerref}))) {
 9421:                 $currbalancer->{$server} = 1;
 9422:                 $currtargets->{$server} = $targetsref->{$server};
 9423:             }
 9424:         }
 9425:     }
 9426:     return;
 9427: }
 9428: 
 9429: sub loadbalancing_rules {
 9430:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9431:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9432:         $css_class,$balnum,$islast) = @_;
 9433:     my $output;
 9434:     my $num = 0;
 9435:     my ($alltypes,$othertypes,$titles) =
 9436:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9437:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9438:         foreach my $type (@{$alltypes}) {
 9439:             $num ++;
 9440:             my $current;
 9441:             if (ref($currrules) eq 'HASH') {
 9442:                 $current = $currrules->{$type};
 9443:             }
 9444:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9445:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9446:                     $current = '';
 9447:                 }
 9448:             }
 9449:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9450:                                              $servers,$currbalancer,$lonhost,$dom,
 9451:                                              $targets_div_style,$homedom_div_style,
 9452:                                              $css_class,$balnum,$num,$islast);
 9453:         }
 9454:     }
 9455:     return $output;
 9456: }
 9457: 
 9458: sub loadbalancing_titles {
 9459:     my ($dom,$intdom,$usertypes,$types) = @_;
 9460:     my %othertypes = (
 9461:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9462:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9463:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9464:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9465:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9466:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9467:                      );
 9468:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9469:     my @available;
 9470:     if (ref($types) eq 'ARRAY') {
 9471:         @available = @{$types};
 9472:     }
 9473:     unless (grep(/^default$/,@available)) {
 9474:         push(@available,'default');
 9475:     }
 9476:     unshift(@alltypes,@available);
 9477:     my %titles;
 9478:     foreach my $type (@alltypes) {
 9479:         if ($type =~ /^_LC_/) {
 9480:             $titles{$type} = $othertypes{$type};
 9481:         } elsif ($type eq 'default') {
 9482:             $titles{$type} = &mt('All users from [_1]',$dom);
 9483:             if (ref($types) eq 'ARRAY') {
 9484:                 if (@{$types} > 0) {
 9485:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9486:                 }
 9487:             }
 9488:         } elsif (ref($usertypes) eq 'HASH') {
 9489:             $titles{$type} = $usertypes->{$type};
 9490:         }
 9491:     }
 9492:     return (\@alltypes,\%othertypes,\%titles);
 9493: }
 9494: 
 9495: sub loadbalance_rule_row {
 9496:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9497:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9498:     my @rulenames;
 9499:     my %ruletitles = &offloadtype_text();
 9500:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9501:         @rulenames = ('balancer','offloadedto','specific');
 9502:     } else {
 9503:         @rulenames = ('default','homeserver');
 9504:         if ($type eq '_LC_external') {
 9505:             push(@rulenames,'externalbalancer');
 9506:         } else {
 9507:             push(@rulenames,'specific');
 9508:         }
 9509:         push(@rulenames,'none');
 9510:     }
 9511:     my $style = $targets_div_style;
 9512:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9513:         $style = $homedom_div_style;
 9514:     }
 9515:     my $space;
 9516:     if ($islast && $num == 1) {
 9517:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9518:     }
 9519:     my $output =
 9520:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9521:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9522:         '<td valaign="top">'.$space.
 9523:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9524:     for (my $i=0; $i<@rulenames; $i++) {
 9525:         my $rule = $rulenames[$i];
 9526:         my ($checked,$extra);
 9527:         if ($rulenames[$i] eq 'default') {
 9528:             $rule = '';
 9529:         }
 9530:         if ($rulenames[$i] eq 'specific') {
 9531:             if (ref($servers) eq 'HASH') {
 9532:                 my $default;
 9533:                 if (($current ne '') && (exists($servers->{$current}))) {
 9534:                     $checked = ' checked="checked"';
 9535:                 }
 9536:                 unless ($checked) {
 9537:                     $default = ' selected="selected"';
 9538:                 }
 9539:                 $extra =
 9540:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9541:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9542:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9543:                     '<option value=""'.$default.'></option>'."\n";
 9544:                 foreach my $server (sort(keys(%{$servers}))) {
 9545:                     if (ref($currbalancer) eq 'HASH') {
 9546:                         next if (exists($currbalancer->{$server}));
 9547:                     }
 9548:                     my $selected;
 9549:                     if ($server eq $current) {
 9550:                         $selected = ' selected="selected"';
 9551:                     }
 9552:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9553:                 }
 9554:                 $extra .= '</select>';
 9555:             }
 9556:         } elsif ($rule eq $current) {
 9557:             $checked = ' checked="checked"';
 9558:         }
 9559:         $output .= '<span class="LC_nobreak"><label>'.
 9560:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9561:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9562:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9563:                    ')"'.$checked.' />&nbsp;';
 9564:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9565:             $output .= $ruletitles{'particular'};
 9566:         } else {
 9567:             $output .= $ruletitles{$rulenames[$i]};
 9568:         }
 9569:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9570:     }
 9571:     $output .= '</div></td></tr>'."\n";
 9572:     return $output;
 9573: }
 9574: 
 9575: sub offloadtype_text {
 9576:     my %ruletitles = &Apache::lonlocal::texthash (
 9577:            'default'          => 'Offloads to default destinations',
 9578:            'homeserver'       => "Offloads to user's home server",
 9579:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9580:            'specific'         => 'Offloads to specific server',
 9581:            'none'             => 'No offload',
 9582:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9583:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9584:            'particular'       => 'Session hosted (after re-auth) on server:',
 9585:     );
 9586:     return %ruletitles;
 9587: }
 9588: 
 9589: sub sparestype_titles {
 9590:     my %typestitles = &Apache::lonlocal::texthash (
 9591:                           'primary' => 'primary',
 9592:                           'default' => 'default',
 9593:                       );
 9594:     return %typestitles;
 9595: }
 9596: 
 9597: sub contact_titles {
 9598:     my %titles = &Apache::lonlocal::texthash (
 9599:                    'supportemail'    => 'Support E-mail address',
 9600:                    'adminemail'      => 'Default Server Admin E-mail address',
 9601:                    'errormail'       => 'Error reports to be e-mailed to',
 9602:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9603:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9604:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9605:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9606:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9607:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9608:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9609:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9610:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9611:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9612:                    'errorweights'    => 'Weights used to compute error count',
 9613:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9614:                  );
 9615:     my %short_titles = &Apache::lonlocal::texthash (
 9616:                            adminemail   => 'Admin E-mail address',
 9617:                            supportemail => 'Support E-mail',
 9618:                        );   
 9619:     return (\%titles,\%short_titles);
 9620: }
 9621: 
 9622: sub helpform_fields {
 9623:     my %titles =  &Apache::lonlocal::texthash (
 9624:                        'username'   => 'Name',
 9625:                        'user'       => 'Username/domain',
 9626:                        'phone'      => 'Phone',
 9627:                        'cc'         => 'Cc e-mail',
 9628:                        'course'     => 'Course Details',
 9629:                        'section'    => 'Sections',
 9630:                        'screenshot' => 'File upload',
 9631:     );
 9632:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9633:     my %possoptions = (
 9634:                         username     => ['yes','no','req'],
 9635:                         phone        => ['yes','no','req'],
 9636:                         user         => ['yes','no'],
 9637:                         cc           => ['yes','no'],
 9638:                         course       => ['yes','no'],
 9639:                         section      => ['yes','no'],
 9640:                         screenshot   => ['yes','no'],
 9641:                       );
 9642:     my %fieldoptions = &Apache::lonlocal::texthash (
 9643:                          'yes'  => 'Optional',
 9644:                          'req'  => 'Required',
 9645:                          'no'   => "Not shown",
 9646:     );
 9647:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9648: }
 9649: 
 9650: sub tool_titles {
 9651:     my %titles = &Apache::lonlocal::texthash (
 9652:                      aboutme    => 'Personal web page',
 9653:                      blog       => 'Blog',
 9654:                      portfolio  => 'Portfolio',
 9655:                      portaccess => 'Share portfolio files',
 9656:                      timezone   => 'Can set time zone',
 9657:                      official   => 'Official courses (with institutional codes)',
 9658:                      unofficial => 'Unofficial courses',
 9659:                      community  => 'Communities',
 9660:                      textbook   => 'Textbook courses',
 9661:                      placement  => 'Placement tests',
 9662:                  );
 9663:     return %titles;
 9664: }
 9665: 
 9666: sub courserequest_titles {
 9667:     my %titles = &Apache::lonlocal::texthash (
 9668:                                    official   => 'Official',
 9669:                                    unofficial => 'Unofficial',
 9670:                                    community  => 'Communities',
 9671:                                    textbook   => 'Textbook',
 9672:                                    placement  => 'Placement tests',
 9673:                                    lti        => 'LTI Provider',
 9674:                                    norequest  => 'Not allowed',
 9675:                                    approval   => 'Approval by DC',
 9676:                                    validate   => 'With validation',
 9677:                                    autolimit  => 'Numerical limit',
 9678:                                    unlimited  => '(blank for unlimited)',
 9679:                  );
 9680:     return %titles;
 9681: }
 9682: 
 9683: sub authorrequest_titles {
 9684:     my %titles = &Apache::lonlocal::texthash (
 9685:                                    norequest  => 'Not allowed',
 9686:                                    approval   => 'Approval by Dom. Coord.',
 9687:                                    automatic  => 'Automatic approval',
 9688:                  );
 9689:     return %titles;
 9690: }
 9691: 
 9692: sub courserequest_conditions {
 9693:     my %conditions = &Apache::lonlocal::texthash (
 9694:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9695:        validate   => '(Processing of request subject to institutional validation).',
 9696:                  );
 9697:     return %conditions;
 9698: }
 9699: 
 9700: 
 9701: sub print_usercreation {
 9702:     my ($position,$dom,$settings,$rowtotal) = @_;
 9703:     my $numinrow = 4;
 9704:     my $datatable;
 9705:     if ($position eq 'top') {
 9706:         $$rowtotal ++;
 9707:         my $rowcount = 0;
 9708:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9709:         if (ref($rules) eq 'HASH') {
 9710:             if (keys(%{$rules}) > 0) {
 9711:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9712:                                                 $ruleorder,$numinrow,$rowcount);
 9713:                 $$rowtotal ++;
 9714:                 $rowcount ++;
 9715:             }
 9716:         }
 9717:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9718:         if (ref($idrules) eq 'HASH') {
 9719:             if (keys(%{$idrules}) > 0) {
 9720:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9721:                                                 $idruleorder,$numinrow,$rowcount);
 9722:                 $$rowtotal ++;
 9723:                 $rowcount ++;
 9724:             }
 9725:         }
 9726:         if ($rowcount == 0) {
 9727:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9728:             $$rowtotal ++;
 9729:             $rowcount ++;
 9730:         }
 9731:     } elsif ($position eq 'middle') {
 9732:         my @creators = ('author','course','requestcrs');
 9733:         my ($rules,$ruleorder) =
 9734:             &Apache::lonnet::inst_userrules($dom,'username');
 9735:         my %lt = &usercreation_types();
 9736:         my %checked;
 9737:         if (ref($settings) eq 'HASH') {
 9738:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9739:                 foreach my $item (@creators) {
 9740:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9741:                 }
 9742:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9743:                 foreach my $item (@creators) {
 9744:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9745:                         $checked{$item} = 'none';
 9746:                     }
 9747:                 }
 9748:             }
 9749:         }
 9750:         my $rownum = 0;
 9751:         foreach my $item (@creators) {
 9752:             $rownum ++;
 9753:             if ($checked{$item} eq '') {
 9754:                 $checked{$item} = 'any';
 9755:             }
 9756:             my $css_class;
 9757:             if ($rownum%2) {
 9758:                 $css_class = '';
 9759:             } else {
 9760:                 $css_class = ' class="LC_odd_row" ';
 9761:             }
 9762:             $datatable .= '<tr'.$css_class.'>'.
 9763:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9764:                          '</span></td><td style="text-align: right">';
 9765:             my @options = ('any');
 9766:             if (ref($rules) eq 'HASH') {
 9767:                 if (keys(%{$rules}) > 0) {
 9768:                     push(@options,('official','unofficial'));
 9769:                 }
 9770:             }
 9771:             push(@options,'none');
 9772:             foreach my $option (@options) {
 9773:                 my $type = 'radio';
 9774:                 my $check = ' ';
 9775:                 if ($checked{$item} eq $option) {
 9776:                     $check = ' checked="checked" ';
 9777:                 } 
 9778:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9779:                               '<input type="'.$type.'" name="can_createuser_'.
 9780:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9781:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9782:             }
 9783:             $datatable .= '</td></tr>';
 9784:         }
 9785:     } else {
 9786:         my @contexts = ('author','course','domain');
 9787:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9788:         my %checked;
 9789:         if (ref($settings) eq 'HASH') {
 9790:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9791:                 foreach my $item (@contexts) {
 9792:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9793:                         foreach my $auth (@authtypes) {
 9794:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9795:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9796:                             }
 9797:                         }
 9798:                     }
 9799:                 }
 9800:             }
 9801:         } else {
 9802:             foreach my $item (@contexts) {
 9803:                 foreach my $auth (@authtypes) {
 9804:                     $checked{$item}{$auth} = ' checked="checked" ';
 9805:                 }
 9806:             }
 9807:         }
 9808:         my %title = &context_names();
 9809:         my %authname = &authtype_names();
 9810:         my $rownum = 0;
 9811:         my $css_class; 
 9812:         foreach my $item (@contexts) {
 9813:             if ($rownum%2) {
 9814:                 $css_class = '';
 9815:             } else {
 9816:                 $css_class = ' class="LC_odd_row" ';
 9817:             }
 9818:             $datatable .=   '<tr'.$css_class.'>'.
 9819:                             '<td>'.$title{$item}.
 9820:                             '</td><td class="LC_left_item">'.
 9821:                             '<span class="LC_nobreak">';
 9822:             foreach my $auth (@authtypes) {
 9823:                 $datatable .= '<label>'. 
 9824:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9825:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9826:                               $authname{$auth}.'</label>&nbsp;';
 9827:             }
 9828:             $datatable .= '</span></td></tr>';
 9829:             $rownum ++;
 9830:         }
 9831:         $$rowtotal += $rownum;
 9832:     }
 9833:     return $datatable;
 9834: }
 9835: 
 9836: sub print_selfcreation {
 9837:     my ($position,$dom,$settings,$rowtotal) = @_;
 9838:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9839:         $emaildomain,$datatable);
 9840:     if (ref($settings) eq 'HASH') {
 9841:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9842:             $createsettings = $settings->{'cancreate'};
 9843:             if (ref($createsettings) eq 'HASH') {
 9844:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9845:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9846:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9847:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9848:                         @selfcreate = ('email','login','sso');
 9849:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9850:                         @selfcreate = ($createsettings->{'selfcreate'});
 9851:                     }
 9852:                 }
 9853:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9854:                     $processing = $createsettings->{'selfcreateprocessing'};
 9855:                 }
 9856:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9857:                     $emailoptions = $createsettings->{'emailoptions'};
 9858:                 }
 9859:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9860:                     $emailverified = $createsettings->{'emailverified'};
 9861:                 }
 9862:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9863:                     $emaildomain = $createsettings->{'emaildomain'};
 9864:                 }
 9865:             }
 9866:         }
 9867:     }
 9868:     my %radiohash;
 9869:     my $numinrow = 4;
 9870:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9871:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9872:     if ($position eq 'top') {
 9873:         my %choices = &Apache::lonlocal::texthash (
 9874:                                                       cancreate_login      => 'Institutional Login',
 9875:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9876:                                                   );
 9877:         my @toggles = sort(keys(%choices));
 9878:         my %defaultchecked = (
 9879:                                'cancreate_login' => 'off',
 9880:                                'cancreate_sso'   => 'off',
 9881:                              );
 9882:         my ($onclick,$itemcount);
 9883:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9884:                                                      \%choices,$itemcount,$onclick);
 9885:         $$rowtotal += $itemcount;
 9886: 
 9887:         if (ref($usertypes) eq 'HASH') {
 9888:             if (keys(%{$usertypes}) > 0) {
 9889:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9890:                                              $dom,$numinrow,$othertitle,
 9891:                                              'statustocreate',$rowtotal);
 9892:                 $$rowtotal ++;
 9893:             }
 9894:         }
 9895:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9896:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9897:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9898:         my $rem;
 9899:         my $numperrow = 2;
 9900:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9901:         $datatable .= '<tr'.$css_class.'>'.
 9902:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9903:                      '<td class="LC_left_item">'."\n".
 9904:                      '<table>'."\n";
 9905:         for (my $i=0; $i<@fields; $i++) {
 9906:             $rem = $i%($numperrow);
 9907:             if ($rem == 0) {
 9908:                 if ($i > 0) {
 9909:                     $datatable .= '</tr>';
 9910:                 }
 9911:                 $datatable .= '<tr>';
 9912:             }
 9913:             my $currval;
 9914:             if (ref($createsettings) eq 'HASH') {
 9915:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9916:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9917:                 }
 9918:             }
 9919:             $datatable .= '<td class="LC_left_item">'.
 9920:                           '<span class="LC_nobreak">'.
 9921:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9922:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9923:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9924:         }
 9925:         my $colsleft = $numperrow - $rem;
 9926:         if ($colsleft > 1 ) {
 9927:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9928:                          '&nbsp;</td>';
 9929:         } elsif ($colsleft == 1) {
 9930:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9931:         }
 9932:         $datatable .= '</tr></table></td></tr>';
 9933:         $$rowtotal ++;
 9934:     } elsif ($position eq 'middle') {
 9935:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9936:         my @posstypes;
 9937:         if (ref($types) eq 'ARRAY') {
 9938:             @posstypes = @{$types};
 9939:         }
 9940:         unless (grep(/^default$/,@posstypes)) {
 9941:             push(@posstypes,'default');
 9942:         }
 9943:         my %usertypeshash;
 9944:         if (ref($usertypes) eq 'HASH') {
 9945:             %usertypeshash = %{$usertypes};
 9946:         }
 9947:         $usertypeshash{'default'} = $othertitle;
 9948:         foreach my $status (@posstypes) {
 9949:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9950:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9951:             $$rowtotal ++;
 9952:         }
 9953:     } else {
 9954:         my %choices = &Apache::lonlocal::texthash (
 9955:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9956:                                                   );
 9957:         my @toggles = sort(keys(%choices));
 9958:         my %defaultchecked = (
 9959:                                'cancreate_email' => 'off',
 9960:                              );
 9961:         my $customclass = 'LC_selfcreate_email';
 9962:         my $classprefix = 'LC_canmodify_emailusername_';
 9963:         my $optionsprefix = 'LC_options_emailusername_';
 9964:         my $display = 'none';
 9965:         my $rowstyle = 'display:none';
 9966:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9967:             $display = 'block';
 9968:             $rowstyle = 'display:table-row';
 9969:         }
 9970:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9971:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9972:                                                      \%choices,$$rowtotal,$onclick);
 9973:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9974:                                          $rowstyle);
 9975:         $$rowtotal ++;
 9976:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9977:                                       $rowstyle);
 9978:         $$rowtotal ++;
 9979:         my (@ordered,@posstypes,%usertypeshash);
 9980:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9981:         my ($emailrules,$emailruleorder) =
 9982:             &Apache::lonnet::inst_userrules($dom,'email');
 9983:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9984:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9985:         if (ref($types) eq 'ARRAY') {
 9986:             @posstypes = @{$types};
 9987:         }
 9988:         if (@posstypes) {
 9989:             unless (grep(/^default$/,@posstypes)) {
 9990:                 push(@posstypes,'default');
 9991:             }
 9992:             if (ref($usertypes) eq 'HASH') {
 9993:                 %usertypeshash = %{$usertypes};
 9994:             }
 9995:             my $currassign;
 9996:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9997:                 $currassign = {
 9998:                                   selfassign => $domdefaults{'inststatusguest'},
 9999:                               };
10000:                 @ordered = @{$domdefaults{'inststatusguest'}};
10001:             } else {
10002:                 $currassign = { selfassign => [] };
10003:             }
10004:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
10005:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
10006:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
10007:                                          $numinrow,$othertitle,'selfassign',
10008:                                          $rowtotal,$onclicktypes,$customclass,
10009:                                          $rowstyle);
10010:             $$rowtotal ++;
10011:             $usertypeshash{'default'} = $othertitle;
10012:             foreach my $status (@posstypes) {
10013:                 my $css_class;
10014:                 if ($$rowtotal%2) {
10015:                     $css_class = 'LC_odd_row ';
10016:                 }
10017:                 $css_class .= $customclass;
10018:                 my $rowid = $optionsprefix.$status;
10019:                 my $hidden = 1;
10020:                 my $currstyle = 'display:none';
10021:                 if (grep(/^\Q$status\E$/,@ordered)) {
10022:                     $currstyle = $rowstyle;
10023:                     $hidden = 0;
10024:                 }
10025:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10026:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
10027:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
10028:                 unless ($hidden) {
10029:                     $$rowtotal ++;
10030:                 }
10031:             }
10032:         } else {
10033:             my $css_class;
10034:             if ($$rowtotal%2) {
10035:                 $css_class = 'LC_odd_row ';
10036:             }
10037:             $css_class .= $customclass;
10038:             $usertypeshash{'default'} = $othertitle;
10039:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10040:                                          $emailrules,$emailruleorder,$settings,'default','',
10041:                                          $othertitle,$css_class,$rowstyle,$intdom);
10042:             $$rowtotal ++;
10043:         }
10044:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10045:         $numinrow = 1;
10046:         if (@posstypes) {
10047:             foreach my $status (@posstypes) {
10048:                 my $rowid = $classprefix.$status;
10049:                 my $datarowstyle = 'display:none';
10050:                 if (grep(/^\Q$status\E$/,@ordered)) {
10051:                     $datarowstyle = $rowstyle;
10052:                 }
10053:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
10054:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10055:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
10056:                 unless ($datarowstyle eq 'display:none') {
10057:                     $$rowtotal ++;
10058:                 }
10059:             }
10060:         } else {
10061:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
10062:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10063:                                                    $infotitles,'',$customclass,$rowstyle);
10064:         }
10065:     }
10066:     return $datatable;
10067: }
10068: 
10069: sub selfcreate_javascript {
10070:     return <<"ENDSCRIPT";
10071: 
10072: <script type="text/javascript">
10073: // <![CDATA[
10074: 
10075: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
10076:     var x = document.getElementsByClassName(target);
10077:     var insttypes = 0;
10078:     var insttypeRegExp = new RegExp(prefix);
10079:     if ((x.length != undefined) && (x.length > 0)) {
10080:         if (form.elements[radio].length != undefined) {
10081:             for (var i=0; i<form.elements[radio].length; i++) {
10082:                 if (form.elements[radio][i].checked) {
10083:                     if (form.elements[radio][i].value == 1) {
10084:                         for (var j=0; j<x.length; j++) {
10085:                             if (x[j].id == 'undefined') {
10086:                                 x[j].style.display = 'table-row';
10087:                             } else if (insttypeRegExp.test(x[j].id)) {
10088:                                 insttypes ++;
10089:                             } else {
10090:                                 x[j].style.display = 'table-row';
10091:                             }
10092:                         }
10093:                     } else {
10094:                         for (var j=0; j<x.length; j++) {
10095:                             x[j].style.display = 'none';
10096:                         }
10097:                     }
10098:                     break;
10099:                 }
10100:             }
10101:             if (insttypes > 0) {
10102:                 toggleDataRow(form,checkbox,target,altprefix);
10103:                 toggleDataRow(form,checkbox,target,prefix,1);
10104:             }
10105:         }
10106:     }
10107:     return;
10108: }
10109: 
10110: function toggleDataRow(form,checkbox,target,prefix,docount) {
10111:     if (form.elements[checkbox].length != undefined) {
10112:         var count = 0;
10113:         if (docount) {
10114:             for (var i=0; i<form.elements[checkbox].length; i++) {
10115:                 if (form.elements[checkbox][i].checked) {
10116:                     count ++;
10117:                 }
10118:             }
10119:         }
10120:         for (var i=0; i<form.elements[checkbox].length; i++) {
10121:             var type = form.elements[checkbox][i].value;
10122:             if (document.getElementById(prefix+type)) {
10123:                 if (form.elements[checkbox][i].checked) {
10124:                     document.getElementById(prefix+type).style.display = 'table-row';
10125:                     if (count % 2 == 1) {
10126:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
10127:                     } else {
10128:                         document.getElementById(prefix+type).className = target;
10129:                     }
10130:                     count ++;
10131:                 } else {
10132:                     document.getElementById(prefix+type).style.display = 'none';
10133:                 }
10134:             }
10135:         }
10136:     }
10137:     return;
10138: }
10139: 
10140: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
10141:     var caller = radio+'_'+status;
10142:     if (form.elements[caller].length != undefined) {
10143:         for (var i=0; i<form.elements[caller].length; i++) {
10144:             if (form.elements[caller][i].checked) {
10145:                 if (document.getElementById(altprefix+'_inst_'+status)) {
10146:                     var curr = form.elements[caller][i].value;
10147:                     if (prefix) {
10148:                         document.getElementById(prefix+'_'+status).style.display = 'none';
10149:                     }
10150:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
10151:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
10152:                     if (curr == 'custom') {
10153:                         if (prefix) {
10154:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
10155:                         }
10156:                     } else if (curr == 'inst') {
10157:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
10158:                     } else if (curr == 'noninst') {
10159:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
10160:                     }
10161:                     break;
10162:                 }
10163:             }
10164:         }
10165:     }
10166: }
10167: 
10168: // ]]>
10169: </script>
10170: 
10171: ENDSCRIPT
10172: }
10173: 
10174: sub noninst_users {
10175:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
10176:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
10177:     my $class = 'LC_left_item';
10178:     if ($css_class) {
10179:         $css_class = ' class="'.$css_class.'"';
10180:     }
10181:     if ($rowid) {
10182:         $rowid = ' id="'.$rowid.'"';
10183:     }
10184:     if ($rowstyle) {
10185:         $rowstyle = ' style="'.$rowstyle.'"';
10186:     }
10187:     my ($output,$description);
10188:     if ($type eq 'default') {
10189:         $description = &mt('Requests for: [_1]',$typetitle);
10190:     } else {
10191:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
10192:     }
10193:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
10194:               "<td>$description</td>\n".
10195:               '<td class="'.$class.'" colspan="2">'.
10196:               '<table><tr>';
10197:     my %headers = &Apache::lonlocal::texthash(
10198:               approve  => 'Processing',
10199:               email    => 'E-mail',
10200:               username => 'Username',
10201:     );
10202:     foreach my $item ('approve','email','username') {
10203:         $output .= '<th>'.$headers{$item}.'</th>';
10204:     }
10205:     $output .= '</tr><tr>';
10206:     foreach my $item ('approve','email','username') {
10207:         $output .= '<td style="vertical-align: top">';
10208:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
10209:         if ($item eq 'approve') {
10210:             %choices = &Apache::lonlocal::texthash (
10211:                                                      automatic => 'Automatically approved',
10212:                                                      approval  => 'Queued for approval',
10213:                                                    );
10214:             @options = ('automatic','approval');
10215:             $hashref = $processing;
10216:             $defoption = 'automatic';
10217:             $name = 'cancreate_emailprocess_'.$type;
10218:         } elsif ($item eq 'email') {
10219:             %choices = &Apache::lonlocal::texthash (
10220:                                                      any     => 'Any e-mail',
10221:                                                      inst    => 'Institutional only',
10222:                                                      noninst => 'Non-institutional only',
10223:                                                      custom  => 'Custom restrictions',
10224:                                                    );
10225:             @options = ('any','inst','noninst');
10226:             my $showcustom;
10227:             if (ref($emailrules) eq 'HASH') {
10228:                 if (keys(%{$emailrules}) > 0) {
10229:                     push(@options,'custom');
10230:                     $showcustom = 'cancreate_emailrule';
10231:                     if (ref($settings) eq 'HASH') {
10232:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
10233:                             foreach my $rule (@{$settings->{'email_rule'}}) {
10234:                                 if (exists($emailrules->{$rule})) {
10235:                                     $hascustom ++;
10236:                                 }
10237:                             }
10238:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
10239:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
10240:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
10241:                                     if (exists($emailrules->{$rule})) {
10242:                                         $hascustom ++;
10243:                                     }
10244:                                 }
10245:                             }
10246:                         }
10247:                     }
10248:                 }
10249:             }
10250:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
10251:                                                      "'cancreate_emaildomain','$type'".');"';
10252:             $hashref = $emailoptions;
10253:             $defoption = 'any';
10254:             $name = 'cancreate_emailoptions_'.$type;
10255:         } elsif ($item eq 'username') {
10256:             %choices = &Apache::lonlocal::texthash (
10257:                                                      all    => 'Same as e-mail',
10258:                                                      first  => 'Omit @domain',
10259:                                                      free   => 'Free to choose',
10260:                                                    );
10261:             @options = ('all','first','free');
10262:             $hashref = $emailverified;
10263:             $defoption = 'all';
10264:             $name = 'cancreate_usernameoptions_'.$type;
10265:         }
10266:         foreach my $option (@options) {
10267:             my $checked;
10268:             if (ref($hashref) eq 'HASH') {
10269:                 if ($type eq '') {
10270:                     if (!exists($hashref->{'default'})) {
10271:                         if ($option eq $defoption) {
10272:                             $checked = ' checked="checked"';
10273:                         }
10274:                     } else {
10275:                         if ($hashref->{'default'} eq $option) {
10276:                             $checked = ' checked="checked"';
10277:                         }
10278:                     }
10279:                 } else {
10280:                     if (!exists($hashref->{$type})) {
10281:                         if ($option eq $defoption) {
10282:                             $checked = ' checked="checked"';
10283:                         }
10284:                     } else {
10285:                         if ($hashref->{$type} eq $option) {
10286:                             $checked = ' checked="checked"';
10287:                         }
10288:                     }
10289:                 }
10290:             } elsif (($item eq 'email') && ($hascustom)) {
10291:                 if ($option eq 'custom') {
10292:                     $checked = ' checked="checked"';
10293:                 }
10294:             } elsif ($option eq $defoption) {
10295:                 $checked = ' checked="checked"';
10296:             }
10297:             $output .= '<span class="LC_nobreak"><label>'.
10298:                        '<input type="radio" name="'.$name.'"'.
10299:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10300:                        $choices{$option}.'</label></span><br />';
10301:             if ($item eq 'email') {
10302:                 if ($option eq 'custom') {
10303:                     my $id = 'cancreate_emailrule_'.$type;
10304:                     my $display = 'none';
10305:                     if ($checked) {
10306:                         $display = 'inline';
10307:                     }
10308:                     my $numinrow = 2;
10309:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10310:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10311:                                &user_formats_row('email',$settings,$emailrules,
10312:                                                  $emailruleorder,$numinrow,'',$type);
10313:                               '</table></fieldset>';
10314:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10315:                     my %text = &Apache::lonlocal::texthash (
10316:                                                              inst    => 'must end:',
10317:                                                              noninst => 'cannot end:',
10318:                                                            );
10319:                     my $value;
10320:                     if (ref($emaildomain) eq 'HASH') {
10321:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10322:                             $value = $emaildomain->{$type}->{$option};
10323:                         }
10324:                     }
10325:                     if ($value eq '') {
10326:                         $value = '@'.$intdom;
10327:                     }
10328:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10329:                     my $display = 'none';
10330:                     if ($checked) {
10331:                         $display = 'inline';
10332:                     }
10333:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10334:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10335:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10336:                                '</div>';
10337:                 }
10338:             }
10339:         }
10340:         $output .= '</td>'."\n";
10341:     }
10342:     $output .= "</tr></table></td></tr>\n";
10343:     return $output;
10344: }
10345: 
10346: sub captcha_choice {
10347:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10348:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10349:         $vertext,$currver);
10350:     my %lt = &captcha_phrases();
10351:     $keyentry = 'hidden';
10352:     my $colspan=2;
10353:     if ($context eq 'cancreate') {
10354:         $rowname = &mt('CAPTCHA validation');
10355:     } elsif ($context eq 'login') {
10356:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10357:     } elsif ($context eq 'passwords') {
10358:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10359:         $colspan=1;
10360:     }
10361:     if (ref($settings) eq 'HASH') {
10362:         if ($settings->{'captcha'}) {
10363:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10364:         } else {
10365:             $checked{'original'} = ' checked="checked"';
10366:         }
10367:         if ($settings->{'captcha'} eq 'recaptcha') {
10368:             $pubtext = $lt{'pub'};
10369:             $privtext = $lt{'priv'};
10370:             $keyentry = 'text';
10371:             $vertext = $lt{'ver'};
10372:             $currver = $settings->{'recaptchaversion'};
10373:             if ($currver ne '2') {
10374:                 $currver = 1;
10375:             }
10376:         }
10377:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10378:             $currpub = $settings->{'recaptchakeys'}{'public'};
10379:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10380:         }
10381:     } else {
10382:         $checked{'original'} = ' checked="checked"';
10383:     }
10384:     my $css_class;
10385:     if ($itemcount%2) {
10386:         $css_class = 'LC_odd_row';
10387:     }
10388:     if ($customcss) {
10389:         $css_class .= " $customcss";
10390:     }
10391:     $css_class =~ s/^\s+//;
10392:     if ($css_class) {
10393:         $css_class = ' class="'.$css_class.'"';
10394:     }
10395:     if ($rowstyle) {
10396:         $css_class .= ' style="'.$rowstyle.'"';
10397:     }
10398:     my $output = '<tr'.$css_class.'>'.
10399:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10400:                  '<table><tr><td>'."\n";
10401:     foreach my $option ('original','recaptcha','notused') {
10402:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10403:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10404:                    $lt{$option}.'</label></span>';
10405:         unless ($option eq 'notused') {
10406:             $output .= ('&nbsp;'x2)."\n";
10407:         }
10408:     }
10409: #
10410: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10411: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10412: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10413: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10414: #
10415:     $output .= '</td></tr>'."\n".
10416:                '<tr><td class="LC_zero_height">'."\n".
10417:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10418:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10419:                $currpub.'" size="40" /></span><br />'."\n".
10420:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10421:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10422:                $currpriv.'" size="40" /></span><br />'.
10423:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10424:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10425:                $currver.'" size="3" /></span><br />'.
10426:                '</td></tr></table>'."\n".
10427:                '</td></tr>';
10428:     return $output;
10429: }
10430: 
10431: sub user_formats_row {
10432:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10433:     my $output;
10434:     my %text = (
10435:                    'username' => 'new usernames',
10436:                    'id'       => 'IDs',
10437:                );
10438:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10439:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10440:         $output = '<tr '.$css_class.'>'.
10441:                   '<td><span class="LC_nobreak">'.
10442:                   &mt("Format rules to check for $text{$type}: ").
10443:                   '</td><td class="LC_left_item" colspan="2"><table>';
10444:     }
10445:     my $rem;
10446:     if (ref($ruleorder) eq 'ARRAY') {
10447:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10448:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10449:                 my $rem = $i%($numinrow);
10450:                 if ($rem == 0) {
10451:                     if ($i > 0) {
10452:                         $output .= '</tr>';
10453:                     }
10454:                     $output .= '<tr>';
10455:                 }
10456:                 my $check = ' ';
10457:                 if (ref($settings) eq 'HASH') {
10458:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10459:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10460:                             $check = ' checked="checked" ';
10461:                         }
10462:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10463:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10464:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10465:                                 $check = ' checked="checked" ';
10466:                             }
10467:                         }
10468:                     }
10469:                 }
10470:                 my $name = $type.'_rule';
10471:                 if ($type eq 'email') {
10472:                     $name .= '_'.$status;
10473:                 }
10474:                 $output .= '<td class="LC_left_item">'.
10475:                            '<span class="LC_nobreak"><label>'.
10476:                            '<input type="checkbox" name="'.$name.'" '.
10477:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10478:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10479:             }
10480:         }
10481:         $rem = @{$ruleorder}%($numinrow);
10482:     }
10483:     my $colsleft;
10484:     if ($rem) {
10485:         $colsleft = $numinrow - $rem;
10486:     }
10487:     if ($colsleft > 1 ) {
10488:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10489:                    '&nbsp;</td>';
10490:     } elsif ($colsleft == 1) {
10491:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10492:     }
10493:     $output .= '</tr>';
10494:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10495:         $output .= '</table></td></tr>';
10496:     }
10497:     return $output;
10498: }
10499: 
10500: sub usercreation_types {
10501:     my %lt = &Apache::lonlocal::texthash (
10502:                     author     => 'When adding a co-author',
10503:                     course     => 'When adding a user to a course',
10504:                     requestcrs => 'When requesting a course',
10505:                     any        => 'Any',
10506:                     official   => 'Institutional only ',
10507:                     unofficial => 'Non-institutional only',
10508:                     none       => 'None',
10509:     );
10510:     return %lt;
10511: }
10512: 
10513: sub selfcreation_types {
10514:     my %lt = &Apache::lonlocal::texthash (
10515:                     selfcreate => 'User creates own account',
10516:                     any        => 'Any',
10517:                     official   => 'Institutional only ',
10518:                     unofficial => 'Non-institutional only',
10519:                     email      => 'E-mail address',
10520:                     login      => 'Institutional Login',
10521:                     sso        => 'SSO',
10522:              );
10523: }
10524: 
10525: sub authtype_names {
10526:     my %lt = &Apache::lonlocal::texthash(
10527:                       int    => 'Internal',
10528:                       krb4   => 'Kerberos 4',
10529:                       krb5   => 'Kerberos 5',
10530:                       loc    => 'Local',
10531:                       lti    => 'LTI',
10532:                   );
10533:     return %lt;
10534: }
10535: 
10536: sub context_names {
10537:     my %context_title = &Apache::lonlocal::texthash(
10538:        author => 'Creating users when an Author',
10539:        course => 'Creating users when in a course',
10540:        domain => 'Creating users when a Domain Coordinator',
10541:     );
10542:     return %context_title;
10543: }
10544: 
10545: sub print_usermodification {
10546:     my ($position,$dom,$settings,$rowtotal) = @_;
10547:     my $numinrow = 4;
10548:     my ($context,$datatable,$rowcount);
10549:     if ($position eq 'top') {
10550:         $rowcount = 0;
10551:         $context = 'author'; 
10552:         foreach my $role ('ca','aa') {
10553:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10554:                                                    $numinrow,$rowcount);
10555:             $$rowtotal ++;
10556:             $rowcount ++;
10557:         }
10558:     } elsif ($position eq 'bottom') {
10559:         $context = 'course';
10560:         $rowcount = 0;
10561:         foreach my $role ('st','ep','ta','in','cr') {
10562:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10563:                                                    $numinrow,$rowcount);
10564:             $$rowtotal ++;
10565:             $rowcount ++;
10566:         }
10567:     }
10568:     return $datatable;
10569: }
10570: 
10571: sub print_defaults {
10572:     my ($position,$dom,$settings,$rowtotal) = @_;
10573:     my $rownum = 0;
10574:     my ($datatable,$css_class,$titles);
10575:     unless ($position eq 'bottom') {
10576:         $titles = &defaults_titles($dom);
10577:     }
10578:     if ($position eq 'top') {
10579:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10580:                      'datelocale_def','portal_def');
10581:         my %defaults;
10582:         if (ref($settings) eq 'HASH') {
10583:             %defaults = %{$settings};
10584:         } else {
10585:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10586:             foreach my $item (@items) {
10587:                 $defaults{$item} = $domdefaults{$item};
10588:             }
10589:         }
10590:         foreach my $item (@items) {
10591:             if ($rownum%2) {
10592:                 $css_class = '';
10593:             } else {
10594:                 $css_class = ' class="LC_odd_row" ';
10595:             }
10596:             $datatable .= '<tr'.$css_class.'>'.
10597:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10598:                           '</span></td><td class="LC_right_item" colspan="3">';
10599:             if ($item eq 'auth_def') {
10600:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10601:                 my %shortauth = (
10602:                                  internal => 'int',
10603:                                  krb4 => 'krb4',
10604:                                  krb5 => 'krb5',
10605:                                  localauth  => 'loc',
10606:                                  lti => 'lti',
10607:                                 );
10608:                 my %authnames = &authtype_names();
10609:                 foreach my $auth (@authtypes) {
10610:                     my $checked = ' ';
10611:                     if ($defaults{$item} eq $auth) {
10612:                         $checked = ' checked="checked" ';
10613:                     }
10614:                     $datatable .= '<label><input type="radio" name="'.$item.
10615:                                   '" value="'.$auth.'"'.$checked.'/>'.
10616:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10617:                 }
10618:             } elsif ($item eq 'timezone_def') {
10619:                 my $includeempty = 1;
10620:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10621:             } elsif ($item eq 'datelocale_def') {
10622:                 my $includeempty = 1;
10623:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10624:             } elsif ($item eq 'lang_def') {
10625:                 my $includeempty = 1;
10626:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10627:             } elsif ($item eq 'portal_def') {
10628:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10629:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10630:                 my $portalsty = 'none';
10631:                 if ($defaults{$item}) {
10632:                     $portalsty = 'block';
10633:                 }
10634:                 foreach my $field ('email','web') {
10635:                     my $checkedoff = ' checked="checked"';
10636:                     my $checkedon;
10637:                     if ($defaults{$item.'_'.$field}) {
10638:                         $checkedon = $checkedoff;
10639:                         $checkedoff = '';
10640:                     }
10641:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10642:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10643:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10644:                               ('&nbsp;'x2).
10645:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10646:                               '</div>';
10647:                 }
10648:             } else {
10649:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10650:             }
10651:             $datatable .= '</td></tr>';
10652:             $rownum ++;
10653:         }
10654:     } elsif ($position eq 'middle') {
10655:         my %defaults;
10656:         if (ref($settings) eq 'HASH') {
10657:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10658:                 my $maxnum = @{$settings->{'inststatusorder'}};
10659:                 for (my $i=0; $i<$maxnum; $i++) {
10660:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10661:                     my $item = $settings->{'inststatusorder'}->[$i];
10662:                     my $title = $settings->{'inststatustypes'}->{$item};
10663:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10664:                     $datatable .= '<tr'.$css_class.'>'.
10665:                                   '<td><span class="LC_nobreak">'.
10666:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10667:                     for (my $k=0; $k<=$maxnum; $k++) {
10668:                         my $vpos = $k+1;
10669:                         my $selstr;
10670:                         if ($k == $i) {
10671:                             $selstr = ' selected="selected" ';
10672:                         }
10673:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10674:                     }
10675:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10676:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10677:                                   &mt('delete').'</span></td>'.
10678:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10679:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10680:                                   '</span></td></tr>';
10681:                 }
10682:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10683:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10684:                 $datatable .= '<tr '.$css_class.'>'.
10685:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10686:                 for (my $k=0; $k<=$maxnum; $k++) {
10687:                     my $vpos = $k+1;
10688:                     my $selstr;
10689:                     if ($k == $maxnum) {
10690:                         $selstr = ' selected="selected" ';
10691:                     }
10692:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10693:                 }
10694:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10695:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10696:                               '&nbsp;'.&mt('(new)').
10697:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10698:                               &mt('Name displayed').':'.
10699:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10700:                               '</tr>'."\n";
10701:                 $rownum ++;
10702:             }
10703:         }
10704:     } else {
10705:         my ($unamemaprules,$ruleorder) =
10706:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10707:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10708:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10709:             my $numinrow = 2;
10710:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10711:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10712:                                             $ruleorder,$numinrow).
10713:                           '</table></td></tr>';
10714:         }
10715:         if ($datatable eq '') {
10716:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10717:                           &mt('No rules set for domain in customized localenroll.pm').
10718:                           '</td></tr>';
10719:         }
10720:     }
10721:     $$rowtotal += $rownum;
10722:     return $datatable;
10723: }
10724: 
10725: sub get_languages_hash {
10726:     my %langchoices;
10727:     foreach my $id (&Apache::loncommon::languageids()) {
10728:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10729:         if ($code ne '') {
10730:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10731:         }
10732:     }
10733:     return %langchoices;
10734: }
10735: 
10736: sub defaults_titles {
10737:     my ($dom) = @_;
10738:     my %titles = &Apache::lonlocal::texthash (
10739:                    'auth_def'      => 'Default authentication type',
10740:                    'auth_arg_def'  => 'Default authentication argument',
10741:                    'lang_def'      => 'Default language',
10742:                    'timezone_def'  => 'Default timezone',
10743:                    'datelocale_def' => 'Default locale for dates',
10744:                    'portal_def'     => 'Portal/Default URL',
10745:                    'email'          => 'Email links use portal URL',
10746:                    'web'            => 'Public web links use portal URL',
10747:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10748:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10749:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10750:                  );
10751:     if ($dom) {
10752:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10753:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10754:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10755:         $protocol = 'http' if ($protocol ne 'https');
10756:         if ($uint_dom) {
10757:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10758:                                          $uint_dom);
10759:         }
10760:     }
10761:     return (\%titles);
10762: }
10763: 
10764: sub print_scantron {
10765:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10766:     if ($position eq 'top') {
10767:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10768:     } else {
10769:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10770:     }
10771: }
10772: 
10773: sub scantron_javascript {
10774:     return <<"ENDSCRIPT";
10775: 
10776: <script type="text/javascript">
10777: // <![CDATA[
10778: 
10779: function toggleScantron(form) {
10780:     var csvfieldset = new Array();
10781:     if (document.getElementById('scantroncsv_cols')) {
10782:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10783:     }
10784:     if (document.getElementById('scantroncsv_options')) {
10785:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10786:     }
10787:     if (csvfieldset.length) {
10788:         if (document.getElementById('scantronconfcsv')) {
10789:             var scantroncsv = document.getElementById('scantronconfcsv');
10790:             if (scantroncsv.checked) {
10791:                 for (var i=0; i<csvfieldset.length; i++) {
10792:                     csvfieldset[i].style.display = 'block';
10793:                 }
10794:             } else {
10795:                 for (var i=0; i<csvfieldset.length; i++) {
10796:                     csvfieldset[i].style.display = 'none';
10797:                 }
10798:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10799:                 if (csvselects.length) {
10800:                     for (var j=0; j<csvselects.length; j++) {
10801:                         csvselects[j].selectedIndex = 0;
10802:                     }
10803:                 }
10804:             }
10805:         }
10806:     }
10807:     return;
10808: }
10809: // ]]>
10810: </script>
10811: 
10812: ENDSCRIPT
10813: 
10814: }
10815: 
10816: sub print_scantronformat {
10817:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10818:     my $itemcount = 1;
10819:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10820:         %confhash);
10821:     my $switchserver = &check_switchserver($dom,$confname);
10822:     my %lt = &Apache::lonlocal::texthash (
10823:                 default => 'Default bubblesheet format file error',
10824:                 custom  => 'Custom bubblesheet format file error',
10825:              );
10826:     my %scantronfiles = (
10827:         default => 'default.tab',
10828:         custom => 'custom.tab',
10829:     );
10830:     foreach my $key (keys(%scantronfiles)) {
10831:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10832:                               .$scantronfiles{$key};
10833:     }
10834:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10835:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10836:         if (!$switchserver) {
10837:             my $servadm = $r->dir_config('lonAdmEMail');
10838:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10839:             if ($configuserok eq 'ok') {
10840:                 if ($author_ok eq 'ok') {
10841:                     my %legacyfile = (
10842:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10843:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10844:                     );
10845:                     my %md5chk;
10846:                     foreach my $type (keys(%legacyfile)) {
10847:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10848:                         chomp($md5chk{$type});
10849:                     }
10850:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10851:                         foreach my $type (keys(%legacyfile)) {
10852:                             ($scantronurls{$type},my $error) =
10853:                                 &legacy_scantronformat($r,$dom,$confname,
10854:                                                  $type,$legacyfile{$type},
10855:                                                  $scantronurls{$type},
10856:                                                  $scantronfiles{$type});
10857:                             if ($error ne '') {
10858:                                 $error{$type} = $error;
10859:                             }
10860:                         }
10861:                         if (keys(%error) == 0) {
10862:                             $is_custom = 1;
10863:                             $confhash{'scantron'}{'scantronformat'} =
10864:                                 $scantronurls{'custom'};
10865:                             my $putresult =
10866:                                 &Apache::lonnet::put_dom('configuration',
10867:                                                          \%confhash,$dom);
10868:                             if ($putresult ne 'ok') {
10869:                                 $error{'custom'} =
10870:                                     '<span class="LC_error">'.
10871:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10872:                             }
10873:                         }
10874:                     } else {
10875:                         ($scantronurls{'default'},my $error) =
10876:                             &legacy_scantronformat($r,$dom,$confname,
10877:                                           'default',$legacyfile{'default'},
10878:                                           $scantronurls{'default'},
10879:                                           $scantronfiles{'default'});
10880:                         if ($error eq '') {
10881:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10882:                             my $putresult =
10883:                                 &Apache::lonnet::put_dom('configuration',
10884:                                                          \%confhash,$dom);
10885:                             if ($putresult ne 'ok') {
10886:                                 $error{'default'} =
10887:                                     '<span class="LC_error">'.
10888:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10889:                             }
10890:                         } else {
10891:                             $error{'default'} = $error;
10892:                         }
10893:                     }
10894:                 }
10895:             }
10896:         } else {
10897:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10898:         }
10899:     }
10900:     if (ref($settings) eq 'HASH') {
10901:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10902:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10903:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10904:                 $scantronurl = '';
10905:             } else {
10906:                 $scantronurl = $settings->{'scantronformat'};
10907:             }
10908:             $is_custom = 1;
10909:         } else {
10910:             $scantronurl = $scantronurls{'default'};
10911:         }
10912:     } else {
10913:         if ($is_custom) {
10914:             $scantronurl = $scantronurls{'custom'};
10915:         } else {
10916:             $scantronurl = $scantronurls{'default'};
10917:         }
10918:     }
10919:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10920:     $datatable .= '<tr'.$css_class.'>';
10921:     if (!$is_custom) {
10922:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10923:                       '<span class="LC_nobreak">';
10924:         if ($scantronurl) {
10925:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10926:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10927:         } else {
10928:             $datatable = &mt('File unavailable for display');
10929:         }
10930:         $datatable .= '</span></td>';
10931:         if (keys(%error) == 0) { 
10932:             $datatable .= '<td style="vertical-align: bottom">';
10933:             if (!$switchserver) {
10934:                 $datatable .= &mt('Upload:').'<br />';
10935:             }
10936:         } else {
10937:             my $errorstr;
10938:             foreach my $key (sort(keys(%error))) {
10939:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10940:             }
10941:             $datatable .= '<td>'.$errorstr;
10942:         }
10943:     } else {
10944:         if (keys(%error) > 0) {
10945:             my $errorstr;
10946:             foreach my $key (sort(keys(%error))) {
10947:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10948:             } 
10949:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10950:         } elsif ($scantronurl) {
10951:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10952:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10953:             $datatable .= '<td><span class="LC_nobreak">'.
10954:                           $link.
10955:                           '<label><input type="checkbox" name="scantronformat_del"'.
10956:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10957:                           '<td><span class="LC_nobreak">&nbsp;'.
10958:                           &mt('Replace:').'</span><br />';
10959:         }
10960:     }
10961:     if (keys(%error) == 0) {
10962:         if ($switchserver) {
10963:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10964:         } else {
10965:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10966:                          '<input type="file" name="scantronformat" /></span>';
10967:         }
10968:     }
10969:     $datatable .= '</td></tr>';
10970:     $$rowtotal ++;
10971:     return $datatable;
10972: }
10973: 
10974: sub legacy_scantronformat {
10975:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10976:     my ($url,$error);
10977:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10978:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10979:         my $modified = [];
10980:         (my $result,$url) =
10981:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
10982:                                                     'scantron','','',$newfile,$modified);
10983:         if ($result eq 'ok') {
10984:             &update_modify_urls($r,$modified);
10985:         } else {
10986:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10987:         }
10988:     }
10989:     return ($url,$error);
10990: }
10991: 
10992: sub print_scantronconfig {
10993:     my ($dom,$settings,$rowtotal) = @_;
10994:     my $itemcount = 2;
10995:     my $is_checked = ' checked="checked"';
10996:     my %optionson = (
10997:                      hdr => ' checked="checked"',
10998:                      pad => ' checked="checked"',
10999:                      rem => ' checked="checked"',
11000:                     );
11001:     my %optionsoff = (
11002:                       hdr => '',
11003:                       pad => '',
11004:                       rem => '',
11005:                      );
11006:     my $currcsvsty = 'none';
11007:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
11008:     my @fields = &scantroncsv_fields();
11009:     my %titles = &scantronconfig_titles();
11010:     if (ref($settings) eq 'HASH') {
11011:         if (ref($settings->{config}) eq 'HASH') {
11012:             if ($settings->{config}->{dat}) {
11013:                 $checked{'dat'} = $is_checked;
11014:             }
11015:             if (ref($settings->{config}->{csv}) eq 'HASH') {
11016:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
11017:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
11018:                     if (keys(%csvfields) > 0) {
11019:                         $checked{'csv'} = $is_checked;
11020:                         $currcsvsty = 'block';
11021:                     }
11022:                 }
11023:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
11024:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
11025:                     foreach my $option (keys(%optionson)) {
11026:                         unless ($csvoptions{$option}) {
11027:                             $optionsoff{$option} = $optionson{$option};
11028:                             $optionson{$option} = '';
11029:                         }
11030:                     }
11031:                 }
11032:             }
11033:         } else {
11034:             $checked{'dat'} = $is_checked;
11035:         }
11036:     } else {
11037:         $checked{'dat'} = $is_checked;
11038:     }
11039:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
11040:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11041:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
11042:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
11043:     foreach my $item ('dat','csv') {
11044:         my $id;
11045:         if ($item eq 'csv') {
11046:             $id = 'id="scantronconfcsv" ';
11047:         }
11048:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
11049:                       $titles{$item}.'</label>'.('&nbsp;'x3);
11050:         if ($item eq 'csv') {
11051:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
11052:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
11053:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
11054:             foreach my $col (@fields) {
11055:                 my $selnone;
11056:                 if ($csvfields{$col} eq '') {
11057:                     $selnone = ' selected="selected"';
11058:                 }
11059:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
11060:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
11061:                               '<option value=""'.$selnone.'></option>';
11062:                 for (my $i=0; $i<20; $i++) {
11063:                     my $shown = $i+1;
11064:                     my $sel;
11065:                     unless ($selnone) {
11066:                         if (exists($csvfields{$col})) {
11067:                             if ($csvfields{$col} == $i) {
11068:                                 $sel = ' selected="selected"';
11069:                             }
11070:                         }
11071:                     }
11072:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
11073:                 }
11074:                 $datatable .= '</select></td></tr>';
11075:            }
11076:            $datatable .= '</table></fieldset>'.
11077:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
11078:                          '<legend>'.&mt('CSV Options').'</legend>';
11079:            foreach my $option ('hdr','pad','rem') {
11080:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
11081:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
11082:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
11083:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
11084:            }
11085:            $datatable .= '</fieldset>';
11086:            $itemcount ++;
11087:         }
11088:     }
11089:     $datatable .= '</td></tr>';
11090:     $$rowtotal ++;
11091:     return $datatable;
11092: }
11093: 
11094: sub scantronconfig_titles {
11095:     return &Apache::lonlocal::texthash(
11096:                                           dat => 'Standard format (.dat)',
11097:                                           csv => 'Comma separated values (.csv)',
11098:                                           hdr => 'Remove first line in file (contains column titles)',
11099:                                           pad => 'Prepend 0s to PaperID',
11100:                                           rem => 'Remove leading spaces (except Question Response columns)',
11101:                                           CODE => 'CODE',
11102:                                           ID   => 'Student ID',
11103:                                           PaperID => 'Paper ID',
11104:                                           FirstName => 'First Name',
11105:                                           LastName => 'Last Name',
11106:                                           FirstQuestion => 'First Question Response',
11107:                                           Section => 'Section',
11108:     );
11109: }
11110: 
11111: sub scantroncsv_fields {
11112:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
11113: }
11114: 
11115: sub print_coursecategories {
11116:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
11117:     my $datatable;
11118:     if ($position eq 'top') {
11119:         my (%checked);
11120:         my @catitems = ('unauth','auth');
11121:         my @cattypes = ('std','domonly','codesrch','none');
11122:         $checked{'unauth'} = 'std';
11123:         $checked{'auth'} = 'std';
11124:         if (ref($settings) eq 'HASH') {
11125:             foreach my $type (@cattypes) {
11126:                 if ($type eq $settings->{'unauth'}) {
11127:                     $checked{'unauth'} = $type;
11128:                 }
11129:                 if ($type eq $settings->{'auth'}) {
11130:                     $checked{'auth'} = $type;
11131:                 }
11132:             }
11133:         }
11134:         my %lt = &Apache::lonlocal::texthash (
11135:                                                unauth   => 'Catalog type for unauthenticated users',
11136:                                                auth     => 'Catalog type for authenticated users',
11137:                                                none     => 'No catalog',
11138:                                                std      => 'Standard catalog',
11139:                                                domonly  => 'Domain-only catalog',
11140:                                                codesrch => "Code search form",
11141:                                              );
11142:        my $itemcount = 0;
11143:        foreach my $item (@catitems) {
11144:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11145:            $datatable .= '<tr '.$css_class.'>'.
11146:                          '<td>'.$lt{$item}.'</td>'.
11147:                          '<td class="LC_right_item"><span class="LC_nobreak">';
11148:            foreach my $type (@cattypes) {
11149:                my $ischecked;
11150:                if ($checked{$item} eq $type) {
11151:                    $ischecked=' checked="checked"';
11152:                }
11153:                $datatable .= '<label>'.
11154:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
11155:                              ' />'.$lt{$type}.'</label>&nbsp;';
11156:            }
11157:            $datatable .= '</span></td></tr>';
11158:            $itemcount ++;
11159:         }
11160:         $$rowtotal += $itemcount;
11161:     } elsif ($position eq 'middle') {
11162:         my $toggle_cats_crs = ' ';
11163:         my $toggle_cats_dom = ' checked="checked" ';
11164:         my $can_cat_crs = ' ';
11165:         my $can_cat_dom = ' checked="checked" ';
11166:         my $toggle_catscomm_comm = ' ';
11167:         my $toggle_catscomm_dom = ' checked="checked" ';
11168:         my $can_catcomm_comm = ' ';
11169:         my $can_catcomm_dom = ' checked="checked" ';
11170:         my $toggle_catsplace_place = ' ';
11171:         my $toggle_catsplace_dom = ' checked="checked" ';
11172:         my $can_catplace_place = ' ';
11173:         my $can_catplace_dom = ' checked="checked" ';
11174: 
11175:         if (ref($settings) eq 'HASH') {
11176:             if ($settings->{'togglecats'} eq 'crs') {
11177:                 $toggle_cats_crs = $toggle_cats_dom;
11178:                 $toggle_cats_dom = ' ';
11179:             }
11180:             if ($settings->{'categorize'} eq 'crs') {
11181:                 $can_cat_crs = $can_cat_dom;
11182:                 $can_cat_dom = ' ';
11183:             }
11184:             if ($settings->{'togglecatscomm'} eq 'comm') {
11185:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
11186:                 $toggle_catscomm_dom = ' ';
11187:             }
11188:             if ($settings->{'categorizecomm'} eq 'comm') {
11189:                 $can_catcomm_comm = $can_catcomm_dom;
11190:                 $can_catcomm_dom = ' ';
11191:             }
11192:             if ($settings->{'togglecatsplace'} eq 'place') {
11193:                 $toggle_catsplace_place = $toggle_catsplace_dom;
11194:                 $toggle_catsplace_dom = ' ';
11195:             }
11196:             if ($settings->{'categorizeplace'} eq 'place') {
11197:                 $can_catplace_place = $can_catplace_dom;
11198:                 $can_catplace_dom = ' ';
11199:             }
11200:         }
11201:         my %title = &Apache::lonlocal::texthash (
11202:                      togglecats      => 'Show/Hide a course in catalog',
11203:                      togglecatscomm  => 'Show/Hide a community in catalog',
11204:                      togglecatsplace => 'Show/Hide a placement test in catalog',
11205:                      categorize      => 'Assign a category to a course',
11206:                      categorizecomm  => 'Assign a category to a community',
11207:                      categorizeplace => 'Assign a category to a placement test',
11208:                     );
11209:         my %level = &Apache::lonlocal::texthash (
11210:                      dom   => 'Set in Domain',
11211:                      crs   => 'Set in Course',
11212:                      comm  => 'Set in Community',
11213:                      place => 'Set in Placement Test',
11214:                     );
11215:         $datatable = '<tr class="LC_odd_row">'.
11216:                   '<td>'.$title{'togglecats'}.'</td>'.
11217:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11218:                   '<input type="radio" name="togglecats"'.
11219:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11220:                   '<label><input type="radio" name="togglecats"'.
11221:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
11222:                   '</tr><tr>'.
11223:                   '<td>'.$title{'categorize'}.'</td>'.
11224:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11225:                   '<label><input type="radio" name="categorize"'.
11226:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11227:                   '<label><input type="radio" name="categorize"'.
11228:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
11229:                   '</tr><tr class="LC_odd_row">'.
11230:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
11231:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11232:                   '<input type="radio" name="togglecatscomm"'.
11233:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11234:                   '<label><input type="radio" name="togglecatscomm"'.
11235:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
11236:                   '</tr><tr>'.
11237:                   '<td>'.$title{'categorizecomm'}.'</td>'.
11238:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11239:                   '<label><input type="radio" name="categorizecomm"'.
11240:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11241:                   '<label><input type="radio" name="categorizecomm"'.
11242:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
11243:                   '</tr><tr class="LC_odd_row">'.
11244:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
11245:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11246:                   '<input type="radio" name="togglecatsplace"'.
11247:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11248:                   '<label><input type="radio" name="togglecatscomm"'.
11249:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
11250:                   '</tr><tr>'.
11251:                   '<td>'.$title{'categorizeplace'}.'</td>'.
11252:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11253:                   '<label><input type="radio" name="categorizeplace"'.
11254:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11255:                   '<label><input type="radio" name="categorizeplace"'.
11256:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
11257:                   '</tr>';
11258:         $$rowtotal += 6;
11259:     } else {
11260:         my $css_class;
11261:         my $itemcount = 1;
11262:         my $cathash; 
11263:         if (ref($settings) eq 'HASH') {
11264:             $cathash = $settings->{'cats'};
11265:         }
11266:         if (ref($cathash) eq 'HASH') {
11267:             my (@cats,@trails,%allitems,%idx,@jsarray);
11268:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11269:                                                    \%allitems,\%idx,\@jsarray);
11270:             my $maxdepth = scalar(@cats);
11271:             my $colattrib = '';
11272:             if ($maxdepth > 2) {
11273:                 $colattrib = ' colspan="2" ';
11274:             }
11275:             my @path;
11276:             if (@cats > 0) {
11277:                 if (ref($cats[0]) eq 'ARRAY') {
11278:                     my $numtop = @{$cats[0]};
11279:                     my $maxnum = $numtop;
11280:                     my %default_names = (
11281:                           instcode    => &mt('Official courses'),
11282:                           communities => &mt('Communities'),
11283:                           placement   => &mt('Placement Tests'),
11284:                     );
11285: 
11286:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11287:                         ($cathash->{'instcode::0'} eq '') ||
11288:                         (!grep(/^communities$/,@{$cats[0]})) || 
11289:                         ($cathash->{'communities::0'} eq '') ||
11290:                         (!grep(/^placement$/,@{$cats[0]})) ||
11291:                         ($cathash->{'placement::0'} eq '')) {
11292:                         $maxnum ++;
11293:                     }
11294:                     my $lastidx;
11295:                     for (my $i=0; $i<$numtop; $i++) {
11296:                         my $parent = $cats[0][$i];
11297:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11298:                         my $item = &escape($parent).'::0';
11299:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11300:                         $lastidx = $idx{$item};
11301:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11302:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11303:                         for (my $k=0; $k<=$maxnum; $k++) {
11304:                             my $vpos = $k+1;
11305:                             my $selstr;
11306:                             if ($k == $i) {
11307:                                 $selstr = ' selected="selected" ';
11308:                             }
11309:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11310:                         }
11311:                         $datatable .= '</select></span></td><td>';
11312:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11313:                             $datatable .=  '<span class="LC_nobreak">'
11314:                                            .$default_names{$parent}.'</span>';
11315:                             if ($parent eq 'instcode') {
11316:                                 $datatable .= '<br /><span class="LC_nobreak">('
11317:                                               .&mt('with institutional codes')
11318:                                               .')</span></td><td'.$colattrib.'>';
11319:                             } else {
11320:                                 $datatable .= '<table><tr><td>';
11321:                             }
11322:                             $datatable .= '<span class="LC_nobreak">'
11323:                                           .'<label><input type="radio" name="'
11324:                                           .$parent.'" value="1" checked="checked" />'
11325:                                           .&mt('Display').'</label>';
11326:                             if ($parent eq 'instcode') {
11327:                                 $datatable .= '&nbsp;';
11328:                             } else {
11329:                                 $datatable .= '</span></td></tr><tr><td>'
11330:                                               .'<span class="LC_nobreak">';
11331:                             }
11332:                             $datatable .= '<label><input type="radio" name="'
11333:                                           .$parent.'" value="0" />'
11334:                                           .&mt('Do not display').'</label></span>';
11335:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11336:                                 $datatable .= '</td></tr></table>';
11337:                             }
11338:                             $datatable .= '</td>';
11339:                         } else {
11340:                             $datatable .= $parent
11341:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11342:                                           .'<input type="checkbox" name="deletecategory" '
11343:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11344:                         }
11345:                         my $depth = 1;
11346:                         push(@path,$parent);
11347:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11348:                         pop(@path);
11349:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11350:                         $itemcount ++;
11351:                     }
11352:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11353:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11354:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11355:                     for (my $k=0; $k<=$maxnum; $k++) {
11356:                         my $vpos = $k+1;
11357:                         my $selstr;
11358:                         if ($k == $numtop) {
11359:                             $selstr = ' selected="selected" ';
11360:                         }
11361:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11362:                     }
11363:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11364:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11365:                                   .'</tr>'."\n";
11366:                     $itemcount ++;
11367:                     foreach my $default ('instcode','communities','placement') {
11368:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11369:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11370:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11371:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11372:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11373:                             for (my $k=0; $k<=$maxnum; $k++) {
11374:                                 my $vpos = $k+1;
11375:                                 my $selstr;
11376:                                 if ($k == $maxnum) {
11377:                                     $selstr = ' selected="selected" ';
11378:                                 }
11379:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11380:                             }
11381:                             $datatable .= '</select></span></td>'.
11382:                                           '<td><span class="LC_nobreak">'.
11383:                                           $default_names{$default}.'</span>';
11384:                             if ($default eq 'instcode') {
11385:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11386:                                               .&mt('with institutional codes').')</span>';
11387:                             }
11388:                             $datatable .= '</td>'
11389:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11390:                                           .&mt('Display').'</label>&nbsp;'
11391:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11392:                                           .&mt('Do not display').'</label></span></td></tr>';
11393:                         }
11394:                     }
11395:                 }
11396:             } else {
11397:                 $datatable .= &initialize_categories($itemcount);
11398:             }
11399:         } else {
11400:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11401:                           .&initialize_categories($itemcount);
11402:         }
11403:         $$rowtotal += $itemcount;
11404:     }
11405:     return $datatable;
11406: }
11407: 
11408: sub print_serverstatuses {
11409:     my ($dom,$settings,$rowtotal) = @_;
11410:     my $datatable;
11411:     my @pages = &serverstatus_pages();
11412:     my (%namedaccess,%machineaccess);
11413:     foreach my $type (@pages) {
11414:         $namedaccess{$type} = '';
11415:         $machineaccess{$type}= '';
11416:     }
11417:     if (ref($settings) eq 'HASH') {
11418:         foreach my $type (@pages) {
11419:             if (exists($settings->{$type})) {
11420:                 if (ref($settings->{$type}) eq 'HASH') {
11421:                     foreach my $key (keys(%{$settings->{$type}})) {
11422:                         if ($key eq 'namedusers') {
11423:                             $namedaccess{$type} = $settings->{$type}->{$key};
11424:                         } elsif ($key eq 'machines') {
11425:                             $machineaccess{$type} = $settings->{$type}->{$key};
11426:                         }
11427:                     }
11428:                 }
11429:             }
11430:         }
11431:     }
11432:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11433:     my $rownum = 0;
11434:     my $css_class;
11435:     foreach my $type (@pages) {
11436:         $rownum ++;
11437:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11438:         $datatable .= '<tr'.$css_class.'>'.
11439:                       '<td><span class="LC_nobreak">'.
11440:                       $titles->{$type}.'</span></td>'.
11441:                       '<td class="LC_left_item">'.
11442:                       '<input type="text" name="'.$type.'_namedusers" '.
11443:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11444:                       '<td class="LC_right_item">'.
11445:                       '<span class="LC_nobreak">'.
11446:                       '<input type="text" name="'.$type.'_machines" '.
11447:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11448:                       '</span></td></tr>'."\n";
11449:     }
11450:     $$rowtotal += $rownum;
11451:     return $datatable;
11452: }
11453: 
11454: sub serverstatus_pages {
11455:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11456:             'checksums','clusterstatus','certstatus','metadata_keywords',
11457:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11458:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11459: }
11460: 
11461: sub defaults_javascript {
11462:     my ($settings) = @_;
11463:     return unless (ref($settings) eq 'HASH');
11464:     my $portal_js = <<"ENDPORTAL";
11465: 
11466: function portalExtras(caller) {
11467:     var x = caller.value;
11468:     var y = new Array('email','web');
11469:     for (var i=0; i<y.length; i++) {
11470:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11471:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11472:             if (x.length > 0) {
11473:                 z.style.display = 'block';
11474:             } else {
11475:                 z.style.display = 'none';
11476:             }
11477:         }
11478:     }
11479: }
11480: ENDPORTAL
11481:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11482:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11483:         if ($maxnum eq '') {
11484:             $maxnum = 0;
11485:         }
11486:         $maxnum ++;
11487:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11488:         return <<"ENDSCRIPT";
11489: <script type="text/javascript">
11490: // <![CDATA[
11491: function reorderTypes(form,caller) {
11492:     var changedVal;
11493: $jstext 
11494:     var newpos = 'addinststatus_pos';
11495:     var current = new Array;
11496:     var maxh = $maxnum;
11497:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11498:     var oldVal;
11499:     if (caller == newpos) {
11500:         changedVal = newitemVal;
11501:     } else {
11502:         var curritem = 'inststatus_pos_'+caller;
11503:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11504:         current[newitemVal] = newpos;
11505:     }
11506:     for (var i=0; i<inststatuses.length; i++) {
11507:         if (inststatuses[i] != caller) {
11508:             var elementName = 'inststatus_pos_'+inststatuses[i];
11509:             if (form.elements[elementName]) {
11510:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11511:                 current[currVal] = elementName;
11512:             }
11513:         }
11514:     }
11515:     for (var j=0; j<maxh; j++) {
11516:         if (current[j] == undefined) {
11517:             oldVal = j;
11518:         }
11519:     }
11520:     if (oldVal < changedVal) {
11521:         for (var k=oldVal+1; k<=changedVal ; k++) {
11522:            var elementName = current[k];
11523:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11524:         }
11525:     } else {
11526:         for (var k=changedVal; k<oldVal; k++) {
11527:             var elementName = current[k];
11528:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11529:         }
11530:     }
11531:     return;
11532: }
11533: 
11534: $portal_js
11535: 
11536: // ]]>
11537: </script>
11538: 
11539: ENDSCRIPT
11540:     } else {
11541: return <<"ENDSCRIPT";
11542: <script type="text/javascript">
11543: // <![CDATA[
11544: $portal_js
11545: // ]]>
11546: </script>
11547: 
11548: ENDSCRIPT
11549:     }
11550:     return;
11551: }
11552: 
11553: sub passwords_javascript {
11554:     my ($prefix) = @_;
11555:     my %intalert;
11556:     if ($prefix eq 'passwords') {
11557:         %intalert = &Apache::lonlocal::texthash (
11558:             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.',
11559:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11560:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11561:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11562:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11563:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11564:         );
11565:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11566:         %intalert = &Apache::lonlocal::texthash (
11567:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11568:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11569:         );
11570:     }
11571:     &js_escape(\%intalert);
11572:     my $defmin = $Apache::lonnet::passwdmin;
11573:     my $intauthjs;
11574:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11575: 
11576: function warnIntAuth(field) {
11577:     if (field.name == 'intauth_check') {
11578:         if (field.value == '2') {
11579:             alert('$intalert{authcheck}');
11580:         }
11581:     }
11582:     if (field.name == 'intauth_cost') {
11583:         field.value.replace(/\s/g,'');
11584:         if (field.value != '') {
11585:             var regexdigit=/^\\d+\$/;
11586:             if (!regexdigit.test(field.value)) {
11587:                 alert('$intalert{authcost}');
11588:             }
11589:         }
11590:     }
11591:     return;
11592: }
11593: 
11594: ENDSCRIPT
11595: 
11596:      }
11597: 
11598:      $intauthjs .= <<"ENDSCRIPT";
11599: 
11600: function warnInt$prefix(field) {
11601:     field.value.replace(/^\s+/,'');
11602:     field.value.replace(/\s+\$/,'');
11603:     var regexdigit=/^\\d+\$/;
11604:     if (field.name == '${prefix}_min') {
11605:         if (field.value == '') {
11606:             alert('$intalert{passmin}');
11607:             field.value = '$defmin';
11608:         } else {
11609:             if (!regexdigit.test(field.value)) {
11610:                 alert('$intalert{passmin}');
11611:                 field.value = '$defmin';
11612:             }
11613:             var minval = parseInt(field.value,10);
11614:             if (minval < $defmin) {
11615:                 alert('$intalert{passmin}');
11616:                 field.value = '$defmin';
11617:             }
11618:         }
11619:     } else {
11620:         if (field.value == '0') {
11621:             field.value = '';
11622:         }
11623:         if (field.value != '') {
11624:             if (field.name == '${prefix}_expire') {
11625:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11626:                 if (!regexpposnum.test(field.value)) {
11627:                     alert('$intalert{passexp}');
11628:                     field.value = '';
11629:                 } else {
11630:                     var expval = parseFloat(field.value);
11631:                     if (expval == 0) {
11632:                         alert('$intalert{passexp}');
11633:                         field.value = '';
11634:                     }
11635:                 }
11636:             } else {
11637:                 if (!regexdigit.test(field.value)) {
11638:                     if (field.name == '${prefix}_max') {
11639:                         alert('$intalert{passmax}');
11640:                     } else {
11641:                         if (field.name == '${prefix}_numsaved') {
11642:                             alert('$intalert{passnum}');
11643:                         }
11644:                     }
11645:                     field.value = '';
11646:                 }
11647:             }
11648:         }
11649:     }
11650:     return;
11651: }
11652: 
11653: ENDSCRIPT
11654:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11655: }
11656: 
11657: sub coursecategories_javascript {
11658:     my ($settings) = @_;
11659:     my ($output,$jstext,$cathash);
11660:     if (ref($settings) eq 'HASH') {
11661:         $cathash = $settings->{'cats'};
11662:     }
11663:     if (ref($cathash) eq 'HASH') {
11664:         my (@cats,@jsarray,%idx);
11665:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11666:         if (@jsarray > 0) {
11667:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11668:             for (my $i=0; $i<@jsarray; $i++) {
11669:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11670:                     my $catstr = join('","',@{$jsarray[$i]});
11671:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11672:                 }
11673:             }
11674:         }
11675:     } else {
11676:         $jstext  = '    var categories = Array(1);'."\n".
11677:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11678:     }
11679:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11680:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11681:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11682:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11683:     &js_escape(\$instcode_reserved);
11684:     &js_escape(\$communities_reserved);
11685:     &js_escape(\$placement_reserved);
11686:     &js_escape(\$choose_again);
11687:     $output = <<"ENDSCRIPT";
11688: <script type="text/javascript">
11689: // <![CDATA[
11690: function reorderCats(form,parent,item,idx) {
11691:     var changedVal;
11692: $jstext
11693:     var newpos = 'addcategory_pos';
11694:     if (parent == '') {
11695:         var has_instcode = 0;
11696:         var maxtop = categories[idx].length;
11697:         for (var j=0; j<maxtop; j++) {
11698:             if (categories[idx][j] == 'instcode::0') {
11699:                 has_instcode == 1;
11700:             }
11701:         }
11702:         if (has_instcode == 0) {
11703:             categories[idx][maxtop] = 'instcode_pos';
11704:         }
11705:     } else {
11706:         newpos += '_'+parent;
11707:     }
11708:     var maxh = 1 + categories[idx].length;
11709:     var current = new Array;
11710:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11711:     if (item == newpos) {
11712:         changedVal = newitemVal;
11713:     } else {
11714:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11715:         current[newitemVal] = newpos;
11716:     }
11717:     for (var i=0; i<categories[idx].length; i++) {
11718:         var elementName = categories[idx][i];
11719:         if (elementName != item) {
11720:             if (form.elements[elementName]) {
11721:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11722:                 current[currVal] = elementName;
11723:             }
11724:         }
11725:     }
11726:     var oldVal;
11727:     for (var j=0; j<maxh; j++) {
11728:         if (current[j] == undefined) {
11729:             oldVal = j;
11730:         }
11731:     }
11732:     if (oldVal < changedVal) {
11733:         for (var k=oldVal+1; k<=changedVal ; k++) {
11734:            var elementName = current[k];
11735:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11736:         }
11737:     } else {
11738:         for (var k=changedVal; k<oldVal; k++) {
11739:             var elementName = current[k];
11740:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11741:         }
11742:     }
11743:     return;
11744: }
11745: 
11746: function categoryCheck(form) {
11747:     if (form.elements['addcategory_name'].value == 'instcode') {
11748:         alert('$instcode_reserved\\n$choose_again');
11749:         return false;
11750:     }
11751:     if (form.elements['addcategory_name'].value == 'communities') {
11752:         alert('$communities_reserved\\n$choose_again');
11753:         return false;
11754:     }
11755:     if (form.elements['addcategory_name'].value == 'placement') {
11756:         alert('$placement_reserved\\n$choose_again');
11757:         return false;
11758:     }
11759:     return true;
11760: }
11761: 
11762: // ]]>
11763: </script>
11764: 
11765: ENDSCRIPT
11766:     return $output;
11767: }
11768: 
11769: sub initialize_categories {
11770:     my ($itemcount) = @_;
11771:     my ($datatable,$css_class,$chgstr);
11772:     my %default_names = &Apache::lonlocal::texthash (
11773:                       instcode    => 'Official courses (with institutional codes)',
11774:                       communities => 'Communities',
11775:                       placement   => 'Placement Tests',
11776:                         );
11777:     my %selnum = (
11778:                    instcode    => '0',
11779:                    communities => '1',
11780:                    placement   => '2',
11781:                  );
11782:     my %selected;
11783:     foreach my $default ('instcode','communities','placement') {
11784:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11785:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11786:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11787:         $selected{$selnum{$default}} = ' selected="selected"';
11788:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11789:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11790:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11791:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11792:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11793:                      .'<option value="3">4</option></select>&nbsp;'
11794:                      .$default_names{$default}
11795:                      .'</span></td><td><span class="LC_nobreak">'
11796:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11797:                      .&mt('Display').'</label>&nbsp;<label>'
11798:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11799:                  .'</label></span></td></tr>';
11800:         $itemcount ++;
11801:     }
11802:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11803:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11804:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11805:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11806:                   .'<option value="0">1</option>'
11807:                   .'<option value="1">2</option>'
11808:                   .'<option value="2">3</option>'
11809:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11810:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11811:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11812:                   .'</td></tr>';
11813:     return $datatable;
11814: }
11815: 
11816: sub build_category_rows {
11817:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11818:     my ($text,$name,$item,$chgstr);
11819:     if (ref($cats) eq 'ARRAY') {
11820:         my $maxdepth = scalar(@{$cats});
11821:         if (ref($cats->[$depth]) eq 'HASH') {
11822:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11823:                 my $numchildren = @{$cats->[$depth]{$parent}};
11824:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11825:                 $text .= '<td><table class="LC_data_table">';
11826:                 my ($idxnum,$parent_name,$parent_item);
11827:                 my $higher = $depth - 1;
11828:                 if ($higher == 0) {
11829:                     $parent_name = &escape($parent).'::'.$higher;
11830:                 } else {
11831:                     if (ref($path) eq 'ARRAY') {
11832:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11833:                     }
11834:                 }
11835:                 $parent_item = 'addcategory_pos_'.$parent_name;
11836:                 for (my $j=0; $j<=$numchildren; $j++) {
11837:                     if ($j < $numchildren) {
11838:                         $name = $cats->[$depth]{$parent}[$j];
11839:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11840:                         $idxnum = $idx->{$item};
11841:                     } else {
11842:                         $name = $parent_name;
11843:                         $item = $parent_item;
11844:                     }
11845:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11846:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11847:                     for (my $i=0; $i<=$numchildren; $i++) {
11848:                         my $vpos = $i+1;
11849:                         my $selstr;
11850:                         if ($j == $i) {
11851:                             $selstr = ' selected="selected" ';
11852:                         }
11853:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11854:                     }
11855:                     $text .= '</select>&nbsp;';
11856:                     if ($j < $numchildren) {
11857:                         my $deeper = $depth+1;
11858:                         $text .= $name.'&nbsp;'
11859:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11860:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11861:                         if(ref($path) eq 'ARRAY') {
11862:                             push(@{$path},$name);
11863:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11864:                             pop(@{$path});
11865:                         }
11866:                     } else {
11867:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11868:                         if ($j == $numchildren) {
11869:                             $text .= $name;
11870:                         } else {
11871:                             $text .= $item;
11872:                         }
11873:                         $text .= '" value="" />';
11874:                     }
11875:                     $text .= '</td></tr>';
11876:                 }
11877:                 $text .= '</table></td>';
11878:             } else {
11879:                 my $higher = $depth-1;
11880:                 if ($higher == 0) {
11881:                     $name = &escape($parent).'::'.$higher;
11882:                 } else {
11883:                     if (ref($path) eq 'ARRAY') {
11884:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11885:                     }
11886:                 }
11887:                 my $colspan;
11888:                 if ($parent ne 'instcode') {
11889:                     $colspan = $maxdepth - $depth - 1;
11890:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11891:                 }
11892:             }
11893:         }
11894:     }
11895:     return $text;
11896: }
11897: 
11898: sub modifiable_userdata_row {
11899:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11900:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11901:     my ($role,$rolename,$statustype);
11902:     $role = $item;
11903:     if ($context eq 'cancreate') {
11904:         if ($item =~ /^(emailusername)_(.+)$/) {
11905:             $role = $1;
11906:             $statustype = $2;
11907:             if (ref($usertypes) eq 'HASH') {
11908:                 if ($usertypes->{$statustype}) {
11909:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11910:                 } else {
11911:                     $rolename = &mt('Data provided by user');
11912:                 }
11913:             }
11914:         }
11915:     } elsif ($context eq 'selfcreate') {
11916:         if (ref($usertypes) eq 'HASH') {
11917:             $rolename = $usertypes->{$role};
11918:         } else {
11919:             $rolename = $role;
11920:         }
11921:     } elsif ($context eq 'lti') {
11922:         $rolename = &mt('Institutional data used (if available)');
11923:     } elsif ($context eq 'privacy') {
11924:         $rolename = $itemdesc;
11925:     } else {
11926:         if ($role eq 'cr') {
11927:             $rolename = &mt('Custom role');
11928:         } else {
11929:             $rolename = &Apache::lonnet::plaintext($role);
11930:         }
11931:     }
11932:     my (@fields,%fieldtitles);
11933:     if (ref($fieldsref) eq 'ARRAY') {
11934:         @fields = @{$fieldsref};
11935:     } else {
11936:         @fields = ('lastname','firstname','middlename','generation',
11937:                    'permanentemail','id');
11938:     }
11939:     if ((ref($titlesref) eq 'HASH')) {
11940:         %fieldtitles = %{$titlesref};
11941:     } else {
11942:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11943:     }
11944:     my $output;
11945:     my $css_class;
11946:     if ($rowcount%2) {
11947:         $css_class = 'LC_odd_row';
11948:     }
11949:     if ($customcss) {
11950:         $css_class .= " $customcss";
11951:     }
11952:     $css_class =~ s/^\s+//;
11953:     if ($css_class) {
11954:         $css_class = ' class="'.$css_class.'"';
11955:     }
11956:     if ($rowstyle) {
11957:         $css_class .= ' style="'.$rowstyle.'"';
11958:     }
11959:     if ($rowid) {
11960:         $rowid = ' id="'.$rowid.'"';
11961:     }
11962:     $output = '<tr '.$css_class.$rowid.'>'.
11963:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11964:               '<td class="LC_left_item" colspan="2"><table>';
11965:     my $rem;
11966:     my %checks;
11967:     if (ref($settings) eq 'HASH') {
11968:         my $hashref;
11969:         if ($context eq 'lti') {
11970:             if (ref($settings) eq 'HASH') {
11971:                 $hashref = $settings->{'instdata'};
11972:             }
11973:         } elsif ($context eq 'privacy') {
11974:             my ($key,$inner) = split(/_/,$role);
11975:             if (ref($settings) eq 'HASH') {
11976:                 if (ref($settings->{$key}) eq 'HASH') {
11977:                     $hashref = $settings->{$key}->{$inner};
11978:                 }
11979:             }
11980:         } elsif (ref($settings->{$context}) eq 'HASH') {
11981:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11982:                 $hashref = $settings->{'lti_instdata'};
11983:             }
11984:             if ($role eq 'emailusername') {
11985:                 if ($statustype) {
11986:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11987:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11988:                     }
11989:                 }
11990:             }
11991:         }
11992:         if (ref($hashref) eq 'HASH') {
11993:             foreach my $field (@fields) {
11994:                 if ($hashref->{$field}) {
11995:                     if ($role eq 'emailusername') {
11996:                         $checks{$field} = $hashref->{$field};
11997:                     } else {
11998:                         $checks{$field} = ' checked="checked" ';
11999:                     }
12000:                 }
12001:             }
12002:         }
12003:     }
12004:     my $total = scalar(@fields);
12005:     for (my $i=0; $i<$total; $i++) {
12006:         $rem = $i%($numinrow);
12007:         if ($rem == 0) {
12008:             if ($i > 0) {
12009:                 $output .= '</tr>';
12010:             }
12011:             $output .= '<tr>';
12012:         }
12013:         my $check = ' ';
12014:         unless ($role eq 'emailusername') {
12015:             if (exists($checks{$fields[$i]})) {
12016:                 $check = $checks{$fields[$i]};
12017:             } elsif ($context eq 'privacy') {
12018:                 if ($role =~ /^priv_(domain|course)$/) {
12019:                     if (ref($settings) ne 'HASH') {
12020:                         $check = ' checked="checked" ';
12021:                     }
12022:                 } elsif ($role =~ /^priv_(author|community)$/) {
12023:                     if (ref($settings) ne 'HASH') {
12024:                         unless ($fields[$i] eq 'id') {
12025:                             $check = ' checked="checked" ';
12026:                         }
12027:                     }
12028:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
12029:                     if (ref($settings) ne 'HASH') {
12030:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
12031:                             $check = ' checked="checked" ';
12032:                         }
12033:                     }
12034:                 }
12035:             } elsif ($context ne 'lti') {
12036:                 if ($role eq 'st') {
12037:                     if (ref($settings) ne 'HASH') {
12038:                         $check = ' checked="checked" '; 
12039:                     }
12040:                 }
12041:             }
12042:         }
12043:         $output .= '<td class="LC_left_item">'.
12044:                    '<span class="LC_nobreak">';
12045:         my $prefix = 'canmodify';
12046:         if ($role eq 'emailusername') {
12047:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
12048:                 $checks{$fields[$i]} = 'omit';
12049:             }
12050:             foreach my $option ('required','optional','omit') {
12051:                 my $checked='';
12052:                 if ($checks{$fields[$i]} eq $option) {
12053:                     $checked='checked="checked" ';
12054:                 }
12055:                 $output .= '<label>'.
12056:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
12057:                            &mt($option).'</label>'.('&nbsp;' x2);
12058:             }
12059:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
12060:         } else {
12061:             if ($context eq 'lti') {
12062:                 $prefix = 'lti';
12063:             } elsif ($context eq 'privacy') {
12064:                 $prefix = 'privacy';
12065:             }
12066:             $output .= '<label>'.
12067:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
12068:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
12069:                        '</label>';
12070:         }
12071:         $output .= '</span></td>';
12072:     }
12073:     $rem = $total%$numinrow;
12074:     my $colsleft;
12075:     if ($rem) {
12076:         $colsleft = $numinrow - $rem;
12077:     }
12078:     if ($colsleft > 1) {
12079:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
12080:                    '&nbsp;</td>';
12081:     } elsif ($colsleft == 1) {
12082:         $output .= '<td class="LC_left_item">&nbsp;</td>';
12083:     }
12084:     $output .= '</tr></table></td></tr>';
12085:     return $output;
12086: }
12087: 
12088: sub insttypes_row {
12089:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
12090:         $customcss,$rowstyle) = @_;
12091:     my %lt = &Apache::lonlocal::texthash (
12092:                       cansearch      => 'Users allowed to search',
12093:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
12094:                       lockablenames  => 'User preference to lock name',
12095:                       selfassign     => 'Self-reportable affiliations',
12096:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
12097:                       webdav         => 'WebDAV access available',
12098:                       authorquota    => 'Authoring Space quota (MB)',
12099:              );
12100:     my ($showdom,$defaultquota);
12101:     if ($context eq 'cansearch') {
12102:         $showdom = ' ('.$dom.')';
12103:     } elsif ($context eq 'authorquota') {
12104:         $defaultquota = 500;
12105:     }
12106:     my $class = 'LC_left_item';
12107:     if ($context eq 'statustocreate') {
12108:         $class = 'LC_right_item';
12109:     }
12110:     my $css_class;
12111:     if ($$rowtotal%2) {
12112:         $css_class = 'LC_odd_row';
12113:     }
12114:     if ($customcss) {
12115:         $css_class .= ' '.$customcss;
12116:     }
12117:     $css_class =~ s/^\s+//;
12118:     if ($css_class) {
12119:         $css_class = ' class="'.$css_class.'"';
12120:     }
12121:     if ($rowstyle) {
12122:         $css_class .= ' style="'.$rowstyle.'"';
12123:     }
12124:     if ($onclick) {
12125:         $onclick = 'onclick="'.$onclick.'" ';
12126:     }
12127:     my $output = '<tr'.$css_class.'>'.
12128:                  '<td>'.$lt{$context}.$showdom.
12129:                  '</td><td class="'.$class.'" colspan="2"><table>';
12130:     my $rem;
12131:     if (ref($types) eq 'ARRAY') {
12132:         for (my $i=0; $i<@{$types}; $i++) {
12133:             if (defined($usertypes->{$types->[$i]})) {
12134:                 my $rem = $i%($numinrow);
12135:                 if ($rem == 0) {
12136:                     if ($i > 0) {
12137:                         $output .= '</tr>';
12138:                     }
12139:                     $output .= '<tr>';
12140:                 }
12141:                 if ($context eq 'authorquota') {
12142:                     my $currquota;
12143:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
12144:                         $currquota = $settings->{$context}->{$types->[$i]};
12145:                     } else {
12146:                         $currquota = $defaultquota;
12147:                     }
12148:                     $output .= '<td class="LC_left_item">'."\n".
12149:                                '<label><span class="LC_nobreak">'."\n".
12150:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
12151:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
12152:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12153:                                '</label></td>';
12154:                 } else {
12155:                     my $check = ' ';
12156:                     if (ref($settings) eq 'HASH') {
12157:                         if (ref($settings->{$context}) eq 'ARRAY') {
12158:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
12159:                                 $check = ' checked="checked" ';
12160:                             }
12161:                         } elsif (ref($settings->{$context}) eq 'HASH') {
12162:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
12163:                                 $check = ' checked="checked" ';
12164:                             } elsif ($context eq 'webdav') {
12165:                                 if ($settings->{$context}->{$types->[$i]}) {
12166:                                     $check = ' checked="checked" ';
12167:                                 }
12168:                             }
12169:                         } elsif ($context eq 'statustocreate') {
12170:                             $check = ' checked="checked" ';
12171:                         }
12172:                     }
12173:                     $output .= '<td class="LC_left_item">'.
12174:                                '<span class="LC_nobreak"><label>'.
12175:                                '<input type="checkbox" name="'.$context.'" '.
12176:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
12177:                                $usertypes->{$types->[$i]}.'</label></span></td>';
12178:                 }
12179:             }
12180:         }
12181:         $rem = @{$types}%($numinrow);
12182:     }
12183:     my $colsleft = $numinrow - $rem;
12184:     if ($context eq 'overrides') {
12185:         if ($colsleft > 1) {
12186:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12187:         } else {
12188:             $output .= '<td class="LC_left_item">';
12189:         }
12190:         $output .= '&nbsp;';
12191:     } else {
12192:         if ($rem == 0) {
12193:             $output .= '<tr>';
12194:         }
12195:         if ($colsleft > 1) {
12196:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12197:         } else {
12198:             $output .= '<td class="LC_left_item">';
12199:         }
12200:         if ($context eq 'authorquota') {
12201:             my $currquota = 500;
12202:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
12203:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
12204:                     $currquota = $settings->{$context}{'default'};
12205:                 }
12206:             }
12207:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
12208:                        '<input type="text" name="'.$context.'_default" '.
12209:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12210:                        '</label>';
12211:         } else {
12212:             my $defcheck = ' ';
12213:             if (ref($settings) eq 'HASH') {
12214:                 if (ref($settings->{$context}) eq 'ARRAY') {
12215:                     if (grep(/^default$/,@{$settings->{$context}})) {
12216:                         $defcheck = ' checked="checked" ';
12217:                     }
12218:                 } elsif (ref($settings->{$context}) eq 'HASH') {
12219:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
12220:                         $defcheck = ' checked="checked" ';
12221:                     } elsif ($context eq 'webdav') {
12222:                         if ($settings->{$context}->{'default'}) {
12223:                             $defcheck = ' checked="checked" ';
12224:                         }
12225:                     }
12226:                 } elsif ($context eq 'statustocreate') {
12227:                     $defcheck = ' checked="checked" ';
12228:                 }
12229:             }
12230:             $output .= '<span class="LC_nobreak"><label>'.
12231:                        '<input type="checkbox" name="'.$context.'" '.
12232:                        'value="default"'.$defcheck.$onclick.'/>'.
12233:                        $othertitle.'</label></span>';
12234:         }
12235:     }
12236:     $output .= '</td></tr></table></td></tr>';
12237:     return $output;
12238: }
12239: 
12240: sub sorted_searchtitles {
12241:     my %searchtitles = &Apache::lonlocal::texthash(
12242:                          'uname' => 'username',
12243:                          'lastname' => 'last name',
12244:                          'lastfirst' => 'last name, first name',
12245:                      );
12246:     my @titleorder = ('uname','lastname','lastfirst');
12247:     return (\%searchtitles,\@titleorder);
12248: }
12249: 
12250: sub sorted_searchtypes {
12251:     my %srchtypes_desc = (
12252:                            exact    => 'is exact match',
12253:                            contains => 'contains ..',
12254:                            begins   => 'begins with ..',
12255:                          );
12256:     my @srchtypeorder = ('exact','begins','contains');
12257:     return (\%srchtypes_desc,\@srchtypeorder);
12258: }
12259: 
12260: sub usertype_update_row {
12261:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
12262:     my $datatable;
12263:     my $numinrow = 4;
12264:     foreach my $type (@{$types}) {
12265:         if (defined($usertypes->{$type})) {
12266:             $$rownums ++;
12267:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
12268:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
12269:                           '</td><td class="LC_left_item"><table>';
12270:             for (my $i=0; $i<@{$fields}; $i++) {
12271:                 my $rem = $i%($numinrow);
12272:                 if ($rem == 0) {
12273:                     if ($i > 0) {
12274:                         $datatable .= '</tr>';
12275:                     }
12276:                     $datatable .= '<tr>';
12277:                 }
12278:                 my $check = ' ';
12279:                 if (ref($settings) eq 'HASH') {
12280:                     if (ref($settings->{'fields'}) eq 'HASH') {
12281:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
12282:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
12283:                                 $check = ' checked="checked" ';
12284:                             }
12285:                         }
12286:                     }
12287:                 }
12288: 
12289:                 if ($i == @{$fields}-1) {
12290:                     my $colsleft = $numinrow - $rem;
12291:                     if ($colsleft > 1) {
12292:                         $datatable .= '<td colspan="'.$colsleft.'">';
12293:                     } else {
12294:                         $datatable .= '<td>';
12295:                     }
12296:                 } else {
12297:                     $datatable .= '<td>';
12298:                 }
12299:                 $datatable .= '<span class="LC_nobreak"><label>'.
12300:                               '<input type="checkbox" name="updateable_'.$type.
12301:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
12302:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
12303:             }
12304:             $datatable .= '</tr></table></td></tr>';
12305:         }
12306:     }
12307:     return $datatable;
12308: }
12309: 
12310: sub modify_login {
12311:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12312:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12313:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12314:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12315:     %title = ( coursecatalog => 'Display course catalog',
12316:                adminmail => 'Display administrator E-mail address',
12317:                helpdesk  => 'Display "Contact Helpdesk" link',
12318:                newuser => 'Link for visitors to create a user account',
12319:                loginheader => 'Log-in box header',
12320:                saml => 'Dual SSO and non-SSO login');
12321:     @offon = ('off','on');
12322:     if (ref($domconfig{login}) eq 'HASH') {
12323:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12324:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12325:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12326:             }
12327:         }
12328:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12329:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12330:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12331:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12332:                     $saml{$lonhost} = 1;
12333:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12334:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12335:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12336:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12337:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12338:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12339:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12340:                 }
12341:             }
12342:         }
12343:     }
12344:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12345:                                            \%domconfig,\%loginhash);
12346:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12347:     foreach my $item (@toggles) {
12348:         $loginhash{login}{$item} = $env{'form.'.$item};
12349:     }
12350:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12351:     if (ref($colchanges{'login'}) eq 'HASH') {  
12352:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12353:                                          \%loginhash);
12354:     }
12355: 
12356:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12357:     my %domservers = &Apache::lonnet::get_servers($dom);
12358:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12359:     if (keys(%servers) > 1) {
12360:         foreach my $lonhost (keys(%servers)) {
12361:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12362:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12363:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12364:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12365:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12366:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12367:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12368:                         $changes{'loginvia'}{$lonhost} = 1;
12369:                     } else {
12370:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12371:                         $changes{'loginvia'}{$lonhost} = 1;
12372:                     }
12373:                 } else {
12374:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12375:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12376:                         $changes{'loginvia'}{$lonhost} = 1;
12377:                     }
12378:                 }
12379:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12380:                     foreach my $item (@loginvia_attribs) {
12381:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12382:                     }
12383:                 } else {
12384:                     foreach my $item (@loginvia_attribs) {
12385:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12386:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12387:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12388:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12389:                                 $new = '/';
12390:                             }
12391:                         }
12392:                         if (($item eq 'custompath') && 
12393:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12394:                             $new = '';
12395:                         }
12396:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12397:                             $changes{'loginvia'}{$lonhost} = 1;
12398:                         }
12399:                         if ($item eq 'exempt') {
12400:                             $new = &check_exempt_addresses($new);
12401:                         }
12402:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12403:                     }
12404:                 }
12405:             } else {
12406:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12407:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12408:                     $changes{'loginvia'}{$lonhost} = 1;
12409:                     foreach my $item (@loginvia_attribs) {
12410:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12411:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12412:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12413:                                 $new = '/';
12414:                             }
12415:                         }
12416:                         if (($item eq 'custompath') && 
12417:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12418:                             $new = '';
12419:                         }
12420:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12421:                     }
12422:                 }
12423:             }
12424:         }
12425:     }
12426: 
12427:     my $servadm = $r->dir_config('lonAdmEMail');
12428:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12429:     if (ref($domconfig{'login'}) eq 'HASH') {
12430:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12431:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12432:                 if ($lang eq 'nolang') {
12433:                     push(@currlangs,$lang);
12434:                 } elsif (defined($langchoices{$lang})) {
12435:                     push(@currlangs,$lang);
12436:                 } else {
12437:                     next;
12438:                 }
12439:             }
12440:         }
12441:     }
12442:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12443:     if (@currlangs > 0) {
12444:         foreach my $lang (@currlangs) {
12445:             if (grep(/^\Q$lang\E$/,@delurls)) {
12446:                 $changes{'helpurl'}{$lang} = 1;
12447:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12448:                 $changes{'helpurl'}{$lang} = 1;
12449:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12450:                 push(@newlangs,$lang);
12451:             } else {
12452:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12453:             }
12454:         }
12455:     }
12456:     unless (grep(/^nolang$/,@currlangs)) {
12457:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12458:             $changes{'helpurl'}{'nolang'} = 1;
12459:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12460:             push(@newlangs,'nolang');
12461:         }
12462:     }
12463:     if ($env{'form.loginhelpurl_add_lang'}) {
12464:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12465:             ($env{'form.loginhelpurl_add_file.filename'})) {
12466:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12467:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12468:         }
12469:     }
12470:     if ((@newlangs > 0) || ($addedfile)) {
12471:         my $error;
12472:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12473:         if ($configuserok eq 'ok') {
12474:             if ($switchserver) {
12475:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12476:             } elsif ($author_ok eq 'ok') {
12477:                 my @allnew = @newlangs;
12478:                 if ($addedfile ne '') {
12479:                     push(@allnew,$addedfile);
12480:                 }
12481:                 my $modified = [];
12482:                 foreach my $lang (@allnew) {
12483:                     my $formelem = 'loginhelpurl_'.$lang;
12484:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12485:                         $formelem = 'loginhelpurl_add_file';
12486:                     }
12487:                     (my $result,$newurl{$lang}) =
12488:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12489:                                                                 "help/$lang",'','',$newfile{$lang},
12490:                                                                 $modified);
12491:                     if ($result eq 'ok') {
12492:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12493:                         $changes{'helpurl'}{$lang} = 1;
12494:                     } else {
12495:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12496:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12497:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12498:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12499:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12500:                         }
12501:                     }
12502:                 }
12503:                 &update_modify_urls($r,$modified);
12504:             } else {
12505:                 $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);
12506:             }
12507:         } else {
12508:             $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);
12509:         }
12510:         if ($error) {
12511:             &Apache::lonnet::logthis($error);
12512:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12513:         }
12514:     }
12515: 
12516:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12517:     if (ref($domconfig{'login'}) eq 'HASH') {
12518:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12519:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12520:                 if ($domservers{$lonhost}) {
12521:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12522:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12523:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12524:                     }
12525:                 }
12526:             }
12527:         }
12528:     }
12529:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12530:     foreach my $lonhost (sort(keys(%domservers))) {
12531:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12532:             $changes{'headtag'}{$lonhost} = 1;
12533:         } else {
12534:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12535:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12536:             }
12537:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12538:                 push(@newhosts,$lonhost);
12539:             } elsif ($currheadtagurls{$lonhost}) {
12540:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12541:                 if ($currexempt{$lonhost}) {
12542:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12543:                         $changes{'headtag'}{$lonhost} = 1;
12544:                     }
12545:                 } elsif ($possexempt{$lonhost}) {
12546:                     $changes{'headtag'}{$lonhost} = 1;
12547:                 }
12548:                 if ($possexempt{$lonhost}) {
12549:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12550:                 }
12551:             }
12552:         }
12553:     }
12554:     if (@newhosts) {
12555:         my $error;
12556:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12557:         if ($configuserok eq 'ok') {
12558:             if ($switchserver) {
12559:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12560:             } elsif ($author_ok eq 'ok') {
12561:                 my $modified = [];
12562:                 foreach my $lonhost (@newhosts) {
12563:                     my $formelem = 'loginheadtag_'.$lonhost;
12564:                     (my $result,$newheadtagurls{$lonhost}) =
12565:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12566:                                                                 "login/headtag/$lonhost",'','',
12567:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12568:                                                                 $modified);
12569:                     if ($result eq 'ok') {
12570:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12571:                         $changes{'headtag'}{$lonhost} = 1;
12572:                         if ($possexempt{$lonhost}) {
12573:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12574:                         }
12575:                     } else {
12576:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12577:                                            $newheadtagurls{$lonhost},$result);
12578:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12579:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12580:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12581:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12582:                         }
12583:                     }
12584:                 }
12585:                 &update_modify_urls($r,$modified);
12586:             } else {
12587:                 $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);
12588:             }
12589:         } else {
12590:             $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);
12591:         }
12592:         if ($error) {
12593:             &Apache::lonnet::logthis($error);
12594:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12595:         }
12596:     }
12597:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12598:     my @newsamlimgs;
12599:     foreach my $lonhost (keys(%domservers)) {
12600:         if ($env{'form.saml_'.$lonhost}) {
12601:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12602:                 push(@newsamlimgs,$lonhost);
12603:             }
12604:             foreach my $item ('text','alt','url','title','window','notsso') {
12605:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12606:             }
12607:             if ($saml{$lonhost}) {
12608:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12609:                     $env{'form.saml_window_'.$lonhost} = '';
12610:                 }
12611:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12612: #FIXME Need to obsolete published image
12613:                     delete($currsaml{$lonhost}{'img'});
12614:                     $changes{'saml'}{$lonhost} = 1;
12615:                 }
12616:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12617:                     $changes{'saml'}{$lonhost} = 1;
12618:                 }
12619:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12620:                     $changes{'saml'}{$lonhost} = 1;
12621:                 }
12622:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12623:                     $changes{'saml'}{$lonhost} = 1;
12624:                 }
12625:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12626:                     $changes{'saml'}{$lonhost} = 1;
12627:                 }
12628:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12629:                     $changes{'saml'}{$lonhost} = 1;
12630:                 }
12631:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12632:                     $changes{'saml'}{$lonhost} = 1;
12633:                 }
12634:             } else {
12635:                 $changes{'saml'}{$lonhost} = 1;
12636:             }
12637:             foreach my $item ('text','alt','url','title','window','notsso') {
12638:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12639:             }
12640:         } else {
12641:             if ($saml{$lonhost}) {
12642:                 $changes{'saml'}{$lonhost} = 1;
12643:                 delete($currsaml{$lonhost});
12644:             }
12645:         }
12646:     }
12647:     foreach my $posshost (keys(%currsaml)) {
12648:         unless (exists($domservers{$posshost})) {
12649:             delete($currsaml{$posshost});
12650:         }
12651:     }
12652:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12653:     if (@newsamlimgs) {
12654:         my $error;
12655:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12656:         if ($configuserok eq 'ok') {
12657:             if ($switchserver) {
12658:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12659:             } elsif ($author_ok eq 'ok') {
12660:                 my $modified = [];
12661:                 foreach my $lonhost (@newsamlimgs) {
12662:                     my $formelem = 'saml_img_'.$lonhost;
12663:                     my ($result,$imgurl) =
12664:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12665:                                                                 "login/saml/$lonhost",'','',
12666:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12667:                                                                 $modified);
12668:                     if ($result eq 'ok') {
12669:                         $currsaml{$lonhost}{'img'} = $imgurl;
12670:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12671:                         $changes{'saml'}{$lonhost} = 1;
12672:                     } else {
12673:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12674:                                            $lonhost,$result);
12675:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12676:                     }
12677:                 }
12678:                 &update_modify_urls($r,$modified);
12679:             } else {
12680:                 $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);
12681:             }
12682:         } else {
12683:             $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);
12684:         }
12685:         if ($error) {
12686:             &Apache::lonnet::logthis($error);
12687:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12688:         }
12689:     }
12690:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12691: 
12692:     my $defaulthelpfile = '/adm/loginproblems.html';
12693:     my $defaulttext = &mt('Default in use');
12694: 
12695:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12696:                                              $dom);
12697:     if ($putresult eq 'ok') {
12698:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12699:         my %defaultchecked = (
12700:                     'coursecatalog' => 'on',
12701:                     'helpdesk'      => 'on',
12702:                     'adminmail'     => 'off',
12703:                     'newuser'       => 'off',
12704:         );
12705:         if (ref($domconfig{'login'}) eq 'HASH') {
12706:             foreach my $item (@toggles) {
12707:                 if ($defaultchecked{$item} eq 'on') { 
12708:                     if (($domconfig{'login'}{$item} eq '0') &&
12709:                         ($env{'form.'.$item} eq '1')) {
12710:                         $changes{$item} = 1;
12711:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12712:                               $domconfig{'login'}{$item} eq '1') &&
12713:                              ($env{'form.'.$item} eq '0')) {
12714:                         $changes{$item} = 1;
12715:                     }
12716:                 } elsif ($defaultchecked{$item} eq 'off') {
12717:                     if (($domconfig{'login'}{$item} eq '1') &&
12718:                         ($env{'form.'.$item} eq '0')) {
12719:                         $changes{$item} = 1;
12720:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12721:                               $domconfig{'login'}{$item} eq '0') &&
12722:                              ($env{'form.'.$item} eq '1')) {
12723:                         $changes{$item} = 1;
12724:                     }
12725:                 }
12726:             }
12727:         }
12728:         if (keys(%changes) > 0 || $colchgtext) {
12729:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12730:             if (exists($changes{'saml'})) {
12731:                 my $hostid_in_use;
12732:                 my @hosts = &Apache::lonnet::current_machine_ids();
12733:                 if (@hosts > 1) {
12734:                     foreach my $hostid (@hosts) {
12735:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12736:                             $hostid_in_use = $hostid;
12737:                             last;
12738:                         }
12739:                     }
12740:                 } else {
12741:                     $hostid_in_use = $r->dir_config('lonHostID');
12742:                 }
12743:                 if (($hostid_in_use) &&
12744:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12745:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12746:                 }
12747:                 if (ref($lastactref) eq 'HASH') {
12748:                     if (ref($changes{'saml'}) eq 'HASH') {
12749:                         my %updates;
12750:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12751:                         $lastactref->{'samllanding'} = \%updates;
12752:                     }
12753:                 }
12754:             }
12755:             if (ref($lastactref) eq 'HASH') {
12756:                 $lastactref->{'domainconfig'} = 1;
12757:             }
12758:             $resulttext = &mt('Changes made:').'<ul>';
12759:             foreach my $item (sort(keys(%changes))) {
12760:                 if ($item eq 'loginvia') {
12761:                     if (ref($changes{$item}) eq 'HASH') {
12762:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12763:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12764:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12765:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12766:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12767:                                     $protocol = 'http' if ($protocol ne 'https');
12768:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12769: 
12770:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12771:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12772:                                     } else {
12773:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12774:                                     }
12775:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12776:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12777:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12778:                                     }
12779:                                     $resulttext .= '</li>';
12780:                                 } else {
12781:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12782:                                 }
12783:                             } else {
12784:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12785:                             }
12786:                         }
12787:                         $resulttext .= '</ul></li>';
12788:                     }
12789:                 } elsif ($item eq 'helpurl') {
12790:                     if (ref($changes{$item}) eq 'HASH') {
12791:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12792:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12793:                                 my ($chg,$link);
12794:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12795:                                 if ($lang eq 'nolang') {
12796:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12797:                                 } else {
12798:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12799:                                 }
12800:                                 $resulttext .= '<li>'.$chg.'</li>';
12801:                             } else {
12802:                                 my $chg;
12803:                                 if ($lang eq 'nolang') {
12804:                                     $chg = &mt('custom log-in help file for no preferred language');
12805:                                 } else {
12806:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12807:                                 }
12808:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12809:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12810:                                                       '?inhibitmenu=yes',$chg,600,500).
12811:                                                '</li>';
12812:                             }
12813:                         }
12814:                     }
12815:                 } elsif ($item eq 'headtag') {
12816:                     if (ref($changes{$item}) eq 'HASH') {
12817:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12818:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12819:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12820:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12821:                                 $resulttext .= '<li><a href="'.
12822:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12823:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12824:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12825:                                 if ($possexempt{$lonhost}) {
12826:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12827:                                 } else {
12828:                                     $resulttext .= &mt('included for any client IP');
12829:                                 }
12830:                                 $resulttext .= '</li>';
12831:                             }
12832:                         }
12833:                     }
12834:                 } elsif ($item eq 'saml') {
12835:                     if (ref($changes{$item}) eq 'HASH') {
12836:                         my %notlt = (
12837:                                        text   => 'Text for log-in by SSO',
12838:                                        img    => 'SSO button image',
12839:                                        alt    => 'Alt text for button image',
12840:                                        url    => 'SSO URL',
12841:                                        title  => 'Tooltip for SSO link',
12842:                                        window => 'Pop-up window if iframe',
12843:                                        notsso => 'Text for non-SSO log-in',
12844:                                     );
12845:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12846:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12847:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12848:                                                '<ul>';
12849:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
12850:                                     if ($currsaml{$lonhost}{$key} eq '') {
12851:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12852:                                     } else {
12853:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12854:                                         if ($key eq 'img') {
12855:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12856:                                         } elsif ($key eq 'window') {
12857:                                             $value = 'On';
12858:                                         }
12859:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12860:                                                                   $value).'</li>';
12861:                                     }
12862:                                 }
12863:                                 $resulttext .= '</ul></li>';
12864:                             } else {
12865:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12866:                             }
12867:                         }
12868:                     }
12869:                 } elsif ($item eq 'captcha') {
12870:                     if (ref($loginhash{'login'}) eq 'HASH') {
12871:                         my $chgtxt;
12872:                         if ($loginhash{'login'}{$item} eq 'notused') {
12873:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12874:                         } else {
12875:                             my %captchas = &captcha_phrases();
12876:                             if ($captchas{$loginhash{'login'}{$item}}) {
12877:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12878:                             } else {
12879:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12880:                             }
12881:                         }
12882:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12883:                     }
12884:                 } elsif ($item eq 'recaptchakeys') {
12885:                     if (ref($loginhash{'login'}) eq 'HASH') {
12886:                         my ($privkey,$pubkey);
12887:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12888:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12889:                             $privkey = $loginhash{'login'}{$item}{'private'};
12890:                         }
12891:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12892:                         if (!$pubkey) {
12893:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12894:                         } else {
12895:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12896:                         }
12897:                         if (!$privkey) {
12898:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12899:                         } else {
12900:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12901:                         }
12902:                         $chgtxt .= '</ul>';
12903:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12904:                     }
12905:                 } elsif ($item eq 'recaptchaversion') {
12906:                     if (ref($loginhash{'login'}) eq 'HASH') {
12907:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12908:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12909:                                            '</li>';
12910:                         }
12911:                     }
12912:                 } else {
12913:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12914:                 }
12915:             }
12916:             $resulttext .= $colchgtext.'</ul>';
12917:         } else {
12918:             $resulttext = &mt('No changes made to log-in page settings');
12919:         }
12920:     } else {
12921:         $resulttext = '<span class="LC_error">'.
12922: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12923:     }
12924:     if ($errors) {
12925:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12926:                        $errors.'</ul>';
12927:     }
12928:     return $resulttext;
12929: }
12930: 
12931: sub check_exempt_addresses {
12932:     my ($iplist) = @_;
12933:     $iplist =~ s/^\s+//;
12934:     $iplist =~ s/\s+$//;
12935:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12936:     my (@okips,$new);
12937:     foreach my $ip (@poss_ips) {
12938:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12939:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12940:                 push(@okips,$ip);
12941:             }
12942:         }
12943:     }
12944:     if (@okips > 0) {
12945:         $new = join(',',@okips);
12946:     } else {
12947:         $new = '';
12948:     }
12949:     return $new;
12950: }
12951: 
12952: sub color_font_choices {
12953:     my %choices =
12954:         &Apache::lonlocal::texthash (
12955:             img => "Header",
12956:             bgs => "Background colors",
12957:             links => "Link colors",
12958:             images => "Images",
12959:             font => "Font color",
12960:             fontmenu => "Font menu",
12961:             pgbg => "Page",
12962:             tabbg => "Header",
12963:             sidebg => "Border",
12964:             link => "Link",
12965:             alink => "Active link",
12966:             vlink => "Visited link",
12967:         );
12968:     return %choices;
12969: }
12970: 
12971: sub modify_ipaccess {
12972:     my ($dom,$lastactref,%domconfig) = @_;
12973:     my (@allpos,%changes,%confhash,$errors,$resulttext);
12974:     my (@items,%deletions,%itemids,@warnings);
12975:     my ($typeorder,$types) = &commblocktype_text();
12976:     if ($env{'form.ipaccess_add'}) {
12977:         my $name = $env{'form.ipaccess_name_add'};
12978:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
12979:         if ($newid) {
12980:             $itemids{'add'} = $newid;
12981:             push(@items,'add');
12982:             $changes{$newid} = 1;
12983:         } else {
12984:             $error = &mt('Failed to acquire unique ID for new IP access control item');
12985:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12986:         }
12987:     }
12988:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12989:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
12990:         if (@todelete) {
12991:             map { $deletions{$_} = 1; } @todelete;
12992:         }
12993:         my $maxnum = $env{'form.ipaccess_maxnum'};
12994:         for (my $i=0; $i<$maxnum; $i++) {
12995:             my $itemid = $env{'form.ipaccess_id_'.$i};
12996:             $itemid =~ s/\D+//g;
12997:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12998:                 if ($deletions{$itemid}) {
12999:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
13000:                 } else {
13001:                     push(@items,$i);
13002:                     $itemids{$i} = $itemid;
13003:                 }
13004:             }
13005:         }
13006:     }
13007:     foreach my $idx (@items) {
13008:         my $itemid = $itemids{$idx};
13009:         next unless ($itemid);
13010:         my %current;
13011:         unless ($idx eq 'add') {
13012:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13013:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
13014:             }
13015:         }
13016:         my $position = $env{'form.ipaccess_pos_'.$itemid};
13017:         $position =~ s/\D+//g;
13018:         if ($position ne '') {
13019:             $allpos[$position] = $itemid;
13020:         }
13021:         my $name = $env{'form.ipaccess_name_'.$idx};
13022:         $name =~ s/^\s+|\s+$//g;
13023:         $confhash{$itemid}{'name'} = $name;
13024:         my $possrange = $env{'form.ipaccess_range_'.$idx};
13025:         $possrange =~ s/^\s+|\s+$//g;
13026:         unless ($possrange eq '') {
13027:             $possrange =~ s/[\r\n]+/\s/g;
13028:             $possrange =~ s/\s*-\s*/-/g;
13029:             $possrange =~ s/\s+/,/g;
13030:             $possrange =~ s/,+/,/g;
13031:             if ($possrange ne '') {
13032:                 my (@ok,$count);
13033:                 $count = 0;
13034:                 foreach my $poss (split(/\,/,$possrange)) {
13035:                     $count ++;
13036:                     $poss = &validate_ip_pattern($poss);
13037:                     if ($poss ne '') {
13038:                         push(@ok,$poss);
13039:                     }
13040:                 }
13041:                 my $diff = $count - scalar(@ok);
13042:                 if ($diff) {
13043:                     $errors .= '<li><span class="LC_error">'.
13044:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
13045:                                    $diff,$name).
13046:                                '</span></li>';
13047:                 }
13048:                 if (@ok) {
13049:                     my @cidr_list;
13050:                     foreach my $item (@ok) {
13051:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
13052:                     }
13053:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
13054:                 }
13055:             }
13056:         }
13057:         foreach my $field ('name','ip') {
13058:             unless (($idx eq 'add') || ($changes{$itemid})) {
13059:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
13060:                     $changes{$itemid} = 1;
13061:                     last;
13062:                 }
13063:             }
13064:         }
13065:         $confhash{$itemid}{'commblocks'} = {};
13066: 
13067:         my %commblocks;
13068:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
13069:         foreach my $type (@{$typeorder}) {
13070:             if ($commblocks{$type}) {
13071:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
13072:             }
13073:             unless (($idx eq 'add') || ($changes{$itemid})) {
13074:                 if (ref($current{'commblocks'}) eq 'HASH') {
13075:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
13076:                         $changes{$itemid} = 1;
13077:                     }
13078:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
13079:                     $changes{$itemid} = 1;
13080:                 }
13081:             }
13082:         }
13083:         $confhash{$itemid}{'courses'} = {};
13084:         my %crsdeletions;
13085:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
13086:         if (@delcrs) {
13087:             map { $crsdeletions{$_} = 1; } @delcrs;
13088:         }
13089:         if (ref($current{'courses'}) eq 'HASH') {
13090:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
13091:                 if ($crsdeletions{$cid}) {
13092:                     $changes{$itemid} = 1;
13093:                 } else {
13094:                     $confhash{$itemid}{'courses'}{$cid} = 1;
13095:                 }
13096:             }
13097:         }
13098:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
13099:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
13100:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
13101:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
13102:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
13103:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
13104:                 $errors .= '<li><span class="LC_error">'.
13105:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
13106:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
13107:                            '</span></li>';
13108:             } else {
13109:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
13110:                 $changes{$itemid} = 1;
13111:             }
13112:         }
13113:     }
13114:     if (@allpos > 0) {
13115:         my $idx = 0;
13116:         foreach my $itemid (@allpos) {
13117:             if ($itemid ne '') {
13118:                 $confhash{$itemid}{'order'} = $idx;
13119:                 unless ($changes{$itemid}) {
13120:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13121:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13122:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
13123:                                 $changes{$itemid} = 1;
13124:                             }
13125:                         }
13126:                     }
13127:                 }
13128:                 $idx ++;
13129:             }
13130:         }
13131:     }
13132:     if (keys(%changes)) {
13133:         my %defaultshash = (
13134:                               ipaccess => \%confhash,
13135:                            );
13136:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13137:                                                  $dom);
13138:         if ($putresult eq 'ok') {
13139:             my $cachetime = 1800;
13140:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
13141:             if (ref($lastactref) eq 'HASH') {
13142:                 $lastactref->{'ipaccess'} = 1;
13143:             }
13144:             $resulttext = &mt('Changes made:').'<ul>';
13145:             my %bynum;
13146:             foreach my $itemid (sort(keys(%changes))) {
13147:                 if (ref($confhash{$itemid}) eq 'HASH') {
13148:                     my $position = $confhash{$itemid}{'order'};
13149:                     if ($position =~ /^\d+$/) {
13150:                         $bynum{$position} = $itemid;
13151:                     }
13152:                 }
13153:             }
13154:             if (keys(%deletions)) {
13155:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
13156:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13157:                 }
13158:             }
13159:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13160:                 my $itemid = $bynum{$pos};
13161:                 if (ref($confhash{$itemid}) eq 'HASH') {
13162:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
13163:                     my $position = $pos + 1;
13164:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13165:                     if ($confhash{$itemid}{'ip'} eq '') {
13166:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
13167:                     } else {
13168:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
13169:                     }
13170:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
13171:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
13172:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
13173:                                        '</li>';
13174:                     } else {
13175:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
13176:                     }
13177:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
13178:                         my @courses;
13179:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
13180:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
13181:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
13182:                         }
13183:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
13184:                                              join('</li><li>',@courses).'</li></ul>';
13185:                     } else {
13186:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
13187:                     }
13188:                     $resulttext .= '</ul></li>';
13189:                 }
13190:             }
13191:             $resulttext .= '</ul>';
13192:         } else {
13193:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13194:         }
13195:     } else {
13196:         $resulttext = &mt('No changes made');
13197:     }
13198:     if ($errors) {
13199:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13200:                        $errors.'</ul></p>';
13201:     }
13202:     return $resulttext;
13203: }
13204: 
13205: sub get_ipaccess_id {
13206:     my ($domain,$location) = @_;
13207:     # get lock on ipaccess db
13208:     my $lockhash = {
13209:                       lock => $env{'user.name'}.
13210:                               ':'.$env{'user.domain'},
13211:                    };
13212:     my $tries = 0;
13213:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13214:     my ($id,$error);
13215: 
13216:     while (($gotlock ne 'ok') && ($tries<10)) {
13217:         $tries ++;
13218:         sleep (0.1);
13219:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13220:     }
13221:     if ($gotlock eq 'ok') {
13222:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
13223:         if ($currids{'lock'}) {
13224:             delete($currids{'lock'});
13225:             if (keys(%currids)) {
13226:                 my @curr = sort { $a <=> $b } keys(%currids);
13227:                 if ($curr[-1] =~ /^\d+$/) {
13228:                     $id = 1 + $curr[-1];
13229:                 }
13230:             } else {
13231:                 $id = 1;
13232:             }
13233:             if ($id) {
13234:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
13235:                     $error = 'nostore';
13236:                 }
13237:             } else {
13238:                 $error = 'nonumber';
13239:             }
13240:         }
13241:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
13242:     } else {
13243:         $error = 'nolock';
13244:     }
13245:     return ($id,$error);
13246: }
13247: 
13248: sub modify_authordefaults {
13249:     my ($dom,$lastactref,%domconfig) = @_;
13250: #
13251: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
13252: #
13253:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
13254:     if (ref($domconfig{'quotas'}) eq 'HASH') {
13255:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
13256:             if ($key =~ /^webdav|authorquota$/) {
13257:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
13258:             } else {
13259:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
13260:             }
13261:         }
13262:     }
13263:     my %staticdefaults = (
13264:                            'copyright'    => 'default',
13265:                            'sourceavail'  => 'closed',
13266:                            'nocodemirror' => 'off',
13267:                            'domcoordacc'  => 'on',
13268:                            'editors'      => ['edit','xml'].
13269:                            'authorquota'  => 500,
13270:                            'webdav'       => 0,
13271:                          );
13272:     my %titles = &authordefaults_titles();
13273:     foreach my $item ('nocodemirror','domcoordacc') {
13274:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13275:             $confhash{$item} = $env{'form.'.$item};
13276:         }
13277:     }
13278:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
13279:         $confhash{'copyright'} = $1;
13280:     }
13281:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
13282:         $confhash{'sourceavail'} = $1;
13283:     }
13284:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
13285:     my @okeditors = ('edit','xml','daxe');
13286:     my @editors;
13287:     foreach my $item (@posseditors) {
13288:         if (grep(/^\Q$item\E$/,@okeditors)) {
13289:             push(@editors,$item);
13290:         }
13291:     }
13292:     $confhash{'editors'} = \@editors;
13293:    
13294:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13295:     my @insttypes;
13296:     if (ref($types) eq 'ARRAY') {
13297:         @insttypes = @{$types};
13298:     }
13299:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
13300:     my %webdav;
13301:     map { $webdav{$_} = 1; } @webdavon;
13302:     foreach my $type (@insttypes,'default') {
13303:         my $possquota = $env{'form.authorquota_'.$type};
13304:         if ($possquota =~ /^\d+$/) {
13305:             $save_quotas{'authorquota'}{$type} = $possquota;
13306:         }
13307:         if ($webdav{$type}) {
13308:             $save_quotas{'webdav'}{$type} = 1;
13309:         } else {
13310:             $save_quotas{'webdav'}{$type} = 0;
13311:         }
13312:     }
13313:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
13314:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
13315:     }
13316:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
13317:         foreach my $item ('nocodemirror','domcoordacc','copyright','sourceavail') {
13318:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
13319:                 $changes{$item} = 1;
13320:              }
13321:         }
13322:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
13323:             my @diffs = 
13324:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13325:                                                    $domconfig{'authordefaults'}{'editors'});
13326:             unless (@diffs == 0) {
13327:                 $changes{'editors'} = 1;
13328:             }
13329:         } else {
13330:             my @diffs =
13331:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13332:                                                    $staticdefaults{'editors'});
13333:             unless (@diffs == 0) {
13334:                 $changes{'editors'} = 1;
13335:             }
13336:         }
13337:     } else {
13338:         my @offon = ('off','on');
13339:         foreach my $item ('nocodemirror','domcoordacc') {
13340:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) { 
13341:                 $changes{$item} = 1; 
13342:             }
13343:         }
13344:         foreach my $item ('copyright','sourceavail') {
13345:             if ($confhash{$item} ne $staticdefaults{$item}) {
13346:                 $changes{$item} = 1;
13347:             }
13348:         }
13349:     }
13350:     foreach my $key ('authorquota','webdav') {
13351:         if (ref($curr_quotas{$key}) eq 'HASH') {
13352:             foreach my $type (@insttypes,'default') {
13353:                 if (exists($save_quotas{$key}{$type})) {
13354:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
13355:                         $changes{$key}{$type} = 1;
13356:                     }
13357:                 } elsif (exists($curr_quotas{$key}{$type})) {
13358:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
13359:                 } else {
13360:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13361:                 }
13362:             }
13363:         } else {
13364:             foreach my $type (@insttypes,'default') {
13365:                 if (exists($save_quotas{$key}{$type})) {
13366:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
13367:                         $changes{$key}{$type} = 1;
13368:                     }
13369:                 } else {
13370:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13371:                 }
13372:             }
13373:         }
13374:     }
13375:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
13376:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13377:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) { 
13378:                 $changes{'webdav_LC_adv'} = 1;
13379:             }
13380:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
13381:             $changes{'webdav_LC_adv'} = 1;
13382:         }
13383:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13384:         $changes{'webdav_LC_adv'} = 1;
13385:     }
13386:     my %confighash = (
13387:                         quotas  => \%save_quotas,
13388:                         authordefaults => \%confhash,
13389:                      );
13390:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
13391:                                              $dom);
13392:     my $resulttext;
13393:     if ($putresult eq 'ok') {
13394:         if (keys(%changes)) {
13395:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
13396:                 ($changes{'webdav_LC_adv'})) { 
13397:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13398:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
13399:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
13400:                 }
13401:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
13402:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
13403:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
13404:                 }
13405:                 my $cachetime = 24*60*60;
13406:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13407:                 if (ref($lastactref) eq 'HASH') {
13408:                     $lastactref->{'domdefaults'} = 1;
13409:                 }
13410:             }
13411:             $resulttext = &mt('Changes made:').'<ul>';
13412:             my $authoroverride;
13413:             foreach my $key ('nocodemirror','domcoordacc','copyright','sourceavail') {
13414:                 if (exists($changes{$key})) {
13415:                     my $shown;
13416:                     unless ($authoroverride) {
13417:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
13418:                         $authoroverride = 1;
13419:                     }
13420:                     if (($key eq 'nocodemirror') || ($key eq 'domcoordacc')) { 
13421:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13422:                     } elsif ($key eq 'copyright') {
13423:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
13424:                     } elsif ($key eq 'sourceavail') {
13425:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
13426:                     }
13427:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>'; 
13428:                 }
13429:             }
13430:             if ($authoroverride) {
13431:                 $resulttext .= '</ul></li>';
13432:             }
13433:             my $domcoordoverride;
13434:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv') {
13435:                 if (exists($changes{$key})) {
13436:                     my $shown;
13437:                     unless ($domcoordoverride) {
13438:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
13439:                         $domcoordoverride = 1;
13440:                     }
13441:                     if ($key eq 'editors') {
13442:                         if (@{$confhash{'editors'}}) {
13443:                             $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
13444:                         } else {
13445:                             $shown = &mt('None');
13446:                         }
13447:                     } elsif ($key eq 'authorquota') {
13448:                         foreach my $type (@insttypes) {
13449:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
13450:                         }
13451:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
13452:                     } elsif ($key eq 'webdav') {
13453:                         foreach my $type (@insttypes) {
13454:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
13455:                         }
13456:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
13457:                     } elsif ($key eq 'webdav_LC_adv') {
13458:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13459:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
13460:                         } else {
13461:                             $shown = $titles{'none'};
13462:                         }
13463:                     }
13464:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13465:                 }                               
13466:             }
13467:             if ($domcoordoverride) {
13468:                 $resulttext .= '</ul></li>';
13469:             }
13470:         } else {
13471:             $resulttext = &mt('No changes made to Authoring Space defaults');
13472:         }
13473:     }
13474:     return $resulttext;
13475: }
13476: 
13477: sub modify_rolecolors {
13478:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
13479:     my ($resulttext,%rolehash);
13480:     $rolehash{'rolecolors'} = {};
13481:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
13482:         if ($domconfig{'rolecolors'} eq '') {
13483:             $domconfig{'rolecolors'} = {};
13484:         }
13485:     }
13486:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
13487:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
13488:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
13489:                                              $dom);
13490:     if ($putresult eq 'ok') {
13491:         if (keys(%changes) > 0) {
13492:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13493:             if (ref($lastactref) eq 'HASH') {
13494:                 $lastactref->{'domainconfig'} = 1;
13495:             }
13496:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
13497:                                              $rolehash{'rolecolors'});
13498:         } else {
13499:             $resulttext = &mt('No changes made to default color schemes');
13500:         }
13501:     } else {
13502:         $resulttext = '<span class="LC_error">'.
13503: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13504:     }
13505:     if ($errors) {
13506:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13507:                        $errors.'</ul>';
13508:     }
13509:     return $resulttext;
13510: }
13511: 
13512: sub modify_colors {
13513:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
13514:     my (%changes,%choices);
13515:     my @bgs;
13516:     my @links = ('link','alink','vlink');
13517:     my @logintext;
13518:     my @images;
13519:     my $servadm = $r->dir_config('lonAdmEMail');
13520:     my $errors;
13521:     my %defaults;
13522:     foreach my $role (@{$roles}) {
13523:         if ($role eq 'login') {
13524:             %choices = &login_choices();
13525:             @logintext = ('textcol','bgcol');
13526:         } else {
13527:             %choices = &color_font_choices();
13528:         }
13529:         if ($role eq 'login') {
13530:             @images = ('img','logo','domlogo','login');
13531:             @bgs = ('pgbg','mainbg','sidebg');
13532:         } else {
13533:             @images = ('img');
13534:             @bgs = ('pgbg','tabbg','sidebg');
13535:         }
13536:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
13537:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
13538:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13539:         }
13540:         if ($role eq 'login') {
13541:             foreach my $item (@logintext) {
13542:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13543:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13544:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13545:                 }
13546:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13547:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13548:                 }
13549:             }
13550:         } else {
13551:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13552:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13553:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13554:             }
13555:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13556:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13557:             }
13558:         }
13559:         foreach my $item (@bgs) {
13560:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13561:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13562:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13563:             }
13564:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13565:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13566:             }
13567:         }
13568:         foreach my $item (@links) {
13569:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13570:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13571:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13572:             }
13573:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13574:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13575:             }
13576:         }
13577:         my ($configuserok,$author_ok,$switchserver) = 
13578:             &config_check($dom,$confname,$servadm);
13579:         my ($width,$height) = &thumb_dimensions();
13580:         if (ref($domconfig->{$role}) ne 'HASH') {
13581:             $domconfig->{$role} = {};
13582:         }
13583:         foreach my $img (@images) {
13584:             if ($role eq 'login') {
13585:                 if (($img eq 'img') || ($img eq 'logo')) {  
13586:                     if (defined($env{'form.login_showlogo_'.$img})) {
13587:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13588:                     } else { 
13589:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13590:                     }
13591:                 }
13592:                 if ($env{'form.login_alt_'.$img} ne '') {
13593:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13594:                 }
13595:             }
13596: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13597: 		 && !defined($domconfig->{$role}{$img})
13598: 		 && !$env{'form.'.$role.'_del_'.$img}
13599: 		 && $env{'form.'.$role.'_import_'.$img}) {
13600: 		# import the old configured image from the .tab setting
13601: 		# if they haven't provided a new one 
13602: 		$domconfig->{$role}{$img} = 
13603: 		    $env{'form.'.$role.'_import_'.$img};
13604: 	    }
13605:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13606:                 my $error;
13607:                 if ($configuserok eq 'ok') {
13608:                     if ($switchserver) {
13609:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13610:                     } else {
13611:                         if ($author_ok eq 'ok') {
13612:                             my $modified = [];
13613:                             my ($result,$logourl) = 
13614:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13615:                                                                         $dom,$confname,$img,$width,$height,
13616:                                                                         '',$modified);
13617:                             if ($result eq 'ok') {
13618:                                 $confhash->{$role}{$img} = $logourl;
13619:                                 $changes{$role}{'images'}{$img} = 1;
13620:                                 &update_modify_urls($r,$modified);
13621:                             } else {
13622:                                 $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);
13623:                             }
13624:                         } else {
13625:                             $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);
13626:                         }
13627:                     }
13628:                 } else {
13629:                     $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);
13630:                 }
13631:                 if ($error) {
13632:                     &Apache::lonnet::logthis($error);
13633:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13634:                 }
13635:             } elsif ($domconfig->{$role}{$img} ne '') {
13636:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13637:                     my $error;
13638:                     if ($configuserok eq 'ok') {
13639: # is confname an author?
13640:                         if ($switchserver eq '') {
13641:                             if ($author_ok eq 'ok') {
13642:                                 my $modified = [];
13643:                                 my ($result,$logourl) = 
13644:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13645:                                                                             $dom,$confname,$img,$width,$height,
13646:                                                                             '',$modified);
13647:                                 if ($result eq 'ok') {
13648:                                     $confhash->{$role}{$img} = $logourl;
13649: 				    $changes{$role}{'images'}{$img} = 1;
13650:                                     &update_modify_urls($r,$modified);
13651:                                 }
13652:                             }
13653:                         }
13654:                     }
13655:                 }
13656:             }
13657:         }
13658:         if (ref($domconfig) eq 'HASH') {
13659:             if (ref($domconfig->{$role}) eq 'HASH') {
13660:                 foreach my $img (@images) {
13661:                     if ($domconfig->{$role}{$img} ne '') {
13662:                         if ($env{'form.'.$role.'_del_'.$img}) {
13663:                             $confhash->{$role}{$img} = '';
13664:                             $changes{$role}{'images'}{$img} = 1;
13665:                         } else {
13666:                             if ($confhash->{$role}{$img} eq '') {
13667:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13668:                             }
13669:                         }
13670:                     } else {
13671:                         if ($env{'form.'.$role.'_del_'.$img}) {
13672:                             $confhash->{$role}{$img} = '';
13673:                             $changes{$role}{'images'}{$img} = 1;
13674:                         } 
13675:                     }
13676:                     if ($role eq 'login') {
13677:                         if (($img eq 'logo') || ($img eq 'img')) {
13678:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13679:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13680:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13681:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13682:                                 }
13683:                             } else {
13684:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13685:                                     $changes{$role}{'showlogo'}{$img} = 1;
13686:                                 }
13687:                             }
13688:                         }
13689:                         if ($img ne 'login') {
13690:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13691:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13692:                                     $domconfig->{$role}{'alttext'}{$img}) {
13693:                                     $changes{$role}{'alttext'}{$img} = 1;
13694:                                 }
13695:                             } else {
13696:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13697:                                     $changes{$role}{'alttext'}{$img} = 1;
13698:                                 }
13699:                             }
13700:                         }
13701:                     }
13702:                 }
13703:                 if ($domconfig->{$role}{'font'} ne '') {
13704:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13705:                         $changes{$role}{'font'} = 1;
13706:                     }
13707:                 } else {
13708:                     if ($confhash->{$role}{'font'}) {
13709:                         $changes{$role}{'font'} = 1;
13710:                     }
13711:                 }
13712:                 if ($role ne 'login') {
13713:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13714:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13715:                             $changes{$role}{'fontmenu'} = 1;
13716:                         }
13717:                     } else {
13718:                         if ($confhash->{$role}{'fontmenu'}) {
13719:                             $changes{$role}{'fontmenu'} = 1;
13720:                         }
13721:                     }
13722:                 }
13723:                 foreach my $item (@bgs) {
13724:                     if ($domconfig->{$role}{$item} ne '') {
13725:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13726:                             $changes{$role}{'bgs'}{$item} = 1;
13727:                         } 
13728:                     } else {
13729:                         if ($confhash->{$role}{$item}) {
13730:                             $changes{$role}{'bgs'}{$item} = 1;
13731:                         }
13732:                     }
13733:                 }
13734:                 foreach my $item (@links) {
13735:                     if ($domconfig->{$role}{$item} ne '') {
13736:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13737:                             $changes{$role}{'links'}{$item} = 1;
13738:                         }
13739:                     } else {
13740:                         if ($confhash->{$role}{$item}) {
13741:                             $changes{$role}{'links'}{$item} = 1;
13742:                         }
13743:                     }
13744:                 }
13745:                 foreach my $item (@logintext) {
13746:                     if ($domconfig->{$role}{$item} ne '') {
13747:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13748:                             $changes{$role}{'logintext'}{$item} = 1;
13749:                         }
13750:                     } else {
13751:                         if ($confhash->{$role}{$item}) {
13752:                             $changes{$role}{'logintext'}{$item} = 1;
13753:                         }
13754:                     }
13755:                 }
13756:             } else {
13757:                 &default_change_checker($role,\@images,\@links,\@bgs,
13758:                                         \@logintext,$confhash,\%changes); 
13759:             }
13760:         } else {
13761:             &default_change_checker($role,\@images,\@links,\@bgs,
13762:                                     \@logintext,$confhash,\%changes); 
13763:         }
13764:     }
13765:     return ($errors,%changes);
13766: }
13767: 
13768: sub config_check {
13769:     my ($dom,$confname,$servadm) = @_;
13770:     my ($configuserok,$author_ok,$switchserver,%currroles);
13771:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13772:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13773:                                                    $confname,$servadm);
13774:     if ($configuserok eq 'ok') {
13775:         $switchserver = &check_switchserver($dom,$confname);
13776:         if ($switchserver eq '') {
13777:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13778:         }
13779:     }
13780:     return ($configuserok,$author_ok,$switchserver);
13781: }
13782: 
13783: sub default_change_checker {
13784:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13785:     foreach my $item (@{$links}) {
13786:         if ($confhash->{$role}{$item}) {
13787:             $changes->{$role}{'links'}{$item} = 1;
13788:         }
13789:     }
13790:     foreach my $item (@{$bgs}) {
13791:         if ($confhash->{$role}{$item}) {
13792:             $changes->{$role}{'bgs'}{$item} = 1;
13793:         }
13794:     }
13795:     foreach my $item (@{$logintext}) {
13796:         if ($confhash->{$role}{$item}) {
13797:             $changes->{$role}{'logintext'}{$item} = 1;
13798:         }
13799:     }
13800:     foreach my $img (@{$images}) {
13801:         if ($env{'form.'.$role.'_del_'.$img}) {
13802:             $confhash->{$role}{$img} = '';
13803:             $changes->{$role}{'images'}{$img} = 1;
13804:         }
13805:         if ($role eq 'login') {
13806:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13807:                 $changes->{$role}{'showlogo'}{$img} = 1;
13808:             }
13809:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13810:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13811:                     $changes->{$role}{'alttext'}{$img} = 1;
13812:                 }
13813:             }
13814:         }
13815:     }
13816:     if ($confhash->{$role}{'font'}) {
13817:         $changes->{$role}{'font'} = 1;
13818:     }
13819: }
13820: 
13821: sub display_colorchgs {
13822:     my ($dom,$changes,$roles,$confhash) = @_;
13823:     my (%choices,$resulttext);
13824:     if (!grep(/^login$/,@{$roles})) {
13825:         $resulttext = &mt('Changes made:').'<br />';
13826:     }
13827:     foreach my $role (@{$roles}) {
13828:         if ($role eq 'login') {
13829:             %choices = &login_choices();
13830:         } else {
13831:             %choices = &color_font_choices();
13832:         }
13833:         if (ref($changes->{$role}) eq 'HASH') {
13834:             if ($role ne 'login') {
13835:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13836:             }
13837:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
13838:                 if ($role ne 'login') {
13839:                     $resulttext .= '<ul>';
13840:                 }
13841:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
13842:                     if ($role ne 'login') {
13843:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
13844:                     }
13845:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
13846:                         if (($role eq 'login') && ($key eq 'showlogo')) {
13847:                             if ($confhash->{$role}{$key}{$item}) {
13848:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
13849:                             } else {
13850:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
13851:                             }
13852:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
13853:                             if ($confhash->{$role}{$key}{$item} ne '') {
13854:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
13855:                                                $confhash->{$role}{$key}{$item}).'</li>';
13856:                             } else {
13857:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
13858:                             }
13859:                         } elsif ($confhash->{$role}{$item} eq '') {
13860:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
13861:                         } else {
13862:                             my $newitem = $confhash->{$role}{$item};
13863:                             if ($key eq 'images') {
13864:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
13865:                             }
13866:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
13867:                         }
13868:                     }
13869:                     if ($role ne 'login') {
13870:                         $resulttext .= '</ul></li>';
13871:                     }
13872:                 } else {
13873:                     if ($confhash->{$role}{$key} eq '') {
13874:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
13875:                     } else {
13876:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
13877:                     }
13878:                 }
13879:                 if ($role ne 'login') {
13880:                     $resulttext .= '</ul>';
13881:                 }
13882:             }
13883:         }
13884:     }
13885:     return $resulttext;
13886: }
13887: 
13888: sub thumb_dimensions {
13889:     return ('200','50');
13890: }
13891: 
13892: sub check_dimensions {
13893:     my ($inputfile) = @_;
13894:     my ($fullwidth,$fullheight);
13895:     if ($inputfile =~ m|^[/\w.\-]+$|) {
13896:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
13897:             my $imageinfo = <PIPE>;
13898:             if (!close(PIPE)) {
13899:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
13900:             }
13901:             chomp($imageinfo);
13902:             my ($fullsize) = 
13903:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
13904:             if ($fullsize) {
13905:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
13906:             }
13907:         }
13908:     }
13909:     return ($fullwidth,$fullheight);
13910: }
13911: 
13912: sub check_configuser {
13913:     my ($uhome,$dom,$confname,$servadm) = @_;
13914:     my ($configuserok,%currroles);
13915:     if ($uhome eq 'no_host') {
13916:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
13917:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
13918:         $configuserok = 
13919:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
13920:                              $configpass,'','','','','',undef,$servadm);
13921:     } else {
13922:         $configuserok = 'ok';
13923:         %currroles = 
13924:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
13925:     }
13926:     return ($configuserok,%currroles);
13927: }
13928: 
13929: sub check_authorstatus {
13930:     my ($dom,$confname,%currroles) = @_;
13931:     my $author_ok;
13932:     if (!$currroles{':'.$dom.':au'}) {
13933:         my $start = time;
13934:         my $end = 0;
13935:         $author_ok = 
13936:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
13937:                                         'au',$end,$start,'','','domconfig');
13938:     } else {
13939:         $author_ok = 'ok';
13940:     }
13941:     return $author_ok;
13942: }
13943: 
13944: sub update_modify_urls {
13945:     my ($r,$modified) = @_;
13946:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
13947:         push(@{$modified_urls},$modified);
13948:         unless ($registered_cleanup) {
13949:             my $handlers = $r->get_handlers('PerlCleanupHandler');
13950:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13951:             $registered_cleanup=1;
13952:         }
13953:     }
13954: }
13955: 
13956: sub notifysubscribed {
13957:     foreach my $targetsource (@{$modified_urls}){
13958:         next unless (ref($targetsource) eq 'ARRAY');
13959:         my ($target,$source)=@{$targetsource};
13960:         if ($source ne '') {
13961:             if (open(my $logfh,">>",$source.'.log')) {
13962:                 print $logfh "\nCleanup phase: Notifications\n";
13963:                 my @subscribed=&subscribed_hosts($target);
13964:                 foreach my $subhost (@subscribed) {
13965:                     print $logfh "\nNotifying host ".$subhost.':';
13966:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
13967:                     print $logfh $reply;
13968:                 }
13969:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
13970:                 foreach my $subhost (@subscribedmeta) {
13971:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
13972:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
13973:                                                         $subhost);
13974:                     print $logfh $reply;
13975:                 }
13976:                 print $logfh "\n============ Done ============\n";
13977:                 close($logfh);
13978:             }
13979:         }
13980:     }
13981:     return OK;
13982: }
13983: 
13984: sub subscribed_hosts {
13985:     my ($target) = @_;
13986:     my @subscribed;
13987:     if (open(my $fh,"<","$target.subscription")) {
13988:         while (my $subline=<$fh>) {
13989:             if ($subline =~ /^($match_lonid):/) {
13990:                 my $host = $1;
13991:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
13992:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
13993:                         push(@subscribed,$host);
13994:                     }
13995:                 }
13996:             }
13997:         }
13998:     }
13999:     return @subscribed;
14000: }
14001: 
14002: sub check_switchserver {
14003:     my ($dom,$confname) = @_;
14004:     my ($allowed,$switchserver,$home);
14005:     if ($confname eq '') {
14006:         $home = &Apache::lonnet::domain($dom,'primary');
14007:     } else {
14008:         $home = &Apache::lonnet::homeserver($confname,$dom);
14009:         if ($home eq 'no_host') {
14010:             $home = &Apache::lonnet::domain($dom,'primary');
14011:         }
14012:     }
14013:     my @ids=&Apache::lonnet::current_machine_ids();
14014:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
14015:     if (!$allowed) {
14016: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
14017:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
14018:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
14019:     }
14020:     return $switchserver;
14021: }
14022: 
14023: sub modify_quotas {
14024:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14025:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
14026:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
14027:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
14028:         $validationfieldsref);
14029:     if ($action eq 'quotas') {
14030:         $context = 'tools';
14031:     } else {
14032:         $context = $action;
14033:     }
14034:     if ($context eq 'requestcourses') {
14035:         @usertools = ('official','unofficial','community','textbook','placement','lti');
14036:         @options =('norequest','approval','validate','autolimit');
14037:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
14038:         %titles = &courserequest_titles();
14039:         $toolregexp = join('|',@usertools);
14040:         %conditions = &courserequest_conditions();
14041:         $confname = $dom.'-domainconfig';
14042:         my $servadm = $r->dir_config('lonAdmEMail');
14043:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14044:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
14045:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
14046:     } elsif ($context eq 'requestauthor') {
14047:         @usertools = ('author');
14048:         %titles = &authorrequest_titles();
14049:     } else {
14050:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
14051:         %titles = &tool_titles();
14052:     }
14053:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14054:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14055:     foreach my $key (keys(%env)) {
14056:         if ($context eq 'requestcourses') {
14057:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
14058:                 my $item = $1;
14059:                 my $type = $2;
14060:                 if ($type =~ /^limit_(.+)/) {
14061:                     $limithash{$item}{$1} = $env{$key};
14062:                 } else {
14063:                     $confhash{$item}{$type} = $env{$key};
14064:                 }
14065:             }
14066:         } elsif ($context eq 'requestauthor') {
14067:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
14068:                 $confhash{$1} = $env{$key};
14069:             }
14070:         } else {
14071:             if ($key =~ /^form\.quota_(.+)$/) {
14072:                 $confhash{'defaultquota'}{$1} = $env{$key};
14073:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
14074:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
14075:             }
14076:         }
14077:     }
14078:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14079:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
14080:         @approvalnotify = sort(@approvalnotify);
14081:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
14082:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
14083:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
14084:         foreach my $type (@hasuniquecode) {
14085:             if (grep(/^\Q$type\E$/,@crstypes)) {
14086:                 $confhash{'uniquecode'}{$type} = 1;
14087:             }
14088:         }
14089:         my (%newbook,%allpos);
14090:         if ($context eq 'requestcourses') {
14091:             foreach my $type ('textbooks','templates') {
14092:                 @{$allpos{$type}} = (); 
14093:                 my $invalid;
14094:                 if ($type eq 'textbooks') {
14095:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
14096:                 } else {
14097:                     $invalid = &mt('Invalid LON-CAPA course for template');
14098:                 }
14099:                 if ($env{'form.'.$type.'_addbook'}) {
14100:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
14101:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
14102:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
14103:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
14104:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14105:                         } else {
14106:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
14107:                             my $position = $env{'form.'.$type.'_addbook_pos'};
14108:                             $position =~ s/\D+//g;
14109:                             if ($position ne '') {
14110:                                 $allpos{$type}[$position] = $newbook{$type};
14111:                             }
14112:                         }
14113:                     } else {
14114:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14115:                     }
14116:                 }
14117:             } 
14118:         }
14119:         if (ref($domconfig{$action}) eq 'HASH') {
14120:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
14121:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
14122:                     $changes{'notify'}{'approval'} = 1;
14123:                 }
14124:             } else {
14125:                 if ($confhash{'notify'}{'approval'}) {
14126:                     $changes{'notify'}{'approval'} = 1;
14127:                 }
14128:             }
14129:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
14130:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
14131:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
14132:                         unless ($confhash{'uniquecode'}{$crstype}) {
14133:                             $changes{'uniquecode'} = 1;
14134:                         }
14135:                     }
14136:                     unless ($changes{'uniquecode'}) {
14137:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
14138:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
14139:                                 $changes{'uniquecode'} = 1;
14140:                             }
14141:                         }
14142:                     }
14143:                } else {
14144:                    $changes{'uniquecode'} = 1;
14145:                }
14146:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
14147:                 $changes{'uniquecode'} = 1;
14148:             }
14149:             if ($context eq 'requestcourses') {
14150:                 foreach my $type ('textbooks','templates') {
14151:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
14152:                         my %deletions;
14153:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
14154:                         if (@todelete) {
14155:                             map { $deletions{$_} = 1; } @todelete;
14156:                         }
14157:                         my %imgdeletions;
14158:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
14159:                         if (@todeleteimages) {
14160:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
14161:                         }
14162:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
14163:                         for (my $i=0; $i<=$maxnum; $i++) {
14164:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
14165:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
14166:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
14167:                                 if ($deletions{$key}) {
14168:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
14169:                                         #FIXME need to obsolete item in RES space
14170:                                     }
14171:                                     next;
14172:                                 } else {
14173:                                     my $newpos = $env{'form.'.$itemid};
14174:                                     $newpos =~ s/\D+//g;
14175:                                     foreach my $item ('subject','title','publisher','author') {
14176:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14177:                                                  ($type eq 'templates'));
14178:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
14179:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
14180:                                             $changes{$type}{$key} = 1;
14181:                                         }
14182:                                     }
14183:                                     $allpos{$type}[$newpos] = $key;
14184:                                 }
14185:                                 if ($imgdeletions{$key}) {
14186:                                     $changes{$type}{$key} = 1;
14187:                                     #FIXME need to obsolete item in RES space
14188:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
14189:                                     my ($cdom,$cnum) = split(/_/,$key);
14190:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14191:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14192:                                     } else {
14193:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
14194:                                                                                       $cdom,$cnum,$type,$configuserok,
14195:                                                                                       $switchserver,$author_ok);
14196:                                         if ($imgurl) {
14197:                                             $confhash{$type}{$key}{'image'} = $imgurl;
14198:                                             $changes{$type}{$key} = 1; 
14199:                                         }
14200:                                         if ($error) {
14201:                                             &Apache::lonnet::logthis($error);
14202:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14203:                                         }
14204:                                     }
14205:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
14206:                                     $confhash{$type}{$key}{'image'} = 
14207:                                         $domconfig{$action}{$type}{$key}{'image'};
14208:                                 }
14209:                             }
14210:                         }
14211:                     }
14212:                 }
14213:             }
14214:         } else {
14215:             if ($confhash{'notify'}{'approval'}) {
14216:                 $changes{'notify'}{'approval'} = 1;
14217:             }
14218:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
14219:                 $changes{'uniquecode'} = 1;
14220:             }
14221:         }
14222:         if ($context eq 'requestcourses') {
14223:             foreach my $type ('textbooks','templates') {
14224:                 if ($newbook{$type}) {
14225:                     $changes{$type}{$newbook{$type}} = 1;
14226:                     foreach my $item ('subject','title','publisher','author') {
14227:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14228:                                  ($type eq 'template'));
14229:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
14230:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
14231:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
14232:                         }
14233:                     }
14234:                     if ($type eq 'textbooks') {
14235:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
14236:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
14237:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14238:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14239:                             } else {
14240:                                 my ($imageurl,$error) =
14241:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
14242:                                                             $configuserok,$switchserver,$author_ok);
14243:                                 if ($imageurl) {
14244:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
14245:                                 }
14246:                                 if ($error) {
14247:                                     &Apache::lonnet::logthis($error);
14248:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14249:                                 }
14250:                             }
14251:                         }
14252:                     }
14253:                 }
14254:                 if (@{$allpos{$type}} > 0) {
14255:                     my $idx = 0;
14256:                     foreach my $item (@{$allpos{$type}}) {
14257:                         if ($item ne '') {
14258:                             $confhash{$type}{$item}{'order'} = $idx;
14259:                             if (ref($domconfig{$action}) eq 'HASH') {
14260:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
14261:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
14262:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
14263:                                             $changes{$type}{$item} = 1;
14264:                                         }
14265:                                     }
14266:                                 }
14267:                             }
14268:                             $idx ++;
14269:                         }
14270:                     }
14271:                 }
14272:             }
14273:             if (ref($validationitemsref) eq 'ARRAY') {
14274:                 foreach my $item (@{$validationitemsref}) {
14275:                     if ($item eq 'fields') {
14276:                         my @changed;
14277:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
14278:                         if (@{$confhash{'validation'}{$item}} > 0) {
14279:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
14280:                         }
14281:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14282:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14283:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
14284:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
14285:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
14286:                                 } else {
14287:                                     @changed = @{$confhash{'validation'}{$item}};
14288:                                 }
14289:                             } else {
14290:                                 @changed = @{$confhash{'validation'}{$item}};
14291:                             }
14292:                         } else {
14293:                             @changed = @{$confhash{'validation'}{$item}};
14294:                         }
14295:                         if (@changed) {
14296:                             if ($confhash{'validation'}{$item}) {
14297:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
14298:                             } else {
14299:                                 $changes{'validation'}{$item} = &mt('None');
14300:                             }
14301:                         }
14302:                     } else {
14303:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
14304:                         if ($item eq 'markup') {
14305:                             if ($env{'form.requestcourses_validation_'.$item}) {
14306:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14307:                             }
14308:                         }
14309:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14310:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14311:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
14312:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14313:                                 }
14314:                             } else {
14315:                                 if ($confhash{'validation'}{$item} ne '') {
14316:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14317:                                 }
14318:                             }
14319:                         } else {
14320:                             if ($confhash{'validation'}{$item} ne '') {
14321:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14322:                             }
14323:                         }
14324:                     }
14325:                 }
14326:             }
14327:             if ($env{'form.validationdc'}) {
14328:                 my $newval = $env{'form.validationdc'};
14329:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14330:                 if (exists($domcoords{$newval})) {
14331:                     $confhash{'validation'}{'dc'} = $newval;
14332:                 }
14333:             }
14334:             if (ref($confhash{'validation'}) eq 'HASH') {
14335:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14336:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14337:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14338:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
14339:                                 if ($confhash{'validation'}{'dc'} eq '') {
14340:                                     $changes{'validation'}{'dc'} = &mt('None');
14341:                                 } else {
14342:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14343:                                 }
14344:                             }
14345:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
14346:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14347:                         }
14348:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
14349:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14350:                     }
14351:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
14352:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14353:                 }
14354:             } else {
14355:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14356:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14357:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14358:                             $changes{'validation'}{'dc'} = &mt('None');
14359:                         }
14360:                     }
14361:                 }
14362:             }
14363:         }
14364:     } else {
14365:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
14366:     }
14367:     foreach my $item (@usertools) {
14368:         foreach my $type (@{$types},'default','_LC_adv') {
14369:             my $unset; 
14370:             if ($context eq 'requestcourses') {
14371:                 $unset = '0';
14372:                 if ($type eq '_LC_adv') {
14373:                     $unset = '';
14374:                 }
14375:                 if ($confhash{$item}{$type} eq 'autolimit') {
14376:                     $confhash{$item}{$type} .= '=';
14377:                     unless ($limithash{$item}{$type} =~ /\D/) {
14378:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
14379:                     }
14380:                 }
14381:             } elsif ($context eq 'requestauthor') {
14382:                 $unset = '0';
14383:                 if ($type eq '_LC_adv') {
14384:                     $unset = '';
14385:                 }
14386:             } else {
14387:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
14388:                     $confhash{$item}{$type} = 1;
14389:                 } else {
14390:                     $confhash{$item}{$type} = 0;
14391:                 }
14392:             }
14393:             if (ref($domconfig{$action}) eq 'HASH') {
14394:                 if ($action eq 'requestauthor') {
14395:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
14396:                         $changes{$type} = 1;
14397:                     }
14398:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
14399:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
14400:                         $changes{$item}{$type} = 1;
14401:                     }
14402:                 } else {
14403:                     if ($context eq 'requestcourses') {
14404:                         if ($confhash{$item}{$type} ne $unset) {
14405:                             $changes{$item}{$type} = 1;
14406:                         }
14407:                     } else {
14408:                         if (!$confhash{$item}{$type}) {
14409:                             $changes{$item}{$type} = 1;
14410:                         }
14411:                     }
14412:                 }
14413:             } else {
14414:                 if ($context eq 'requestcourses') {
14415:                     if ($confhash{$item}{$type} ne $unset) {
14416:                         $changes{$item}{$type} = 1;
14417:                     }
14418:                 } elsif ($context eq 'requestauthor') {
14419:                     if ($confhash{$type} ne $unset) {
14420:                         $changes{$type} = 1;
14421:                     }
14422:                 } else {
14423:                     if (!$confhash{$item}{$type}) {
14424:                         $changes{$item}{$type} = 1;
14425:                     }
14426:                 }
14427:             }
14428:         }
14429:     }
14430:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14431:         if (ref($domconfig{'quotas'}) eq 'HASH') {
14432:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14433:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
14434:                     if (exists($confhash{'defaultquota'}{$key})) {
14435:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
14436:                             $changes{'defaultquota'}{$key} = 1;
14437:                         }
14438:                     } else {
14439:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
14440:                     }
14441:                 }
14442:             } else {
14443:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
14444:                     if (exists($confhash{'defaultquota'}{$key})) {
14445:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
14446:                             $changes{'defaultquota'}{$key} = 1;
14447:                         }
14448:                     } else {
14449:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
14450:                     }
14451:                 }
14452:             }
14453:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
14454:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
14455:             }
14456:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
14457:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
14458:             }
14459:         }
14460:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
14461:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
14462:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
14463:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14464:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
14465:                             $changes{'defaultquota'}{$key} = 1;
14466:                         }
14467:                     } else {
14468:                         if (!exists($domconfig{'quotas'}{$key})) {
14469:                             $changes{'defaultquota'}{$key} = 1;
14470:                         }
14471:                     }
14472:                 } else {
14473:                     $changes{'defaultquota'}{$key} = 1;
14474:                 }
14475:             }
14476:         }
14477:     }
14478: 
14479:     if ($context eq 'requestauthor') {
14480:         $domdefaults{'requestauthor'} = \%confhash;
14481:     } else {
14482:         foreach my $key (keys(%confhash)) {
14483:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
14484:                 $domdefaults{$key} = $confhash{$key};
14485:             }
14486:         }
14487:     }
14488: 
14489:     my %quotahash = (
14490:                       $action => { %confhash }
14491:                     );
14492:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
14493:                                              $dom);
14494:     if ($putresult eq 'ok') {
14495:         if (keys(%changes) > 0) {
14496:             my $cachetime = 24*60*60;
14497:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14498:             if (ref($lastactref) eq 'HASH') {
14499:                 $lastactref->{'domdefaults'} = 1;
14500:             }
14501:             $resulttext = &mt('Changes made:').'<ul>';
14502:             unless (($context eq 'requestcourses') ||
14503:                     ($context eq 'requestauthor')) {
14504:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
14505:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
14506:                     foreach my $type (@{$types},'default') {
14507:                         if (defined($changes{'defaultquota'}{$type})) {
14508:                             my $typetitle = $usertypes->{$type};
14509:                             if ($type eq 'default') {
14510:                                 $typetitle = $othertitle;
14511:                             }
14512:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
14513:                         }
14514:                     }
14515:                     $resulttext .= '</ul></li>';
14516:                 }
14517:             }
14518:             my %newenv;
14519:             foreach my $item (@usertools) {
14520:                 my (%haschgs,%inconf);
14521:                 if ($context eq 'requestauthor') {
14522:                     %haschgs = %changes;
14523:                     %inconf = %confhash;
14524:                 } else {
14525:                     if (ref($changes{$item}) eq 'HASH') {
14526:                         %haschgs = %{$changes{$item}};
14527:                     }
14528:                     if (ref($confhash{$item}) eq 'HASH') {
14529:                         %inconf = %{$confhash{$item}};
14530:                     }
14531:                 }
14532:                 if (keys(%haschgs) > 0) {
14533:                     my $newacc = 
14534:                         &Apache::lonnet::usertools_access($env{'user.name'},
14535:                                                           $env{'user.domain'},
14536:                                                           $item,'reload',$context);
14537:                     if (($context eq 'requestcourses') ||
14538:                         ($context eq 'requestauthor')) {
14539:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14540:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14541:                         }
14542:                     } else {
14543:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14544:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14545:                         }
14546:                     }
14547:                     unless ($context eq 'requestauthor') {
14548:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14549:                     }
14550:                     foreach my $type (@{$types},'default','_LC_adv') {
14551:                         if ($haschgs{$type}) {
14552:                             my $typetitle = $usertypes->{$type};
14553:                             if ($type eq 'default') {
14554:                                 $typetitle = $othertitle;
14555:                             } elsif ($type eq '_LC_adv') {
14556:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14557:                             }
14558:                             if ($inconf{$type}) {
14559:                                 if ($context eq 'requestcourses') {
14560:                                     my $cond;
14561:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14562:                                         if ($1 eq '') {
14563:                                             $cond = &mt('(Automatic processing of any request).');
14564:                                         } else {
14565:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14566:                                         }
14567:                                     } else { 
14568:                                         $cond = $conditions{$inconf{$type}};
14569:                                     }
14570:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14571:                                 } elsif ($context eq 'requestauthor') {
14572:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14573:                                                              $titles{$inconf{$type}},$typetitle);
14574: 
14575:                                 } else {
14576:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14577:                                 }
14578:                             } else {
14579:                                 if ($type eq '_LC_adv') {
14580:                                     if ($inconf{$type} eq '0') {
14581:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14582:                                     } else { 
14583:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14584:                                     }
14585:                                 } else {
14586:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14587:                                 }
14588:                             }
14589:                         }
14590:                     }
14591:                     unless ($context eq 'requestauthor') {
14592:                         $resulttext .= '</ul></li>';
14593:                     }
14594:                 }
14595:             }
14596:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14597:                 if (ref($changes{'notify'}) eq 'HASH') {
14598:                     if ($changes{'notify'}{'approval'}) {
14599:                         if (ref($confhash{'notify'}) eq 'HASH') {
14600:                             if ($confhash{'notify'}{'approval'}) {
14601:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14602:                             } else {
14603:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14604:                             }
14605:                         }
14606:                     }
14607:                 }
14608:             }
14609:             if ($action eq 'requestcourses') {
14610:                 my @offon = ('off','on');
14611:                 if ($changes{'uniquecode'}) {
14612:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14613:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14614:                         $resulttext .= '<li>'.
14615:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14616:                                        '</li>';
14617:                     } else {
14618:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14619:                                        '</li>';
14620:                     }
14621:                 }
14622:                 foreach my $type ('textbooks','templates') {
14623:                     if (ref($changes{$type}) eq 'HASH') {
14624:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14625:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14626:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14627:                             my $coursetitle = $coursehash{'description'};
14628:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14629:                             $resulttext .= '<li>';
14630:                             foreach my $item ('subject','title','publisher','author') {
14631:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14632:                                          ($type eq 'templates'));
14633:                                 my $name = $item.':';
14634:                                 $name =~ s/^(\w)/\U$1/;
14635:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14636:                             }
14637:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14638:                             if ($type eq 'textbooks') {
14639:                                 if ($confhash{$type}{$key}{'image'}) {
14640:                                     $resulttext .= ' '.&mt('Image: [_1]',
14641:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14642:                                                    ' alt="Textbook cover" />').'<br />';
14643:                                 }
14644:                             }
14645:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14646:                         }
14647:                         $resulttext .= '</ul></li>';
14648:                     }
14649:                 }
14650:                 if (ref($changes{'validation'}) eq 'HASH') {
14651:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14652:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14653:                         foreach my $item (@{$validationitemsref}) {
14654:                             if (exists($changes{'validation'}{$item})) {
14655:                                 if ($item eq 'markup') {
14656:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14657:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14658:                                 } else {
14659:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14660:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14661:                                 }
14662:                             }
14663:                         }
14664:                         if (exists($changes{'validation'}{'dc'})) {
14665:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14666:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14667:                         }
14668:                     }
14669:                 }
14670:             }
14671:             $resulttext .= '</ul>';
14672:             if (keys(%newenv)) {
14673:                 &Apache::lonnet::appenv(\%newenv);
14674:             }
14675:         } else {
14676:             if ($context eq 'requestcourses') {
14677:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14678:             } elsif ($context eq 'requestauthor') {
14679:                 $resulttext = &mt('No changes made to rights to request author space.');
14680:             } else {
14681:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14682:             }
14683:         }
14684:     } else {
14685:         $resulttext = '<span class="LC_error">'.
14686: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14687:     }
14688:     if ($errors) {
14689:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14690:                        '<ul>'.$errors.'</ul></p>';
14691:     }
14692:     return $resulttext;
14693: }
14694: 
14695: sub process_textbook_image {
14696:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14697:     my $filename = $env{'form.'.$caller.'.filename'};
14698:     my ($error,$url);
14699:     my ($width,$height) = (50,50);
14700:     if ($configuserok eq 'ok') {
14701:         if ($switchserver) {
14702:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14703:                          $switchserver);
14704:         } elsif ($author_ok eq 'ok') {
14705:             my $modified = [];
14706:             my ($result,$imageurl) =
14707:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14708:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14709:                                                         '',$modified);
14710:             if ($result eq 'ok') {
14711:                 $url = $imageurl;
14712:                 &update_modify_urls($r,$modified);
14713:             } else {
14714:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14715:             }
14716:         } else {
14717:             $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);
14718:         }
14719:     } else {
14720:         $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);
14721:     }
14722:     return ($url,$error);
14723: }
14724: 
14725: sub modify_ltitools {
14726:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14727:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14728:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14729: 
14730:     my $confname = $dom.'-domainconfig';
14731:     my $servadm = $r->dir_config('lonAdmEMail');
14732:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14733: 
14734:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14735:     my $toolserror =
14736:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14737:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14738: 
14739:     my $home = &Apache::lonnet::domain($dom,'primary');
14740:     unless (($home eq 'no_host') || ($home eq '')) {
14741:         my @ids=&Apache::lonnet::current_machine_ids();
14742:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14743:     }
14744: 
14745:     if (keys(%ltitoolschg)) {
14746:         foreach my $id (keys(%ltitoolschg)) {
14747:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14748:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14749:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14750:                         if ($is_home) {
14751:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14752:                         }
14753:                     }
14754:                 }
14755:             }
14756:         }
14757:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14758:         if (keys(%ltitoolschg)) {
14759:             %newltitools = %ltitoolschg;
14760:         }
14761:     }
14762:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14763:         foreach my $id (%{$domconfig{'ltitools'}}) {
14764:             next if ($id !~ /^\d+$/);
14765:             unless (exists($ltitoolschg{$id})) {
14766:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14767:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14768:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14769:                             if ($is_home) {
14770:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14771:                             }
14772:                         } else {
14773:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14774:                         }
14775:                     }
14776:                 } else {
14777:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14778:                 }
14779:             }
14780:         }
14781:     }
14782:     if ($toolserror) {
14783:         $errors = '<li>'.$toolserror.'</li>';
14784:     }
14785:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14786:         $resulttext = &mt('No changes made.');
14787:         if ($errors) {
14788:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14789:                                  $errors.'</ul>';
14790:         }
14791:         return $resulttext;
14792:     }
14793:     my %ltitoolshash = (
14794:                           $action => { %newltitools }
14795:                        );
14796:     if (keys(%secchanges)) {
14797:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14798:     }
14799:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14800:     if ($putresult eq 'ok') {
14801:         my %keystore;
14802:         if ($is_home) {
14803:             my %toolsenchash = (
14804:                                    $action => { %newtoolsenc }
14805:                                );
14806:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14807:             my $cachetime = 24*60*60;
14808:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
14809:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14810:         }
14811:         $resulttext = &mt('Changes made:').'<ul>';
14812:         if (keys(%secchanges) > 0) {
14813:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14814:         }
14815:         if (keys(%ltitoolschg) > 0) {
14816:             $resulttext .= $ltitoolsoutput;
14817:         }
14818:         my $cachetime = 24*60*60;
14819:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
14820:         if (ref($lastactref) eq 'HASH') {
14821:             $lastactref->{'ltitools'} = 1;
14822:         }
14823:     } else {
14824:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14825:     }
14826:     if ($errors) {
14827:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14828:                        $errors.'</ul></p>';
14829:     }
14830:     return $resulttext;
14831: }
14832: 
14833: sub fetch_secrets {
14834:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
14835:     my %keyset;
14836:     %{$currsec} = ();
14837:     $newsec->{'private'}{'keys'} = [];
14838:     $newsec->{'encrypt'} = {};
14839:     $newsec->{'rules'} = {};
14840:     if ($context eq 'ltisec') {
14841:         $newsec->{'linkprot'} = {};
14842:     }
14843:     if (ref($domconfig->{$context}) eq 'HASH') {
14844:         %{$currsec} = %{$domconfig->{$context}};
14845:         if ($context eq 'ltisec') {
14846:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
14847:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
14848:                     unless ($id =~ /^\d+$/) {
14849:                         delete($currsec->{'linkprot'}{$id});
14850:                     }
14851:                 }
14852:             }
14853:         }
14854:         if (ref($currsec->{'private'}) eq 'HASH') {
14855:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
14856:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
14857:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
14858:             }
14859:         }
14860:     }
14861:     my @items= ('crs','dom');
14862:     if ($context eq 'ltisec') {
14863:         push(@items,'consumers');
14864:     }
14865:     foreach my $item (@items) {
14866:         my $formelement;
14867:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
14868:             $formelement = 'form.'.$context.'_'.$item;
14869:         } else {
14870:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
14871:         }
14872:         if ($env{$formelement}) {
14873:             $newsec->{'encrypt'}{$item} = 1;
14874:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
14875:                 unless ($currsec->{'encrypt'}{$item}) {
14876:                     $secchanges->{'encrypt'} = 1;
14877:                 }
14878:             } else {
14879:                 $secchanges->{'encrypt'} = 1;
14880:             }
14881:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
14882:             if ($currsec->{'encrypt'}{$item}) {
14883:                 $secchanges->{'encrypt'} = 1;
14884:             }
14885:         }
14886:     }
14887:     my $secrets;
14888:     if ($context eq 'ltisec') {
14889:         $secrets = 'ltisecrets';
14890:     } else {
14891:         $secrets = 'toolsecrets';
14892:     }
14893:     unless (exists($currsec->{'rules'})) {
14894:         $currsec->{'rules'} = {};
14895:     }
14896:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
14897: 
14898:     my @ids=&Apache::lonnet::current_machine_ids();
14899:     my %servers = &Apache::lonnet::get_servers($dom,'library');
14900: 
14901:     foreach my $hostid (keys(%servers)) {
14902:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
14903:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
14904:             if (exists($env{$keyitem})) {
14905:                 $env{$keyitem} =~ s/(`)/'/g;
14906:                 if ($keyset{$hostid}) {
14907:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
14908:                         if ($env{$keyitem} ne '') {
14909:                             $secchanges->{'private'} = 1;
14910:                             $newkeyset->{$hostid} = $env{$keyitem};
14911:                         }
14912:                     }
14913:                 } elsif ($env{$keyitem} ne '') {
14914:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
14915:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
14916:                     }
14917:                     $secchanges->{'private'} = 1;
14918:                     $newkeyset->{$hostid} = $env{$keyitem};
14919:                 }
14920:             }
14921:         }
14922:     }
14923: }
14924: 
14925: sub store_security {
14926:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
14927:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
14928:                    (ref($keystore) eq 'HASH'));
14929:     if (keys(%{$secchanges})) {
14930:         if ($secchanges->{'private'}) {
14931:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
14932:             foreach my $hostid (keys(%{$newkeyset})) {
14933:                 my $storehash = {
14934:                                    key => $newkeyset->{$hostid},
14935:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
14936:                                 };
14937:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
14938:                                                                   $dom,$hostid);
14939:             }
14940:         }
14941:     }
14942: }
14943: 
14944: sub lti_security_results {
14945:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
14946:     my $output;
14947:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
14948:     my $needs_update;
14949:     foreach my $item (keys(%{$secchanges})) {
14950:         if ($item eq 'encrypt') {
14951:             $needs_update = 1;
14952:             my %encrypted;
14953:             if ($context eq 'lti') {
14954:                 %encrypted = (
14955:                               crs  => {
14956:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
14957:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
14958:                                       },
14959:                               dom => {
14960:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
14961:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
14962:                                      },
14963:                               consumers => {
14964:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
14965:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
14966:                                            },
14967:                              );
14968:             } else {
14969:                 %encrypted = (
14970:                               crs  => {
14971:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
14972:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
14973:                                       },
14974:                               dom => {
14975:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
14976:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
14977:                                      },
14978:                              );
14979: 
14980:             }
14981:             my @types= ('crs','dom');
14982:             if ($context eq 'lti') {
14983:                 foreach my $type (@types) {
14984:                     undef($domdefaults{'linkprotenc_'.$type});
14985:                 }
14986:                 push(@types,'consumers');
14987:                 undef($domdefaults{'ltienc_consumers'});
14988:             } elsif ($context eq 'ltitools') {
14989:                 foreach my $type (@types) {
14990:                     undef($domdefaults{'toolenc_'.$type});
14991:                 }
14992:             }
14993:             foreach my $type (@types) {
14994:                 my $shown = $encrypted{$type}{'off'};
14995:                 if (ref($newsec->{$item}) eq 'HASH') {
14996:                     if ($newsec->{$item}{$type}) {
14997:                         if ($context eq 'lti') {
14998:                             if ($type eq 'consumers') {
14999:                                 $domdefaults{'ltienc_consumers'} = 1;
15000:                             } else {
15001:                                 $domdefaults{'linkprotenc_'.$type} = 1;
15002:                             }
15003:                         } elsif ($context eq 'ltitools') {
15004:                             $domdefaults{'toolenc_'.$type} = 1;
15005:                         }
15006:                         $shown = $encrypted{$type}{'on'};
15007:                     }
15008:                 }
15009:                 $output .= '<li>'.$shown.'</li>';
15010:             }
15011:         } elsif ($item eq 'rules') {
15012:             my %titles = &Apache::lonlocal::texthash(
15013:                                       min   => 'Minimum password length',
15014:                                       max   => 'Maximum password length',
15015:                                       chars => 'Required characters',
15016:                          );
15017:             foreach my $rule ('min','max') {
15018:                 if ($newsec->{rules}{$rule} eq '') {
15019:                     if ($rule eq 'min') {
15020:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15021:                                    ' '.&mt('Default of [_1] will be used',
15022:                                            $Apache::lonnet::passwdmin).'</li>';
15023:                     } else {
15024:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15025:                     }
15026:                 } else {
15027:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
15028:                 }
15029:             }
15030:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
15031:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
15032:                     my %rulenames = &Apache::lonlocal::texthash(
15033:                                              uc => 'At least one upper case letter',
15034:                                              lc => 'At least one lower case letter',
15035:                                              num => 'At least one number',
15036:                                              spec => 'At least one non-alphanumeric',
15037:                                     );
15038:                     my $needed = '<ul><li>'.
15039:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
15040:                                  '</li></ul>';
15041:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15042:                 } else {
15043:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15044:                 }
15045:             } else {
15046:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15047:             }
15048:         } elsif ($item eq 'private') {
15049:             $needs_update = 1;
15050:             if ($context eq 'lti') {
15051:                 undef($domdefaults{'ltiprivhosts'});
15052:             } elsif ($context eq 'ltitools') {
15053:                 undef($domdefaults{'toolprivhosts'});
15054:             }
15055:             if (keys(%{$newkeyset})) {
15056:                 my @privhosts;
15057:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
15058:                     if ($keystore->{$hostid} eq 'ok') {
15059:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
15060:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
15061:                             push(@privhosts,$hostid);
15062:                         }
15063:                     }
15064:                 }
15065:                 if (@privhosts) {
15066:                     if ($context eq 'lti') {
15067:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
15068:                     } elsif ($context eq 'ltitools') {
15069:                         $domdefaults{'toolprivhosts'} = \@privhosts;
15070:                     }
15071:                 }
15072:             }
15073:         } elsif ($item eq 'linkprot') {
15074:             next;
15075:         }
15076:     }
15077:     if ($needs_update) {
15078:         my $cachetime = 24*60*60;
15079:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15080:     }
15081:     return $output;
15082: }
15083: 
15084: sub modify_proctoring {
15085:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15086:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15087:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
15088:     my $confname = $dom.'-domainconfig';
15089:     my $servadm = $r->dir_config('lonAdmEMail');
15090:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
15091:     my %providernames = &proctoring_providernames();
15092:     my $maxnum = scalar(keys(%providernames));
15093: 
15094:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
15095:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
15096:     if (ref($requref) eq 'HASH') {
15097:         %requserfields = %{$requref};
15098:     }
15099:     if (ref($opturef) eq 'HASH') {
15100:         %optuserfields = %{$opturef};
15101:     }
15102:     if (ref($defref) eq 'HASH') {
15103:         %defaults = %{$defref};
15104:     }
15105:     if (ref($extref) eq 'HASH') {
15106:         %extended = %{$extref};
15107:     }
15108:     if (ref($crsref) eq 'HASH') {
15109:         %crsconf = %{$crsref};
15110:     }
15111:     if (ref($rolesref) eq 'ARRAY') {
15112:         @courseroles = @{$rolesref};
15113:     }
15114:     if (ref($ltiref) eq 'ARRAY') {
15115:         @ltiroles = @{$ltiref};
15116:     }
15117: 
15118:     if (ref($domconfig{$action}) eq 'HASH') {
15119:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
15120:         if (@todeleteimages) {
15121:             map { $imgdeletions{$_} = 1; } @todeleteimages;
15122:         }
15123:     }
15124:     my %customadds;
15125:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
15126:     if (@newcustom) {
15127:         map { $customadds{$_} = 1; } @newcustom;
15128:     }
15129:     foreach my $provider (sort(keys(%providernames))) {
15130:         $confhash{$provider} = {};
15131:         my $pos = $env{'form.proctoring_pos_'.$provider};
15132:         $pos =~ s/\D+//g;
15133:         $allpos[$pos] = $provider;
15134:         my (%current,%currentenc);
15135:         my $showroles = 0;
15136:         if (ref($domconfig{$action}) eq 'HASH') {
15137:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15138:                 %current = %{$domconfig{$action}{$provider}};
15139:                 foreach my $item ('key','secret') { 
15140:                     $currentenc{$item} = $current{$item};
15141:                     delete($current{$item});
15142:                 }
15143:             }
15144:         }
15145:         if ($env{'form.proctoring_available_'.$provider}) {
15146:             $confhash{$provider}{'available'} = 1;
15147:             unless ($current{'available'}) {
15148:                 $changes{$provider} = 1;
15149:             }
15150:         } else {
15151:             %{$confhash{$provider}} = %current;
15152:             %{$encconfhash{$provider}} = %currentenc;
15153:             $confhash{$provider}{'available'} = 0;
15154:             if ($current{'available'}) {
15155:                 $changes{$provider} = 1;
15156:             }
15157:         }
15158:         if ($confhash{$provider}{'available'}) {
15159:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
15160:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
15161:                 if ($field eq 'lifetime') {
15162:                     if ($possval =~ /^\d+$/) {
15163:                         $confhash{$provider}{$field} = $possval;
15164:                     }
15165:                 } elsif ($field eq 'version') {
15166:                     if ($possval =~ /^\d+\.\d+$/) {
15167:                         $confhash{$provider}{$field} = $possval;
15168:                     }
15169:                 } elsif ($field eq 'sigmethod') {
15170:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
15171:                         $confhash{$provider}{$field} = $possval;
15172:                     }
15173:                 } elsif ($field eq 'url') {
15174:                     $confhash{$provider}{$field} = $possval;
15175:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
15176:                     $encconfhash{$provider}{$field} = $possval;
15177:                     unless ($currentenc{$field} eq $possval) {
15178:                         $changes{$provider} = 1;
15179:                     }
15180:                 }
15181:                 unless (($field eq 'key') || ($field eq 'secret')) {
15182:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
15183:                         $changes{$provider} = 1;
15184:                     }
15185:                 }
15186:             }
15187:             if ($imgdeletions{$provider}) {
15188:                 $changes{$provider} = 1;
15189:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
15190:                 my ($imageurl,$error) =
15191:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
15192:                                               $configuserok,$switchserver,$author_ok);
15193:                 if ($imageurl) {
15194:                     $confhash{$provider}{'image'} = $imageurl;
15195:                     $changes{$provider} = 1; 
15196:                 }
15197:                 if ($error) {
15198:                     &Apache::lonnet::logthis($error);
15199:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15200:                 }
15201:             } elsif (exists($current{'image'})) {
15202:                 $confhash{$provider}{'image'} = $current{'image'};
15203:             }
15204:             if (ref($requserfields{$provider}) eq 'ARRAY') {
15205:                 if (@{$requserfields{$provider}} > 0) {
15206:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
15207:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
15208:                             $confhash{$provider}{'incdom'} = 1;
15209:                         }
15210:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
15211:                             $changes{$provider} = 1;
15212:                         }
15213:                     }
15214:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
15215:                         $showroles = 1;
15216:                     }
15217:                 }
15218:             }
15219:             $confhash{$provider}{'fields'} = [];
15220:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
15221:                 if (@{$optuserfields{$provider}} > 0) {
15222:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
15223:                     foreach my $field (@{$optuserfields{$provider}}) {
15224:                         if (grep(/^\Q$field\E$/,@optfields)) {
15225:                             push(@{$confhash{$provider}{'fields'}},$field);
15226:                         }
15227:                     }
15228:                 }
15229:                 if (ref($current{'fields'}) eq 'ARRAY') {
15230:                     unless ($changes{$provider}) {
15231:                         my @new = sort(@{$confhash{$provider}{'fields'}});
15232:                         my @old = sort(@{$current{'fields'}}); 
15233:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15234:                         if (@diffs) {
15235:                             $changes{$provider} = 1;
15236:                         }
15237:                     }
15238:                 } elsif (@{$confhash{$provider}{'fields'}}) {
15239:                     $changes{$provider} = 1;
15240:                 }
15241:             }
15242:             if (ref($defaults{$provider}) eq 'ARRAY') {  
15243:                 if (@{$defaults{$provider}} > 0) {
15244:                     my %options;
15245:                     if (ref($extended{$provider}) eq 'HASH') {
15246:                         %options = %{$extended{$provider}};
15247:                     }
15248:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
15249:                     foreach my $field (@{$defaults{$provider}}) {
15250:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
15251:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
15252:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
15253:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
15254:                             }
15255:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
15256:                             foreach my $inner (keys(%{$options{$field}})) {
15257:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
15258:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
15259:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
15260:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
15261:                                     }
15262:                                 } else {
15263:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
15264:                                 }
15265:                             }
15266:                         } else {
15267:                             if (grep(/^\Q$field\E$/,@checked)) {
15268:                                 push(@{$confhash{$provider}{'defaults'}},$field);
15269:                             }
15270:                         }
15271:                     }
15272:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15273:                         if (ref($current{'defaults'}) eq 'ARRAY') {
15274:                             unless ($changes{$provider}) {
15275:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
15276:                                 my @old = sort(@{$current{'defaults'}});
15277:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15278:                                 if (@diffs) {
15279:                                     $changes{$provider} = 1; 
15280:                                 }
15281:                             }
15282:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
15283:                             if (@{$current{'defaults'}}) {
15284:                                 $changes{$provider} = 1;
15285:                             }
15286:                         }
15287:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15288:                         if (ref($current{'defaults'}) eq 'HASH') {
15289:                             unless ($changes{$provider}) {
15290:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15291:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15292:                                         $changes{$provider} = 1;
15293:                                         last;
15294:                                     }
15295:                                 }
15296:                             }
15297:                             unless ($changes{$provider}) {
15298:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15299:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15300:                                         $changes{$provider} = 1;
15301:                                         last;
15302:                                     }
15303:                                 }
15304:                             }
15305:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15306:                             $changes{$provider} = 1;
15307:                         }
15308:                     }
15309:                 }
15310:             }
15311:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15312:                 if (@{$crsconf{$provider}} > 0) {
15313:                     $confhash{$provider}{'crsconf'} = [];
15314:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15315:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15316:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15317:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15318:                         }
15319:                     }
15320:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15321:                         unless ($changes{$provider}) {  
15322:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15323:                             my @old = sort(@{$current{'crsconf'}});
15324:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15325:                             if (@diffs) {
15326:                                 $changes{$provider} = 1;
15327:                             }
15328:                         }
15329:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15330:                         $changes{$provider} = 1;
15331:                     }
15332:                 }
15333:             }
15334:             if ($showroles) {
15335:                 $confhash{$provider}{'roles'} = {};
15336:                 foreach my $role (@courseroles) {
15337:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15338:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15339:                         $confhash{$provider}{'roles'}{$role} = $poss;
15340:                     }
15341:                 }
15342:                 unless ($changes{$provider}) {
15343:                     if (ref($current{'roles'}) eq 'HASH') {
15344:                         foreach my $role (keys(%{$current{'roles'}})) {
15345:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15346:                                 $changes{$provider} = 1;
15347:                                 last
15348:                             }
15349:                         }
15350:                         unless ($changes{$provider}) {
15351:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15352:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15353:                                     $changes{$provider} = 1;
15354:                                     last;
15355:                                 }
15356:                             }
15357:                         }
15358:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15359:                         $changes{$provider} = 1;
15360:                     }
15361:                 }
15362:             }
15363:             if (ref($current{'custom'}) eq 'HASH') {
15364:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15365:                 foreach my $key (keys(%{$current{'custom'}})) {
15366:                     if (grep(/^\Q$key\E$/,@customdels)) {
15367:                         $changes{$provider} = 1;
15368:                     } else {
15369:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15370:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15371:                             $changes{$provider} = 1;
15372:                         }
15373:                     }
15374:                 }
15375:             }
15376:             if ($customadds{$provider}) {
15377:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15378:                 $name =~ s/(`)/'/g;
15379:                 $name =~ s/^\s+//;
15380:                 $name =~ s/\s+$//;
15381:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15382:                 $value =~ s/(`)/'/g;
15383:                 $value =~ s/^\s+//;
15384:                 $value =~ s/\s+$//;
15385:                 if ($name ne '') {
15386:                     $confhash{$provider}{'custom'}{$name} = $value;
15387:                     $changes{$provider} = 1;
15388:                 }
15389:             }
15390:         }
15391:     }
15392:     if (@allpos > 0) {
15393:         my $idx = 0;
15394:         foreach my $provider (@allpos) {
15395:             if ($provider ne '') {
15396:                 $confhash{$provider}{'order'} = $idx;
15397:                 unless ($changes{$provider}) {
15398:                     if (ref($domconfig{$action}) eq 'HASH') {
15399:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15400:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15401:                                 $changes{$provider} = 1;
15402:                             }
15403:                         }
15404:                     }
15405:                 }
15406:                 $idx ++;
15407:             }
15408:         }
15409:     }
15410:     my %proc_hash = (
15411:                           $action => { %confhash }
15412:                        );
15413:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15414:                                              $dom);
15415:     if ($putresult eq 'ok') {
15416:         my %proc_enchash = (
15417:                              $action => { %encconfhash }
15418:                          );
15419:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15420:         if (keys(%changes) > 0) {
15421:             my $cachetime = 24*60*60;
15422:             my %procall = %confhash;
15423:             foreach my $provider (keys(%procall)) {
15424:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15425:                     foreach my $key ('key','secret') {
15426:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15427:                     }
15428:                 }
15429:             }
15430:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15431:             if (ref($lastactref) eq 'HASH') {
15432:                 $lastactref->{'proctoring'} = 1;
15433:             }
15434:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15435:             my %bynum;
15436:             foreach my $provider (sort(keys(%changes))) {
15437:                 my $position = $confhash{$provider}{'order'};
15438:                 $bynum{$position} = $provider;
15439:             }
15440:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15441:                 my $provider = $bynum{$pos};
15442:                 my %lt = &proctoring_titles($provider);
15443:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15444:                 if (!$confhash{$provider}{'available'}) {
15445:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15446:                 } else {
15447:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15448:                     if ($confhash{$provider}{'image'}) {
15449:                         $resulttext .= '&nbsp;'.
15450:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15451:                                        ' alt="'.&mt('Proctoring icon').'" />';
15452:                     }
15453:                     $resulttext .= '<ul>';
15454:                     my $position = $pos + 1;
15455:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15456:                     foreach my $key ('version','sigmethod','url','lifetime') {
15457:                         if ($confhash{$provider}{$key} ne '') {
15458:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15459:                         }
15460:                     }
15461:                     if ($encconfhash{$provider}{'key'} ne '') {
15462:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15463:                     }
15464:                     if ($encconfhash{$provider}{'secret'} ne '') {
15465:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15466:                         my $num = length($encconfhash{$provider}{'secret'});
15467:                         $resulttext .= ('*'x$num).'</li>';
15468:                     }
15469:                     my (@fields,$showroles);
15470:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15471:                         push(@fields,@{$requserfields{$provider}});
15472:                     }
15473:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15474:                         push(@fields,@{$confhash{$provider}{'fields'}});
15475:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15476:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15477:                     }
15478:                     if (@fields) {
15479:                         if (grep(/^roles$/,@fields)) {
15480:                             $showroles = 1;
15481:                         }
15482:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15483:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15484:                     }
15485:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15486:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15487:                             if ($confhash{$provider}{'incdom'}) {
15488:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15489:                             } else { 
15490:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15491:                             }
15492:                         }
15493:                     }
15494:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15495:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15496:                             $resulttext .= '<li>'.$lt{'defa'};
15497:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15498:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15499:                             }
15500:                             $resulttext =~ s/,$//;
15501:                             $resulttext .= '</li>';
15502:                         }
15503:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15504:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15505:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15506:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15507:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15508:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15509:                                 }
15510:                             }
15511:                             $resulttext .= '</ul></li>';
15512:                         }
15513:                     }
15514:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15515:                         if (@{$crsconf{$provider}} > 0) {
15516:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15517:                             my $numconfig = 0;
15518:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15519:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15520:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15521:                                         $numconfig ++;
15522:                                         if ($provider eq 'examity') {
15523:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15524:                                         } else {
15525:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15526:                                         }
15527:                                     }
15528:                                     $resulttext =~ s/,$//;
15529:                                 }
15530:                             }
15531:                             if (!$numconfig) {
15532:                                 $resulttext .= '&nbsp;'.&mt('None');
15533:                             }
15534:                             $resulttext .= '</li>';
15535:                         }
15536:                     }
15537:                     if ($showroles) {
15538:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15539:                             my $rolemaps;
15540:                             foreach my $role (@courseroles) {
15541:                                 if ($confhash{$provider}{'roles'}{$role}) {
15542:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15543:                                                  $confhash{$provider}{'roles'}{$role}.',';
15544:                                 }
15545:                             }
15546:                             if ($rolemaps) {
15547:                                 $rolemaps =~ s/,$//;
15548:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15549:                             }
15550:                         }
15551:                     }
15552:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15553:                         my $customlist;
15554:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15555:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15556:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15557:                             }
15558:                             $customlist =~ s/,$//;
15559:                         }
15560:                         if ($customlist) {
15561:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15562:                         }
15563:                     } 
15564:                     $resulttext .= '</ul></li>';
15565:                 }
15566:             }
15567:             $resulttext .= '</ul>';
15568:         } else {
15569:             $resulttext = &mt('No changes made.');
15570:         }
15571:     } else {
15572:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15573:     }
15574:     if ($errors) {
15575:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15576:                        $errors.'</ul>';
15577:     }
15578:     return $resulttext;
15579: }
15580: 
15581: sub process_proctoring_image {
15582:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15583:     my $filename = $env{'form.'.$caller.'.filename'};
15584:     my ($error,$url);
15585:     my ($width,$height) = (21,21);
15586:     if ($configuserok eq 'ok') {
15587:         if ($switchserver) {
15588:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15589:                          $switchserver);
15590:         } elsif ($author_ok eq 'ok') {
15591:             my $modified = [];
15592:             my ($result,$imageurl,$madethumb) =
15593:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15594:                                                         "proctoring/$provider/icon",$width,$height,
15595:                                                         '',$modified);
15596:             if ($result eq 'ok') {
15597:                 if ($madethumb) {
15598:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15599:                     my $imagethumb = "$path/tn-".$imagefile;
15600:                     $url = $imagethumb;
15601:                 } else {
15602:                     $url = $imageurl;
15603:                 }
15604:                 &update_modify_urls($r,$modified);
15605:             } else {
15606:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15607:             }
15608:         } else {
15609:             $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);
15610:         }
15611:     } else {
15612:         $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);
15613:     }
15614:     return ($url,$error);
15615: }
15616: 
15617: sub modify_lti {
15618:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15619:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15620:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15621:     my (%posslti,%posslticrs,%posscrstype);
15622:     my @courseroles = ('cc','in','ta','ep','st');
15623:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15624:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15625:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15626:     my %coursetypetitles = &Apache::lonlocal::texthash (
15627:                                official   => 'Official',
15628:                                unofficial => 'Unofficial',
15629:                                community  => 'Community',
15630:                                textbook   => 'Textbook',
15631:                                placement  => 'Placement Test',
15632:                                lti        => 'LTI Provider',
15633:     );
15634:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15635:     my %lt = &lti_names();
15636:     map { $posslti{$_} = 1; } @ltiroles;
15637:     map { $posslticrs{$_} = 1; } @lticourseroles;
15638:     map { $posscrstype{$_} = 1; } @coursetypes;
15639: 
15640:     my %menutitles = &ltimenu_titles();
15641:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15642: 
15643:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15644: 
15645:     my (%linkprotchg,$linkprotoutput,$is_home);
15646:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15647:                                                            \%linkprotchg,'domain');
15648:     my $home = &Apache::lonnet::domain($dom,'primary');
15649:     unless (($home eq 'no_host') || ($home eq '')) {
15650:         my @ids=&Apache::lonnet::current_machine_ids();
15651:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15652:     }
15653: 
15654:     if (keys(%linkprotchg)) {
15655:         $secchanges{'linkprot'} = 1;
15656:         my %oldlinkprot;
15657:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15658:             %oldlinkprot = %{$currltisec{'linkprot'}};
15659:         }
15660:         foreach my $id (keys(%linkprotchg)) {
15661:             if (ref($linkprotchg{$id}) eq 'HASH') {
15662:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15663:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15664:                         if ($is_home) {
15665:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15666:                         }
15667:                     }
15668:                 }
15669:             } else {
15670:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15671:             }
15672:         }
15673:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15674:         if (keys(%linkprotchg)) {
15675:             %{$newltisec{'linkprot'}} = %linkprotchg;
15676:         }
15677:     }
15678:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15679:         foreach my $id (%{$currltisec{'linkprot'}}) {
15680:             next if ($id !~ /^\d+$/);
15681:             unless (exists($linkprotchg{$id})) {
15682:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15683:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15684:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15685:                             if ($is_home) {
15686:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15687:                             }
15688:                         } else {
15689:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15690:                         }
15691:                     }
15692:                 } else {
15693:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15694:                 }
15695:             }
15696:         }
15697:     }
15698:     if ($proterror) {
15699:         $errors .= '<li>'.$proterror.'</li>';
15700:     }
15701:     my (@items,%deletions,%itemids);
15702:     if ($env{'form.lti_add'}) {
15703:         my $consumer = $env{'form.lti_consumer_add'};
15704:         $consumer =~ s/(`)/'/g;
15705:         ($newid,my $error) = &get_lti_id($dom,$consumer);
15706:         if ($newid) {
15707:             $itemids{'add'} = $newid;
15708:             push(@items,'add');
15709:             $changes{$newid} = 1;
15710:         } else {
15711:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15712:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15713:         }
15714:     }
15715:     if (ref($domconfig{$action}) eq 'HASH') {
15716:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15717:         if (@todelete) {
15718:             map { $deletions{$_} = 1; } @todelete;
15719:         }
15720:         my $maxnum = $env{'form.lti_maxnum'};
15721:         for (my $i=0; $i<$maxnum; $i++) {
15722:             my $itemid = $env{'form.lti_id_'.$i};
15723:             $itemid =~ s/\D+//g;
15724:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15725:                 if ($deletions{$itemid}) {
15726:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15727:                 } else {
15728:                     push(@items,$i);
15729:                     $itemids{$i} = $itemid;
15730:                 }
15731:             }
15732:         }
15733:     }
15734:     my (%keystore,$secstored);
15735:     if ($is_home) {
15736:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
15737:     }
15738: 
15739:     my ($cipher,$privnum);
15740:     if ((@items > 0) && ($is_home)) {
15741:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
15742:                                              $newltisec{'encrypt'},$keystore{$home});
15743:     }
15744:     foreach my $idx (@items) {
15745:         my $itemid = $itemids{$idx};
15746:         next unless ($itemid);
15747:         my %currlti;
15748:         unless ($idx eq 'add') {
15749:             if (ref($domconfig{$action}) eq 'HASH') {
15750:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15751:                     %currlti = %{$domconfig{$action}{$itemid}};
15752:                 }
15753:             }
15754:         }
15755:         my $position = $env{'form.lti_pos_'.$itemid};
15756:         $position =~ s/\D+//g;
15757:         if ($position ne '') {
15758:             $allpos[$position] = $itemid;
15759:         }
15760:         foreach my $item ('consumer','lifetime','requser','crsinc') {
15761:             my $formitem = 'form.lti_'.$item.'_'.$idx;
15762:             $env{$formitem} =~ s/(`)/'/g;
15763:             if ($item eq 'lifetime') {
15764:                 $env{$formitem} =~ s/[^\d.]//g;
15765:             }
15766:             if ($env{$formitem} ne '') {
15767:                 $confhash{$itemid}{$item} = $env{$formitem};
15768:                 unless (($idx eq 'add') || ($changes{$itemid})) {
15769:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
15770:                         $changes{$itemid} = 1;
15771:                     }
15772:                 }
15773:             }
15774:         }
15775:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
15776:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
15777:         }
15778:         if ($confhash{$itemid}{'requser'}) {
15779:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
15780:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
15781:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
15782:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
15783:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
15784:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
15785:                 $mapuser =~ s/(`)/'/g;
15786:                 $mapuser =~ s/^\s+|\s+$//g;
15787:                 $confhash{$itemid}{'mapuser'} = $mapuser;
15788:             }
15789:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
15790:             my @makeuser;
15791:             foreach my $ltirole (sort(@possmakeuser)) {
15792:                 if ($posslti{$ltirole}) {
15793:                     push(@makeuser,$ltirole);
15794:                 }
15795:             }
15796:             $confhash{$itemid}{'makeuser'} = \@makeuser;
15797:             if (@makeuser) {
15798:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
15799:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
15800:                     $confhash{$itemid}{'lcauth'} = $lcauth;
15801:                     if ($lcauth ne 'internal') {
15802:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
15803:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
15804:                         $lcauthparm =~ s/`//g;
15805:                         if ($lcauthparm ne '') {
15806:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
15807:                         }
15808:                     }
15809:                 } else {
15810:                     $confhash{$itemid}{'lcauth'} = 'lti';
15811:                 }
15812:             }
15813:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
15814:             if (@possinstdata) {
15815:                 foreach my $field (@possinstdata) {
15816:                     if (exists($fieldtitles{$field})) {
15817:                         push(@{$confhash{$itemid}{'instdata'}});
15818:                     }
15819:                 }
15820:             }
15821:             if ($env{'form.lti_callback_'.$idx}) {
15822:                 if ($env{'form.lti_callbackparam_'.$idx}) {
15823:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
15824:                     $callback =~ s/^\s+|\s+$//g;
15825:                     $confhash{$itemid}{'callback'} = $callback;
15826:                 }
15827:             }
15828:             foreach my $field ('topmenu','inlinemenu') {
15829:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
15830:                     $confhash{$itemid}{$field} = 1;
15831:                 }
15832:             }
15833:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
15834:                 $confhash{$itemid}{lcmenu} = [];
15835:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
15836:                 foreach my $field (@possmenu) {
15837:                     if (exists($menutitles{$field})) {
15838:                         if ($field eq 'grades') {
15839:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
15840:                         }
15841:                         push(@{$confhash{$itemid}{lcmenu}},$field);
15842:                     }
15843:                 }
15844:             }
15845:             if ($confhash{$itemid}{'crsinc'}) {
15846:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
15847:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
15848:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
15849:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
15850:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
15851:                     $mapcrs =~ s/(`)/'/g;
15852:                     $mapcrs =~ s/^\s+|\s+$//g;
15853:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
15854:                 }
15855:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
15856:                 my @crstypes;
15857:                 foreach my $type (sort(@posstypes)) {
15858:                     if ($posscrstype{$type}) {
15859:                         push(@crstypes,$type);
15860:                     }
15861:                 }
15862:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
15863:                 if ($env{'form.lti_storecrs_'.$idx}) {
15864:                     $confhash{$itemid}{'storecrs'} = 1;
15865:                 }       
15866:                 if ($env{'form.lti_makecrs_'.$idx}) {
15867:                     $confhash{$itemid}{'makecrs'} = 1;
15868:                 }
15869:                 foreach my $ltirole (@lticourseroles) {
15870:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
15871:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
15872:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
15873:                     }
15874:                 }
15875:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
15876:                 my @selfenroll;
15877:                 foreach my $type (sort(@possenroll)) {
15878:                     if ($posslticrs{$type}) {
15879:                         push(@selfenroll,$type);
15880:                     }
15881:                 }
15882:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
15883:                 if ($env{'form.lti_crssec_'.$idx}) {
15884:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
15885:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
15886:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
15887:                         my $section = $env{'form.lti_customsection_'.$idx};
15888:                         $section =~ s/(`)/'/g;
15889:                         $section =~ s/^\s+|\s+$//g;
15890:                         if ($section ne '') {
15891:                             $confhash{$itemid}{'section'} = $section;
15892:                         }
15893:                     }
15894:                 }
15895:                 foreach my $field ('passback','roster') {
15896:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
15897:                         $confhash{$itemid}{$field} = 1;
15898:                     }
15899:                 }
15900:                 if ($env{'form.lti_passback_'.$idx}) {
15901:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
15902:                         $confhash{$itemid}{'passbackformat'} = '1.0';
15903:                     } else {
15904:                         $confhash{$itemid}{'passbackformat'} = '1.1';
15905:                     }
15906:                 }
15907:             }
15908:             unless (($idx eq 'add') || ($changes{$itemid})) {
15909:                 if ($confhash{$itemid}{'crsinc'}) {
15910:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15911:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
15912:                             $changes{$itemid} = 1;
15913:                         }
15914:                     }
15915:                     unless ($changes{$itemid}) {
15916:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
15917:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15918:                                 $changes{$itemid} = 1;
15919:                             }
15920:                         }
15921:                     }
15922:                     foreach my $field ('mapcrstype','selfenroll') {
15923:                         unless ($changes{$itemid}) {
15924:                             if (ref($currlti{$field}) eq 'ARRAY') {
15925:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15926:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
15927:                                                                                    $confhash{$itemid}{$field});
15928:                                     if (@diffs) {
15929:                                         $changes{$itemid} = 1;
15930:                                     }
15931:                                 } elsif (@{$currlti{$field}} > 0) {
15932:                                     $changes{$itemid} = 1;
15933:                                 }
15934:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15935:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15936:                                     $changes{$itemid} = 1;
15937:                                 }
15938:                             }
15939:                         }
15940:                     }
15941:                     unless ($changes{$itemid}) {
15942:                         if (ref($currlti{'maproles'}) eq 'HASH') {
15943:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15944:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
15945:                                     if ($currlti{'maproles'}{$ltirole} ne 
15946:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15947:                                         $changes{$itemid} = 1;
15948:                                         last;
15949:                                     }
15950:                                 }
15951:                                 unless ($changes{$itemid}) {
15952:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15953:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15954:                                             $currlti{'maproles'}{$ltirole}) {
15955:                                             $changes{$itemid} = 1;
15956:                                             last;
15957:                                         }
15958:                                     }
15959:                                 }
15960:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
15961:                                 $changes{$itemid} = 1;
15962:                             }
15963:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15964:                             unless ($changes{$itemid}) {
15965:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15966:                                     $changes{$itemid} = 1;
15967:                                 }
15968:                             }
15969:                         }
15970:                     }
15971:                 }
15972:                 unless ($changes{$itemid}) {
15973:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15974:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
15975:                             $changes{$itemid} = 1;
15976:                         }
15977:                     }
15978:                     unless ($changes{$itemid}) {
15979:                         foreach my $field ('makeuser','lcmenu') {
15980:                             if (ref($currlti{$field}) eq 'ARRAY') {
15981:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15982:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
15983:                                                                                    $confhash{$itemid}{$field});
15984:                                     if (@diffs) {
15985:                                         $changes{$itemid} = 1;
15986:                                     }
15987:                                 } elsif (@{$currlti{$field}} > 0) {
15988:                                     $changes{$itemid} = 1;
15989:                                 }
15990:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15991:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15992:                                     $changes{$itemid} = 1;
15993:                                 }
15994:                             }
15995:                         }
15996:                     }
15997:                 }
15998:             }
15999:         }
16000:         if ($is_home) {
16001:             my $keyitem = 'form.lti_key_'.$idx;
16002:             $env{$keyitem} =~ s/(`)/'/g;
16003:             if ($env{$keyitem} ne '') {
16004:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
16005:                 unless ($changes{$itemid}) {
16006:                     if ($currlti{'key'} ne $env{$keyitem}) {
16007:                         $changes{$itemid} = 1;
16008:                     }
16009:                 }
16010:             }
16011:             my $secretitem = 'form.lti_secret_'.$idx;
16012:             $env{$secretitem} =~ s/(`)/'/g;
16013:             if ($currlti{'usable'}) {
16014:                 if ($env{'form.lti_changesecret_'.$idx}) {
16015:                     if ($env{$secretitem} ne '') {
16016:                         if ($privnum && $cipher) {
16017:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16018:                             $confhash{$itemid}{'cipher'} = $privnum;
16019:                         } else {
16020:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
16021:                         }
16022:                         $changes{$itemid} = 1;
16023:                     }
16024:                 } else {
16025:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
16026:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
16027:                 }
16028:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16029:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
16030:                         $confhash{$itemid}{'usable'} = 1;
16031:                     }
16032:                 }
16033:             } elsif ($env{$secretitem} ne '') {
16034:                 if ($privnum && $cipher) {
16035:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16036:                     $confhash{$itemid}{'cipher'} = $privnum;
16037:                 } else {
16038:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
16039:                 }
16040:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16041:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
16042:                         $confhash{$itemid}{'usable'} = 1;
16043:                     }
16044:                 }
16045:                 $changes{$itemid} = 1;
16046:             }
16047:         }
16048:         unless ($changes{$itemid}) {
16049:             foreach my $key (keys(%currlti)) {
16050:                 if (ref($currlti{$key}) eq 'HASH') {
16051:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
16052:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
16053:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
16054:                                 $changes{$itemid} = 1;
16055:                                 last;
16056:                             }
16057:                         }
16058:                     } elsif (keys(%{$currlti{$key}}) > 0) {
16059:                         $changes{$itemid} = 1;
16060:                     }
16061:                 }
16062:                 last if ($changes{$itemid});
16063:             }
16064:         }
16065:     }
16066:     if (@allpos > 0) {
16067:         my $idx = 0;
16068:         foreach my $itemid (@allpos) {
16069:             if ($itemid ne '') {
16070:                 $confhash{$itemid}{'order'} = $idx;
16071:                 if (ref($domconfig{$action}) eq 'HASH') {
16072:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16073:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
16074:                             $changes{$itemid} = 1;
16075:                         }
16076:                     }
16077:                 }
16078:                 $idx ++;
16079:             }
16080:         }
16081:     }
16082: 
16083:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
16084:         return &mt('No changes made.');
16085:     }
16086: 
16087:     my %ltihash = (
16088:                       $action => { %confhash }
16089:                   );
16090:     my %ltienchash;
16091: 
16092:     if ($is_home) {
16093:         %ltienchash = (
16094:                          $action => { %ltienc }
16095:                       );
16096:     }
16097:     if (keys(%secchanges)) {
16098:         $ltihash{'ltisec'} = \%newltisec;
16099:         if ($secchanges{'linkprot'}) {
16100:             if ($is_home) {
16101:                 $ltienchash{'linkprot'} = \%newltienc;
16102:             }
16103:         }
16104:     }
16105:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
16106:     if ($putresult eq 'ok') {
16107:         if (keys(%ltienchash)) {
16108:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
16109:         }
16110:         $resulttext = &mt('Changes made:').'<ul>';
16111:         if (keys(%secchanges) > 0) {
16112:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
16113:             if (exists($secchanges{'linkprot'})) {
16114:                 $resulttext .= $linkprotoutput;
16115:             }
16116:         }
16117:         if (keys(%changes) > 0) {
16118:             my $cachetime = 24*60*60;
16119:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
16120:             if (ref($lastactref) eq 'HASH') {
16121:                 $lastactref->{'lti'} = 1;
16122:             }
16123:             my %bynum;
16124:             foreach my $itemid (sort(keys(%changes))) {
16125:                 if (ref($confhash{$itemid}) eq 'HASH') {
16126:                     my $position = $confhash{$itemid}{'order'};
16127:                     $bynum{$position} = $itemid;
16128:                 }
16129:             }
16130:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
16131:                 my $itemid = $bynum{$pos};
16132:                 if (ref($confhash{$itemid}) eq 'HASH') {
16133:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
16134:                     my $position = $pos + 1;
16135:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
16136:                     foreach my $item ('version','lifetime') {
16137:                         if ($confhash{$itemid}{$item} ne '') {
16138:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
16139:                         }
16140:                     }
16141:                     if ($ltienc{$itemid}{'key'} ne '') {
16142:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
16143:                     }
16144:                     if ($ltienc{$itemid}{'secret'} ne '') {
16145:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
16146:                     }
16147:                     if ($confhash{$itemid}{'requser'}) {
16148:                         if ($confhash{$itemid}{'callback'}) {
16149:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
16150:                         } else {
16151:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
16152:                         }
16153:                         if ($confhash{$itemid}{'mapuser'}) {
16154:                             my $shownmapuser;
16155:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
16156:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
16157:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
16158:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
16159:                             } else {
16160:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
16161:                             }
16162:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
16163:                         }
16164:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
16165:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
16166:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
16167:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
16168:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
16169:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
16170:                                 } else {
16171:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
16172:                                                        $confhash{$itemid}{'lcauth'});
16173:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
16174:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
16175:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
16176:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
16177:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
16178:                                         }
16179:                                     } else {
16180:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
16181:                                     }
16182:                                 }
16183:                                 $resulttext .= '</li>';
16184:                             } else {
16185:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
16186:                             }
16187:                         }
16188:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
16189:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
16190:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
16191:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
16192:                             } else {
16193:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
16194:                             }
16195:                         }
16196:                         foreach my $item ('topmenu','inlinemenu') {
16197:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16198:                             if ($confhash{$itemid}{$item}) {
16199:                                 $resulttext .= &mt('Yes');
16200:                             } else {
16201:                                 $resulttext .= &mt('No');
16202:                             }
16203:                             $resulttext .= '</li>';
16204:                         }
16205:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16206:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16207:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
16208:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
16209:                             } else {
16210:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
16211:                             }
16212:                         }
16213:                         if ($confhash{$itemid}{'crsinc'}) {
16214:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16215:                                 my $rolemaps;
16216:                                 foreach my $role (@ltiroles) {
16217:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
16218:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
16219:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
16220:                                                                                 'Course').',';
16221:                                     }
16222:                                 }
16223:                                 if ($rolemaps) {
16224:                                     $rolemaps =~ s/,$//;
16225:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
16226:                                 }
16227:                             }
16228:                             if ($confhash{$itemid}{'mapcrs'}) {
16229:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
16230:                             }
16231:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
16232:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
16233:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
16234:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
16235:                                                    '</li>';
16236:                                 } else {
16237:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
16238:                                 }
16239:                             }
16240:                             if ($confhash{$itemid}{'storecrs'}) {
16241:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
16242:                             }
16243:                             if ($confhash{$itemid}{'makecrs'}) {
16244:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
16245:                             } else {
16246:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
16247:                             }
16248:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
16249:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
16250:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
16251:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
16252:                                                    '</li>';
16253:                                 } else {
16254:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
16255:                                 }
16256:                             }
16257:                             if ($confhash{$itemid}{'section'}) {
16258:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
16259:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
16260:                                                          ' (course_section_sourcedid)'.'</li>';  
16261:                                 } else {
16262:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
16263:                                                           $confhash{$itemid}{'section'}.'</li>';
16264:                                 }
16265:                             } else {
16266:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
16267:                             }
16268:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
16269:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16270:                                 if ($confhash{$itemid}{$item}) {
16271:                                     $resulttext .= &mt('Yes');
16272:                                     if ($item eq 'passback') {
16273:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
16274:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
16275:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
16276:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
16277:                                         }
16278:                                     }
16279:                                 } else {
16280:                                     $resulttext .= &mt('No');
16281:                                 }
16282:                                 $resulttext .= '</li>';
16283:                             }
16284:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16285:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16286:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
16287:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
16288:                                 } else {
16289:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
16290:                                 }
16291:                             }
16292:                         }
16293:                     }
16294:                     $resulttext .= '</ul></li>';
16295:                 }
16296:             }
16297:             if (keys(%deletions)) {
16298:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
16299:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
16300:                 }
16301:             }
16302:         }
16303:         $resulttext .= '</ul>';
16304:         if (ref($lastactref) eq 'HASH') {
16305:             if (($secchanges{'encrypt'}) || ($secchanges{'private'})) {
16306:                 $lastactref->{'domdefaults'} = 1;
16307:             }
16308:         }
16309:     } else {
16310:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
16311:     }
16312:     if ($errors) {
16313:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
16314:                        $errors.'</ul>';
16315:     }
16316:     return $resulttext;
16317: }
16318: 
16319: sub get_priv_creds {
16320:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
16321:     my ($needenc,$cipher,$privnum);
16322:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
16323:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
16324:         $needenc = $encrypt->{'consumers'} 
16325:     } else {
16326:         $needenc = $domdefs{'ltienc_consumers'};
16327:     }
16328:     if ($needenc) {
16329:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
16330:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
16331:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
16332:             my $privkey = $privhash{'key'};
16333:             $privnum = $privhash{'version'};
16334:             if (($privnum) && ($privkey ne '')) {
16335:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
16336:                                           'cipher'  => 'DES'});
16337:             }
16338:         }
16339:     }
16340:     return ($cipher,$privnum);
16341: }
16342: 
16343: sub get_lti_id {
16344:     my ($domain,$consumer) = @_;
16345:     # get lock on lti db
16346:     my $lockhash = {
16347:                       lock => $env{'user.name'}.
16348:                               ':'.$env{'user.domain'},
16349:                    };
16350:     my $tries = 0;
16351:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
16352:     my ($id,$error);
16353: 
16354:     while (($gotlock ne 'ok') && ($tries<10)) {
16355:         $tries ++;
16356:         sleep (0.1);
16357:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
16358:     }
16359:     if ($gotlock eq 'ok') {
16360:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
16361:         if ($currids{'lock'}) {
16362:             delete($currids{'lock'});
16363:             if (keys(%currids)) {
16364:                 my @curr = sort { $a <=> $b } keys(%currids);
16365:                 if ($curr[-1] =~ /^\d+$/) {
16366:                     $id = 1 + $curr[-1];
16367:                 }
16368:             } else {
16369:                 $id = 1;
16370:             }
16371:             if ($id) {
16372:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
16373:                     $error = 'nostore';
16374:                 }
16375:             } else {
16376:                 $error = 'nonumber';
16377:             }
16378:         }
16379:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
16380:     } else {
16381:         $error = 'nolock';
16382:     }
16383:     return ($id,$error);
16384: }
16385: 
16386: sub modify_autoenroll {
16387:     my ($dom,$lastactref,%domconfig) = @_;
16388:     my ($resulttext,%changes);
16389:     my %currautoenroll;
16390:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
16391:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
16392:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
16393:         }
16394:     }
16395:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
16396:     my %title = ( run => 'Auto-enrollment active',
16397:                   sender => 'Sender for notification messages',
16398:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
16399:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
16400:     my @offon = ('off','on');
16401:     my $sender_uname = $env{'form.sender_uname'};
16402:     my $sender_domain = $env{'form.sender_domain'};
16403:     if ($sender_domain eq '') {
16404:         $sender_uname = '';
16405:     } elsif ($sender_uname eq '') {
16406:         $sender_domain = '';
16407:     }
16408:     my $coowners = $env{'form.autoassign_coowners'};
16409:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
16410:     $autofailsafe =~ s{^\s+|\s+$}{}g;
16411:     if ($autofailsafe =~ /\D/) {
16412:         undef($autofailsafe);
16413:     }
16414:     my $failsafe = $env{'form.autoenroll_failsafe'};
16415:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
16416:         $failsafe = 'off';
16417:         undef($autofailsafe);
16418:     }
16419:     my %autoenrollhash =  (
16420:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
16421:                                        'sender_uname' => $sender_uname,
16422:                                        'sender_domain' => $sender_domain,
16423:                                        'co-owners' => $coowners,
16424:                                        'autofailsafe' => $autofailsafe,
16425:                                        'failsafe' => $failsafe,
16426:                                 }
16427:                      );
16428:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
16429:                                              $dom);
16430:     if ($putresult eq 'ok') {
16431:         if (exists($currautoenroll{'run'})) {
16432:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
16433:                  $changes{'run'} = 1;
16434:              }
16435:         } elsif ($autorun) {
16436:             if ($env{'form.autoenroll_run'} ne '1') {
16437:                  $changes{'run'} = 1;
16438:             }
16439:         }
16440:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
16441:             $changes{'sender'} = 1;
16442:         }
16443:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
16444:             $changes{'sender'} = 1;
16445:         }
16446:         if ($currautoenroll{'co-owners'} ne '') {
16447:             if ($currautoenroll{'co-owners'} ne $coowners) {
16448:                 $changes{'coowners'} = 1;
16449:             }
16450:         } elsif ($coowners) {
16451:             $changes{'coowners'} = 1;
16452:         }
16453:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
16454:             $changes{'autofailsafe'} = 1;
16455:         }
16456:         if ($currautoenroll{'failsafe'} ne $failsafe) {
16457:             $changes{'failsafe'} = 1;
16458:         }
16459:         if (keys(%changes) > 0) {
16460:             $resulttext = &mt('Changes made:').'<ul>';
16461:             if ($changes{'run'}) {
16462:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
16463:             }
16464:             if ($changes{'sender'}) {
16465:                 if ($sender_uname eq '' || $sender_domain eq '') {
16466:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
16467:                 } else {
16468:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
16469:                 }
16470:             }
16471:             if ($changes{'coowners'}) {
16472:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
16473:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16474:                 if (ref($lastactref) eq 'HASH') {
16475:                     $lastactref->{'domainconfig'} = 1;
16476:                 }
16477:             }
16478:             if ($changes{'autofailsafe'}) {
16479:                 if ($autofailsafe ne '') {
16480:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
16481:                 } else {
16482:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16483:                 }
16484:             }
16485:             if ($changes{'failsafe'}) {
16486:                 if ($failsafe eq 'off') {
16487:                     unless ($changes{'autofailsafe'}) {
16488:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16489:                     }
16490:                 } elsif ($failsafe eq 'zero') {
16491:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
16492:                 } else {
16493:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
16494:                 }
16495:             }
16496:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
16497:                 &Apache::lonnet::get_domain_defaults($dom,1);
16498:                 if (ref($lastactref) eq 'HASH') {
16499:                     $lastactref->{'domdefaults'} = 1;
16500:                 }
16501:             }
16502:             $resulttext .= '</ul>';
16503:         } else {
16504:             $resulttext = &mt('No changes made to auto-enrollment settings');
16505:         }
16506:     } else {
16507:         $resulttext = '<span class="LC_error">'.
16508: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16509:     }
16510:     return $resulttext;
16511: }
16512: 
16513: sub modify_autoupdate {
16514:     my ($dom,%domconfig) = @_;
16515:     my ($resulttext,%currautoupdate,%fields,%changes);
16516:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16517:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16518:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16519:         }
16520:     }
16521:     my @offon = ('off','on');
16522:     my %title = &Apache::lonlocal::texthash (
16523:                     run        => 'Auto-update:',
16524:                     classlists => 'Updates to user information in classlists?',
16525:                     unexpired  => 'Skip updates for users without active or future roles?',
16526:                     lastactive => 'Skip updates for inactive users?',
16527:                 );
16528:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16529:     my %fieldtitles = &Apache::lonlocal::texthash (
16530:                         id => 'Student/Employee ID',
16531:                         permanentemail => 'E-mail address',
16532:                         lastname => 'Last Name',
16533:                         firstname => 'First Name',
16534:                         middlename => 'Middle Name',
16535:                         generation => 'Generation',
16536:                       );
16537:     $othertitle = &mt('All users');
16538:     if (keys(%{$usertypes}) >  0) {
16539:         $othertitle = &mt('Other users');
16540:     }
16541:     foreach my $key (keys(%env)) {
16542:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16543:             my ($usertype,$item) = ($1,$2);
16544:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16545:                 if ($usertype eq 'default') {   
16546:                     push(@{$fields{$1}},$2);
16547:                 } elsif (ref($types) eq 'ARRAY') {
16548:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16549:                         push(@{$fields{$1}},$2);
16550:                     }
16551:                 }
16552:             }
16553:         }
16554:     }
16555:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16556:     @lockablenames = sort(@lockablenames);
16557:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16558:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16559:         if (@changed) {
16560:             $changes{'lockablenames'} = 1;
16561:         }
16562:     } else {
16563:         if (@lockablenames) {
16564:             $changes{'lockablenames'} = 1;
16565:         }
16566:     }
16567:     my %updatehash = (
16568:                       autoupdate => { run => $env{'form.autoupdate_run'},
16569:                                       classlists => $env{'form.classlists'},
16570:                                       unexpired  => $env{'form.unexpired'},
16571:                                       fields => {%fields},
16572:                                       lockablenames => \@lockablenames,
16573:                                     }
16574:                      );
16575:     my $lastactivedays;
16576:     if ($env{'form.lastactive'}) {
16577:         $lastactivedays = $env{'form.lastactivedays'};
16578:         $lastactivedays =~ s/^\s+|\s+$//g;
16579:         unless ($lastactivedays =~ /^\d+$/) {
16580:             undef($lastactivedays);
16581:             $env{'form.lastactive'} = 0;
16582:         }
16583:     }
16584:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16585:     foreach my $key (keys(%currautoupdate)) {
16586:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16587:             if (exists($updatehash{autoupdate}{$key})) {
16588:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16589:                     $changes{$key} = 1;
16590:                 }
16591:             }
16592:         } elsif ($key eq 'fields') {
16593:             if (ref($currautoupdate{$key}) eq 'HASH') {
16594:                 foreach my $item (@{$types},'default') {
16595:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16596:                         my $change = 0;
16597:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16598:                             if (!exists($fields{$item})) {
16599:                                 $change = 1;
16600:                                 last;
16601:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16602:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16603:                                     $change = 1;
16604:                                     last;
16605:                                 }
16606:                             }
16607:                         }
16608:                         if ($change) {
16609:                             push(@{$changes{$key}},$item);
16610:                         }
16611:                     } 
16612:                 }
16613:             }
16614:         } elsif ($key eq 'lockablenames') {
16615:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16616:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16617:                 if (@changed) {
16618:                     $changes{'lockablenames'} = 1;
16619:                 }
16620:             } else {
16621:                 if (@lockablenames) {
16622:                     $changes{'lockablenames'} = 1;
16623:                 }
16624:             }
16625:         }
16626:     }
16627:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16628:         if (@lockablenames) {
16629:             $changes{'lockablenames'} = 1;
16630:         }
16631:     }
16632:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16633:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16634:             $changes{'unexpired'} = 1;
16635:         }
16636:     }
16637:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16638:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16639:             $changes{'lastactive'} = 1;
16640:         }
16641:     }
16642:     foreach my $item (@{$types},'default') {
16643:         if (defined($fields{$item})) {
16644:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16645:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16646:                     my $change = 0;
16647:                     if (ref($fields{$item}) eq 'ARRAY') {
16648:                         foreach my $type (@{$fields{$item}}) {
16649:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16650:                                 $change = 1;
16651:                                 last;
16652:                             }
16653:                         }
16654:                     }
16655:                     if ($change) {
16656:                         push(@{$changes{'fields'}},$item);
16657:                     }
16658:                 } else {
16659:                     push(@{$changes{'fields'}},$item);
16660:                 }
16661:             } else {
16662:                 push(@{$changes{'fields'}},$item);
16663:             }
16664:         }
16665:     }
16666:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16667:                                              $dom);
16668:     if ($putresult eq 'ok') {
16669:         if (keys(%changes) > 0) {
16670:             $resulttext = &mt('Changes made:').'<ul>';
16671:             foreach my $key (sort(keys(%changes))) {
16672:                 if ($key eq 'lockablenames') {
16673:                     $resulttext .= '<li>';
16674:                     if (@lockablenames) {
16675:                         $usertypes->{'default'} = $othertitle;
16676:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16677:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16678:                     } else {
16679:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16680:                     }
16681:                     $resulttext .= '</li>';
16682:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16683:                     foreach my $item (@{$changes{$key}}) {
16684:                         my @newvalues;
16685:                         foreach my $type (@{$fields{$item}}) {
16686:                             push(@newvalues,$fieldtitles{$type});
16687:                         }
16688:                         my $newvaluestr;
16689:                         if (@newvalues > 0) {
16690:                             $newvaluestr = join(', ',@newvalues);
16691:                         } else {
16692:                             $newvaluestr = &mt('none');
16693:                         }
16694:                         if ($item eq 'default') {
16695:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16696:                         } else {
16697:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16698:                         }
16699:                     }
16700:                 } else {
16701:                     my $newvalue;
16702:                     if ($key eq 'run') {
16703:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16704:                     } elsif ($key eq 'lastactive') {
16705:                         $newvalue = $offon[$env{'form.lastactive'}];
16706:                         unless ($lastactivedays eq '') {
16707:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16708:                         }
16709:                     } else {
16710:                         $newvalue = $offon[$env{'form.'.$key}];
16711:                     }
16712:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16713:                 }
16714:             }
16715:             $resulttext .= '</ul>';
16716:         } else {
16717:             $resulttext = &mt('No changes made to autoupdates');
16718:         }
16719:     } else {
16720:         $resulttext = '<span class="LC_error">'.
16721: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16722:     }
16723:     return $resulttext;
16724: }
16725: 
16726: sub modify_autocreate {
16727:     my ($dom,%domconfig) = @_;
16728:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16729:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16730:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16731:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16732:         }
16733:     }
16734:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16735:                  req => 'Auto-creation of validated requests for official courses',
16736:                  xmldc => 'Identity of course creator of courses from XML files',
16737:                );
16738:     my @types = ('xml','req');
16739:     foreach my $item (@types) {
16740:         $newvals{$item} = $env{'form.autocreate_'.$item};
16741:         $newvals{$item} =~ s/\D//g;
16742:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16743:     }
16744:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16745:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16746:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16747:         $newvals{'xmldc'} = '';
16748:     } 
16749:     %autocreatehash =  (
16750:                         autocreate => { xml => $newvals{'xml'},
16751:                                         req => $newvals{'req'},
16752:                                       }
16753:                        );
16754:     if ($newvals{'xmldc'} ne '') {
16755:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
16756:     }
16757:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
16758:                                              $dom);
16759:     if ($putresult eq 'ok') {
16760:         my @items = @types;
16761:         if ($newvals{'xml'}) {
16762:             push(@items,'xmldc');
16763:         }
16764:         foreach my $item (@items) {
16765:             if (exists($currautocreate{$item})) {
16766:                 if ($currautocreate{$item} ne $newvals{$item}) {
16767:                     $changes{$item} = 1;
16768:                 }
16769:             } elsif ($newvals{$item}) {
16770:                 $changes{$item} = 1;
16771:             }
16772:         }
16773:         if (keys(%changes) > 0) {
16774:             my @offon = ('off','on'); 
16775:             $resulttext = &mt('Changes made:').'<ul>';
16776:             foreach my $item (@types) {
16777:                 if ($changes{$item}) {
16778:                     my $newtxt = $offon[$newvals{$item}];
16779:                     $resulttext .= '<li>'.
16780:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
16781:                                        '<b>','</b>').
16782:                                    '</li>';
16783:                 }
16784:             }
16785:             if ($changes{'xmldc'}) {
16786:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
16787:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
16788:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
16789:             }
16790:             $resulttext .= '</ul>';
16791:         } else {
16792:             $resulttext = &mt('No changes made to auto-creation settings');
16793:         }
16794:     } else {
16795:         $resulttext = '<span class="LC_error">'.
16796:             &mt('An error occurred: [_1]',$putresult).'</span>';
16797:     }
16798:     return $resulttext;
16799: }
16800: 
16801: sub modify_directorysrch {
16802:     my ($dom,$lastactref,%domconfig) = @_;
16803:     my ($resulttext,%changes);
16804:     my %currdirsrch;
16805:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
16806:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
16807:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
16808:         }
16809:     }
16810:     my %title = ( available => 'Institutional directory search available',
16811:                   localonly => 'Other domains can search institution',
16812:                   lcavailable => 'LON-CAPA directory search available',
16813:                   lclocalonly => 'Other domains can search LON-CAPA domain',
16814:                   searchby => 'Search types',
16815:                   searchtypes => 'Search latitude');
16816:     my @offon = ('off','on');
16817:     my @otherdoms = ('Yes','No');
16818: 
16819:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
16820:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
16821:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
16822: 
16823:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16824:     if (keys(%{$usertypes}) == 0) {
16825:         @cansearch = ('default');
16826:     } else {
16827:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
16828:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
16829:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
16830:                     push(@{$changes{'cansearch'}},$type);
16831:                 }
16832:             }
16833:             foreach my $type (@cansearch) {
16834:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
16835:                     push(@{$changes{'cansearch'}},$type);
16836:                 }
16837:             }
16838:         } else {
16839:             push(@{$changes{'cansearch'}},@cansearch);
16840:         }
16841:     }
16842: 
16843:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
16844:         foreach my $by (@{$currdirsrch{'searchby'}}) {
16845:             if (!grep(/^\Q$by\E$/,@searchby)) {
16846:                 push(@{$changes{'searchby'}},$by);
16847:             }
16848:         }
16849:         foreach my $by (@searchby) {
16850:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
16851:                 push(@{$changes{'searchby'}},$by);
16852:             }
16853:         }
16854:     } else {
16855:         push(@{$changes{'searchby'}},@searchby);
16856:     }
16857: 
16858:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
16859:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
16860:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
16861:                 push(@{$changes{'searchtypes'}},$type);
16862:             }
16863:         }
16864:         foreach my $type (@searchtypes) {
16865:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
16866:                 push(@{$changes{'searchtypes'}},$type);
16867:             }
16868:         }
16869:     } else {
16870:         if (exists($currdirsrch{'searchtypes'})) {
16871:             foreach my $type (@searchtypes) {  
16872:                 if ($type ne $currdirsrch{'searchtypes'}) { 
16873:                     push(@{$changes{'searchtypes'}},$type);
16874:                 }
16875:             }
16876:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
16877:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
16878:             }   
16879:         } else {
16880:             push(@{$changes{'searchtypes'}},@searchtypes); 
16881:         }
16882:     }
16883: 
16884:     my %dirsrch_hash =  (
16885:             directorysrch => { available => $env{'form.dirsrch_available'},
16886:                                cansearch => \@cansearch,
16887:                                localonly => $env{'form.dirsrch_instlocalonly'},
16888:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
16889:                                lcavailable => $env{'form.dirsrch_domavailable'},
16890:                                searchby => \@searchby,
16891:                                searchtypes => \@searchtypes,
16892:                              }
16893:             );
16894:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
16895:                                              $dom);
16896:     if ($putresult eq 'ok') {
16897:         if (exists($currdirsrch{'available'})) {
16898:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
16899:                  $changes{'available'} = 1;
16900:              }
16901:         } else {
16902:             if ($env{'form.dirsrch_available'} eq '1') {
16903:                 $changes{'available'} = 1;
16904:             }
16905:         }
16906:         if (exists($currdirsrch{'lcavailable'})) {
16907:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
16908:                 $changes{'lcavailable'} = 1;
16909:             }
16910:         } else {
16911:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
16912:                 $changes{'lcavailable'} = 1;
16913:             }
16914:         }
16915:         if (exists($currdirsrch{'localonly'})) {
16916:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
16917:                 $changes{'localonly'} = 1;
16918:             }
16919:         } else {
16920:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
16921:                 $changes{'localonly'} = 1;
16922:             }
16923:         }
16924:         if (exists($currdirsrch{'lclocalonly'})) {
16925:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
16926:                 $changes{'lclocalonly'} = 1;
16927:             }
16928:         } else {
16929:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
16930:                 $changes{'lclocalonly'} = 1;
16931:             }
16932:         }
16933:         if (keys(%changes) > 0) {
16934:             $resulttext = &mt('Changes made:').'<ul>';
16935:             if ($changes{'available'}) {
16936:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
16937:             }
16938:             if ($changes{'lcavailable'}) {
16939:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
16940:             }
16941:             if ($changes{'localonly'}) {
16942:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
16943:             }
16944:             if ($changes{'lclocalonly'}) {
16945:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
16946:             }
16947:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
16948:                 my $chgtext;
16949:                 if (ref($usertypes) eq 'HASH') {
16950:                     if (keys(%{$usertypes}) > 0) {
16951:                         foreach my $type (@{$types}) {
16952:                             if (grep(/^\Q$type\E$/,@cansearch)) {
16953:                                 $chgtext .= $usertypes->{$type}.'; ';
16954:                             }
16955:                         }
16956:                         if (grep(/^default$/,@cansearch)) {
16957:                             $chgtext .= $othertitle;
16958:                         } else {
16959:                             $chgtext =~ s/\; $//;
16960:                         }
16961:                         $resulttext .=
16962:                             '<li>'.
16963:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
16964:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
16965:                             '</li>';
16966:                     }
16967:                 }
16968:             }
16969:             if (ref($changes{'searchby'}) eq 'ARRAY') {
16970:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
16971:                 my $chgtext;
16972:                 foreach my $type (@{$titleorder}) {
16973:                     if (grep(/^\Q$type\E$/,@searchby)) {
16974:                         if (defined($searchtitles->{$type})) {
16975:                             $chgtext .= $searchtitles->{$type}.'; ';
16976:                         }
16977:                     }
16978:                 }
16979:                 $chgtext =~ s/\; $//;
16980:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
16981:             }
16982:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
16983:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
16984:                 my $chgtext;
16985:                 foreach my $type (@{$srchtypeorder}) {
16986:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
16987:                         if (defined($srchtypes_desc->{$type})) {
16988:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
16989:                         }
16990:                     }
16991:                 }
16992:                 $chgtext =~ s/\; $//;
16993:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
16994:             }
16995:             $resulttext .= '</ul>';
16996:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
16997:             if (ref($lastactref) eq 'HASH') {
16998:                 $lastactref->{'directorysrch'} = 1;
16999:             }
17000:         } else {
17001:             $resulttext = &mt('No changes made to directory search settings');
17002:         }
17003:     } else {
17004:         $resulttext = '<span class="LC_error">'.
17005:                       &mt('An error occurred: [_1]',$putresult).'</span>';
17006:     }
17007:     return $resulttext;
17008: }
17009: 
17010: sub modify_contacts {
17011:     my ($dom,$lastactref,%domconfig) = @_;
17012:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
17013:     if (ref($domconfig{'contacts'}) eq 'HASH') {
17014:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
17015:             $currsetting{$key} = $domconfig{'contacts'}{$key};
17016:         }
17017:     }
17018:     my (%others,%to,%bcc,%includestr,%includeloc);
17019:     my @contacts = ('supportemail','adminemail');
17020:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
17021:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
17022:     my @toggles = ('reporterrors','reportupdates','reportstatus');
17023:     my @lonstatus = ('threshold','sysmail','weights','excluded');
17024:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
17025:     foreach my $type (@mailings) {
17026:         @{$newsetting{$type}} = 
17027:             &Apache::loncommon::get_env_multiple('form.'.$type);
17028:         foreach my $item (@contacts) {
17029:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
17030:                 $contacts_hash{contacts}{$type}{$item} = 1;
17031:             } else {
17032:                 $contacts_hash{contacts}{$type}{$item} = 0;
17033:             }
17034:         }
17035:         $others{$type} = $env{'form.'.$type.'_others'};
17036:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
17037:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17038:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
17039:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
17040:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
17041:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
17042:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
17043:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17044:             }
17045:         }
17046:     }
17047:     foreach my $item (@contacts) {
17048:         $to{$item} = $env{'form.'.$item};
17049:         $contacts_hash{'contacts'}{$item} = $to{$item};
17050:     }
17051:     foreach my $item (@toggles) {
17052:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
17053:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
17054:         }
17055:     }
17056:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
17057:     foreach my $item (@lonstatus) {
17058:         if ($item eq 'excluded') {
17059:             my (%serverhomes,@excluded);
17060:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
17061:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
17062:             if (@possexcluded) {
17063:                 foreach my $id (sort(@possexcluded)) {
17064:                     if ($serverhomes{$id}) {
17065:                         push(@excluded,$id);
17066:                     }
17067:                 }
17068:             }
17069:             if (@excluded) {
17070:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
17071:             }
17072:         } elsif ($item eq 'weights') {
17073:             foreach my $type ('E','W','N','U') {
17074:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
17075:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
17076:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
17077:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
17078:                             $env{'form.error'.$item.'_'.$type};
17079:                     }
17080:                 }
17081:             }
17082:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
17083:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
17084:             if ($env{'form.error'.$item} =~ /^\d+$/) {
17085:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
17086:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
17087:                 }
17088:             }
17089:         }
17090:     }
17091:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
17092:         foreach my $field (@{$fields}) {
17093:             if (ref($possoptions->{$field}) eq 'ARRAY') {
17094:                 my $value = $env{'form.helpform_'.$field};
17095:                 $value =~ s/^\s+|\s+$//g;
17096:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
17097:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
17098:                     if ($field eq 'screenshot') {
17099:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
17100:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
17101:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
17102:                         }
17103:                     }
17104:                 }
17105:             }
17106:         }
17107:     }
17108:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17109:     my (@statuses,%usertypeshash,@overrides);
17110:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
17111:         @statuses = @{$types};
17112:         if (ref($usertypes) eq 'HASH') {
17113:             %usertypeshash = %{$usertypes};
17114:         }
17115:     }
17116:     if (@statuses) {
17117:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
17118:         foreach my $type (@possoverrides) {
17119:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
17120:                 push(@overrides,$type);
17121:             }
17122:         }
17123:         if (@overrides) {
17124:             foreach my $type (@overrides) {
17125:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
17126:                 foreach my $item (@contacts) {
17127:                     if (grep(/^\Q$item\E$/,@standard)) {
17128:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
17129:                         $newsetting{'override_'.$type}{$item} = 1;
17130:                     } else {
17131:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
17132:                         $newsetting{'override_'.$type}{$item} = 0;
17133:                     }
17134:                 }
17135:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
17136:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17137:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
17138:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17139:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
17140:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
17141:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
17142:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17143:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
17144:                 }
17145:             }
17146:         }
17147:     }
17148:     if (keys(%currsetting) > 0) {
17149:         foreach my $item (@contacts) {
17150:             if ($to{$item} ne $currsetting{$item}) {
17151:                 $changes{$item} = 1;
17152:             }
17153:         }
17154:         foreach my $type (@mailings) {
17155:             foreach my $item (@contacts) {
17156:                 if (ref($currsetting{$type}) eq 'HASH') {
17157:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
17158:                         push(@{$changes{$type}},$item);
17159:                     }
17160:                 } else {
17161:                     push(@{$changes{$type}},@{$newsetting{$type}});
17162:                 }
17163:             }
17164:             if ($others{$type} ne $currsetting{$type}{'others'}) {
17165:                 push(@{$changes{$type}},'others');
17166:             }
17167:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17168:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
17169:                     push(@{$changes{$type}},'bcc'); 
17170:                 }
17171:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
17172:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
17173:                     push(@{$changes{$type}},'include');
17174:                 }
17175:             }
17176:         }
17177:         if (ref($fields) eq 'ARRAY') {
17178:             if (ref($currsetting{'helpform'}) eq 'HASH') {
17179:                 foreach my $field (@{$fields}) {
17180:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
17181:                         push(@{$changes{'helpform'}},$field);
17182:                     }
17183:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17184:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
17185:                             push(@{$changes{'helpform'}},'maxsize');
17186:                         }
17187:                     }
17188:                 }
17189:             } else {
17190:                 foreach my $field (@{$fields}) {
17191:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17192:                         push(@{$changes{'helpform'}},$field);
17193:                     }
17194:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17195:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17196:                             push(@{$changes{'helpform'}},'maxsize');
17197:                         }
17198:                     }
17199:                 }
17200:             }
17201:         }
17202:         if (@statuses) {
17203:             if (ref($currsetting{'overrides'}) eq 'HASH') {
17204:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
17205:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
17206:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
17207:                             foreach my $item (@contacts,'bcc','others','include') {
17208:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
17209:                                     push(@{$changes{'overrides'}},$key);
17210:                                     last;
17211:                                 }
17212:                             }
17213:                         } else {
17214:                             push(@{$changes{'overrides'}},$key);
17215:                         }
17216:                     }
17217:                 }
17218:                 foreach my $key (@overrides) {
17219:                     unless (exists($currsetting{'overrides'}{$key})) {
17220:                         push(@{$changes{'overrides'}},$key);
17221:                     }
17222:                 }
17223:             } else {
17224:                 foreach my $key (@overrides) {
17225:                     push(@{$changes{'overrides'}},$key);
17226:                 }
17227:             }
17228:         }
17229:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
17230:             foreach my $key ('excluded','weights','threshold','sysmail') {
17231:                 if ($key eq 'excluded') {
17232:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17233:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
17234:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17235:                             (@{$currsetting{'lonstatus'}{$key}})) {
17236:                             my @diffs =
17237:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
17238:                                                                    $currsetting{'lonstatus'}{$key});
17239:                             if (@diffs) {
17240:                                 push(@{$changes{'lonstatus'}},$key);
17241:                             }
17242:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
17243:                             push(@{$changes{'lonstatus'}},$key);
17244:                         }
17245:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17246:                              (@{$currsetting{'lonstatus'}{$key}})) {
17247:                         push(@{$changes{'lonstatus'}},$key);
17248:                     }
17249:                 } elsif ($key eq 'weights') {
17250:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17251:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
17252:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17253:                             foreach my $type ('E','W','N','U') {
17254:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
17255:                                         $currsetting{'lonstatus'}{$key}{$type}) {
17256:                                     push(@{$changes{'lonstatus'}},$key);
17257:                                     last;
17258:                                 }
17259:                             }
17260:                         } else {
17261:                             foreach my $type ('E','W','N','U') {
17262:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
17263:                                     push(@{$changes{'lonstatus'}},$key);
17264:                                     last;
17265:                                 }
17266:                             }
17267:                         }
17268:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17269:                         foreach my $type ('E','W','N','U') {
17270:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
17271:                                 push(@{$changes{'lonstatus'}},$key);
17272:                                 last;
17273:                             }
17274:                         }
17275:                     }
17276:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
17277:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17278:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17279:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
17280:                                 push(@{$changes{'lonstatus'}},$key);
17281:                             }
17282:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
17283:                             push(@{$changes{'lonstatus'}},$key);
17284:                         }
17285:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17286:                         push(@{$changes{'lonstatus'}},$key);
17287:                     }
17288:                 }
17289:             }
17290:         } else {
17291:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17292:                 foreach my $key ('excluded','weights','threshold','sysmail') {
17293:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17294:                         push(@{$changes{'lonstatus'}},$key);
17295:                     }
17296:                 }
17297:             }
17298:         }
17299:     } else {
17300:         my %default;
17301:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
17302:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
17303:         $default{'errormail'} = 'adminemail';
17304:         $default{'packagesmail'} = 'adminemail';
17305:         $default{'helpdeskmail'} = 'supportemail';
17306:         $default{'otherdomsmail'} = 'supportemail';
17307:         $default{'lonstatusmail'} = 'adminemail';
17308:         $default{'requestsmail'} = 'adminemail';
17309:         $default{'updatesmail'} = 'adminemail';
17310:         $default{'hostipmail'} = 'adminemail';
17311:         foreach my $item (@contacts) {
17312:            if ($to{$item} ne $default{$item}) {
17313:                $changes{$item} = 1;
17314:            }
17315:         }
17316:         foreach my $type (@mailings) {
17317:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
17318:                 push(@{$changes{$type}},@{$newsetting{$type}});
17319:             }
17320:             if ($others{$type} ne '') {
17321:                 push(@{$changes{$type}},'others');
17322:             }
17323:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17324:                 if ($bcc{$type} ne '') {
17325:                     push(@{$changes{$type}},'bcc');
17326:                 }
17327:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
17328:                     push(@{$changes{$type}},'include');
17329:                 }
17330:             }
17331:         }
17332:         if (ref($fields) eq 'ARRAY') {
17333:             foreach my $field (@{$fields}) {
17334:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17335:                     push(@{$changes{'helpform'}},$field);
17336:                 }
17337:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17338:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17339:                         push(@{$changes{'helpform'}},'maxsize');
17340:                     }
17341:                 }
17342:             }
17343:         }
17344:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17345:             foreach my $key ('excluded','weights','threshold','sysmail') {
17346:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17347:                     push(@{$changes{'lonstatus'}},$key);
17348:                 }
17349:             }
17350:         }
17351:     }
17352:     foreach my $item (@toggles) {
17353:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
17354:             $changes{$item} = 1;
17355:         } elsif ((!$env{'form.'.$item}) &&
17356:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
17357:             $changes{$item} = 1;
17358:         }
17359:     }
17360:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
17361:                                              $dom);
17362:     if ($putresult eq 'ok') {
17363:         if (keys(%changes) > 0) {
17364:             &Apache::loncommon::devalidate_domconfig_cache($dom);
17365:             if (ref($lastactref) eq 'HASH') {
17366:                 $lastactref->{'domainconfig'} = 1;
17367:             }
17368:             my ($titles,$short_titles)  = &contact_titles();
17369:             $resulttext = &mt('Changes made:').'<ul>';
17370:             foreach my $item (@contacts) {
17371:                 if ($changes{$item}) {
17372:                     $resulttext .= '<li>'.$titles->{$item}.
17373:                                     &mt(' set to: ').
17374:                                     '<span class="LC_cusr_emph">'.
17375:                                     $to{$item}.'</span></li>';
17376:                 }
17377:             }
17378:             foreach my $type (@mailings) {
17379:                 if (ref($changes{$type}) eq 'ARRAY') {
17380:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17381:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
17382:                     } else {
17383:                         $resulttext .= '<li>'.$titles->{$type}.': ';
17384:                     }
17385:                     my @text;
17386:                     foreach my $item (@{$newsetting{$type}}) {
17387:                         push(@text,$short_titles->{$item});
17388:                     }
17389:                     if ($others{$type} ne '') {
17390:                         push(@text,$others{$type});
17391:                     }
17392:                     if (@text) {
17393:                         $resulttext .= '<span class="LC_cusr_emph">'.
17394:                                        join(', ',@text).'</span>';
17395:                     }
17396:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17397:                         if ($bcc{$type} ne '') {
17398:                             my $bcctext;
17399:                             if (@text) {
17400:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
17401:                             } else {
17402:                                 $bcctext = '(Bcc)';
17403:                             }
17404:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
17405:                         } elsif (!@text) {
17406:                             $resulttext .= &mt('No one');
17407:                         }
17408:                         if ($includestr{$type} ne '') {
17409:                             if ($includeloc{$type} eq 'b') {
17410:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
17411:                             } elsif ($includeloc{$type} eq 's') {
17412:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
17413:                             }
17414:                         }
17415:                     } elsif (!@text) {
17416:                         $resulttext .= &mt('No recipients');
17417:                     }
17418:                     $resulttext .= '</li>';
17419:                 }
17420:             }
17421:             if (ref($changes{'overrides'}) eq 'ARRAY') {
17422:                 my @deletions;
17423:                 foreach my $type (@{$changes{'overrides'}}) {
17424:                     if ($usertypeshash{$type}) {
17425:                         if (grep(/^\Q$type\E/,@overrides)) {
17426:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
17427:                                                       $usertypeshash{$type}).'<ul><li>';
17428:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
17429:                                 my @text;
17430:                                 foreach my $item (@contacts) {
17431:                                     if ($newsetting{'override_'.$type}{$item}) {
17432:                                         push(@text,$short_titles->{$item});
17433:                                     }
17434:                                 }
17435:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
17436:                                     push(@text,$newsetting{'override_'.$type}{'others'});
17437:                                 }
17438: 
17439:                                 if (@text) {
17440:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
17441:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
17442:                                 }
17443:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
17444:                                     my $bcctext;
17445:                                     if (@text) {
17446:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
17447:                                     } else {
17448:                                         $bcctext = '(Bcc)';
17449:                                     }
17450:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
17451:                                 } elsif (!@text) {
17452:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
17453:                                 }
17454:                                 $resulttext .= '</li>';
17455:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
17456:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
17457:                                     if ($loc eq 'b') {
17458:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
17459:                                     } elsif ($loc eq 's') {
17460:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
17461:                                     }
17462:                                 }
17463:                             }
17464:                             $resulttext .= '</li></ul></li>';
17465:                         } else {
17466:                             push(@deletions,$usertypeshash{$type});
17467:                         }
17468:                     }
17469:                 }
17470:                 if (@deletions) {
17471:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
17472:                                               join(', ',@deletions)).'</li>';
17473:                 }
17474:             }
17475:             my @offon = ('off','on');
17476:             my $corelink = &core_link_msu();
17477:             if ($changes{'reporterrors'}) {
17478:                 $resulttext .= '<li>'.
17479:                                &mt('E-mail error reports to [_1] set to "'.
17480:                                    $offon[$env{'form.reporterrors'}].'".',
17481:                                    $corelink).
17482:                                '</li>';
17483:             }
17484:             if ($changes{'reportupdates'}) {
17485:                 $resulttext .= '<li>'.
17486:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
17487:                                     $offon[$env{'form.reportupdates'}].'".',
17488:                                     $corelink).
17489:                                 '</li>';
17490:             }
17491:             if ($changes{'reportstatus'}) {
17492:                 $resulttext .= '<li>'.
17493:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
17494:                                     $offon[$env{'form.reportstatus'}].'".',
17495:                                     $corelink).
17496:                                 '</li>';
17497:             }
17498:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
17499:                 $resulttext .= '<li>'.
17500:                                &mt('Nightly status check e-mail settings').':<ul>';
17501:                 my (%defval,%use_def,%shown);
17502:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17503:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17504:                 $defval{'weights'} =
17505:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17506:                 $defval{'excluded'} = &mt('None');
17507:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17508:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17509:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17510:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17511:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17512:                             } elsif ($item eq 'weights') {
17513:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17514:                                     foreach my $type ('E','W','N','U') {
17515:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17516:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17517:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17518:                                         } else {
17519:                                             $shown{$item} .= $lonstatus_defs->{$type};
17520:                                         }
17521:                                         $shown{$item} .= ', ';
17522:                                     }
17523:                                     $shown{$item} =~ s/, $//;
17524:                                 } else {
17525:                                     $shown{$item} = $defval{$item};
17526:                                 }
17527:                             } elsif ($item eq 'excluded') {
17528:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17529:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17530:                                 } else {
17531:                                     $shown{$item} = $defval{$item};
17532:                                 }
17533:                             }
17534:                         } else {
17535:                             $shown{$item} = $defval{$item};
17536:                         }
17537:                     }
17538:                 } else {
17539:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17540:                         $shown{$item} = $defval{$item};
17541:                     }
17542:                 }
17543:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17544:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17545:                                           $shown{$item}).'</li>';
17546:                 }
17547:                 $resulttext .= '</ul></li>';
17548:             }
17549:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17550:                 my (@optional,@required,@unused,$maxsizechg);
17551:                 foreach my $field (@{$changes{'helpform'}}) {
17552:                     if ($field eq 'maxsize') {
17553:                         $maxsizechg = 1;
17554:                         next;
17555:                     }
17556:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17557:                         push(@optional,$field);
17558:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17559:                         push(@unused,$field);
17560:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17561:                         push(@required,$field);
17562:                     }
17563:                 }
17564:                 if (@optional) {
17565:                     $resulttext .= '<li>'.
17566:                                    &mt('Help form fields changed to "Optional": [_1].',
17567:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17568:                                    '</li>';
17569:                 }
17570:                 if (@required) {
17571:                     $resulttext .= '<li>'.
17572:                                    &mt('Help form fields changed to "Required": [_1].',
17573:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17574:                                    '</li>';
17575:                 }
17576:                 if (@unused) {
17577:                     $resulttext .= '<li>'.
17578:                                    &mt('Help form fields changed to "Not shown": [_1].',
17579:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17580:                                    '</li>';
17581:                 }
17582:                 if ($maxsizechg) {
17583:                     $resulttext .= '<li>'.
17584:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17585:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17586:                                    '</li>';
17587:                 }
17588:             }
17589:             $resulttext .= '</ul>';
17590:         } else {
17591:             $resulttext = &mt('No changes made to contacts and form settings');
17592:         }
17593:     } else {
17594:         $resulttext = '<span class="LC_error">'.
17595:             &mt('An error occurred: [_1].',$putresult).'</span>';
17596:     }
17597:     return $resulttext;
17598: }
17599: 
17600: sub modify_privacy {
17601:     my ($dom,$lastactref,%domconfig) = @_;
17602:     my ($resulttext,%current,%changes);
17603:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17604:         %current = %{$domconfig{'privacy'}};
17605:     }
17606:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17607:     my @items = ('domain','author','course','community');
17608:     my %names = &Apache::lonlocal::texthash (
17609:                    domain => 'Assigned domain role(s)',
17610:                    author => 'Assigned co-author role(s)',
17611:                    course => 'Assigned course role(s)',
17612:                    community => 'Assigned community role(s)',
17613:                 );
17614:     my %roles = &Apache::lonlocal::texthash (
17615:                    domain => 'Domain role',
17616:                    author => 'Co-author role',
17617:                    course => 'Course role',
17618:                    community => 'Community role',
17619:                 );
17620:     my %titles = &Apache::lonlocal::texthash (
17621:                   approval => 'Approval for role in different domain',
17622:                   othdom   => 'User information available in other domain',
17623:                   priv     => 'Information viewable by privileged user in same domain',
17624:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17625:                   instdom  => 'Other domain shares institution/provider',
17626:                   extdom   => 'Other domain has different institution/provider',
17627:                   none     => 'Not allowed',
17628:                   user     => 'User authorizes',
17629:                   domain   => 'Domain Coordinator authorizes',
17630:                   auto     => 'Unrestricted',
17631:                   notify   => 'Notify when role needs authorization',
17632:     );
17633:     my %fieldnames = &Apache::lonlocal::texthash (
17634:                         id => 'Student/Employee ID',
17635:                         permanentemail => 'E-mail address',
17636:                         lastname => 'Last Name',
17637:                         firstname => 'First Name',
17638:                         middlename => 'Middle Name',
17639:                         generation => 'Generation',
17640:     );
17641:     my ($othertitle,$usertypes,$types) =
17642:         &Apache::loncommon::sorted_inst_types($dom);
17643:     my (%by_ip,%by_location,@intdoms,@instdoms);
17644:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17645: 
17646:     my %privacyhash = (
17647:                        'approval' => {
17648:                                        instdom => {},
17649:                                        extdom  => {},
17650:                                      },
17651:                        'othdom'   => {},
17652:                        'priv'     => {},
17653:                        'unpriv'   => {},
17654:                       );
17655:     foreach my $item (@items) {
17656:         if (@instdoms > 1) {
17657:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17658:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17659:             }
17660:             if (ref($current{'approval'}) eq 'HASH') {
17661:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17662:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17663:                         $changes{'approval'} = 1;
17664:                     }
17665:                 }
17666:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17667:                 $changes{'approval'} = 1;
17668:             }
17669:         }
17670:         if (keys(%by_location) > 0) {
17671:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17672:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17673:             }
17674:             if (ref($current{'approval'}) eq 'HASH') {
17675:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17676:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17677:                         $changes{'approval'} = 1;
17678:                     }
17679:                 }
17680:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17681:                 $changes{'approval'} = 1;
17682:             }
17683:         }
17684:         foreach my $status ('priv','unpriv') {
17685:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17686:             my @newvalues;
17687:             foreach my $field (@possibles) {
17688:                 if (grep(/^\Q$field\E$/,@fields)) {
17689:                     $privacyhash{$status}{$item}{$field} = 1;
17690:                     push(@newvalues,$field);
17691:                 }
17692:             }
17693:             @newvalues = sort(@newvalues);
17694:             if (ref($current{$status}) eq 'HASH') {
17695:                 if (ref($current{$status}{$item}) eq 'HASH') {
17696:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17697:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17698:                     if (@diffs > 0) {
17699:                         $changes{$status} = 1;
17700:                     }
17701:                 }
17702:             } else {
17703:                 my @stdfields;
17704:                 foreach my $field (@fields) {
17705:                     if ($field eq 'id') {
17706:                         next if ($status eq 'unpriv');
17707:                         next if (($status eq 'priv') && ($item eq 'community'));
17708:                     }
17709:                     push(@stdfields,$field);
17710:                 }
17711:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17712:                 if (@diffs > 0) {
17713:                     $changes{$status} = 1;
17714:                 }
17715:             }
17716:         }
17717:     }
17718:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17719:         my @statuses;
17720:         if (ref($types) eq 'ARRAY') {
17721:             @statuses = @{$types};
17722:         }
17723:         foreach my $type (@statuses,'default') {
17724:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17725:             my @newvalues;
17726:             foreach my $field (sort(@possfields)) {
17727:                 if (grep(/^\Q$field\E$/,@fields)) {
17728:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17729:                     push(@newvalues,$field);
17730:                 }
17731:             }
17732:             @newvalues = sort(@newvalues);
17733:             if (ref($current{'othdom'}) eq 'HASH') {
17734:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17735:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17736:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17737:                     if (@diffs > 0) {
17738:                         $changes{'othdom'} = 1;
17739:                     }
17740:                 }
17741:             } else {
17742:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17743:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17744:                 if (@diffs > 0) {
17745:                     $changes{'othdom'} = 1;
17746:                 }
17747:             }
17748:         }
17749:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17750:         my %notify;
17751:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
17752:             if (exists($domcoords{$possdc})) {
17753:                 $notify{$possdc} = 1;
17754:             }
17755:         }
17756:         my $notify = join(',',sort(keys(%notify)));
17757:         if ($current{'notify'} ne $notify) {
17758:             $changes{'notify'} = 1;
17759:         }
17760:         $privacyhash{'notify'} = $notify;
17761:     }
17762:     my %confighash = (
17763:                         privacy => \%privacyhash,
17764:                      );
17765:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17766:     if ($putresult eq 'ok') {
17767:         if (keys(%changes) > 0) {
17768:             $resulttext = &mt('Changes made: ').'<ul>';
17769:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
17770:                 if ($changes{$key}) {
17771:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17772:                     if ($key eq 'approval') {
17773:                         if (keys(%{$privacyhash{$key}{instdom}})) {
17774:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
17775:                             foreach my $item (@items) {
17776:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
17777:                             }
17778:                             $resulttext .= '</ul></li>';
17779:                         }
17780:                         if (keys(%{$privacyhash{$key}{extdom}})) {
17781:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
17782:                             foreach my $item (@items) {
17783:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
17784:                             }
17785:                             $resulttext .= '</ul></li>';
17786:                         }
17787:                     } elsif ($key eq 'notify') {
17788:                         if ($privacyhash{$key}) {
17789:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
17790:                                 my ($dcname,$dcdom) = split(/:/,$dc);
17791:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
17792:                             }
17793:                         } else {
17794:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
17795:                         }
17796:                     } elsif ($key eq 'othdom') {
17797:                         my @statuses;
17798:                         if (ref($types) eq 'ARRAY') {
17799:                             @statuses = @{$types};
17800:                         }
17801:                         if (ref($privacyhash{$key}) eq 'HASH') {
17802:                             foreach my $status (@statuses,'default') {
17803:                                 if ($status eq 'default') {
17804:                                     $resulttext .= '<li>'.$othertitle.': ';
17805:                                 } elsif (ref($usertypes) eq 'HASH') {
17806:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
17807:                                 } else {
17808:                                     next;
17809:                                 }
17810:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
17811:                                     if (keys(%{$privacyhash{$key}{$status}})) {
17812:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
17813:                                     } else {
17814:                                         $resulttext .= &mt('none');
17815:                                     }
17816:                                 }
17817:                                 $resulttext .= '</li>';
17818:                             }
17819:                         }
17820:                     } else {
17821:                         foreach my $item (@items) {
17822:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
17823:                                 $resulttext .= '<li>'.$names{$item}.': ';
17824:                                 if (keys(%{$privacyhash{$key}{$item}})) {
17825:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
17826:                                 } else {
17827:                                     $resulttext .= &mt('none');
17828:                                 }
17829:                                 $resulttext .= '</li>';
17830:                             }
17831:                         }
17832:                     }
17833:                     $resulttext .= '</ul></li>';
17834:                 }
17835:             }
17836:             $resulttext .= '</ul>';
17837:             if ($changes{'approval'}) {
17838:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
17839:                 delete($domdefaults{'userapprovals'});
17840:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
17841:                     foreach my $domtype ('instdom','extdom') {
17842:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
17843:                             foreach my $roletype ('domain','author','course','community') {
17844:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
17845:                                     $domdefaults{'userapprovals'} = 1;
17846:                                     last;
17847:                                 }
17848:                             }
17849:                         }
17850:                         last if ($domdefaults{'userapprovals'});               
17851:                     }
17852:                 }
17853:                 my $cachetime = 24*60*60;
17854:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17855:                 if (ref($lastactref) eq 'HASH') {
17856:                     $lastactref->{'domdefaults'} = 1;
17857:                 }
17858:             }
17859:         } else {
17860:             $resulttext = &mt('No changes made to user information settings');
17861:         }
17862:     } else {
17863:         $resulttext = '<span class="LC_error">'.
17864:             &mt('An error occurred: [_1]',$putresult).'</span>';
17865:     }
17866:     return $resulttext;
17867: }
17868: 
17869: sub modify_passwords {
17870:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17871:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
17872:         $updatedefaults,$updateconf);
17873:     my $customfn = 'resetpw.html';
17874:     if (ref($domconfig{'passwords'}) eq 'HASH') {
17875:         %current = %{$domconfig{'passwords'}};
17876:     }
17877:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17878:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17879:     if (ref($types) eq 'ARRAY') {
17880:         @oktypes = @{$types};
17881:     }
17882:     push(@oktypes,'default');
17883: 
17884:     my %titles = &Apache::lonlocal::texthash (
17885:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
17886:         intauth_check  => 'Check bcrypt cost if authenticated',
17887:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
17888:         permanent      => 'Permanent e-mail address',
17889:         critical       => 'Critical notification address',
17890:         notify         => 'Notification address',
17891:         min            => 'Minimum password length',
17892:         max            => 'Maximum password length',
17893:         chars          => 'Required characters',
17894:         expire         => 'Password expiration (days)',
17895:         numsaved       => 'Number of previous passwords to save',
17896:         reset          => 'Resetting Forgotten Password',
17897:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
17898:         rules          => 'Rules for LON-CAPA Passwords',
17899:         crsownerchg    => 'Course Owner Changing Student Passwords',
17900:         username       => 'Username',
17901:         email          => 'E-mail address',
17902:     );
17903: 
17904: #
17905: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
17906: #
17907:     my (%curr_defaults,%save_defaults);
17908:     if (ref($domconfig{'defaults'}) eq 'HASH') {
17909:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
17910:             if ($key =~ /^intauth_(cost|check|switch)$/) {
17911:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
17912:             } else {
17913:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
17914:             }
17915:         }
17916:     }
17917:     my %staticdefaults = (
17918:         'resetlink'      => 2,
17919:         'resetcase'      => \@oktypes,
17920:         'resetprelink'   => 'both',
17921:         'resetemail'     => ['critical','notify','permanent'],
17922:         'intauth_cost'   => 10,
17923:         'intauth_check'  => 0,
17924:         'intauth_switch' => 0,
17925:     );
17926:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17927:     foreach my $type (@oktypes) {
17928:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
17929:     }
17930:     my $linklife = $env{'form.passwords_link'};
17931:     $linklife =~ s/^\s+|\s+$//g;
17932:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
17933:         $newvalues{'resetlink'} = $linklife;
17934:         if ($current{'resetlink'}) {
17935:             if ($current{'resetlink'} ne $linklife) {
17936:                 $changes{'reset'} = 1;
17937:             }
17938:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17939:             if ($staticdefaults{'resetlink'} ne $linklife) {
17940:                 $changes{'reset'} = 1;
17941:             }
17942:         }
17943:     } elsif ($current{'resetlink'}) {
17944:         $changes{'reset'} = 1;
17945:     }
17946:     my @casesens;
17947:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
17948:     foreach my $case (sort(@posscase)) {
17949:         if (grep(/^\Q$case\E$/,@oktypes)) {
17950:             push(@casesens,$case);
17951:         }
17952:     }
17953:     $newvalues{'resetcase'} = \@casesens;
17954:     if (ref($current{'resetcase'}) eq 'ARRAY') {
17955:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
17956:         if (@diffs > 0) {
17957:             $changes{'reset'} = 1;
17958:         }
17959:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17960:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
17961:         if (@diffs > 0) {
17962:             $changes{'reset'} = 1;
17963:         }
17964:     }
17965:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
17966:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
17967:         if (exists($current{'resetprelink'})) {
17968:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
17969:                 $changes{'reset'} = 1;
17970:             }
17971:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17972:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
17973:                 $changes{'reset'} = 1;
17974:             }
17975:         }
17976:     } elsif ($current{'resetprelink'}) {
17977:         $changes{'reset'} = 1;
17978:     }
17979:     foreach my $type (@oktypes) {
17980:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
17981:         my @postlink;
17982:         foreach my $item (sort(@possplink)) {
17983:             if ($item =~ /^(email|username)$/) {
17984:                 push(@postlink,$item);
17985:             }
17986:         }
17987:         $newvalues{'resetpostlink'}{$type} = \@postlink;
17988:         unless ($changes{'reset'}) {
17989:             if (ref($current{'resetpostlink'}) eq 'HASH') {
17990:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
17991:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
17992:                     if (@diffs > 0) {
17993:                         $changes{'reset'} = 1;
17994:                     }
17995:                 } else {
17996:                     $changes{'reset'} = 1;
17997:                 }
17998:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17999:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
18000:                 if (@diffs > 0) {
18001:                     $changes{'reset'} = 1;
18002:                 }
18003:             }
18004:         }
18005:     }
18006:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
18007:     my @resetemail;
18008:     foreach my $item (sort(@possemailsrc)) {
18009:         if ($item =~ /^(permanent|critical|notify)$/) {
18010:             push(@resetemail,$item);
18011:         }
18012:     }
18013:     $newvalues{'resetemail'} = \@resetemail;
18014:     unless ($changes{'reset'}) {
18015:         if (ref($current{'resetemail'}) eq 'ARRAY') {
18016:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
18017:             if (@diffs > 0) {
18018:                 $changes{'reset'} = 1;
18019:             }
18020:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18021:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
18022:             if (@diffs > 0) {
18023:                 $changes{'reset'} = 1;
18024:             }
18025:         }
18026:     }
18027:     if ($env{'form.passwords_stdtext'} == 0) {
18028:         $newvalues{'resetremove'} = 1;
18029:         unless ($current{'resetremove'}) {
18030:             $changes{'reset'} = 1;
18031:         }
18032:     } elsif ($current{'resetremove'}) {
18033:         $changes{'reset'} = 1;
18034:     }
18035:     if ($env{'form.passwords_customfile.filename'} ne '') {
18036:         my $servadm = $r->dir_config('lonAdmEMail');
18037:         my ($configuserok,$author_ok,$switchserver) =
18038:             &config_check($dom,$confname,$servadm);
18039:         my $error;
18040:         if ($configuserok eq 'ok') {
18041:             if ($switchserver) {
18042:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
18043:             } else {
18044:                 if ($author_ok eq 'ok') {
18045:                     my $modified = [];
18046:                     my ($result,$customurl) =
18047:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
18048:                                                                 $confname,'customtext/resetpw','','',$customfn,
18049:                                                                 $modified);
18050:                     if ($result eq 'ok') {
18051:                         $newvalues{'resetcustom'} = $customurl;
18052:                         $changes{'reset'} = 1;
18053:                         &update_modify_urls($r,$modified);
18054:                     } else {
18055:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
18056:                     }
18057:                 } else {
18058:                     $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);
18059:                 }
18060:             }
18061:         } else {
18062:             $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);
18063:         }
18064:         if ($error) {
18065:             &Apache::lonnet::logthis($error);
18066:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18067:         }
18068:     } elsif ($current{'resetcustom'}) {
18069:         if ($env{'form.passwords_custom_del'}) {
18070:             $changes{'reset'} = 1;
18071:         } else {
18072:             $newvalues{'resetcustom'} = $current{'resetcustom'};
18073:         }
18074:     }
18075:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
18076:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
18077:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
18078:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
18079:             $changes{'intauth'} = 1;
18080:         }
18081:     } else {
18082:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
18083:     }
18084:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
18085:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
18086:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
18087:             $changes{'intauth'} = 1;
18088:         }
18089:     } else {
18090:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18091:     }
18092:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
18093:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
18094:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
18095:             $changes{'intauth'} = 1;
18096:         }
18097:     } else {
18098:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18099:     }
18100:     foreach my $item ('cost','check','switch') {
18101:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
18102:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
18103:             $updatedefaults = 1;
18104:         }
18105:     }
18106:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
18107:     my %crsownerchg = (
18108:                         by => [],
18109:                         for => [],
18110:                       );
18111:     foreach my $item ('by','for') {
18112:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
18113:         foreach my $type (sort(@posstypes)) {
18114:             if (grep(/^\Q$type\E$/,@oktypes)) {
18115:                 push(@{$crsownerchg{$item}},$type);
18116:             }
18117:         }
18118:     }
18119:     $newvalues{'crsownerchg'} = \%crsownerchg;
18120:     if (ref($current{'crsownerchg'}) eq 'HASH') {
18121:         foreach my $item ('by','for') {
18122:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
18123:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
18124:                 if (@diffs > 0) {
18125:                     $changes{'crsownerchg'} = 1;
18126:                     last;
18127:                 }
18128:             }
18129:         }
18130:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
18131:         foreach my $item ('by','for') {
18132:             if (@{$crsownerchg{$item}} > 0) {
18133:                 $changes{'crsownerchg'} = 1;
18134:                 last;
18135:             }
18136:         }
18137:     }
18138: 
18139:     my %confighash = (
18140:                         defaults  => \%save_defaults,
18141:                         passwords => \%newvalues,
18142:                      );
18143:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
18144: 
18145:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18146:     if ($putresult eq 'ok') {
18147:         if (keys(%changes) > 0) {
18148:             $resulttext = &mt('Changes made: ').'<ul>';
18149:             foreach my $key ('reset','intauth','rules','crsownerchg') {
18150:                 if ($changes{$key}) {
18151:                     unless ($key eq 'intauth') {
18152:                         $updateconf = 1;
18153:                     }
18154:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18155:                     if ($key eq 'reset') {
18156:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
18157:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
18158:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
18159:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
18160:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
18161:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
18162:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
18163:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
18164:                             }
18165:                         } else {
18166:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
18167:                         }
18168:                         if ($confighash{'passwords'}{'resetlink'}) {
18169:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
18170:                         } else {
18171:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
18172:                                                   &mt('Will default to 2 hours').'</li>';
18173:                         }
18174:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
18175:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
18176:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
18177:                             } else {
18178:                                 my $casesens;
18179:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
18180:                                     if ($type eq 'default') {
18181:                                         $casesens .= $othertitle.', ';
18182:                                     } elsif ($usertypes->{$type} ne '') {
18183:                                         $casesens .= $usertypes->{$type}.', ';
18184:                                     }
18185:                                 }
18186:                                 $casesens =~ s/\Q, \E$//;
18187:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
18188:                             }
18189:                         } else {
18190:                             $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>';
18191:                         }
18192:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
18193:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
18194:                         } else {
18195:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
18196:                         }
18197:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
18198:                             my $output;
18199:                             if (ref($types) eq 'ARRAY') {
18200:                                 foreach my $type (@{$types}) {
18201:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
18202:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
18203:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
18204:                                         } else {
18205:                                             $output .= $usertypes->{$type}.' -- '.
18206:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
18207:                                         }
18208:                                     }
18209:                                 }
18210:                             }
18211:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
18212:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
18213:                                     $output .= $othertitle.' -- '.&mt('none');
18214:                                 } else {
18215:                                     $output .= $othertitle.' -- '.
18216:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
18217:                                 }
18218:                             }
18219:                             if ($output) {
18220:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
18221:                             } else {
18222:                                 $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>';
18223:                             }
18224:                         } else {
18225:                             $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>';
18226:                         }
18227:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
18228:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
18229:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
18230:                             } else {
18231:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18232:                             }
18233:                         } else {
18234:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18235:                         }
18236:                         if ($confighash{'passwords'}{'resetremove'}) {
18237:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
18238:                         } else {
18239:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
18240:                         }
18241:                         if ($confighash{'passwords'}{'resetcustom'}) {
18242:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
18243:                                                                             &mt('custom text'),600,500,undef,undef,
18244:                                                                             undef,undef,'background-color:#ffffff');
18245:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
18246:                         } else {
18247:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
18248:                         }
18249:                     } elsif ($key eq 'intauth') {
18250:                         foreach my $item ('cost','switch','check') {
18251:                             my $value = $save_defaults{$key.'_'.$item};
18252:                             if ($item eq 'switch') {
18253:                                 my %optiondesc = &Apache::lonlocal::texthash (
18254:                                                      0 => 'No',
18255:                                                      1 => 'Yes',
18256:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
18257:                                                  );
18258:                                 if ($value =~ /^(0|1|2)$/) {
18259:                                     $value = $optiondesc{$value};
18260:                                 } else {
18261:                                     $value = &mt('none -- defaults to No');
18262:                                 }
18263:                             } elsif ($item eq 'check') {
18264:                                 my %optiondesc = &Apache::lonlocal::texthash (
18265:                                                      0 => 'No',
18266:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
18267:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
18268:                                                  );
18269:                                 if ($value =~ /^(0|1|2)$/) {
18270:                                     $value = $optiondesc{$value};
18271:                                 } else {
18272:                                     $value = &mt('none -- defaults to No');
18273:                                 }
18274:                             }
18275:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
18276:                         }
18277:                     } elsif ($key eq 'rules') {
18278:                         foreach my $rule ('min','max','expire','numsaved') {
18279:                             if ($confighash{'passwords'}{$rule} eq '') {
18280:                                 if ($rule eq 'min') {
18281:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
18282:                                                    ' '.&mt('Default of [_1] will be used',
18283:                                                            $Apache::lonnet::passwdmin).'</li>';
18284:                                 } else {
18285:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
18286:                                 }
18287:                             } else {
18288:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
18289:                             }
18290:                         }
18291:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
18292:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
18293:                                 my %rulenames = &Apache::lonlocal::texthash(
18294:                                                      uc => 'At least one upper case letter',
18295:                                                      lc => 'At least one lower case letter',
18296:                                                      num => 'At least one number',
18297:                                                      spec => 'At least one non-alphanumeric',
18298:                                                    );
18299:                                 my $needed = '<ul><li>'.
18300:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
18301:                                              '</li></ul>';
18302:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
18303:                             } else {
18304:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18305:                             }
18306:                         } else {
18307:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18308:                         }
18309:                     } elsif ($key eq 'crsownerchg') {
18310:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
18311:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
18312:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
18313:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18314:                             } else {
18315:                                 my %crsownerstr;
18316:                                 foreach my $item ('by','for') {
18317:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
18318:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
18319:                                             if ($type eq 'default') {
18320:                                                 $crsownerstr{$item} .= $othertitle.', ';
18321:                                             } elsif ($usertypes->{$type} ne '') {
18322:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
18323:                                             }
18324:                                         }
18325:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
18326:                                     }
18327:                                 }
18328:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
18329:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
18330:                             }
18331:                         } else {
18332:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18333:                         }
18334:                     }
18335:                     $resulttext .= '</ul></li>';
18336:                 }
18337:             }
18338:             $resulttext .= '</ul>';
18339:         } else {
18340:             $resulttext = &mt('No changes made to password settings');
18341:         }
18342:         my $cachetime = 24*60*60;
18343:         if ($updatedefaults) {
18344:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18345:             if (ref($lastactref) eq 'HASH') {
18346:                 $lastactref->{'domdefaults'} = 1;
18347:             }
18348:         }
18349:         if ($updateconf) {
18350:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
18351:             if (ref($lastactref) eq 'HASH') {
18352:                 $lastactref->{'passwdconf'} = 1;
18353:             }
18354:         }
18355:     } else {
18356:         $resulttext = '<span class="LC_error">'.
18357:             &mt('An error occurred: [_1]',$putresult).'</span>';
18358:     }
18359:     if ($errors) {
18360:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18361:                        $errors.'</ul></p>';
18362:     }
18363:     return $resulttext;
18364: }
18365: 
18366: sub password_rule_changes {
18367:     my ($prefix,$newvalues,$current,$changes) = @_;
18368:     return unless ((ref($newvalues) eq 'HASH') &&
18369:                    (ref($current) eq 'HASH') &&
18370:                    (ref($changes) eq 'HASH'));
18371:     my (@rules,%staticdefaults);
18372:     if ($prefix eq 'passwords') {
18373:         @rules = ('min','max','expire','numsaved');
18374:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
18375:         @rules = ('min','max');
18376:     }
18377:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18378:     foreach my $rule (@rules) {
18379:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
18380:         my $ruleok;
18381:         if ($rule eq 'expire') {
18382:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
18383:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18384:                 $ruleok = 1;
18385:             }
18386:         } elsif ($rule eq 'min') {
18387:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
18388:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
18389:                     $ruleok = 1;
18390:                 }
18391:             }
18392:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
18393:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18394:             $ruleok = 1;
18395:         }
18396:         if ($ruleok) {
18397:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
18398:             if (exists($current->{$rule})) {
18399:                 if ($newvalues->{$rule} ne $current->{$rule}) {
18400:                     $changes->{'rules'} = 1;
18401:                 }
18402:             } elsif ($rule eq 'min') {
18403:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
18404:                     $changes->{'rules'} = 1;
18405:                 }
18406:             } else {
18407:                 $changes->{'rules'} = 1;
18408:             }
18409:         } elsif (exists($current->{$rule})) {
18410:             $changes->{'rules'} = 1;
18411:         }
18412:     }
18413:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
18414:     my @chars;
18415:     foreach my $item (sort(@posschars)) {
18416:         if ($item =~ /^(uc|lc|num|spec)$/) {
18417:             push(@chars,$item);
18418:         }
18419:     }
18420:     $newvalues->{'chars'} = \@chars;
18421:     unless ($changes->{'rules'}) {
18422:         if (ref($current->{'chars'}) eq 'ARRAY') {
18423:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
18424:             if (@diffs > 0) {
18425:                 $changes->{'rules'} = 1;
18426:             }
18427:         } else {
18428:             if (@chars > 0) {
18429:                 $changes->{'rules'} = 1;
18430:             }
18431:         }
18432:     }
18433:     return;
18434: }
18435: 
18436: sub modify_usercreation {
18437:     my ($dom,%domconfig) = @_;
18438:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
18439:     my $warningmsg;
18440:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18441:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18442:             if ($key eq 'cancreate') {
18443:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18444:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18445:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
18446:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18447:                         } else {
18448:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18449:                         }
18450:                     }
18451:                 }
18452:             } elsif ($key eq 'email_rule') {
18453:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18454:             } else {
18455:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18456:             }
18457:         }
18458:     }
18459:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
18460:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
18461:     my @contexts = ('author','course','requestcrs');
18462:     foreach my $item(@contexts) {
18463:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
18464:     }
18465:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18466:         foreach my $item (@contexts) {
18467:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
18468:                 push(@{$changes{'cancreate'}},$item);
18469:             }
18470:         }
18471:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
18472:         foreach my $item (@contexts) {
18473:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
18474:                 if ($cancreate{$item} ne 'any') {
18475:                     push(@{$changes{'cancreate'}},$item);
18476:                 }
18477:             } else {
18478:                 if ($cancreate{$item} ne 'none') {
18479:                     push(@{$changes{'cancreate'}},$item);
18480:                 }
18481:             }
18482:         }
18483:     } else {
18484:         foreach my $item (@contexts)  {
18485:             push(@{$changes{'cancreate'}},$item);
18486:         }
18487:     }
18488: 
18489:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
18490:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
18491:             if (!grep(/^\Q$type\E$/,@username_rule)) {
18492:                 push(@{$changes{'username_rule'}},$type);
18493:             }
18494:         }
18495:         foreach my $type (@username_rule) {
18496:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
18497:                 push(@{$changes{'username_rule'}},$type);
18498:             }
18499:         }
18500:     } else {
18501:         push(@{$changes{'username_rule'}},@username_rule);
18502:     }
18503: 
18504:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18505:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18506:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18507:                 push(@{$changes{'id_rule'}},$type);
18508:             }
18509:         }
18510:         foreach my $type (@id_rule) {
18511:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18512:                 push(@{$changes{'id_rule'}},$type);
18513:             }
18514:         }
18515:     } else {
18516:         push(@{$changes{'id_rule'}},@id_rule);
18517:     }
18518: 
18519:     my @authen_contexts = ('author','course','domain');
18520:     my @authtypes = ('int','krb4','krb5','loc','lti');
18521:     my %authhash;
18522:     foreach my $item (@authen_contexts) {
18523:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18524:         foreach my $auth (@authtypes) {
18525:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18526:                 $authhash{$item}{$auth} = 1;
18527:             } else {
18528:                 $authhash{$item}{$auth} = 0;
18529:             }
18530:         }
18531:     }
18532:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18533:         foreach my $item (@authen_contexts) {
18534:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18535:                 foreach my $auth (@authtypes) {
18536:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18537:                         push(@{$changes{'authtypes'}},$item);
18538:                         last;
18539:                     }
18540:                 }
18541:             }
18542:         }
18543:     } else {
18544:         foreach my $item (@authen_contexts) {
18545:             push(@{$changes{'authtypes'}},$item);
18546:         }
18547:     }
18548: 
18549:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18550:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18551:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18552:     $save_usercreate{'id_rule'} = \@id_rule;
18553:     $save_usercreate{'username_rule'} = \@username_rule,
18554:     $save_usercreate{'authtypes'} = \%authhash;
18555: 
18556:     my %usercreation_hash =  (
18557:         usercreation     => \%save_usercreate,
18558:     );
18559: 
18560:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18561:                                              $dom);
18562: 
18563:     if ($putresult eq 'ok') {
18564:         if (keys(%changes) > 0) {
18565:             $resulttext = &mt('Changes made:').'<ul>';
18566:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18567:                 my %lt = &usercreation_types();
18568:                 foreach my $type (@{$changes{'cancreate'}}) {
18569:                     my $chgtext = $lt{$type}.', ';
18570:                     if ($cancreate{$type} eq 'none') {
18571:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18572:                     } elsif ($cancreate{$type} eq 'any') {
18573:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18574:                     } elsif ($cancreate{$type} eq 'official') {
18575:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18576:                     } elsif ($cancreate{$type} eq 'unofficial') {
18577:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18578:                     }
18579:                     $resulttext .= '<li>'.$chgtext.'</li>';
18580:                 }
18581:             }
18582:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18583:                 my ($rules,$ruleorder) = 
18584:                     &Apache::lonnet::inst_userrules($dom,'username');
18585:                 my $chgtext = '<ul>';
18586:                 foreach my $type (@username_rule) {
18587:                     if (ref($rules->{$type}) eq 'HASH') {
18588:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18589:                     }
18590:                 }
18591:                 $chgtext .= '</ul>';
18592:                 if (@username_rule > 0) {
18593:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18594:                 } else {
18595:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18596:                 }
18597:             }
18598:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18599:                 my ($idrules,$idruleorder) = 
18600:                     &Apache::lonnet::inst_userrules($dom,'id');
18601:                 my $chgtext = '<ul>';
18602:                 foreach my $type (@id_rule) {
18603:                     if (ref($idrules->{$type}) eq 'HASH') {
18604:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18605:                     }
18606:                 }
18607:                 $chgtext .= '</ul>';
18608:                 if (@id_rule > 0) {
18609:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18610:                 } else {
18611:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18612:                 }
18613:             }
18614:             my %authname = &authtype_names();
18615:             my %context_title = &context_names();
18616:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18617:                 my $chgtext = '<ul>';
18618:                 foreach my $type (@{$changes{'authtypes'}}) {
18619:                     my @allowed;
18620:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18621:                     foreach my $auth (@authtypes) {
18622:                         if ($authhash{$type}{$auth}) {
18623:                             push(@allowed,$authname{$auth});
18624:                         }
18625:                     }
18626:                     if (@allowed > 0) {
18627:                         $chgtext .= join(', ',@allowed).'</li>';
18628:                     } else {
18629:                         $chgtext .= &mt('none').'</li>';
18630:                     }
18631:                 }
18632:                 $chgtext .= '</ul>';
18633:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18634:                 $resulttext .= '</li>';
18635:             }
18636:             $resulttext .= '</ul>';
18637:         } else {
18638:             $resulttext = &mt('No changes made to user creation settings');
18639:         }
18640:     } else {
18641:         $resulttext = '<span class="LC_error">'.
18642:             &mt('An error occurred: [_1]',$putresult).'</span>';
18643:     }
18644:     if ($warningmsg ne '') {
18645:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18646:     }
18647:     return $resulttext;
18648: }
18649: 
18650: sub modify_selfcreation {
18651:     my ($dom,$lastactref,%domconfig) = @_;
18652:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18653:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18654:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18655:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18656:     if (ref($typesref) eq 'ARRAY') {
18657:         @types = @{$typesref};
18658:     }
18659:     if (ref($usertypesref) eq 'HASH') {
18660:         %usertypes = %{$usertypesref};
18661:     }
18662:     $usertypes{'default'} = $othertitle;
18663: #
18664: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18665: #
18666:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18667:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18668:             if ($key eq 'cancreate') {
18669:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18670:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18671:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18672:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18673:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18674:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18675:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18676:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18677:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18678:                         } else {
18679:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18680:                         }
18681:                     }
18682:                 }
18683:             } elsif ($key eq 'email_rule') {
18684:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18685:             } else {
18686:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18687:             }
18688:         }
18689:     }
18690: #
18691: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18692: #
18693:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18694:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18695:             if ($key eq 'selfcreate') {
18696:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18697:             } else {
18698:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18699:             }
18700:         }
18701:     }
18702: #
18703: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18704: #
18705:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18706:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18707:             if ($key eq 'inststatusguest') {
18708:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18709:             } else {
18710:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
18711:             }
18712:         }
18713:     }
18714: 
18715:     my @contexts = ('selfcreate');
18716:     @{$cancreate{'selfcreate'}} = ();
18717:     %{$cancreate{'emailusername'}} = ();
18718:     if (@types) {
18719:         @{$cancreate{'statustocreate'}} = ();
18720:     }
18721:     %{$cancreate{'selfcreateprocessing'}} = ();
18722:     %{$cancreate{'shibenv'}} = ();
18723:     %{$cancreate{'emailverified'}} = ();
18724:     %{$cancreate{'emailoptions'}} = ();
18725:     %{$cancreate{'emaildomain'}} = ();
18726:     my %selfcreatetypes = (
18727:                              sso   => 'users authenticated by institutional single sign on',
18728:                              login => 'users authenticated by institutional log-in',
18729:                              email => 'users verified by e-mail',
18730:                           );
18731: #
18732: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18733: # is permitted.
18734: #
18735:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18736: 
18737:     my (@statuses,%email_rule);
18738:     foreach my $item ('login','sso','email') {
18739:         if ($item eq 'email') {
18740:             if ($env{'form.cancreate_email'}) {
18741:                 if (@types) {
18742:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18743:                     foreach my $status (@poss_statuses) {
18744:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18745:                             push(@statuses,$status);
18746:                         }
18747:                     }
18748:                     $save_inststatus{'inststatusguest'} = \@statuses;
18749:                 } else {
18750:                     push(@statuses,'default');
18751:                 }
18752:                 if (@statuses) {
18753:                     my %curr_rule;
18754:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
18755:                         foreach my $type (@statuses) {
18756:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
18757:                         }
18758:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
18759:                         foreach my $type (@statuses) {
18760:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
18761:                         }
18762:                     }
18763:                     push(@{$cancreate{'selfcreate'}},'email');
18764:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
18765:                     my %curremaildom;
18766:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
18767:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
18768:                     }
18769:                     foreach my $type (@statuses) {
18770:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
18771:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
18772:                         }
18773:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
18774:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
18775:                         }
18776:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
18777: #
18778: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
18779: #
18780:                             my $chosen = $1;
18781:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
18782:                                 my $emaildom;
18783:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
18784:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
18785:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
18786:                                     if (ref($curremaildom{$type}) eq 'HASH') {
18787:                                         if (exists($curremaildom{$type}{$chosen})) {
18788:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
18789:                                                 push(@{$changes{'cancreate'}},'emaildomain');
18790:                                             }
18791:                                         } elsif ($emaildom ne '') {
18792:                                             push(@{$changes{'cancreate'}},'emaildomain');
18793:                                         }
18794:                                     } elsif ($emaildom ne '') {
18795:                                         push(@{$changes{'cancreate'}},'emaildomain');
18796:                                     }
18797:                                 }
18798:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18799:                             } elsif ($chosen eq 'custom') {
18800:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
18801:                                 $email_rule{$type} = [];
18802:                                 if (ref($emailrules) eq 'HASH') {
18803:                                     foreach my $rule (@possemail_rules) {
18804:                                         if (exists($emailrules->{$rule})) {
18805:                                             push(@{$email_rule{$type}},$rule);
18806:                                         }
18807:                                     }
18808:                                 }
18809:                                 if (@{$email_rule{$type}}) {
18810:                                     $cancreate{'emailoptions'}{$type} = 'custom';
18811:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
18812:                                         if (@{$curr_rule{$type}} > 0) {
18813:                                             foreach my $rule (@{$curr_rule{$type}}) {
18814:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
18815:                                                     push(@{$changes{'email_rule'}},$type);
18816:                                                 }
18817:                                             }
18818:                                         }
18819:                                         foreach my $type (@{$email_rule{$type}}) {
18820:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
18821:                                                 push(@{$changes{'email_rule'}},$type);
18822:                                             }
18823:                                         }
18824:                                     } else {
18825:                                         push(@{$changes{'email_rule'}},$type);
18826:                                     }
18827:                                 }
18828:                             } else {
18829:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18830:                             }
18831:                         }
18832:                     }
18833:                     if (@types) {
18834:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18835:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
18836:                             if (@changed) {
18837:                                 push(@{$changes{'inststatus'}},'inststatusguest');
18838:                             }
18839:                         } else {
18840:                             push(@{$changes{'inststatus'}},'inststatusguest');
18841:                         }
18842:                     }
18843:                 } else {
18844:                     delete($env{'form.cancreate_email'});
18845:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18846:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18847:                             push(@{$changes{'inststatus'}},'inststatusguest');
18848:                         }
18849:                     }
18850:                 }
18851:             } else {
18852:                 $save_inststatus{'inststatusguest'} = [];
18853:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18854:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18855:                         push(@{$changes{'inststatus'}},'inststatusguest');
18856:                     }
18857:                 }
18858:             }
18859:         } else {
18860:             if ($env{'form.cancreate_'.$item}) {
18861:                 push(@{$cancreate{'selfcreate'}},$item);
18862:             }
18863:         }
18864:     }
18865:     my (%userinfo,%savecaptcha);
18866:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
18867: #
18868: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
18869: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
18870: #
18871: 
18872:     if ($env{'form.cancreate_email'}) {
18873:         push(@contexts,'emailusername');
18874:         if (@statuses) {
18875:             foreach my $type (@statuses) {
18876:                 if (ref($infofields) eq 'ARRAY') {
18877:                     foreach my $field (@{$infofields}) {
18878:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
18879:                             $cancreate{'emailusername'}{$type}{$field} = $1;
18880:                         }
18881:                     }
18882:                 }
18883:             }
18884:         }
18885: #
18886: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
18887: # queued requests for self-creation of account verified by e-mail.
18888: #
18889: 
18890:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
18891:         @approvalnotify = sort(@approvalnotify);
18892:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
18893:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18894:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
18895:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
18896:                     push(@{$changes{'cancreate'}},'notify');
18897:                 }
18898:             } else {
18899:                 if ($cancreate{'notify'}{'approval'}) {
18900:                     push(@{$changes{'cancreate'}},'notify');
18901:                 }
18902:             }
18903:         } elsif ($cancreate{'notify'}{'approval'}) {
18904:             push(@{$changes{'cancreate'}},'notify');
18905:         }
18906: 
18907:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
18908:     }
18909: #  
18910: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
18911: # institutional log-in.
18912: #
18913:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
18914:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
18915:                ($domdefaults{'auth_def'} eq 'localauth'))) {
18916:             $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.').' '.
18917:                           &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.');
18918:         }
18919:     }
18920:     my @fields = ('lastname','firstname','middlename','generation',
18921:                   'permanentemail','id');
18922:     my @shibfields = (@fields,'inststatus');
18923:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18924: #
18925: # Where usernames may created for institutional log-in and/or institutional single sign on:
18926: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
18927: # may self-create accounts 
18928: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
18929: # which the user may supply, if institutional data is unavailable.
18930: #
18931:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
18932:         if (@types) {
18933:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
18934:             push(@contexts,'statustocreate');
18935:             foreach my $type (@types) {
18936:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
18937:                 foreach my $field (@fields) {
18938:                     if (grep(/^\Q$field\E$/,@modifiable)) {
18939:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
18940:                     } else {
18941:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
18942:                     }
18943:                 }
18944:             }
18945:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
18946:                 foreach my $type (@types) {
18947:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
18948:                         foreach my $field (@fields) {
18949:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
18950:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
18951:                                 push(@{$changes{'selfcreate'}},$type);
18952:                                 last;
18953:                             }
18954:                         }
18955:                     }
18956:                 }
18957:             } else {
18958:                 foreach my $type (@types) {
18959:                     push(@{$changes{'selfcreate'}},$type);
18960:                 }
18961:             }
18962:         }
18963:         foreach my $field (@shibfields) {
18964:             if ($env{'form.shibenv_'.$field} ne '') {
18965:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
18966:             }
18967:         }
18968:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18969:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
18970:                 foreach my $field (@shibfields) {
18971:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
18972:                         push(@{$changes{'cancreate'}},'shibenv');
18973:                     }
18974:                 }
18975:             } else {
18976:                 foreach my $field (@shibfields) {
18977:                     if ($env{'form.shibenv_'.$field}) {
18978:                         push(@{$changes{'cancreate'}},'shibenv');
18979:                         last;
18980:                     }
18981:                 }
18982:             }
18983:         }
18984:     }
18985:     foreach my $item (@contexts) {
18986:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
18987:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
18988:                 if (ref($cancreate{$item}) eq 'ARRAY') {
18989:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
18990:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18991:                             push(@{$changes{'cancreate'}},$item);
18992:                         }
18993:                     }
18994:                 }
18995:             }
18996:             if (ref($cancreate{$item}) eq 'ARRAY') {
18997:                 foreach my $type (@{$cancreate{$item}}) {
18998:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
18999:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19000:                             push(@{$changes{'cancreate'}},$item);
19001:                         }
19002:                     }
19003:                 }
19004:             }
19005:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
19006:             if (ref($cancreate{$item}) eq 'HASH') {
19007:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
19008:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19009:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
19010:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19011:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19012:                                     push(@{$changes{'cancreate'}},$item);
19013:                                 }
19014:                             }
19015:                         }
19016:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19017:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19018:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19019:                                 push(@{$changes{'cancreate'}},$item);
19020:                             }
19021:                         }
19022:                     }
19023:                 }
19024:                 foreach my $type (keys(%{$cancreate{$item}})) {
19025:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19026:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19027:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19028:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19029:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19030:                                         push(@{$changes{'cancreate'}},$item);
19031:                                     }
19032:                                 }
19033:                             } else {
19034:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19035:                                     push(@{$changes{'cancreate'}},$item);
19036:                                 }
19037:                             }
19038:                         }
19039:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19040:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19041:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19042:                                 push(@{$changes{'cancreate'}},$item);
19043:                             }
19044:                         }
19045:                     }
19046:                 }
19047:             }
19048:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
19049:             if (ref($cancreate{$item}) eq 'ARRAY') {
19050:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
19051:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19052:                         push(@{$changes{'cancreate'}},$item);
19053:                     }
19054:                 }
19055:             }
19056:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19057:             if (ref($cancreate{$item}) eq 'HASH') {
19058:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19059:                     push(@{$changes{'cancreate'}},$item);
19060:                 }
19061:             }
19062:         } elsif ($item eq 'emailusername') {
19063:             if (ref($cancreate{$item}) eq 'HASH') {
19064:                 foreach my $type (keys(%{$cancreate{$item}})) {
19065:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19066:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19067:                             if ($cancreate{$item}{$type}{$field}) {
19068:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19069:                                     push(@{$changes{'cancreate'}},$item);
19070:                                 }
19071:                                 last;
19072:                             }
19073:                         }
19074:                     }
19075:                 }
19076:             }
19077:         }
19078:     }
19079: #
19080: # Populate %save_usercreate hash with updates to self-creation configuration.
19081: #
19082:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
19083:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
19084:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
19085:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
19086:     if (ref($cancreate{'notify'}) eq 'HASH') {
19087:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
19088:     }
19089:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
19090:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
19091:     }
19092:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
19093:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
19094:     }
19095:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
19096:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
19097:     }
19098:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
19099:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
19100:     }
19101:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19102:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
19103:     }
19104:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
19105:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
19106:     }
19107:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
19108:     $save_usercreate{'email_rule'} = \%email_rule;
19109: 
19110:     my %userconfig_hash = (
19111:             usercreation     => \%save_usercreate,
19112:             usermodification => \%save_usermodify,
19113:             inststatus       => \%save_inststatus,
19114:     );
19115: 
19116:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
19117:                                              $dom);
19118: #
19119: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
19120: #
19121:     if ($putresult eq 'ok') {
19122:         if (keys(%changes) > 0) {
19123:             $resulttext = &mt('Changes made:').'<ul>';
19124:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
19125:                 my %lt = &selfcreation_types();
19126:                 foreach my $type (@{$changes{'cancreate'}}) {
19127:                     my $chgtext = '';
19128:                     if ($type eq 'selfcreate') {
19129:                         if (@{$cancreate{$type}} == 0) {
19130:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
19131:                         } else {
19132:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
19133:                                         '<ul>';
19134:                             foreach my $case (@{$cancreate{$type}}) {
19135:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
19136:                             }
19137:                             $chgtext .= '</ul>';
19138:                             if (ref($cancreate{$type}) eq 'ARRAY') {
19139:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
19140:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19141:                                         if (@{$cancreate{'statustocreate'}} == 0) {
19142:                                             $chgtext .= '<span class="LC_warning">'.
19143:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
19144:                                                         '</span><br />';
19145:                                         }
19146:                                     }
19147:                                 }
19148:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
19149:                                     if (!@statuses) {
19150:                                         $chgtext .= '<span class="LC_warning">'.
19151:                                                     &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.").
19152:                                                     '</span><br />';
19153: 
19154:                                     }
19155:                                 }
19156:                             }
19157:                         }
19158:                     } elsif ($type eq 'shibenv') {
19159:                         if (keys(%{$cancreate{$type}}) == 0) {
19160:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
19161:                         } else {
19162:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
19163:                                         '<ul>';
19164:                             foreach my $field (@shibfields) {
19165:                                 next if ($cancreate{$type}{$field} eq '');
19166:                                 if ($field eq 'inststatus') {
19167:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
19168:                                 } else {
19169:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
19170:                                 }
19171:                             }
19172:                             $chgtext .= '</ul>';
19173:                         }
19174:                     } elsif ($type eq 'statustocreate') {
19175:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
19176:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
19177:                             if (@{$cancreate{'selfcreate'}} > 0) {
19178:                                 if (@{$cancreate{'statustocreate'}} == 0) {
19179:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
19180:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
19181:                                         $chgtext .= '<br />'.
19182:                                                     '<span class="LC_warning">'.
19183:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
19184:                                                     '</span>';
19185:                                     }
19186:                                 } elsif (keys(%usertypes) > 0) {
19187:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19188:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
19189:                                     } else {
19190:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
19191:                                     }
19192:                                     $chgtext .= '<ul>';
19193:                                     foreach my $case (@{$cancreate{$type}}) {
19194:                                         if ($case eq 'default') {
19195:                                             $chgtext .= '<li>'.$othertitle.'</li>';
19196:                                         } else {
19197:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
19198:                                         }
19199:                                     }
19200:                                     $chgtext .= '</ul>';
19201:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19202:                                         $chgtext .= '<span class="LC_warning">'.
19203:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
19204:                                                     '</span>';
19205:                                     }
19206:                                 }
19207:                             } else {
19208:                                 if (@{$cancreate{$type}} == 0) {
19209:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
19210:                                 } else {
19211:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
19212:                                 }
19213:                             }
19214:                             $chgtext .= '<br />';
19215:                         }
19216:                     } elsif ($type eq 'selfcreateprocessing') {
19217:                         my %choices = &Apache::lonlocal::texthash (
19218:                                                                     automatic => 'Automatic approval',
19219:                                                                     approval  => 'Queued for approval',
19220:                                                                   );
19221:                         if (@types) {
19222:                             if (@statuses) {
19223:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
19224:                                             '<ul>';
19225:                                 foreach my $status (@statuses) {
19226:                                     if ($status eq 'default') {
19227:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19228:                                     } else {
19229:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19230:                                     }
19231:                                 }
19232:                                 $chgtext .= '</ul>';
19233:                             }
19234:                         } else {
19235:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
19236:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
19237:                         }
19238:                     } elsif ($type eq 'emailverified') {
19239:                         my %options = &Apache::lonlocal::texthash (
19240:                                                                     all   => 'Same as e-mail',
19241:                                                                     first => 'Omit @domain',
19242:                                                                     free  => 'Free to choose',
19243:                                                                   );
19244:                         if (@types) {
19245:                             if (@statuses) {
19246:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
19247:                                             '<ul>';
19248:                                 foreach my $status (@statuses) {
19249:                                     if ($status eq 'default') {
19250:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19251:                                     } else {
19252:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19253:                                     }
19254:                                 }
19255:                                 $chgtext .= '</ul>';
19256:                             }
19257:                         } else {
19258:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
19259:                                             $options{$cancreate{'emailverified'}{'default'}});
19260:                         }
19261:                     } elsif ($type eq 'emailoptions') {
19262:                         my %options = &Apache::lonlocal::texthash (
19263:                                                                     any     => 'Any e-mail',
19264:                                                                     inst    => 'Institutional only',
19265:                                                                     noninst => 'Non-institutional only',
19266:                                                                     custom  => 'Custom restrictions',
19267:                                                                   );
19268:                         if (@types) {
19269:                             if (@statuses) {
19270:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
19271:                                             '<ul>';
19272:                                 foreach my $status (@statuses) {
19273:                                     if ($type eq 'default') {
19274:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19275:                                     } else {
19276:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19277:                                     }
19278:                                 }
19279:                                 $chgtext .= '</ul>';
19280:                             }
19281:                         } else {
19282:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
19283:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
19284:                             } else {
19285:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
19286:                                                 $options{$cancreate{'emailoptions'}{'default'}});
19287:                             }
19288:                         }
19289:                     } elsif ($type eq 'emaildomain') {
19290:                         my $output;
19291:                         if (@statuses) {
19292:                             foreach my $type (@statuses) {
19293:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
19294:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
19295:                                         if ($type eq 'default') {
19296:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19297:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19298:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19299:                                             } else {
19300:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
19301:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19302:                                             }
19303:                                         } else {
19304:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19305:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19306:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19307:                                             } else {
19308:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
19309:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19310:                                             }
19311:                                         }
19312:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
19313:                                         if ($type eq 'default') {
19314:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19315:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19316:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19317:                                             } else {
19318:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
19319:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19320:                                             }
19321:                                         } else {
19322:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19323:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19324:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19325:                                             } else {
19326:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
19327:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19328:                                             }
19329:                                         }
19330:                                     }
19331:                                 }
19332:                             }
19333:                         }
19334:                         if ($output ne '') {
19335:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
19336:                                         '<ul>'.$output.'</ul>';
19337:                         }
19338:                     } elsif ($type eq 'captcha') {
19339:                         if ($savecaptcha{$type} eq 'notused') {
19340:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
19341:                         } else {
19342:                             my %captchas = &captcha_phrases();
19343:                             if ($captchas{$savecaptcha{$type}}) {
19344:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
19345:                             } else {
19346:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
19347:                             }
19348:                         }
19349:                     } elsif ($type eq 'recaptchakeys') {
19350:                         my ($privkey,$pubkey);
19351:                         if (ref($savecaptcha{$type}) eq 'HASH') {
19352:                             $pubkey = $savecaptcha{$type}{'public'};
19353:                             $privkey = $savecaptcha{$type}{'private'};
19354:                         }
19355:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
19356:                         if (!$pubkey) {
19357:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
19358:                         } else {
19359:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
19360:                         }
19361:                         if (!$privkey) {
19362:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
19363:                         } else {
19364:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
19365:                         }
19366:                         $chgtext .= '</ul>';
19367:                     } elsif ($type eq 'recaptchaversion') {
19368:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
19369:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
19370:                         }
19371:                     } elsif ($type eq 'emailusername') {
19372:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
19373:                             if (@statuses) {
19374:                                 foreach my $type (@statuses) {
19375:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
19376:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
19377:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
19378:                                                     '<ul>';
19379:                                             foreach my $field (@{$infofields}) {
19380:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
19381:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
19382:                                                 }
19383:                                             }
19384:                                             $chgtext .= '</ul>';
19385:                                         } else {
19386:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19387:                                         }
19388:                                     } else {
19389:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19390:                                     }
19391:                                 }
19392:                             }
19393:                         }
19394:                     } elsif ($type eq 'notify') {
19395:                         my $numapprove = 0;
19396:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
19397:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
19398:                                 if ($cancreate{'notify'}{'approval'}) {
19399:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
19400:                                     $numapprove ++;
19401:                                 }
19402:                             }
19403:                         }
19404:                         unless ($numapprove) {
19405:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
19406:                         }
19407:                     }
19408:                     if ($chgtext) {
19409:                         $resulttext .= '<li>'.$chgtext.'</li>';
19410:                     }
19411:                 }
19412:             }
19413:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
19414:                 my ($emailrules,$emailruleorder) =
19415:                     &Apache::lonnet::inst_userrules($dom,'email');
19416:                 foreach my $type (@{$changes{'email_rule'}}) {
19417:                     if (ref($email_rule{$type}) eq 'ARRAY') {
19418:                         my $chgtext = '<ul>';
19419:                         foreach my $rule (@{$email_rule{$type}}) {
19420:                             if (ref($emailrules->{$rule}) eq 'HASH') {
19421:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
19422:                             }
19423:                         }
19424:                         $chgtext .= '</ul>';
19425:                         my $typename;
19426:                         if (@types) {
19427:                             if ($type eq 'default') {
19428:                                 $typename = $othertitle;
19429:                             } else {
19430:                                 $typename = $usertypes{$type};
19431:                             }
19432:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
19433:                         }
19434:                         if (@{$email_rule{$type}} > 0) {
19435:                             $resulttext .= '<li>'.
19436:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
19437:                                                $usertypes{$type}).
19438:                                            $chgtext.
19439:                                            '</li>';
19440:                         } else {
19441:                             $resulttext .= '<li>'.
19442:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
19443:                                            '</li>'.
19444:                                            &mt('(Affiliation: [_1])',$typename);
19445:                         }
19446:                     }
19447:                 }
19448:             }
19449:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
19450:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
19451:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
19452:                         my $chgtext = '<ul>';
19453:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
19454:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
19455:                         }
19456:                         $chgtext .= '</ul>';
19457:                         $resulttext .= '<li>'.
19458:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
19459:                                           $chgtext.
19460:                                        '</li>';
19461:                     } else {
19462:                         $resulttext .= '<li>'.
19463:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
19464:                                        '</li>';
19465:                     }
19466:                 }
19467:             }
19468:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
19469:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
19470:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19471:                 foreach my $type (@{$changes{'selfcreate'}}) {
19472:                     my $typename = $type;
19473:                     if (keys(%usertypes) > 0) {
19474:                         if ($usertypes{$type} ne '') {
19475:                             $typename = $usertypes{$type};
19476:                         }
19477:                     }
19478:                     my @modifiable;
19479:                     $resulttext .= '<li>'.
19480:                                     &mt('Self-creation of account by users with status: [_1]',
19481:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
19482:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
19483:                     foreach my $field (@fields) {
19484:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
19485:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
19486:                         }
19487:                     }
19488:                     if (@modifiable > 0) {
19489:                         $resulttext .= join(', ',@modifiable);
19490:                     } else {
19491:                         $resulttext .= &mt('none');
19492:                     }
19493:                     $resulttext .= '</li>';
19494:                 }
19495:                 $resulttext .= '</ul></li>';
19496:             }
19497:             $resulttext .= '</ul>';
19498:             my $cachetime = 24*60*60;
19499:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
19500:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19501:             if (ref($lastactref) eq 'HASH') {
19502:                 $lastactref->{'domdefaults'} = 1;
19503:             }
19504:         } else {
19505:             $resulttext = &mt('No changes made to self-creation settings');
19506:         }
19507:     } else {
19508:         $resulttext = '<span class="LC_error">'.
19509:             &mt('An error occurred: [_1]',$putresult).'</span>';
19510:     }
19511:     if ($warningmsg ne '') {
19512:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19513:     }
19514:     return $resulttext;
19515: }
19516: 
19517: sub process_captcha {
19518:     my ($container,$changes,$newsettings,$currsettings) = @_;
19519:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19520:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19521:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19522:         $newsettings->{'captcha'} = 'original';
19523:     }
19524:     my %current;
19525:     if (ref($currsettings) eq 'HASH') {
19526:         %current = %{$currsettings};
19527:     }
19528:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19529:         if ($container eq 'cancreate') {
19530:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19531:                 push(@{$changes->{'cancreate'}},'captcha');
19532:             } elsif (!defined($changes->{'cancreate'})) {
19533:                 $changes->{'cancreate'} = ['captcha'];
19534:             }
19535:         } elsif ($container eq 'passwords') {
19536:             $changes->{'reset'} = 1;
19537:         } else {
19538:             $changes->{'captcha'} = 1;
19539:         }
19540:     }
19541:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19542:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19543:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19544:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19545:         $newpub =~ s/[^\w\-]//g;
19546:         $newpriv =~ s/[^\w\-]//g;
19547:         $newsettings->{'recaptchakeys'} = {
19548:                                              public  => $newpub,
19549:                                              private => $newpriv,
19550:                                           };
19551:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19552:         $newversion =~ s/\D//g;
19553:         if ($newversion ne '2') {
19554:             $newversion = 1;
19555:         }
19556:         $newsettings->{'recaptchaversion'} = $newversion;
19557:     }
19558:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19559:         $currpub = $current{'recaptchakeys'}{'public'};
19560:         $currpriv = $current{'recaptchakeys'}{'private'};
19561:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19562:             $newsettings->{'recaptchakeys'} = {
19563:                                                  public  => '',
19564:                                                  private => '',
19565:                                               }
19566:         }
19567:     }
19568:     if ($current{'captcha'} eq 'recaptcha') {
19569:         $currversion = $current{'recaptchaversion'};
19570:         if ($currversion ne '2') {
19571:             $currversion = 1;
19572:         }
19573:     }
19574:     if ($currversion ne $newversion) {
19575:         if ($container eq 'cancreate') {
19576:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19577:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19578:             } elsif (!defined($changes->{'cancreate'})) {
19579:                 $changes->{'cancreate'} = ['recaptchaversion'];
19580:             }
19581:         } elsif ($container eq 'passwords') {
19582:             $changes->{'reset'} = 1;
19583:         } else {
19584:             $changes->{'recaptchaversion'} = 1;
19585:         }
19586:     }
19587:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19588:         if ($container eq 'cancreate') {
19589:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19590:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19591:             } elsif (!defined($changes->{'cancreate'})) {
19592:                 $changes->{'cancreate'} = ['recaptchakeys'];
19593:             }
19594:         } elsif ($container eq 'passwords') {
19595:             $changes->{'reset'} = 1;
19596:         } else {
19597:             $changes->{'recaptchakeys'} = 1;
19598:         }
19599:     }
19600:     return;
19601: }
19602: 
19603: sub modify_usermodification {
19604:     my ($dom,%domconfig) = @_;
19605:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19606:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19607:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19608:             if ($key eq 'selfcreate') {
19609:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19610:             } else {  
19611:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19612:             }
19613:         }
19614:     }
19615:     my @contexts = ('author','course');
19616:     my %context_title = (
19617:                            author => 'In author context',
19618:                            course => 'In course context',
19619:                         );
19620:     my @fields = ('lastname','firstname','middlename','generation',
19621:                   'permanentemail','id');
19622:     my %roles = (
19623:                   author => ['ca','aa'],
19624:                   course => ['st','ep','ta','in','cr'],
19625:                 );
19626:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19627:     foreach my $context (@contexts) {
19628:         foreach my $role (@{$roles{$context}}) {
19629:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
19630:             foreach my $item (@fields) {
19631:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19632:                     $modifyhash{$context}{$role}{$item} = 1;
19633:                 } else {
19634:                     $modifyhash{$context}{$role}{$item} = 0;
19635:                 }
19636:             }
19637:         }
19638:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19639:             foreach my $role (@{$roles{$context}}) {
19640:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19641:                     foreach my $field (@fields) {
19642:                         if ($modifyhash{$context}{$role}{$field} ne 
19643:                                 $curr_usermodification{$context}{$role}{$field}) {
19644:                             push(@{$changes{$context}},$role);
19645:                             last;
19646:                         }
19647:                     }
19648:                 }
19649:             }
19650:         } else {
19651:             foreach my $context (@contexts) {
19652:                 foreach my $role (@{$roles{$context}}) {
19653:                     push(@{$changes{$context}},$role);
19654:                 }
19655:             }
19656:         }
19657:     }
19658:     my %usermodification_hash =  (
19659:                                    usermodification => \%modifyhash,
19660:                                  );
19661:     my $putresult = &Apache::lonnet::put_dom('configuration',
19662:                                              \%usermodification_hash,$dom);
19663:     if ($putresult eq 'ok') {
19664:         if (keys(%changes) > 0) {
19665:             $resulttext = &mt('Changes made: ').'<ul>';
19666:             foreach my $context (@contexts) {
19667:                 if (ref($changes{$context}) eq 'ARRAY') {
19668:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19669:                     if (ref($changes{$context}) eq 'ARRAY') {
19670:                         foreach my $role (@{$changes{$context}}) {
19671:                             my $rolename;
19672:                             if ($role eq 'cr') {
19673:                                 $rolename = &mt('Custom');
19674:                             } else {
19675:                                 $rolename = &Apache::lonnet::plaintext($role);
19676:                             }
19677:                             my @modifiable;
19678:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19679:                             foreach my $field (@fields) {
19680:                                 if ($modifyhash{$context}{$role}{$field}) {
19681:                                     push(@modifiable,$fieldtitles{$field});
19682:                                 }
19683:                             }
19684:                             if (@modifiable > 0) {
19685:                                 $resulttext .= join(', ',@modifiable);
19686:                             } else {
19687:                                 $resulttext .= &mt('none'); 
19688:                             }
19689:                             $resulttext .= '</li>';
19690:                         }
19691:                         $resulttext .= '</ul></li>';
19692:                     }
19693:                 }
19694:             }
19695:             $resulttext .= '</ul>';
19696:         } else {
19697:             $resulttext = &mt('No changes made to user modification settings');
19698:         }
19699:     } else {
19700:         $resulttext = '<span class="LC_error">'.
19701:             &mt('An error occurred: [_1]',$putresult).'</span>';
19702:     }
19703:     return $resulttext;
19704: }
19705: 
19706: sub modify_defaults {
19707:     my ($dom,$lastactref,%domconfig) = @_;
19708:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
19709:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19710:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
19711:                  'portal_def');
19712:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
19713:     foreach my $item (@items) {
19714:         $newvalues{$item} = $env{'form.'.$item};
19715:         if ($item eq 'auth_def') {
19716:             if ($newvalues{$item} ne '') {
19717:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
19718:                     push(@errors,$item);
19719:                 }
19720:             }
19721:         } elsif ($item eq 'lang_def') {
19722:             if ($newvalues{$item} ne '') {
19723:                 if ($newvalues{$item} =~ /^(\w+)/) {
19724:                     my $langcode = $1;
19725:                     if ($langcode ne 'x_chef') {
19726:                         if (code2language($langcode) eq '') {
19727:                             push(@errors,$item);
19728:                         }
19729:                     }
19730:                 } else {
19731:                     push(@errors,$item);
19732:                 }
19733:             }
19734:         } elsif ($item eq 'timezone_def') {
19735:             if ($newvalues{$item} ne '') {
19736:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19737:                     push(@errors,$item);   
19738:                 }
19739:             }
19740:         } elsif ($item eq 'datelocale_def') {
19741:             if ($newvalues{$item} ne '') {
19742:                 my @datelocale_ids = DateTime::Locale->ids();
19743:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19744:                     push(@errors,$item);
19745:                 }
19746:             }
19747:         } elsif ($item eq 'portal_def') {
19748:             if ($newvalues{$item} ne '') {
19749:                 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])\/?$/) {
19750:                     foreach my $field ('email','web') {
19751:                         if ($env{'form.'.$item.'_'.$field}) {
19752:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
19753:                         }
19754:                     }
19755:                 } else {
19756:                     push(@errors,$item);
19757:                 }
19758:             }
19759:         }
19760:         if (grep(/^\Q$item\E$/,@errors)) {
19761:             $newvalues{$item} = $domdefaults{$item};
19762:             if ($item eq 'portal_def') {
19763:                 if ($domdefaults{$item}) {
19764:                     foreach my $field ('email','web') {
19765:                         if (exists($domdefaults{$item.'_'.$field})) {
19766:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
19767:                         }
19768:                     }
19769:                 }
19770:             }
19771:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
19772:             $changes{$item} = 1;
19773:         }
19774:         if ($item eq 'portal_def') {
19775:             unless (grep(/^\Q$item\E$/,@errors)) {
19776:                 if ($newvalues{$item} eq '') {
19777:                     foreach my $field ('email','web') {
19778:                         if (exists($domdefaults{$item.'_'.$field})) {
19779:                             delete($domdefaults{$item.'_'.$field});
19780:                         }
19781:                     }
19782:                 } else {
19783:                     unless ($changes{$item}) {
19784:                         foreach my $field ('email','web') {
19785:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
19786:                                 $changes{$item} = 1;
19787:                                 last;
19788:                             }
19789:                         }
19790:                     }
19791:                     foreach my $field ('email','web') {
19792:                         if ($newvalues{$item.'_'.$field}) {
19793:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
19794:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
19795:                             delete($domdefaults{$item.'_'.$field});
19796:                         }
19797:                     }
19798:                 }
19799:             }
19800:         }
19801:         $domdefaults{$item} = $newvalues{$item};
19802:     }
19803:     my %staticdefaults = (
19804:                            'intauth_cost'   => 10,
19805:                            'intauth_check'  => 0,
19806:                            'intauth_switch' => 0,
19807:                          );
19808:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
19809:         if (exists($domdefaults{$item})) {
19810:             $newvalues{$item} = $domdefaults{$item};
19811:         } else {
19812:             $newvalues{$item} = $staticdefaults{$item};
19813:         }
19814:     }
19815:     my ($unamemaprules,$ruleorder);
19816:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
19817:     if (@possunamemaprules) {
19818:         ($unamemaprules,$ruleorder) =
19819:             &Apache::lonnet::inst_userrules($dom,'unamemap');
19820:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
19821:             if (@{$ruleorder} > 0) {
19822:                 my %possrules;
19823:                 map { $possrules{$_} = 1; } @possunamemaprules;
19824:                 foreach my $rule (@{$ruleorder}) {
19825:                     if ($possrules{$rule}) {
19826:                         push(@{$newvalues{'unamemap_rule'}},$rule);
19827:                     }
19828:                 }
19829:             }
19830:         }
19831:     }
19832:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
19833:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19834:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
19835:                                                                $newvalues{'unamemap_rule'});
19836:             if (@rulediffs) {
19837:                 $changes{'unamemap_rule'} = 1;
19838:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19839:             }
19840:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
19841:             $changes{'unamemap_rule'} = 1;
19842:             delete($domdefaults{'unamemap_rule'});
19843:         }
19844:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19845:         if (@{$newvalues{'unamemap_rule'}} > 0) {
19846:             $changes{'unamemap_rule'} = 1;
19847:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19848:         }
19849:     }
19850:     my %defaults_hash = (
19851:                          defaults => \%newvalues,
19852:                         );
19853:     my $title = &defaults_titles();
19854: 
19855:     my $currinststatus;
19856:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
19857:         $currinststatus = $domconfig{'inststatus'};
19858:     } else {
19859:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19860:         $currinststatus = {
19861:                              inststatustypes => $usertypes,
19862:                              inststatusorder => $types,
19863:                              inststatusguest => [],
19864:                           };
19865:     }
19866:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
19867:     my @allpos;
19868:     my %alltypes;
19869:     my @inststatusguest;
19870:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
19871:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
19872:             unless (grep(/^\Q$type\E$/,@todelete)) {
19873:                 push(@inststatusguest,$type);
19874:             }
19875:         }
19876:     }
19877:     my ($currtitles,$currorder);
19878:     if (ref($currinststatus) eq 'HASH') {
19879:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
19880:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
19881:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
19882:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
19883:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
19884:                     }
19885:                 }
19886:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
19887:                     my $position = $env{'form.inststatus_pos_'.$type};
19888:                     $position =~ s/\D+//g;
19889:                     $allpos[$position] = $type;
19890:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
19891:                     $alltypes{$type} =~ s/`//g;
19892:                 }
19893:             }
19894:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
19895:             $currtitles =~ s/,$//;
19896:         }
19897:     }
19898:     if ($env{'form.addinststatus'}) {
19899:         my $newtype = $env{'form.addinststatus'};
19900:         $newtype =~ s/\W//g;
19901:         unless (exists($alltypes{$newtype})) {
19902:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
19903:             $alltypes{$newtype} =~ s/`//g; 
19904:             my $position = $env{'form.addinststatus_pos'};
19905:             $position =~ s/\D+//g;
19906:             if ($position ne '') {
19907:                 $allpos[$position] = $newtype;
19908:             }
19909:         }
19910:     }
19911:     my @orderedstatus;
19912:     foreach my $type (@allpos) {
19913:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
19914:             push(@orderedstatus,$type);
19915:         }
19916:     }
19917:     foreach my $type (keys(%alltypes)) {
19918:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
19919:             delete($alltypes{$type});
19920:         }
19921:     }
19922:     $defaults_hash{'inststatus'} = {
19923:                                      inststatustypes => \%alltypes,
19924:                                      inststatusorder => \@orderedstatus,
19925:                                      inststatusguest => \@inststatusguest,
19926:                                    };
19927:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
19928:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
19929:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
19930:         }
19931:     }
19932:     if ($currorder ne join(',',@orderedstatus)) {
19933:         $changes{'inststatus'}{'inststatusorder'} = 1;
19934:     }
19935:     my $newtitles;
19936:     foreach my $item (@orderedstatus) {
19937:         $newtitles .= $alltypes{$item}.',';
19938:     }
19939:     $newtitles =~ s/,$//;
19940:     if ($currtitles ne $newtitles) {
19941:         $changes{'inststatus'}{'inststatustypes'} = 1;
19942:     }
19943:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
19944:                                              $dom);
19945:     if ($putresult eq 'ok') {
19946:         if (keys(%changes) > 0) {
19947:             $resulttext = &mt('Changes made:').'<ul>';
19948:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
19949:             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";
19950:             foreach my $item (sort(keys(%changes))) {
19951:                 if ($item eq 'inststatus') {
19952:                     if (ref($changes{'inststatus'}) eq 'HASH') {
19953:                         if (@orderedstatus) {
19954:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
19955:                             foreach my $type (@orderedstatus) { 
19956:                                 $resulttext .= $alltypes{$type}.', ';
19957:                             }
19958:                             $resulttext =~ s/, $//;
19959:                             $resulttext .= '</li>';
19960:                         } else {
19961:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
19962:                         }
19963:                     }
19964:                 } elsif ($item eq 'unamemap_rule') {
19965:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19966:                         my @rulenames;
19967:                         if (ref($unamemaprules) eq 'HASH') {
19968:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
19969:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
19970:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
19971:                                 }
19972:                             }
19973:                         }
19974:                         if (@rulenames) {
19975:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
19976:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
19977:                                            '</li>';
19978:                         } else {
19979:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
19980:                         }
19981:                     } else {
19982:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
19983:                     }
19984:                 } else {
19985:                     my $value = $env{'form.'.$item};
19986:                     if ($value eq '') {
19987:                         $value = &mt('none');
19988:                     } elsif ($item eq 'auth_def') {
19989:                         my %authnames = &authtype_names();
19990:                         my %shortauth = (
19991:                                           internal   => 'int',
19992:                                           krb4       => 'krb4',
19993:                                           krb5       => 'krb5',
19994:                                           localauth  => 'loc',
19995:                                           lti        => 'lti',
19996:                         );
19997:                         $value = $authnames{$shortauth{$value}};
19998:                     }
19999:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
20000:                     $mailmsgtext .= "$title->{$item} set to $value\n";
20001:                     if ($item eq 'portal_def') {
20002:                         if ($env{'form.'.$item} ne '') {
20003:                             foreach my $field ('email','web') {
20004:                                 $value = $env{'form.'.$item.'_'.$field};
20005:                                 if ($value) {
20006:                                     $value = &mt('Yes');
20007:                                 } else {
20008:                                     $value = &mt('No');
20009:                                 }
20010:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
20011:                             }
20012:                         }
20013:                     }
20014:                 }
20015:             }
20016:             $resulttext .= '</ul>';
20017:             $mailmsgtext .= "\n";
20018:             my $cachetime = 24*60*60;
20019:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20020:             if (ref($lastactref) eq 'HASH') {
20021:                 $lastactref->{'domdefaults'} = 1;
20022:             }
20023:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
20024:                 my $notify = 1;
20025:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
20026:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
20027:                         $notify = 0;
20028:                     }
20029:                 }
20030:                 if ($notify) {
20031:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
20032:                                                "LON-CAPA Domain Settings Change - $dom",
20033:                                                $mailmsgtext);
20034:                 }
20035:             }
20036:         } else {
20037:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
20038:         }
20039:     } else {
20040:         $resulttext = '<span class="LC_error">'.
20041:             &mt('An error occurred: [_1]',$putresult).'</span>';
20042:     }
20043:     if (@errors > 0) {
20044:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
20045:         foreach my $item (@errors) {
20046:             $resulttext .= ' "'.$title->{$item}.'",';
20047:         }
20048:         $resulttext =~ s/,$//;
20049:     }
20050:     return $resulttext;
20051: }
20052: 
20053: sub modify_scantron {
20054:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20055:     my ($resulttext,%confhash,%changes,$errors);
20056:     my $custom = 'custom.tab';
20057:     my $default = 'default.tab';
20058:     my $servadm = $r->dir_config('lonAdmEMail');
20059:     my ($configuserok,$author_ok,$switchserver) =
20060:         &config_check($dom,$confname,$servadm);
20061:     if ($env{'form.scantronformat.filename'} ne '') {
20062:         my $error;
20063:         if ($configuserok eq 'ok') {
20064:             if ($switchserver) {
20065:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
20066:             } else {
20067:                 if ($author_ok eq 'ok') {
20068:                     my $modified = [];
20069:                     my ($result,$scantronurl) =
20070:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
20071:                                                                 $confname,'scantron','','',$custom,
20072:                                                                 $modified);
20073:                     if ($result eq 'ok') {
20074:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
20075:                         $changes{'scantronformat'} = 1;
20076:                         &update_modify_urls($r,$modified);
20077:                     } else {
20078:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
20079:                     }
20080:                 } else {
20081:                     $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);
20082:                 }
20083:             }
20084:         } else {
20085:             $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);
20086:         }
20087:         if ($error) {
20088:             &Apache::lonnet::logthis($error);
20089:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
20090:         }
20091:     }
20092:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20093:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
20094:             if ($env{'form.scantronformat_del'}) {
20095:                 $confhash{'scantron'}{'scantronformat'} = '';
20096:                 $changes{'scantronformat'} = 1;
20097:             } else {
20098:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
20099:             }
20100:         }
20101:     }
20102:     my @options = ('hdr','pad','rem');
20103:     my @fields = &scantroncsv_fields();
20104:     my %titles = &scantronconfig_titles();
20105:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
20106:     my ($newdat,$currdat,%newcol,%currcol);
20107:     if (grep(/^dat$/,@formats)) {
20108:         $confhash{'scantron'}{config}{dat} = 1;
20109:         $newdat = 1;
20110:     } else {
20111:         $newdat = 0;
20112:     }
20113:     if (grep(/^csv$/,@formats)) {
20114:         my %bynum;
20115:         foreach my $field (@fields) {
20116:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
20117:                 my $posscol = $1;
20118:                 if (($posscol < 20) && (!$bynum{$posscol})) {
20119:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
20120:                     $bynum{$posscol} = $field;
20121:                     $newcol{$field} = $posscol;
20122:                 }
20123:             }
20124:         }
20125:         if (keys(%newcol)) {
20126:             foreach my $option (@options) {
20127:                 if ($env{'form.scantroncsv_'.$option}) {
20128:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
20129:                 }
20130:             }
20131:         }
20132:     }
20133:     $currdat = 1;
20134:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20135:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
20136:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
20137:                 $currdat = 0;
20138:             }
20139:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20140:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20141:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
20142:                 }
20143:             }
20144:         }
20145:     }
20146:     if ($currdat != $newdat) {
20147:         $changes{'config'} = 1;
20148:     } else {
20149:         foreach my $field (@fields) {
20150:             if ($currcol{$field} ne '') {
20151:                 if ($currcol{$field} ne $newcol{$field}) {
20152:                     $changes{'config'} = 1;
20153:                     last;
20154:                 }
20155:             } elsif ($newcol{$field} ne '') {
20156:                 $changes{'config'} = 1;
20157:                 last;
20158:             }
20159:         }
20160:     }
20161:     if (keys(%confhash) > 0) {
20162:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
20163:                                                  $dom);
20164:         if ($putresult eq 'ok') {
20165:             if (keys(%changes) > 0) {
20166:                 if (ref($confhash{'scantron'}) eq 'HASH') {
20167:                     $resulttext = &mt('Changes made:').'<ul>';
20168:                     if ($changes{'scantronformat'}) {
20169:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
20170:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
20171:                         } else {
20172:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
20173:                         }
20174:                     }
20175:                     if ($changes{'config'}) {
20176:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
20177:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
20178:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
20179:                             }
20180:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20181:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20182:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
20183:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
20184:                                         foreach my $field (@fields) {
20185:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
20186:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
20187:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
20188:                                             }
20189:                                         }
20190:                                         $resulttext .= '</ul></li>';
20191:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
20192:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
20193:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
20194:                                                 foreach my $option (@options) {
20195:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
20196:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
20197:                                                     }
20198:                                                 }
20199:                                                 $resulttext .= '</ul></li>';
20200:                                             }
20201:                                         }
20202:                                     }
20203:                                 }
20204:                             }
20205:                         } else {
20206:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
20207:                         }
20208:                     }
20209:                     $resulttext .= '</ul>';
20210:                 } else {
20211:                     $resulttext = &mt('Changes made to bubblesheet format file.');
20212:                 }
20213:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
20214:                 if (ref($lastactref) eq 'HASH') {
20215:                     $lastactref->{'domainconfig'} = 1;
20216:                 }
20217:             } else {
20218:                 $resulttext = &mt('No changes made to bubblesheet format settings');
20219:             }
20220:         } else {
20221:             $resulttext = '<span class="LC_error">'.
20222:                 &mt('An error occurred: [_1]',$putresult).'</span>';
20223:         }
20224:     } else {
20225:         $resulttext = &mt('No changes made to bubblesheet format file'); 
20226:     }
20227:     if ($errors) {
20228:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
20229:                        $errors.'</ul></p>';
20230:     }
20231:     return $resulttext;
20232: }
20233: 
20234: sub modify_coursecategories {
20235:     my ($dom,$lastactref,%domconfig) = @_;
20236:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
20237:         $cathash);
20238:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
20239:     my @catitems = ('unauth','auth');
20240:     my @cattypes = ('std','domonly','codesrch','none');
20241:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20242:         $cathash = $domconfig{'coursecategories'}{'cats'};
20243:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
20244:             $changes{'togglecats'} = 1;
20245:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
20246:         }
20247:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
20248:             $changes{'categorize'} = 1;
20249:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
20250:         }
20251:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
20252:             $changes{'togglecatscomm'} = 1;
20253:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
20254:         }
20255:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
20256:             $changes{'categorizecomm'} = 1;
20257:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
20258: 
20259:         }
20260:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
20261:             $changes{'togglecatsplace'} = 1;
20262:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
20263:         }
20264:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
20265:             $changes{'categorizeplace'} = 1;
20266:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
20267:         }
20268:         foreach my $item (@catitems) {
20269:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20270:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
20271:                     $changes{$item} = 1;
20272:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20273:                 }
20274:             }
20275:         }
20276:     } else {
20277:         $changes{'togglecats'} = 1;
20278:         $changes{'categorize'} = 1;
20279:         $changes{'togglecatscomm'} = 1;
20280:         $changes{'categorizecomm'} = 1;
20281:         $changes{'togglecatsplace'} = 1;
20282:         $changes{'categorizeplace'} = 1;
20283:         $domconfig{'coursecategories'} = {
20284:                                              togglecats => $env{'form.togglecats'},
20285:                                              categorize => $env{'form.categorize'},
20286:                                              togglecatscomm => $env{'form.togglecatscomm'},
20287:                                              categorizecomm => $env{'form.categorizecomm'},
20288:                                              togglecatsplace => $env{'form.togglecatsplace'},
20289:                                              categorizeplace => $env{'form.categorizeplace'},
20290:                                          };
20291:         foreach my $item (@catitems) {
20292:             if ($env{'form.coursecat_'.$item} ne 'std') {
20293:                 $changes{$item} = 1;
20294:             }
20295:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20296:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20297:             }
20298:         }
20299:     }
20300:     if (ref($cathash) eq 'HASH') {
20301:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
20302:             push (@deletecategory,'instcode::0');
20303:         }
20304:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
20305:             push(@deletecategory,'communities::0');
20306:         }
20307:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
20308:             push(@deletecategory,'placement::0');
20309:         }
20310:     }
20311:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
20312:     if (ref($cathash) eq 'HASH') {
20313:         if (@deletecategory > 0) {
20314:             #FIXME Need to remove category from all courses using a deleted category 
20315:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
20316:             foreach my $item (@deletecategory) {
20317:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
20318:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
20319:                     $deletions{$item} = 1;
20320:                     &recurse_cat_deletes($item,$cathash,\%deletions);
20321:                 }
20322:             }
20323:         }
20324:         foreach my $item (keys(%{$cathash})) {
20325:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20326:             if ($cathash->{$item} ne $env{'form.'.$item}) {
20327:                 $reorderings{$item} = 1;
20328:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
20329:             }
20330:             if ($env{'form.addcategory_name_'.$item} ne '') {
20331:                 my $newcat = $env{'form.addcategory_name_'.$item};
20332:                 my $newdepth = $depth+1;
20333:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20334:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
20335:                 $adds{$newitem} = 1; 
20336:             }
20337:             if ($env{'form.subcat_'.$item} ne '') {
20338:                 my $newcat = $env{'form.subcat_'.$item};
20339:                 my $newdepth = $depth+1;
20340:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20341:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
20342:                 $adds{$newitem} = 1;
20343:             }
20344:         }
20345:     }
20346:     if ($env{'form.instcode'} eq '1') {
20347:         if (ref($cathash) eq 'HASH') {
20348:             my $newitem = 'instcode::0';
20349:             if ($cathash->{$newitem} eq '') {  
20350:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20351:                 $adds{$newitem} = 1;
20352:             }
20353:         } else {
20354:             my $newitem = 'instcode::0';
20355:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20356:             $adds{$newitem} = 1;
20357:         }
20358:     }
20359:     if ($env{'form.communities'} eq '1') {
20360:         if (ref($cathash) eq 'HASH') {
20361:             my $newitem = 'communities::0';
20362:             if ($cathash->{$newitem} eq '') {
20363:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20364:                 $adds{$newitem} = 1;
20365:             }
20366:         } else {
20367:             my $newitem = 'communities::0';
20368:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20369:             $adds{$newitem} = 1;
20370:         }
20371:     }
20372:     if ($env{'form.placement'} eq '1') {
20373:         if (ref($cathash) eq 'HASH') {
20374:             my $newitem = 'placement::0';
20375:             if ($cathash->{$newitem} eq '') {
20376:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20377:                 $adds{$newitem} = 1;
20378:             }
20379:         } else {
20380:             my $newitem = 'placement::0';
20381:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20382:             $adds{$newitem} = 1;
20383:         }
20384:     }
20385:     if ($env{'form.addcategory_name'} ne '') {
20386:         if (($env{'form.addcategory_name'} ne 'instcode') &&
20387:             ($env{'form.addcategory_name'} ne 'communities') &&
20388:             ($env{'form.addcategory_name'} ne 'placement')) {
20389:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
20390:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
20391:             $adds{$newitem} = 1;
20392:         }
20393:     }
20394:     my $putresult;
20395:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20396:         if (keys(%deletions) > 0) {
20397:             foreach my $key (keys(%deletions)) {
20398:                 if ($predelallitems{$key} ne '') {
20399:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
20400:                 }
20401:             }
20402:         }
20403:         my (@chkcats,@chktrails,%chkallitems);
20404:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
20405:         if (ref($chkcats[0]) eq 'ARRAY') {
20406:             my $depth = 0;
20407:             my $chg = 0;
20408:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
20409:                 my $name = $chkcats[0][$i];
20410:                 my $item;
20411:                 if ($name eq '') {
20412:                     $chg ++;
20413:                 } else {
20414:                     $item = &escape($name).'::0';
20415:                     if ($chg) {
20416:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
20417:                     }
20418:                     $depth ++; 
20419:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
20420:                     $depth --;
20421:                 }
20422:             }
20423:         }
20424:     }
20425:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20426:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
20427:         if ($putresult eq 'ok') {
20428:             my %title = (
20429:                          togglecats     => 'Show/Hide a course in catalog',
20430:                          categorize     => 'Assign a category to a course',
20431:                          togglecatscomm => 'Show/Hide a community in catalog',
20432:                          categorizecomm => 'Assign a category to a community',
20433:                         );
20434:             my %level = (
20435:                          dom  => 'set in Domain ("Modify Course/Community")',
20436:                          crs  => 'set in Course ("Course Configuration")',
20437:                          comm => 'set in Community ("Community Configuration")',
20438:                          none     => 'No catalog',
20439:                          std      => 'Standard catalog',
20440:                          domonly  => 'Domain-only catalog',
20441:                          codesrch => 'Code search form',
20442:                         );
20443:             $resulttext = &mt('Changes made:').'<ul>';
20444:             if ($changes{'togglecats'}) {
20445:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
20446:             }
20447:             if ($changes{'categorize'}) {
20448:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
20449:             }
20450:             if ($changes{'togglecatscomm'}) {
20451:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
20452:             }
20453:             if ($changes{'categorizecomm'}) {
20454:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
20455:             }
20456:             if ($changes{'unauth'}) {
20457:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
20458:             }
20459:             if ($changes{'auth'}) {
20460:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
20461:             }
20462:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20463:                 my $cathash;
20464:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20465:                     $cathash = $domconfig{'coursecategories'}{'cats'};
20466:                 } else {
20467:                     $cathash = {};
20468:                 } 
20469:                 my (@cats,@trails,%allitems);
20470:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
20471:                 if (keys(%deletions) > 0) {
20472:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
20473:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
20474:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
20475:                     }
20476:                     $resulttext .= '</ul></li>';
20477:                 }
20478:                 if (keys(%reorderings) > 0) {
20479:                     my %sort_by_trail;
20480:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
20481:                     foreach my $key (keys(%reorderings)) {
20482:                         if ($allitems{$key} ne '') {
20483:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20484:                         }
20485:                     }
20486:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20487:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20488:                     }
20489:                     $resulttext .= '</ul></li>';
20490:                 }
20491:                 if (keys(%adds) > 0) {
20492:                     my %sort_by_trail;
20493:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
20494:                     foreach my $key (keys(%adds)) {
20495:                         if ($allitems{$key} ne '') {
20496:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20497:                         }
20498:                     }
20499:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20500:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20501:                     }
20502:                     $resulttext .= '</ul></li>';
20503:                 }
20504:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20505:                 if (ref($lastactref) eq 'HASH') {
20506:                     $lastactref->{'cats'} = 1;
20507:                 }
20508:             }
20509:             $resulttext .= '</ul>';
20510:             if ($changes{'unauth'} || $changes{'auth'}) {
20511:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20512:                 if ($changes{'auth'}) {
20513:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20514:                 }
20515:                 if ($changes{'unauth'}) {
20516:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20517:                 }
20518:                 my $cachetime = 24*60*60;
20519:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20520:                 if (ref($lastactref) eq 'HASH') {
20521:                     $lastactref->{'domdefaults'} = 1;
20522:                 }
20523:             }
20524:         } else {
20525:             $resulttext = '<span class="LC_error">'.
20526:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20527:         }
20528:     } else {
20529:         $resulttext = &mt('No changes made to course and community categories');
20530:     }
20531:     return $resulttext;
20532: }
20533: 
20534: sub modify_serverstatuses {
20535:     my ($dom,%domconfig) = @_;
20536:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20537:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20538:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20539:     }
20540:     my @pages = &serverstatus_pages();
20541:     foreach my $type (@pages) {
20542:         $newserverstatus{$type}{'namedusers'} = '';
20543:         $newserverstatus{$type}{'machines'} = '';
20544:         if (defined($env{'form.'.$type.'_namedusers'})) {
20545:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20546:             my @okusers;
20547:             foreach my $user (@users) {
20548:                 my ($uname,$udom) = split(/:/,$user);
20549:                 if (($udom =~ /^$match_domain$/) &&   
20550:                     (&Apache::lonnet::domain($udom)) &&
20551:                     ($uname =~ /^$match_username$/)) {
20552:                     if (!grep(/^\Q$user\E/,@okusers)) {
20553:                         push(@okusers,$user);
20554:                     }
20555:                 }
20556:             }
20557:             if (@okusers > 0) {
20558:                  @okusers = sort(@okusers);
20559:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20560:             }
20561:         }
20562:         if (defined($env{'form.'.$type.'_machines'})) {
20563:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20564:             my @okmachines;
20565:             foreach my $ip (@machines) {
20566:                 my @parts = split(/\./,$ip);
20567:                 next if (@parts < 4);
20568:                 my $badip = 0;
20569:                 for (my $i=0; $i<4; $i++) {
20570:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20571:                         $badip = 1;
20572:                         last;
20573:                     }
20574:                 }
20575:                 if (!$badip) {
20576:                     push(@okmachines,$ip);     
20577:                 }
20578:             }
20579:             @okmachines = sort(@okmachines);
20580:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20581:         }
20582:     }
20583:     my %serverstatushash =  (
20584:                                 serverstatuses => \%newserverstatus,
20585:                             );
20586:     foreach my $type (@pages) {
20587:         foreach my $setting ('namedusers','machines') {
20588:             my (@current,@new);
20589:             if (ref($currserverstatus{$type}) eq 'HASH') {
20590:                 if ($currserverstatus{$type}{$setting} ne '') { 
20591:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20592:                 }
20593:             }
20594:             if ($newserverstatus{$type}{$setting} ne '') {
20595:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20596:             }
20597:             if (@current > 0) {
20598:                 if (@new > 0) {
20599:                     foreach my $item (@current) {
20600:                         if (!grep(/^\Q$item\E$/,@new)) {
20601:                             $changes{$type}{$setting} = 1;
20602:                             last;
20603:                         }
20604:                     }
20605:                     foreach my $item (@new) {
20606:                         if (!grep(/^\Q$item\E$/,@current)) {
20607:                             $changes{$type}{$setting} = 1;
20608:                             last;
20609:                         }
20610:                     }
20611:                 } else {
20612:                     $changes{$type}{$setting} = 1;
20613:                 }
20614:             } elsif (@new > 0) {
20615:                 $changes{$type}{$setting} = 1;
20616:             }
20617:         }
20618:     }
20619:     if (keys(%changes) > 0) {
20620:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20621:         my $putresult = &Apache::lonnet::put_dom('configuration',
20622:                                                  \%serverstatushash,$dom);
20623:         if ($putresult eq 'ok') {
20624:             $resulttext .= &mt('Changes made:').'<ul>';
20625:             foreach my $type (@pages) {
20626:                 if (ref($changes{$type}) eq 'HASH') {
20627:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20628:                     if ($changes{$type}{'namedusers'}) {
20629:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20630:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20631:                         } else {
20632:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20633:                         }
20634:                     }
20635:                     if ($changes{$type}{'machines'}) {
20636:                         if ($newserverstatus{$type}{'machines'} eq '') {
20637:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20638:                         } else {
20639:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20640:                         }
20641: 
20642:                     }
20643:                     $resulttext .= '</ul></li>';
20644:                 }
20645:             }
20646:             $resulttext .= '</ul>';
20647:         } else {
20648:             $resulttext = '<span class="LC_error">'.
20649:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20650: 
20651:         }
20652:     } else {
20653:         $resulttext = &mt('No changes made to access to server status pages');
20654:     }
20655:     return $resulttext;
20656: }
20657: 
20658: sub modify_helpsettings {
20659:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20660:     my ($resulttext,$errors,%changes,%helphash);
20661:     my %defaultchecked = ('submitbugs' => 'on');
20662:     my @offon = ('off','on');
20663:     my @toggles = ('submitbugs');
20664:     my %current = ('submitbugs' => '',
20665:                    'adhoc'      => {},
20666:                   );
20667:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20668:         %current = %{$domconfig{'helpsettings'}};
20669:     }
20670:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20671:     foreach my $item (@toggles) {
20672:         if ($defaultchecked{$item} eq 'on') { 
20673:             if ($current{$item} eq '') {
20674:                 if ($env{'form.'.$item} eq '0') {
20675:                     $changes{$item} = 1;
20676:                 }
20677:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20678:                 $changes{$item} = 1;
20679:             }
20680:         } elsif ($defaultchecked{$item} eq 'off') {
20681:             if ($current{$item} eq '') {
20682:                 if ($env{'form.'.$item} eq '1') {
20683:                     $changes{$item} = 1;
20684:                 }
20685:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20686:                 $changes{$item} = 1;
20687:             }
20688:         }
20689:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20690:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20691:         }
20692:     }
20693:     my $maxnum = $env{'form.helproles_maxnum'};
20694:     my $confname = $dom.'-domainconfig';
20695:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20696:     my (@allpos,%newsettings,%changedprivs,$newrole);
20697:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20698:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20699:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20700:     my %lt = &Apache::lonlocal::texthash(
20701:                     s      => 'system',
20702:                     d      => 'domain',
20703:                     order  => 'Display order',
20704:                     access => 'Role usage',
20705:                     all    => 'All with domain helpdesk or helpdesk assistant role',
20706:                     dh     => 'All with domain helpdesk role',
20707:                     da     => 'All with domain helpdesk assistant role',
20708:                     none   => 'None',
20709:                     status => 'Determined based on institutional status',
20710:                     inc    => 'Include all, but exclude specific personnel',
20711:                     exc    => 'Exclude all, but include specific personnel',
20712:     );
20713:     for (my $num=0; $num<=$maxnum; $num++) {
20714:         my ($prefix,$identifier,$rolename,%curr);
20715:         if ($num == $maxnum) {
20716:             next unless ($env{'form.newcusthelp'} == $maxnum);
20717:             $identifier = 'custhelp'.$num;
20718:             $prefix = 'helproles_'.$num;
20719:             $rolename = $env{'form.custhelpname'.$num};
20720:             $rolename=~s/[^A-Za-z0-9]//gs;
20721:             next if ($rolename eq '');
20722:             next if (exists($existing{'rolesdef_'.$rolename}));
20723:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20724:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20725:                                                      $newprivs{'c'},$confname,$dom);
20726:             if ($result ne 'ok') {
20727:                 $errors .= '<li><span class="LC_error">'.
20728:                            &mt('An error occurred storing the new custom role: [_1]',
20729:                            $result).'</span></li>';
20730:                 next;
20731:             } else {
20732:                 $changedprivs{$rolename} = \%newprivs;
20733:                 $newrole = $rolename;
20734:             }
20735:         } else {
20736:             $prefix = 'helproles_'.$num;
20737:             $rolename = $env{'form.'.$prefix};
20738:             next if ($rolename eq '');
20739:             next unless (exists($existing{'rolesdef_'.$rolename}));
20740:             $identifier = 'custhelp'.$num;
20741:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20742:             my %currprivs;
20743:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20744:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20745:             foreach my $level ('c','d','s') {
20746:                 if ($newprivs{$level} ne $currprivs{$level}) {
20747:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20748:                                                              $newprivs{'c'},$confname,$dom);
20749:                     if ($result ne 'ok') {
20750:                         $errors .= '<li><span class="LC_error">'.
20751:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
20752:                                        $rolename,$result).'</span></li>';
20753:                     } else {
20754:                         $changedprivs{$rolename} = \%newprivs;
20755:                     }
20756:                     last;
20757:                 }
20758:             }
20759:             if (ref($current{'adhoc'}) eq 'HASH') {
20760:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20761:                     %curr = %{$current{'adhoc'}{$rolename}};
20762:                 }
20763:             }
20764:         }
20765:         my $newpos = $env{'form.'.$prefix.'_pos'};
20766:         $newpos =~ s/\D+//g;
20767:         $allpos[$newpos] = $rolename;
20768:         my $newdesc = $env{'form.'.$prefix.'_desc'};
20769:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
20770:         if ($curr{'desc'}) {
20771:             if ($curr{'desc'} ne $newdesc) {
20772:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
20773:                 $newsettings{$rolename}{'desc'} = $newdesc;
20774:             }
20775:         } elsif ($newdesc ne '') {
20776:             $changes{'customrole'}{$rolename}{'desc'} = 1;
20777:             $newsettings{$rolename}{'desc'} = $newdesc;
20778:         }
20779:         my $access = $env{'form.'.$prefix.'_access'};
20780:         if (grep(/^\Q$access\E$/,@accesstypes)) {
20781:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
20782:             if ($access eq 'status') {
20783:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
20784:                 if (scalar(@statuses) == 0) {
20785:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
20786:                 } else {
20787:                     my (@shownstatus,$numtypes);
20788:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20789:                     if (ref($types) eq 'ARRAY') {
20790:                         $numtypes = scalar(@{$types});
20791:                         foreach my $type (sort(@statuses)) {
20792:                             if ($type eq 'default') {
20793:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20794:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
20795:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20796:                                 push(@shownstatus,$usertypes->{$type});
20797:                             }
20798:                         }
20799:                     }
20800:                     if (grep(/^default$/,@statuses)) {
20801:                         push(@shownstatus,$othertitle);
20802:                     }
20803:                     if (scalar(@shownstatus) == 1+$numtypes) {
20804:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
20805:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
20806:                     } else {
20807:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
20808:                         if (ref($curr{'status'}) eq 'ARRAY') {
20809:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20810:                             if (@diffs) {
20811:                                 $changes{'customrole'}{$rolename}{$access} = 1;
20812:                             }
20813:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20814:                             $changes{'customrole'}{$rolename}{$access} = 1;
20815:                         }
20816:                     }
20817:                 }
20818:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
20819:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
20820:                 my @newspecstaff;
20821:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20822:                 foreach my $person (sort(@personnel)) {
20823:                     if ($domhelpdesk{$person}) {
20824:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
20825:                     }
20826:                 }
20827:                 if (ref($curr{$access}) eq 'ARRAY') {
20828:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20829:                     if (@diffs) {
20830:                         $changes{'customrole'}{$rolename}{$access} = 1;
20831:                     }
20832:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20833:                     $changes{'customrole'}{$rolename}{$access} = 1;
20834:                 }
20835:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20836:                     my ($uname,$udom) = split(/:/,$person);
20837:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
20838:                 }
20839:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
20840:             }
20841:         } else {
20842:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
20843:         }
20844:         unless ($curr{'access'} eq $access) {
20845:             $changes{'customrole'}{$rolename}{'access'} = 1;
20846:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
20847:         }
20848:     }
20849:     if (@allpos > 0) {
20850:         my $idx = 0;
20851:         foreach my $rolename (@allpos) {
20852:             if ($rolename ne '') {
20853:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
20854:                 if (ref($current{'adhoc'}) eq 'HASH') {
20855:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20856:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
20857:                             $changes{'customrole'}{$rolename}{'order'} = 1;
20858:                             $newsettings{$rolename}{'order'} = $idx+1;
20859:                         }
20860:                     }
20861:                 }
20862:                 $idx ++;
20863:             }
20864:         }
20865:     }
20866:     my $putresult;
20867:     if (keys(%changes) > 0) {
20868:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
20869:         if ($putresult eq 'ok') {
20870:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
20871:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
20872:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
20873:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
20874:                 }
20875:             }
20876:             my $cachetime = 24*60*60;
20877:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20878:             if (ref($lastactref) eq 'HASH') {
20879:                 $lastactref->{'domdefaults'} = 1;
20880:             }
20881:         } else {
20882:             $errors .= '<li><span class="LC_error">'.
20883:                        &mt('An error occurred storing the settings: [_1]',
20884:                            $putresult).'</span></li>';
20885:         }
20886:     }
20887:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
20888:         $resulttext = &mt('Changes made:').'<ul>';
20889:         my (%shownprivs,@levelorder);
20890:         @levelorder = ('c','d','s');
20891:         if ((keys(%changes)) && ($putresult eq 'ok')) {
20892:             foreach my $item (sort(keys(%changes))) {
20893:                 if ($item eq 'submitbugs') {
20894:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
20895:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
20896:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
20897:                 } elsif ($item eq 'customrole') {
20898:                     if (ref($changes{'customrole'}) eq 'HASH') {
20899:                         my @keyorder = ('order','desc','access','status','exc','inc');
20900:                         my %keytext = &Apache::lonlocal::texthash(
20901:                                                                    order  => 'Order',
20902:                                                                    desc   => 'Role description',
20903:                                                                    access => 'Role usage',
20904:                                                                    status => 'Allowed institutional types',
20905:                                                                    exc    => 'Allowed personnel',
20906:                                                                    inc    => 'Disallowed personnel',
20907:                         );
20908:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
20909:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
20910:                                 if ($role eq $newrole) {
20911:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
20912:                                                               $role).'<ul>';
20913:                                 } else {
20914:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20915:                                                               $role).'<ul>';
20916:                                 }
20917:                                 foreach my $key (@keyorder) {
20918:                                     if ($changes{'customrole'}{$role}{$key}) {
20919:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
20920:                                                                   $keytext{$key},$newsettings{$role}{$key}).
20921:                                                        '</li>';
20922:                                     }
20923:                                 }
20924:                                 if (ref($changedprivs{$role}) eq 'HASH') {
20925:                                     $shownprivs{$role} = 1;
20926:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
20927:                                     foreach my $level (@levelorder) {
20928:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20929:                                             next if ($item eq '');
20930:                                             my ($priv) = split(/\&/,$item,2);
20931:                                             if (&Apache::lonnet::plaintext($priv)) {
20932:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20933:                                                 unless ($level eq 'c') {
20934:                                                     $resulttext .= ' ('.$lt{$level}.')';
20935:                                                 }
20936:                                                 $resulttext .= '</li>';
20937:                                             }
20938:                                         }
20939:                                     }
20940:                                     $resulttext .= '</ul>';
20941:                                 }
20942:                                 $resulttext .= '</ul></li>';
20943:                             }
20944:                         }
20945:                     }
20946:                 }
20947:             }
20948:         }
20949:         if (keys(%changedprivs)) {
20950:             foreach my $role (sort(keys(%changedprivs))) {
20951:                 unless ($shownprivs{$role}) {
20952:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20953:                                               $role).'<ul>'.
20954:                                    '<li>'.&mt('Privileges set to :').'<ul>';
20955:                     foreach my $level (@levelorder) {
20956:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20957:                             next if ($item eq '');
20958:                             my ($priv) = split(/\&/,$item,2);
20959:                             if (&Apache::lonnet::plaintext($priv)) {
20960:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20961:                                 unless ($level eq 'c') {
20962:                                     $resulttext .= ' ('.$lt{$level}.')';
20963:                                 }
20964:                                 $resulttext .= '</li>';
20965:                             }
20966:                         }
20967:                     }
20968:                     $resulttext .= '</ul></li></ul></li>';
20969:                 }
20970:             }
20971:         }
20972:         $resulttext .= '</ul>';
20973:     } else {
20974:         $resulttext = &mt('No changes made to help settings');
20975:     }
20976:     if ($errors) {
20977:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
20978:                                     $errors.'</ul>';
20979:     }
20980:     return $resulttext;
20981: }
20982: 
20983: sub modify_coursedefaults {
20984:     my ($dom,$lastactref,%domconfig) = @_;
20985:     my ($resulttext,$errors,%changes,%defaultshash);
20986:     my %defaultchecked = (
20987:                            'canuse_pdfforms' => 'off',
20988:                            'uselcmath'       => 'on',
20989:                            'usejsme'         => 'on',
20990:                            'inline_chem'     => 'on',
20991:                            'ltiauth'         => 'off',
20992:                          );
20993:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
20994:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
20995:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
20996:                    'coursequota_official','coursequota_unofficial','coursequota_community',
20997:                    'coursequota_textbook','coursequota_placement','mysqltables_official',
20998:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook',
20999:                    'mysqltables_placement');
21000:     my @types = ('official','unofficial','community','textbook','placement');
21001:     my %staticdefaults = (
21002:                            anonsurvey_threshold => 10,
21003:                            uploadquota          => 500,
21004:                            coursequota          => 20,
21005:                            postsubmit           => 60,
21006:                            mysqltables          => 172800,
21007:                            domexttool           => 1,
21008:                          );
21009:     my %texoptions = (
21010:                         MathJax  => 'MathJax',
21011:                         mimetex  => &mt('Convert to Images'),
21012:                         tth      => &mt('TeX to HTML'),
21013:                      );
21014:     $defaultshash{'coursedefaults'} = {};
21015: 
21016:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
21017:         if ($domconfig{'coursedefaults'} eq '') {
21018:             $domconfig{'coursedefaults'} = {};
21019:         }
21020:     }
21021: 
21022:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
21023:         foreach my $item (@toggles) {
21024:             if ($defaultchecked{$item} eq 'on') {
21025:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21026:                     ($env{'form.'.$item} eq '0')) {
21027:                     $changes{$item} = 1;
21028:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21029:                     $changes{$item} = 1;
21030:                 }
21031:             } elsif ($defaultchecked{$item} eq 'off') {
21032:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21033:                     ($env{'form.'.$item} eq '1')) {
21034:                     $changes{$item} = 1;
21035:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21036:                     $changes{$item} = 1;
21037:                 }
21038:             }
21039:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
21040:         }
21041:         foreach my $item (@numbers) {
21042:             my ($currdef,$newdef);
21043:             $newdef = $env{'form.'.$item};
21044:             if ($item eq 'anonsurvey_threshold') {
21045:                 $currdef = $domconfig{'coursedefaults'}{$item};
21046:                 $newdef =~ s/\D//g;
21047:                 if ($newdef eq '' || $newdef < 1) {
21048:                     $newdef = 1;
21049:                 }
21050:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
21051:             } else {
21052:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
21053:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
21054:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
21055:                 }
21056:                 $newdef =~ s/[^\w.\-]//g;
21057:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
21058:             }
21059:             if ($currdef ne $newdef) {
21060:                 if ($item eq 'anonsurvey_threshold') {
21061:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
21062:                         $changes{$item} = 1;
21063:                     }
21064:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
21065:                     my $setting = $1;
21066:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
21067:                         $changes{$setting} = 1;
21068:                     }
21069:                 }
21070:             }
21071:         }
21072:         my $texengine;
21073:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
21074:             $texengine = $env{'form.texengine'};
21075:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
21076:             if ($currdef eq '') {
21077:                 unless ($texengine eq $Apache::lonnet::deftex) {
21078:                     $changes{'texengine'} = 1;
21079:                 }
21080:             } elsif ($currdef ne $texengine) {
21081:                 $changes{'texengine'} = 1;
21082:             }
21083:         }
21084:         if ($texengine ne '') {
21085:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
21086:         }
21087:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
21088:         my @currclonecode;
21089:         if (ref($currclone) eq 'HASH') {
21090:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
21091:                 @currclonecode = @{$currclone->{'instcode'}};
21092:             }
21093:         }
21094:         my $newclone;
21095:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
21096:             $newclone = $env{'form.canclone'};
21097:         }
21098:         if ($newclone eq 'instcode') {
21099:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
21100:             my (%codedefaults,@code_order,@clonecode);
21101:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
21102:                                                     \@code_order);
21103:             foreach my $item (@code_order) {
21104:                 if (grep(/^\Q$item\E$/,@newcodes)) {
21105:                     push(@clonecode,$item);
21106:                 }
21107:             }
21108:             if (@clonecode) {
21109:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
21110:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
21111:                 if (@diffs) {
21112:                     $changes{'canclone'} = 1;
21113:                 }
21114:             } else {
21115:                 $newclone eq '';
21116:             }
21117:         } elsif ($newclone ne '') {
21118:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
21119:         }
21120:         if ($newclone ne $currclone) {
21121:             $changes{'canclone'} = 1;
21122:         }
21123:         my %credits;
21124:         foreach my $type (@types) {
21125:             unless ($type eq 'community') {
21126:                 $credits{$type} = $env{'form.'.$type.'_credits'};
21127:                 $credits{$type} =~ s/[^\d.]+//g;
21128:             }
21129:         }
21130:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
21131:             ($env{'form.coursecredits'} eq '1')) {
21132:             $changes{'coursecredits'} = 1;
21133:             foreach my $type (keys(%credits)) {
21134:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21135:             }
21136:         } else {
21137:             if ($env{'form.coursecredits'} eq '1') {
21138:                 foreach my $type (@types) {
21139:                     unless ($type eq 'community') {
21140:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
21141:                             $changes{'coursecredits'} = 1;
21142:                         }
21143:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21144:                     }
21145:                 }
21146:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21147:                 foreach my $type (@types) {
21148:                     unless ($type eq 'community') {
21149:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
21150:                             $changes{'coursecredits'} = 1;
21151:                             last;
21152:                         }
21153:                     }
21154:                 }
21155:             }
21156:         }
21157:         if ($env{'form.postsubmit'} eq '1') {
21158:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
21159:             my %currtimeout;
21160:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21161:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
21162:                     $changes{'postsubmit'} = 1;
21163:                 }
21164:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21165:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
21166:                 }
21167:             } else {
21168:                 $changes{'postsubmit'} = 1;
21169:             }
21170:             foreach my $type (@types) {
21171:                 my $timeout = $env{'form.'.$type.'_timeout'};
21172:                 $timeout =~ s/\D//g;
21173:                 if ($timeout == $staticdefaults{'postsubmit'}) {
21174:                     $timeout = '';
21175:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
21176:                     $timeout = '0';
21177:                 }
21178:                 unless ($timeout eq '') {
21179:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
21180:                 }
21181:                 if (exists($currtimeout{$type})) {
21182:                     if ($timeout ne $currtimeout{$type}) {
21183:                         $changes{'postsubmit'} = 1;
21184:                     }
21185:                 } elsif ($timeout ne '') {
21186:                     $changes{'postsubmit'} = 1;
21187:                 }
21188:             }
21189:         } else {
21190:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
21191:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21192:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
21193:                     $changes{'postsubmit'} = 1;
21194:                 }
21195:             } else {
21196:                 $changes{'postsubmit'} = 1;
21197:             }
21198:         }
21199:         my (%newdomexttool,%newexttool,%olddomexttool,%oldexttool);
21200:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
21201:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
21202:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21203:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
21204:         } else {
21205:            foreach my $type (@types) {
21206:                if ($staticdefaults{'domexttool'}) {
21207:                    $olddomexttool{$type} = 1;
21208:                } else {
21209:                    $olddomexttool{$type} = 0;
21210:                }
21211:             }
21212:         }
21213:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
21214:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
21215:         } else {
21216:             foreach my $type (@types) {
21217:                if ($staticdefaults{'exttool'}) {
21218:                    $oldexttool{$type} = 1;
21219:                } else {
21220:                    $oldexttool{$type} = 0;
21221:                }
21222:             }
21223:         }
21224:         foreach my $type (@types) {
21225:             unless ($newdomexttool{$type}) {
21226:                 $newdomexttool{$type} = 0;
21227:             }
21228:             unless ($newexttool{$type}) {
21229:                 $newexttool{$type} = 0;
21230:             }
21231:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
21232:                 $changes{'domexttool'} = 1;
21233:             }
21234:             if ($newexttool{$type} != $oldexttool{$type}) {
21235:                 $changes{'exttool'} = 1;
21236:             }
21237:         }
21238:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
21239:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
21240:     }
21241:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21242:                                              $dom);
21243:     if ($putresult eq 'ok') {
21244:         if (keys(%changes) > 0) {
21245:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21246:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
21247:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
21248:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
21249:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
21250:                 ($changes{'exttool'}) || ($changes{'coursequota'})) {
21251:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
21252:                                   'ltiauth') {
21253:                     if ($changes{$item}) {
21254:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
21255:                     }
21256:                 }
21257:                 if ($changes{'coursecredits'}) {
21258:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21259:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
21260:                             $domdefaults{$type.'credits'} =
21261:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
21262:                         }
21263:                     }
21264:                 }
21265:                 if ($changes{'postsubmit'}) {
21266:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21267:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
21268:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21269:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
21270:                                 $domdefaults{$type.'postsubtimeout'} =
21271:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21272:                             }
21273:                         }
21274:                     }
21275:                 }
21276:                 if ($changes{'uploadquota'}) {
21277:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21278:                         foreach my $type (@types) {
21279:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
21280:                         }
21281:                     }
21282:                 }
21283:                 if ($changes{'coursequota'}) {
21284:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21285:                         foreach my $type (@types) {
21286:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
21287:                         }
21288:                     }
21289:                 }
21290:                 if ($changes{'canclone'}) {
21291:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21292:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21293:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
21294:                             if (@clonecodes) {
21295:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
21296:                             }
21297:                         }
21298:                     } else {
21299:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
21300:                     }
21301:                 }
21302:                 if ($changes{'domexttool'}) {
21303:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21304:                         foreach my $type (@types) {
21305:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
21306:                         }
21307:                     }
21308:                 }
21309:                 if ($changes{'exttool'}) {
21310:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21311:                         foreach my $type (@types) {
21312:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
21313:                         }
21314:                     }
21315:                 }
21316:                 my $cachetime = 24*60*60;
21317:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21318:                 if (ref($lastactref) eq 'HASH') {
21319:                     $lastactref->{'domdefaults'} = 1;
21320:                 }
21321:             }
21322:             $resulttext = &mt('Changes made:').'<ul>';
21323:             foreach my $item (sort(keys(%changes))) {
21324:                 if ($item eq 'canuse_pdfforms') {
21325:                     if ($env{'form.'.$item} eq '1') {
21326:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
21327:                     } else {
21328:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
21329:                     }
21330:                 } elsif ($item eq 'uselcmath') {
21331:                     if ($env{'form.'.$item} eq '1') {
21332:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
21333:                     } else {
21334:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
21335:                     }
21336:                 } elsif ($item eq 'usejsme') {
21337:                     if ($env{'form.'.$item} eq '1') {
21338:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
21339:                     } else {
21340:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
21341:                     }
21342:                 } elsif ($item eq 'inline_chem') {
21343:                     if ($env{'form.'.$item} eq '1') {
21344:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
21345:                     } else {
21346:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
21347:                     }
21348:                 } elsif ($item eq 'texengine') {
21349:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
21350:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
21351:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
21352:                     }
21353:                 } elsif ($item eq 'anonsurvey_threshold') {
21354:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
21355:                 } elsif ($item eq 'uploadquota') {
21356:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21357:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
21358:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
21359:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
21360:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
21361:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
21362:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
21363:                                        '</ul>'.
21364:                                        '</li>';
21365:                     } else {
21366:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
21367:                     }
21368:                 } elsif ($item eq 'coursequota') {
21369:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21370:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
21371:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
21372:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
21373:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
21374:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'placement'}.'</b>').'</li>'.
21375:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
21376:                                        '</ul>'.
21377:                                        '</li>';
21378:                     } else {
21379:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
21380:                     }
21381:                 } elsif ($item eq 'mysqltables') {
21382:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
21383:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
21384:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
21385:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
21386:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
21387:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
21388:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
21389:                                        '</ul>'.
21390:                                        '</li>';
21391:                     } else {
21392:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
21393:                     }
21394:                 } elsif ($item eq 'postsubmit') {
21395:                     if ($domdefaults{'postsubmit'} eq 'off') {
21396:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
21397:                     } else {
21398:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
21399:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21400:                             $resulttext .= &mt('durations:').'<ul>';
21401:                             foreach my $type (@types) {
21402:                                 $resulttext .= '<li>';
21403:                                 my $timeout;
21404:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21405:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21406:                                 }
21407:                                 my $display;
21408:                                 if ($timeout eq '0') {
21409:                                     $display = &mt('unlimited');
21410:                                 } elsif ($timeout eq '') {
21411:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
21412:                                 } else {
21413:                                     $display = &mt('[quant,_1,second]',$timeout);
21414:                                 }
21415:                                 if ($type eq 'community') {
21416:                                     $resulttext .= &mt('Communities');
21417:                                 } elsif ($type eq 'official') {
21418:                                     $resulttext .= &mt('Official courses');
21419:                                 } elsif ($type eq 'unofficial') {
21420:                                     $resulttext .= &mt('Unofficial courses');
21421:                                 } elsif ($type eq 'textbook') {
21422:                                     $resulttext .= &mt('Textbook courses');
21423:                                 } elsif ($type eq 'placement') {
21424:                                     $resulttext .= &mt('Placement tests');
21425:                                 }
21426:                                 $resulttext .= ' -- '.$display.'</li>';
21427:                             }
21428:                             $resulttext .= '</ul>';
21429:                         }
21430:                         $resulttext .= '</li>';
21431:                     }
21432:                 } elsif ($item eq 'coursecredits') {
21433:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21434:                         if (($domdefaults{'officialcredits'} eq '') &&
21435:                             ($domdefaults{'unofficialcredits'} eq '') &&
21436:                             ($domdefaults{'textbookcredits'} eq '')) {
21437:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21438:                         } else {
21439:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
21440:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
21441:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
21442:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
21443:                                            '</ul>'.
21444:                                            '</li>';
21445:                         }
21446:                     } else {
21447:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21448:                     }
21449:                 } elsif ($item eq 'canclone') {
21450:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21451:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21452:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
21453:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
21454:                         }
21455:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
21456:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
21457:                     } else {
21458:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
21459:                     }
21460:                 } elsif ($item eq 'ltiauth') {
21461:                     if ($env{'form.'.$item} eq '1') {
21462:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
21463:                     } else {
21464:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
21465:                     }
21466:                 } elsif ($item eq 'domexttool') {
21467:                     my @noyes = (&mt('no'),&mt('yes'));
21468:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21469:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used as follows:').'<ul>'.
21470:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'official'}].'</b>').'</li>'.
21471:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'unofficial'}].'</b>').'</li>'.
21472:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'textbook'}].'</b>').'</li>'.
21473:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'placement'}].'</b>').'</li>'.
21474:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'community'}].'</b>').'</li>'.
21475:                                        '</ul>'.
21476:                                        '</li>';
21477:                     } else {
21478:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used in all course types, by default').'</li>';
21479:                     }
21480:                 } elsif ($item eq 'exttool') {
21481:                     my @noyes = (&mt('no'),&mt('yes'));
21482:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21483:                         $resulttext .= '<li>'.&mt('External Tools can be defined and configured in course containers as follows:').'<ul>'.
21484:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'official'}].'</b>').'</li>'.
21485:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'unofficial'}].'</b>').'</li>'.
21486:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'textbook'}].'</b>').'</li>'.
21487:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'placement'}].'</b>').'</li>'.
21488:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'community'}].'</b>').'</li>'.
21489:                                        '</ul>'.
21490:                                        '</li>';
21491:                     } else {
21492:                         $resulttext .= '<li>'.&mt('External Tools can not be defined in any course types, by default').'</li>';
21493:                     }
21494:                 }
21495:             }
21496:             $resulttext .= '</ul>';
21497:         } else {
21498:             $resulttext = &mt('No changes made to course defaults');
21499:         }
21500:     } else {
21501:         $resulttext = '<span class="LC_error">'.
21502:             &mt('An error occurred: [_1]',$putresult).'</span>';
21503:     }
21504:     return $resulttext;
21505: }
21506: 
21507: sub modify_selfenrollment {
21508:     my ($dom,$lastactref,%domconfig) = @_;
21509:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
21510:     my @types = ('official','unofficial','community','textbook','placement');
21511:     my %titles = &tool_titles();
21512:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
21513:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
21514:     $ordered{'default'} = ['types','registered','approval','limit'];
21515: 
21516:     my (%roles,%shown,%toplevel);
21517:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
21518: 
21519:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
21520:         if ($domconfig{'selfenrollment'} eq '') {
21521:             $domconfig{'selfenrollment'} = {};
21522:         }
21523:     }
21524:     %toplevel = (
21525:                   admin      => 'Configuration Rights',
21526:                   default    => 'Default settings',
21527:                   validation => 'Validation of self-enrollment requests',
21528:                 );
21529:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21530: 
21531:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21532:         foreach my $item (@{$ordered{'admin'}}) {
21533:             foreach my $type (@types) {
21534:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21535:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21536:                 } else {
21537:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21538:                 }
21539:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21540:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21541:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21542:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21543:                             push(@{$changes{'admin'}{$type}},$item);
21544:                         }
21545:                     } else {
21546:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21547:                             push(@{$changes{'admin'}{$type}},$item);
21548:                         }
21549:                     }
21550:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21551:                     push(@{$changes{'admin'}{$type}},$item);
21552:                 }
21553:             }
21554:         }
21555:     }
21556: 
21557:     foreach my $item (@{$ordered{'default'}}) {
21558:         foreach my $type (@types) {
21559:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21560:             if ($item eq 'types') {
21561:                 unless (($value eq 'all') || ($value eq 'dom')) {
21562:                     $value = '';
21563:                 }
21564:             } elsif ($item eq 'registered') {
21565:                 unless ($value eq '1') {
21566:                     $value = 0;
21567:                 }
21568:             } elsif ($item eq 'approval') {
21569:                 unless ($value =~ /^[012]$/) {
21570:                     $value = 0;
21571:                 }
21572:             } else {
21573:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21574:                     $value = 'none';
21575:                 }
21576:             }
21577:             $selfenrollhash{'default'}{$type}{$item} = $value;
21578:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21579:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21580:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21581:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21582:                          push(@{$changes{'default'}{$type}},$item);
21583:                     }
21584:                 } else {
21585:                     push(@{$changes{'default'}{$type}},$item);
21586:                 }
21587:             } else {
21588:                 push(@{$changes{'default'}{$type}},$item);
21589:             }
21590:             if ($item eq 'limit') {
21591:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21592:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21593:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21594:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21595:                     }
21596:                 } else {
21597:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21598:                 }
21599:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21600:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21601:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21602:                          push(@{$changes{'default'}{$type}},'cap');
21603:                     }
21604:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21605:                     push(@{$changes{'default'}{$type}},'cap');
21606:                 }
21607:             }
21608:         }
21609:     }
21610: 
21611:     foreach my $item (@{$itemsref}) {
21612:         if ($item eq 'fields') {
21613:             my @changed;
21614:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21615:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21616:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21617:             }
21618:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21619:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21620:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21621:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21622:                 } else {
21623:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21624:                 }
21625:             } else {
21626:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21627:             }
21628:             if (@changed) {
21629:                 if ($selfenrollhash{'validation'}{$item}) { 
21630:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21631:                 } else {
21632:                     $changes{'validation'}{$item} = &mt('None');
21633:                 }
21634:             }
21635:         } else {
21636:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
21637:             if ($item eq 'markup') {
21638:                if ($env{'form.selfenroll_validation_'.$item}) {
21639:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
21640:                }
21641:             }
21642:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21643:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
21644:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
21645:                 }
21646:             }
21647:         }
21648:     }
21649: 
21650:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
21651:                                              $dom);
21652:     if ($putresult eq 'ok') {
21653:         if (keys(%changes) > 0) {
21654:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21655:             $resulttext = &mt('Changes made:').'<ul>';
21656:             foreach my $key ('admin','default','validation') {
21657:                 if (ref($changes{$key}) eq 'HASH') {
21658:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
21659:                     if ($key eq 'validation') {
21660:                         foreach my $item (@{$itemsref}) {
21661:                             if (exists($changes{$key}{$item})) {
21662:                                 if ($item eq 'markup') {
21663:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21664:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
21665:                                 } else {  
21666:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
21667:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
21668:                                 }
21669:                             }
21670:                         }
21671:                     } else {
21672:                         foreach my $type (@types) {
21673:                             if ($type eq 'community') {
21674:                                 $roles{'1'} = &mt('Community personnel');
21675:                             } else {
21676:                                 $roles{'1'} = &mt('Course personnel');
21677:                             }
21678:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
21679:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
21680:                                     if ($key eq 'admin') {
21681:                                         my @mgrdc = ();
21682:                                         if (ref($ordered{$key}) eq 'ARRAY') {
21683:                                             foreach my $item (@{$ordered{'admin'}}) {
21684:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
21685:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
21686:                                                         push(@mgrdc,$item);
21687:                                                     }
21688:                                                 }
21689:                                             }
21690:                                             if (@mgrdc) {
21691:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
21692:                                             } else {
21693:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
21694:                                             }
21695:                                         }
21696:                                     } else {
21697:                                         if (ref($ordered{$key}) eq 'ARRAY') {
21698:                                             foreach my $item (@{$ordered{$key}}) {
21699:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
21700:                                                     $domdefaults{$type.'selfenroll'.$item} =
21701:                                                         $selfenrollhash{$key}{$type}{$item};
21702:                                                 }
21703:                                             }
21704:                                         }
21705:                                     }
21706:                                 }
21707:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
21708:                                 foreach my $item (@{$ordered{$key}}) {
21709:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
21710:                                         $resulttext .= '<li>';
21711:                                         if ($key eq 'admin') {
21712:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
21713:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
21714:                                         } else {
21715:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
21716:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
21717:                                         }
21718:                                         $resulttext .= '</li>';
21719:                                     }
21720:                                 }
21721:                                 $resulttext .= '</ul></li>';
21722:                             }
21723:                         }
21724:                         $resulttext .= '</ul></li>'; 
21725:                     }
21726:                 }
21727:             }
21728:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
21729:                 my $cachetime = 24*60*60;
21730:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21731:                 if (ref($lastactref) eq 'HASH') {
21732:                     $lastactref->{'domdefaults'} = 1;
21733:                 }
21734:             }
21735:             $resulttext .= '</ul>';
21736:         } else {
21737:             $resulttext = &mt('No changes made to self-enrollment settings');
21738:         }
21739:     } else {
21740:         $resulttext = '<span class="LC_error">'.
21741:             &mt('An error occurred: [_1]',$putresult).'</span>';
21742:     }
21743:     return $resulttext;
21744: }
21745: 
21746: sub modify_wafproxy {
21747:     my ($dom,$action,$lastactref,%domconfig) = @_;
21748:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21749:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
21750:         %wafproxy,%changes,%expirecache,%expiresaml);
21751:     foreach my $server (sort(keys(%servers))) {
21752:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
21753:         if ($serverhome eq $server) {
21754:             my $serverdom = &Apache::lonnet::host_domain($server);
21755:             if ($serverdom eq $dom) {
21756:                 $canset{$server} = 1;
21757:             }
21758:         }
21759:     }
21760:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
21761:         %{$values{$dom}} = ();
21762:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
21763:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
21764:         }
21765:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
21766:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
21767:         }
21768:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21769:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
21770:         }
21771:     }
21772:     my $output;
21773:     if (keys(%canset)) {
21774:         %{$wafproxy{'alias'}} = ();
21775:         %{$wafproxy{'saml'}} = ();
21776:         foreach my $key (sort(keys(%canset))) {
21777:             if ($env{'form.wafproxy_'.$dom}) {
21778:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
21779:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
21780:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
21781:                     $changes{'alias'} = 1;
21782:                 }
21783:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
21784:                     $wafproxy{'saml'}{$key} = 1;
21785:                 }
21786:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
21787:                     $changes{'saml'} = 1;
21788:                 }
21789:             } else {
21790:                 $wafproxy{'alias'}{$key} = '';
21791:                 $wafproxy{'saml'}{$key} = '';
21792:                 if ($curralias{$key}) {
21793:                     $changes{'alias'} = 1;
21794:                 }
21795:                 if ($currsaml{$key}) {
21796:                     $changes{'saml'} = 1;
21797:                 }
21798:             }
21799:             if ($wafproxy{'alias'}{$key} eq '') {
21800:                 if ($curralias{$key}) {
21801:                     $expirecache{$key} = 1;
21802:                 }
21803:                 delete($wafproxy{'alias'}{$key});
21804:             }
21805:             if ($wafproxy{'saml'}{$key} eq '') {
21806:                 if ($currsaml{$key}) {
21807:                     $expiresaml{$key} = 1;
21808:                 }
21809:                 delete($wafproxy{'saml'}{$key});
21810:             }
21811:         }
21812:         unless (keys(%{$wafproxy{'alias'}})) {
21813:             delete($wafproxy{'alias'});
21814:         }
21815:         unless (keys(%{$wafproxy{'saml'}})) {
21816:             delete($wafproxy{'saml'});
21817:         }
21818:         # Localization for values in %warn occurs in &mt() calls separately.
21819:         my %warn = (
21820:                      trusted => 'trusted IP range(s)',
21821:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
21822:                      vpnext => 'IP range(s) for backend WAF connections',
21823:                    );
21824:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21825:             my $possible = $env{'form.wafproxy_'.$item};
21826:             $possible =~ s/^\s+|\s+$//g;
21827:             if ($possible ne '') {
21828:                 if ($item eq 'remoteip') {
21829:                     if ($possible =~ /^[mhn]$/) {
21830:                         $wafproxy{$item} = $possible;
21831:                     }
21832:                 } elsif ($item eq 'ipheader') {
21833:                     if ($wafproxy{'remoteip'} eq 'h') {
21834:                         $wafproxy{$item} = $possible;
21835:                     }
21836:                 } elsif ($item eq 'sslopt') {
21837:                     if ($possible =~ /^0|1$/) {
21838:                         $wafproxy{$item} = $possible;
21839:                     }
21840:                 } else {
21841:                     my (@ok,$count);
21842:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
21843:                         unless ($env{'form.wafproxy_vpnaccess'}) {
21844:                             $possible = '';
21845:                         }
21846:                     } elsif ($item eq 'trusted') {
21847:                         unless ($wafproxy{'remoteip'} eq 'h') {
21848:                             $possible = '';
21849:                         }
21850:                     }
21851:                     unless ($possible eq '') {
21852:                         $possible =~ s/[\r\n]+/\s/g;
21853:                         $possible =~ s/\s*-\s*/-/g;
21854:                         $possible =~ s/\s+/,/g;
21855:                         $possible =~ s/,+/,/g;
21856:                     }
21857:                     $count = 0;
21858:                     if ($possible ne '') {
21859:                         foreach my $poss (split(/\,/,$possible)) {
21860:                             $count ++;
21861:                             $poss = &validate_ip_pattern($poss);
21862:                             if ($poss ne '') {
21863:                                 push(@ok,$poss);
21864:                             }
21865:                         }
21866:                         my $diff = $count - scalar(@ok);
21867:                         if ($diff) {
21868:                             push(@warnings,'<li>'.
21869:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
21870:                                      $diff,$warn{$item}).
21871:                                  '</li>');
21872:                         }
21873:                         if (@ok) {
21874:                             my @cidr_list;
21875:                             foreach my $item (@ok) {
21876:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
21877:                             }
21878:                             $wafproxy{$item} = join(',',@cidr_list);
21879:                         }
21880:                     }
21881:                 }
21882:                 if ($wafproxy{$item} ne $currvalue{$item}) {
21883:                     $changes{$item} = 1;
21884:                 }
21885:             } elsif ($currvalue{$item}) {
21886:                 $changes{$item} = 1;
21887:             }
21888:         }
21889:     } else {
21890:         if (keys(%curralias)) {
21891:             $changes{'alias'} = 1;
21892:         }
21893:         if (keys(%currsaml)) {
21894:             $changes{'saml'} = 1;
21895:         }
21896:         if (keys(%currvalue)) {
21897:             foreach my $key (keys(%currvalue)) {
21898:                 $changes{$key} = 1;
21899:             }
21900:         }
21901:     }
21902:     if (keys(%changes)) {
21903:         my %defaultshash = (
21904:                               wafproxy => \%wafproxy,
21905:                            );
21906:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21907:                                                  $dom);
21908:         if ($putresult eq 'ok') {
21909:             my $cachetime = 24*60*60;
21910:             my (%domdefaults,$updatedomdefs);
21911:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
21912:                 if ($changes{$item}) {
21913:                     unless ($updatedomdefs) {
21914:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
21915:                         $updatedomdefs = 1;
21916:                     }
21917:                     if ($wafproxy{$item}) {
21918:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
21919:                     } elsif (exists($domdefaults{'waf_'.$item})) {
21920:                         delete($domdefaults{'waf_'.$item});
21921:                     }
21922:                 }
21923:             }
21924:             if ($updatedomdefs) {
21925:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21926:                 if (ref($lastactref) eq 'HASH') {
21927:                     $lastactref->{'domdefaults'} = 1;
21928:                 }
21929:             }
21930:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
21931:                 my %updates = %expirecache;
21932:                 foreach my $key (keys(%expirecache)) {
21933:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
21934:                 }
21935:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
21936:                     my $cachetime = 24*60*60;
21937:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
21938:                         $updates{$key} = 1;
21939:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
21940:                                                       $cachetime);
21941:                     }
21942:                 }
21943:                 if (ref($lastactref) eq 'HASH') {
21944:                     $lastactref->{'proxyalias'} = \%updates;
21945:                 }
21946:             }
21947:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
21948:                 my %samlupdates = %expiresaml;
21949:                 foreach my $key (keys(%expiresaml)) {
21950:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
21951:                 }
21952:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
21953:                     my $cachetime = 24*60*60;
21954:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
21955:                         $samlupdates{$key} = 1;
21956:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
21957:                                                       $cachetime);
21958:                     }
21959:                 }
21960:                 if (ref($lastactref) eq 'HASH') {
21961:                     $lastactref->{'proxysaml'} = \%samlupdates;
21962:                 }
21963:             }
21964:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
21965:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21966:                 if ($changes{$item}) {
21967:                     if ($item eq 'alias') {
21968:                         my $numaliased = 0;
21969:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
21970:                             my $shown;
21971:                             if (keys(%{$wafproxy{'alias'}})) {
21972:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
21973:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
21974:                                                          &Apache::lonnet::hostname($server),
21975:                                                          $wafproxy{'alias'}{$server}).'</li>';
21976:                                     $numaliased ++;
21977:                                 }
21978:                                 if ($numaliased) {
21979:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
21980:                                                           '<ul>'.$shown.'</ul>').'</li>';
21981:                                 }
21982:                             }
21983:                         }
21984:                         unless ($numaliased) {
21985:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
21986:                         }
21987:                     } elsif ($item eq 'saml') {
21988:                         my $shown;
21989:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
21990:                             if (keys(%{$wafproxy{'saml'}})) {
21991:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
21992:                             }
21993:                         }
21994:                         if ($shown) {
21995:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
21996:                                                   $shown).'</li>';
21997:                         } else {
21998:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
21999:                         }
22000:                     } else {
22001:                         if ($item eq 'remoteip') {
22002:                             my %ip_methods = &remoteip_methods();
22003:                             if ($wafproxy{$item} =~ /^[mh]$/) {
22004:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
22005:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
22006:                             } else {
22007:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
22008:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
22009:                                                '</li>';
22010:                                 } else {
22011:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
22012:                                 }
22013:                             }
22014:                         } elsif ($item eq 'ipheader') {
22015:                             if ($wafproxy{$item}) {
22016:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
22017:                                                       $wafproxy{$item}).'</li>';
22018:                             } else {
22019:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
22020:                             }
22021:                         } elsif ($item eq 'trusted') {
22022:                             if ($wafproxy{$item}) {
22023:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
22024:                                                       $wafproxy{$item}).'</li>';
22025:                             } else {
22026:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
22027:                             }
22028:                         } elsif ($item eq 'vpnint') {
22029:                             if ($wafproxy{$item}) {
22030:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
22031:                                                        $wafproxy{$item}).'</li>';
22032:                             } else {
22033:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
22034:                             }
22035:                         } elsif ($item eq 'vpnext') {
22036:                             if ($wafproxy{$item}) {
22037:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
22038:                                                        $wafproxy{$item}).'</li>';
22039:                             } else {
22040:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
22041:                             }
22042:                         } elsif ($item eq 'sslopt') {
22043:                             if ($wafproxy{$item}) {
22044:                                 $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>';
22045:                             } else {
22046:                                 $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>';
22047:                             }
22048:                         }
22049:                     }
22050:                 }
22051:             }
22052:             $output .= '</ul>';
22053:         } else {
22054:             $output = '<span class="LC_error">'.
22055:                       &mt('An error occurred: [_1]',$putresult).'</span>';
22056:         }
22057:     } elsif (keys(%canset)) {
22058:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
22059:     }
22060:     if (@warnings) {
22061:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
22062:                        join("\n",@warnings).'</ul>';
22063:     }
22064:     return $output;
22065: }
22066: 
22067: sub validate_ip_pattern {
22068:     my ($pattern) = @_;
22069:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
22070:         my ($start,$end) = ($1,$2);
22071:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
22072:             if (($start !~ m{/}) && ($end !~ m{/})) {
22073:                 return $start.'-'.$end;
22074:             }
22075:         }
22076:     } elsif ($pattern ne '') {
22077:         $pattern = &Net::CIDR::cidrvalidate($pattern);
22078:         if ($pattern ne '') {
22079:             return $pattern;
22080:         }
22081:     }
22082:     return;
22083: }
22084: 
22085: sub modify_usersessions {
22086:     my ($dom,$lastactref,%domconfig) = @_;
22087:     my @hostingtypes = ('version','excludedomain','includedomain');
22088:     my @offloadtypes = ('primary','default');
22089:     my %types = (
22090:                   remote => \@hostingtypes,
22091:                   hosted => \@hostingtypes,
22092:                   spares => \@offloadtypes,
22093:                 );
22094:     my @prefixes = ('remote','hosted','spares');
22095:     my @lcversions = &Apache::lonnet::all_loncaparevs();
22096:     my (%by_ip,%by_location,@intdoms,@instdoms);
22097:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22098:     my @locations = sort(keys(%by_location));
22099:     my (%defaultshash,%changes);
22100:     foreach my $prefix (@prefixes) {
22101:         $defaultshash{'usersessions'}{$prefix} = {};
22102:     }
22103:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22104:     my $resulttext;
22105:     my %iphost = &Apache::lonnet::get_iphost();
22106:     foreach my $prefix (@prefixes) {
22107:         next if ($prefix eq 'spares');
22108:         foreach my $type (@{$types{$prefix}}) {
22109:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22110:             if ($type eq 'version') {
22111:                 my $value = $env{'form.'.$prefix.'_'.$type};
22112:                 my $okvalue;
22113:                 if ($value ne '') {
22114:                     if (grep(/^\Q$value\E$/,@lcversions)) {
22115:                         $okvalue = $value;
22116:                     }
22117:                 }
22118:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22119:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22120:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
22121:                             if ($inuse == 0) {
22122:                                 $changes{$prefix}{$type} = 1;
22123:                             } else {
22124:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
22125:                                     $changes{$prefix}{$type} = 1;
22126:                                 }
22127:                                 if ($okvalue ne '') {
22128:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22129:                                 } 
22130:                             }
22131:                         } else {
22132:                             if (($inuse == 1) && ($okvalue ne '')) {
22133:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22134:                                 $changes{$prefix}{$type} = 1;
22135:                             }
22136:                         }
22137:                     } else {
22138:                         if (($inuse == 1) && ($okvalue ne '')) {
22139:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22140:                             $changes{$prefix}{$type} = 1;
22141:                         }
22142:                     }
22143:                 } else {
22144:                     if (($inuse == 1) && ($okvalue ne '')) {
22145:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22146:                         $changes{$prefix}{$type} = 1;
22147:                     }
22148:                 }
22149:             } else {
22150:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22151:                 my @okvals;
22152:                 foreach my $val (@vals) {
22153:                     if ($val =~ /:/) {
22154:                         my @items = split(/:/,$val);
22155:                         foreach my $item (@items) {
22156:                             if (ref($by_location{$item}) eq 'ARRAY') {
22157:                                 push(@okvals,$item);
22158:                             }
22159:                         }
22160:                     } else {
22161:                         if (ref($by_location{$val}) eq 'ARRAY') {
22162:                             push(@okvals,$val);
22163:                         }
22164:                     }
22165:                 }
22166:                 @okvals = sort(@okvals);
22167:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22168:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22169:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22170:                             if ($inuse == 0) {
22171:                                 $changes{$prefix}{$type} = 1; 
22172:                             } else {
22173:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22174:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
22175:                                 if (@changed > 0) {
22176:                                     $changes{$prefix}{$type} = 1;
22177:                                 }
22178:                             }
22179:                         } else {
22180:                             if ($inuse == 1) {
22181:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22182:                                 $changes{$prefix}{$type} = 1;
22183:                             }
22184:                         } 
22185:                     } else {
22186:                         if ($inuse == 1) {
22187:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22188:                             $changes{$prefix}{$type} = 1;
22189:                         }
22190:                     }
22191:                 } else {
22192:                     if ($inuse == 1) {
22193:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22194:                         $changes{$prefix}{$type} = 1;
22195:                     }
22196:                 }
22197:             }
22198:         }
22199:     }
22200: 
22201:     my @alldoms = &Apache::lonnet::all_domains();
22202:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22203:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
22204:     my $savespares;
22205: 
22206:     foreach my $lonhost (sort(keys(%servers))) {
22207:         my $serverhomeID =
22208:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
22209:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
22210:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
22211:         my %spareschg;
22212:         foreach my $type (@{$types{'spares'}}) {
22213:             my @okspares;
22214:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
22215:             foreach my $server (@checked) {
22216:                 if (&Apache::lonnet::hostname($server) ne '') {
22217:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
22218:                         unless (grep(/^\Q$server\E$/,@okspares)) {
22219:                             push(@okspares,$server);
22220:                         }
22221:                     }
22222:                 }
22223:             }
22224:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
22225:             my $newspare;
22226:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
22227:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
22228:                     $newspare = $new;
22229:                 }
22230:             }
22231:             my @spares;
22232:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
22233:                 @spares = sort(@okspares,$newspare);
22234:             } else {
22235:                 @spares = sort(@okspares);
22236:             }
22237:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
22238:             if (ref($spareid{$lonhost}) eq 'HASH') {
22239:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
22240:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
22241:                     if (@diffs > 0) {
22242:                         $spareschg{$type} = 1;
22243:                     }
22244:                 }
22245:             }
22246:         }
22247:         if (keys(%spareschg) > 0) {
22248:             $changes{'spares'}{$lonhost} = \%spareschg;
22249:         }
22250:     }
22251:     $defaultshash{'usersessions'}{'offloadnow'} = {};
22252:     $defaultshash{'usersessions'}{'offloadoth'} = {};
22253:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
22254:     my @okoffload;
22255:     if (@offloadnow) {
22256:         foreach my $server (@offloadnow) {
22257:             if (&Apache::lonnet::hostname($server) ne '') {
22258:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
22259:                     push(@okoffload,$server);
22260:                 }
22261:             }
22262:         }
22263:         if (@okoffload) {
22264:             foreach my $lonhost (@okoffload) {
22265:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
22266:             }
22267:         }
22268:     }
22269:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
22270:     my @okoffloadoth;
22271:     if (@offloadoth) {
22272:         foreach my $server (@offloadoth) {
22273:             if (&Apache::lonnet::hostname($server) ne '') {
22274:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
22275:                     push(@okoffloadoth,$server);
22276:                 }
22277:             }
22278:         }
22279:         if (@okoffloadoth) {
22280:             foreach my $lonhost (@okoffloadoth) {
22281:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
22282:             }
22283:         }
22284:     }
22285:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
22286:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
22287:             if (ref($changes{'spares'}) eq 'HASH') {
22288:                 if (keys(%{$changes{'spares'}}) > 0) {
22289:                     $savespares = 1;
22290:                 }
22291:             }
22292:         } else {
22293:             $savespares = 1;
22294:         }
22295:         foreach my $offload ('offloadnow','offloadoth') {
22296:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
22297:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
22298:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
22299:                         $changes{$offload} = 1;
22300:                         last;
22301:                     }
22302:                 }
22303:                 unless ($changes{$offload}) {
22304:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
22305:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
22306:                             $changes{$offload} = 1;
22307:                             last;
22308:                         }
22309:                     }
22310:                 }
22311:             } else {
22312:                 if (($offload eq 'offloadnow') && (@okoffload)) {
22313:                      $changes{'offloadnow'} = 1;
22314:                 }
22315:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
22316:                     $changes{'offloadoth'} = 1;
22317:                 }
22318:             }
22319:         }
22320:     } else {
22321:         if (@okoffload) {
22322:             $changes{'offloadnow'} = 1;
22323:         }
22324:         if (@okoffloadoth) {
22325:             $changes{'offloadoth'} = 1;
22326:         }
22327:     }
22328:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
22329:     if ((keys(%changes) > 0) || ($savespares)) {
22330:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22331:                                                  $dom);
22332:         if ($putresult eq 'ok') {
22333:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22334:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
22335:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
22336:                 }
22337:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
22338:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
22339:                 }
22340:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22341:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
22342:                 }
22343:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22344:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
22345:                 }
22346:             }
22347:             my $cachetime = 24*60*60;
22348:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22349:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
22350:             if (ref($lastactref) eq 'HASH') {
22351:                 $lastactref->{'domdefaults'} = 1;
22352:                 $lastactref->{'usersessions'} = 1;
22353:             }
22354:             if (keys(%changes) > 0) {
22355:                 my %lt = &usersession_titles();
22356:                 $resulttext = &mt('Changes made:').'<ul>';
22357:                 foreach my $prefix (@prefixes) {
22358:                     if (ref($changes{$prefix}) eq 'HASH') {
22359:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22360:                         if ($prefix eq 'spares') {
22361:                             if (ref($changes{$prefix}) eq 'HASH') {
22362:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
22363:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
22364:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
22365:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
22366:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22367:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
22368:                                         foreach my $type (@{$types{$prefix}}) {
22369:                                             if ($changes{$prefix}{$lonhost}{$type}) {
22370:                                                 my $offloadto = &mt('None');
22371:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
22372:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
22373:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
22374:                                                     }
22375:                                                 }
22376:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
22377:                                             }
22378:                                         }
22379:                                     }
22380:                                     $resulttext .= '</li>';
22381:                                 }
22382:                             }
22383:                         } else {
22384:                             foreach my $type (@{$types{$prefix}}) {
22385:                                 if (defined($changes{$prefix}{$type})) {
22386:                                     my ($newvalue,$notinuse);
22387:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22388:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
22389:                                             if ($type eq 'version') {
22390:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
22391:                                             } else {
22392:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22393:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
22394:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
22395:                                                     }
22396:                                                 } else {
22397:                                                     $notinuse = 1;
22398:                                                 }
22399:                                             }
22400:                                         }
22401:                                     }
22402:                                     if ($newvalue eq '') {
22403:                                         if ($type eq 'version') {
22404:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
22405:                                         } elsif ($notinuse) {
22406:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22407:                                         } else {
22408:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22409:                                         }
22410:                                     } else {
22411:                                         if ($type eq 'version') {
22412:                                             $newvalue .= ' '.&mt('(or later)');
22413:                                         }
22414:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22415:                                     }
22416:                                 }
22417:                             }
22418:                         }
22419:                         $resulttext .= '</ul>';
22420:                     }
22421:                 }
22422:                 if ($changes{'offloadnow'}) {
22423:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22424:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
22425:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
22426:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
22427:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22428:                             }
22429:                             $resulttext .= '</ul>';
22430:                         } else {
22431:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
22432:                         }
22433:                     } else {
22434:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
22435:                     }
22436:                 }
22437:                 if ($changes{'offloadoth'}) {
22438:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22439:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
22440:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
22441:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
22442:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22443:                             }
22444:                             $resulttext .= '</ul>';
22445:                         } else {
22446:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
22447:                         }
22448:                     } else {
22449:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
22450:                     }
22451:                 }
22452:                 $resulttext .= '</ul>';
22453:             } else {
22454:                 $resulttext = $nochgmsg;
22455:             }
22456:         } else {
22457:             $resulttext = '<span class="LC_error">'.
22458:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22459:         }
22460:     } else {
22461:         $resulttext = $nochgmsg;
22462:     }
22463:     return $resulttext;
22464: }
22465: 
22466: sub modify_ssl {
22467:     my ($dom,$lastactref,%domconfig) = @_;
22468:     my (%by_ip,%by_location,@intdoms,@instdoms);
22469:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22470:     my @locations = sort(keys(%by_location));
22471:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22472:     my (%defaultshash,%changes);
22473:     my $action = 'ssl';
22474:     my @prefixes = ('connto','connfrom','replication');
22475:     foreach my $prefix (@prefixes) {
22476:         $defaultshash{$action}{$prefix} = {};
22477:     }
22478:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22479:     my $resulttext;
22480:     my %iphost = &Apache::lonnet::get_iphost();
22481:     my @reptypes = ('certreq','nocertreq');
22482:     my @connecttypes = ('dom','intdom','other');
22483:     my %types = (
22484:                   connto      => \@connecttypes,
22485:                   connfrom    => \@connecttypes,
22486:                   replication => \@reptypes,
22487:                 );
22488:     foreach my $prefix (sort(keys(%types))) {
22489:         foreach my $type (@{$types{$prefix}}) {
22490:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22491:                 my $value = 'yes';
22492:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
22493:                     $value = $env{'form.'.$prefix.'_'.$type};
22494:                 }
22495:                 if (ref($domconfig{$action}) eq 'HASH') {
22496:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22497:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
22498:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
22499:                                 $changes{$prefix}{$type} = 1;
22500:                             }
22501:                             $defaultshash{$action}{$prefix}{$type} = $value;
22502:                         } else {
22503:                             $defaultshash{$action}{$prefix}{$type} = $value;
22504:                             $changes{$prefix}{$type} = 1;
22505:                         }
22506:                     } else {
22507:                         $defaultshash{$action}{$prefix}{$type} = $value;
22508:                         $changes{$prefix}{$type} = 1;
22509:                     }
22510:                 } else {
22511:                     $defaultshash{$action}{$prefix}{$type} = $value;
22512:                     $changes{$prefix}{$type} = 1;
22513:                 }
22514:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
22515:                     delete($changes{$prefix}{$type});
22516:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
22517:                     delete($changes{$prefix}{$type});
22518:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
22519:                     delete($changes{$prefix}{$type});
22520:                 }
22521:             } elsif ($prefix eq 'replication') {
22522:                 if (@locations > 0) {
22523:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22524:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22525:                     my @okvals;
22526:                     foreach my $val (@vals) {
22527:                         if ($val =~ /:/) {
22528:                             my @items = split(/:/,$val);
22529:                             foreach my $item (@items) {
22530:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22531:                                     push(@okvals,$item);
22532:                                 }
22533:                             }
22534:                         } else {
22535:                             if (ref($by_location{$val}) eq 'ARRAY') {
22536:                                 push(@okvals,$val);
22537:                             }
22538:                         }
22539:                     }
22540:                     @okvals = sort(@okvals);
22541:                     if (ref($domconfig{$action}) eq 'HASH') {
22542:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22543:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22544:                                 if ($inuse == 0) {
22545:                                     $changes{$prefix}{$type} = 1;
22546:                                 } else {
22547:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22548:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22549:                                     if (@changed > 0) {
22550:                                         $changes{$prefix}{$type} = 1;
22551:                                     }
22552:                                 }
22553:                             } else {
22554:                                 if ($inuse == 1) {
22555:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22556:                                     $changes{$prefix}{$type} = 1;
22557:                                 }
22558:                             }
22559:                         } else {
22560:                             if ($inuse == 1) {
22561:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22562:                                 $changes{$prefix}{$type} = 1;
22563:                             }
22564:                         }
22565:                     } else {
22566:                         if ($inuse == 1) {
22567:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22568:                             $changes{$prefix}{$type} = 1;
22569:                         }
22570:                     }
22571:                 }
22572:             }
22573:         }
22574:     }
22575:     if (keys(%changes)) {
22576:         foreach my $prefix (keys(%changes)) {
22577:             if (ref($changes{$prefix}) eq 'HASH') {
22578:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22579:                     delete($changes{$prefix});
22580:                 }
22581:             } else {
22582:                 delete($changes{$prefix});
22583:             }
22584:         }
22585:     }
22586:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22587:     if (keys(%changes) > 0) {
22588:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22589:                                                  $dom);
22590:         if ($putresult eq 'ok') {
22591:             if (ref($defaultshash{$action}) eq 'HASH') {
22592:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22593:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22594:                 }
22595:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22596:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22597:                 }
22598:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22599:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22600:                 }
22601:             }
22602:             my $cachetime = 24*60*60;
22603:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22604:             if (ref($lastactref) eq 'HASH') {
22605:                 $lastactref->{'domdefaults'} = 1;
22606:             }
22607:             if (keys(%changes) > 0) {
22608:                 my %titles = &ssl_titles();
22609:                 $resulttext = &mt('Changes made:').'<ul>';
22610:                 foreach my $prefix (@prefixes) {
22611:                     if (ref($changes{$prefix}) eq 'HASH') {
22612:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22613:                         foreach my $type (@{$types{$prefix}}) {
22614:                             if (defined($changes{$prefix}{$type})) {
22615:                                 my ($newvalue,$notinuse);
22616:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22617:                                     if (ref($defaultshash{$action}{$prefix})) {
22618:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22619:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22620:                                         } else {
22621:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22622:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22623:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22624:                                                 }
22625:                                             } else {
22626:                                                 $notinuse = 1;
22627:                                             }
22628:                                         }
22629:                                     }
22630:                                     if ($notinuse) {
22631:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22632:                                     } elsif ($newvalue eq '') {
22633:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
22634:                                     } else {
22635:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
22636:                                     }
22637:                                 }
22638:                             }
22639:                         }
22640:                         $resulttext .= '</ul>';
22641:                     }
22642:                 }
22643:             } else {
22644:                 $resulttext = $nochgmsg;
22645:             }
22646:         } else {
22647:             $resulttext = '<span class="LC_error">'.
22648:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22649:         }
22650:     } else {
22651:         $resulttext = $nochgmsg;
22652:     }
22653:     return $resulttext;
22654: }
22655: 
22656: sub modify_trust {
22657:     my ($dom,$lastactref,%domconfig) = @_;
22658:     my (%by_ip,%by_location,@intdoms,@instdoms);
22659:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22660:     my @locations = sort(keys(%by_location));
22661:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
22662:     my @types = ('exc','inc');
22663:     my (%defaultshash,%changes);
22664:     foreach my $prefix (@prefixes) {
22665:         $defaultshash{'trust'}{$prefix} = {};
22666:     }
22667:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22668:     my $resulttext;
22669:     foreach my $prefix (@prefixes) {
22670:         foreach my $type (@types) {
22671:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22672:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22673:             my @okvals;
22674:             foreach my $val (@vals) {
22675:                 if ($val =~ /:/) {
22676:                     my @items = split(/:/,$val);
22677:                     foreach my $item (@items) {
22678:                         if (ref($by_location{$item}) eq 'ARRAY') {
22679:                             push(@okvals,$item);
22680:                         }
22681:                     }
22682:                 } else {
22683:                     if (ref($by_location{$val}) eq 'ARRAY') {
22684:                         push(@okvals,$val);
22685:                     }
22686:                 }
22687:             }
22688:             @okvals = sort(@okvals);
22689:             if (ref($domconfig{'trust'}) eq 'HASH') {
22690:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
22691:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
22692:                         if ($inuse == 0) {
22693:                             $changes{$prefix}{$type} = 1;
22694:                         } else {
22695:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22696:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
22697:                             if (@changed > 0) {
22698:                                 $changes{$prefix}{$type} = 1;
22699:                             }
22700:                         }
22701:                     } else {
22702:                         if ($inuse == 1) {
22703:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22704:                             $changes{$prefix}{$type} = 1;
22705:                         }
22706:                     }
22707:                 } else {
22708:                     if ($inuse == 1) {
22709:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22710:                         $changes{$prefix}{$type} = 1;
22711:                     }
22712:                 }
22713:             } else {
22714:                 if ($inuse == 1) {
22715:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
22716:                     $changes{$prefix}{$type} = 1;
22717:                 }
22718:             }
22719:         }
22720:     }
22721:     my $nochgmsg = &mt('No changes made to trust settings.');
22722:     if (keys(%changes) > 0) {
22723:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22724:                                                  $dom);
22725:         if ($putresult eq 'ok') {
22726:             if (ref($defaultshash{'trust'}) eq 'HASH') {
22727:                 foreach my $prefix (@prefixes) {
22728:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
22729:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
22730:                     }
22731:                 }
22732:             }
22733:             my $cachetime = 24*60*60;
22734:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22735:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
22736:             if (ref($lastactref) eq 'HASH') {
22737:                 $lastactref->{'domdefaults'} = 1;
22738:                 $lastactref->{'trust'} = 1;
22739:             }
22740:             if (keys(%changes) > 0) {
22741:                 my %lt = &trust_titles();
22742:                 $resulttext = &mt('Changes made:').'<ul>';
22743:                 foreach my $prefix (@prefixes) {
22744:                     if (ref($changes{$prefix}) eq 'HASH') {
22745:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22746:                         foreach my $type (@types) {
22747:                             if (defined($changes{$prefix}{$type})) {
22748:                                 my ($newvalue,$notinuse);
22749:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
22750:                                     if (ref($defaultshash{'trust'}{$prefix})) {
22751:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
22752:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
22753:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
22754:                                             }
22755:                                         } else {
22756:                                             $notinuse = 1;
22757:                                         }
22758:                                     }
22759:                                 }
22760:                                 if ($notinuse) {
22761:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22762:                                 } elsif ($newvalue eq '') {
22763:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22764:                                 } else {
22765:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22766:                                 }
22767:                             }
22768:                         }
22769:                         $resulttext .= '</ul>';
22770:                     }
22771:                 }
22772:                 $resulttext .= '</ul>';
22773:             } else {
22774:                 $resulttext = $nochgmsg;
22775:             }
22776:         } else {
22777:             $resulttext = '<span class="LC_error">'.
22778:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22779:         }
22780:     } else {
22781:         $resulttext = $nochgmsg;
22782:     }
22783:     return $resulttext;
22784: }
22785: 
22786: sub modify_loadbalancing {
22787:     my ($dom,%domconfig) = @_;
22788:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
22789:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
22790:     my ($othertitle,$usertypes,$types) =
22791:         &Apache::loncommon::sorted_inst_types($dom);
22792:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22793:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
22794:     my @sparestypes = ('primary','default');
22795:     my %typetitles = &sparestype_titles();
22796:     my $resulttext;
22797:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22798:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22799:         %existing = %{$domconfig{'loadbalancing'}};
22800:     }
22801:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
22802:                               \%currtargets,\%currrules,\%currcookies);
22803:     my ($saveloadbalancing,%defaultshash,%changes);
22804:     my ($alltypes,$othertypes,$titles) =
22805:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
22806:     my %ruletitles = &offloadtype_text();
22807:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
22808:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
22809:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
22810:         if ($balancer eq '') {
22811:             next;
22812:         }
22813:         if (!exists($servers{$balancer})) {
22814:             if (exists($currbalancer{$balancer})) {
22815:                 push(@{$changes{'delete'}},$balancer);
22816:             }
22817:             next;
22818:         }
22819:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
22820:             push(@{$changes{'delete'}},$balancer);
22821:             next;
22822:         }
22823:         if (!exists($currbalancer{$balancer})) {
22824:             push(@{$changes{'add'}},$balancer);
22825:         }
22826:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
22827:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
22828:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
22829:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22830:             $saveloadbalancing = 1;
22831:         }
22832:         foreach my $sparetype (@sparestypes) {
22833:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
22834:             my @offloadto;
22835:             foreach my $target (@targets) {
22836:                 if (($servers{$target}) && ($target ne $balancer)) {
22837:                     if ($sparetype eq 'default') {
22838:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
22839:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
22840:                         }
22841:                     }
22842:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
22843:                         push(@offloadto,$target);
22844:                     }
22845:                 }
22846:             }
22847:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
22848:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
22849:                     push(@offloadto,$balancer);
22850:                 }
22851:             }
22852:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
22853:         }
22854:         if ($env{'form.loadbalancing_cookie_'.$i}) {
22855:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
22856:             if (exists($currbalancer{$balancer})) {
22857:                 unless ($currcookies{$balancer}) {
22858:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
22859:                 }
22860:             }
22861:         } elsif (exists($currbalancer{$balancer})) {
22862:             if ($currcookies{$balancer}) {
22863:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
22864:             }
22865:         }
22866:         if (ref($currtargets{$balancer}) eq 'HASH') {
22867:             foreach my $sparetype (@sparestypes) {
22868:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
22869:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
22870:                     if (@targetdiffs > 0) {
22871:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22872:                     }
22873:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22874:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22875:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22876:                     }
22877:                 }
22878:             }
22879:         } else {
22880:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
22881:                 foreach my $sparetype (@sparestypes) {
22882:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22883:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22884:                             $changes{'curr'}{$balancer}{'targets'} = 1;
22885:                         }
22886:                     }
22887:                 }
22888:             }
22889:         }
22890:         my $ishomedom;
22891:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
22892:             $ishomedom = 1;
22893:         }
22894:         if (ref($alltypes) eq 'ARRAY') {
22895:             foreach my $type (@{$alltypes}) {
22896:                 my $rule;
22897:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
22898:                          (!$ishomedom)) {
22899:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
22900:                 }
22901:                 if ($rule eq 'specific') {
22902:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
22903:                     if (exists($servers{$specifiedhost})) {
22904:                         $rule = $specifiedhost;
22905:                     }
22906:                 }
22907:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
22908:                 if (ref($currrules{$balancer}) eq 'HASH') {
22909:                     if ($rule ne $currrules{$balancer}{$type}) {
22910:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22911:                     }
22912:                 } elsif ($rule ne '') {
22913:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22914:                 }
22915:             }
22916:         }
22917:     }
22918:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
22919:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
22920:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
22921:             $defaultshash{'loadbalancing'} = {};
22922:         }
22923:         my $putresult = &Apache::lonnet::put_dom('configuration',
22924:                                                  \%defaultshash,$dom);
22925:         if ($putresult eq 'ok') {
22926:             if (keys(%changes) > 0) {
22927:                 my %toupdate;
22928:                 if (ref($changes{'delete'}) eq 'ARRAY') {
22929:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
22930:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
22931:                         $toupdate{$balancer} = 1;
22932:                     }
22933:                 }
22934:                 if (ref($changes{'add'}) eq 'ARRAY') {
22935:                     foreach my $balancer (sort(@{$changes{'add'}})) {
22936:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
22937:                         $toupdate{$balancer} = 1;
22938:                     }
22939:                 }
22940:                 if (ref($changes{'curr'}) eq 'HASH') {
22941:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
22942:                         $toupdate{$balancer} = 1;
22943:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
22944:                             if ($changes{'curr'}{$balancer}{'targets'}) {
22945:                                 my %offloadstr;
22946:                                 foreach my $sparetype (@sparestypes) {
22947:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22948:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22949:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22950:                                         }
22951:                                     }
22952:                                 }
22953:                                 if (keys(%offloadstr) == 0) {
22954:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
22955:                                 } else {
22956:                                     my $showoffload;
22957:                                     foreach my $sparetype (@sparestypes) {
22958:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
22959:                                         if (defined($offloadstr{$sparetype})) {
22960:                                             $showoffload .= $offloadstr{$sparetype};
22961:                                         } else {
22962:                                             $showoffload .= &mt('None');
22963:                                         }
22964:                                         $showoffload .= ('&nbsp;'x3);
22965:                                     }
22966:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
22967:                                 }
22968:                             }
22969:                         }
22970:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
22971:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
22972:                                 foreach my $type (@{$alltypes}) {
22973:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
22974:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22975:                                         my $balancetext;
22976:                                         if ($rule eq '') {
22977:                                             $balancetext =  $ruletitles{'default'};
22978:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
22979:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22980:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22981:                                                 foreach my $sparetype (@sparestypes) {
22982:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22983:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22984:                                                     }
22985:                                                 }
22986:                                                 foreach my $item (@{$alltypes}) {
22987:                                                     next if ($item =~  /^_LC_ipchange/);
22988:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
22989:                                                     if ($hasrule eq 'homeserver') {
22990:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
22991:                                                     } else {
22992:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
22993:                                                             if ($servers{$hasrule}) {
22994:                                                                 $toupdate{$hasrule} = 1;
22995:                                                             }
22996:                                                         }
22997:                                                     }
22998:                                                 }
22999:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
23000:                                                     $balancetext =  $ruletitles{$rule};
23001:                                                 } else {
23002:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23003:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
23004:                                                     if ($receiver) {
23005:                                                         $toupdate{$receiver};
23006:                                                     }
23007:                                                 }
23008:                                             } else {
23009:                                                 $balancetext =  $ruletitles{$rule};
23010:                                             }
23011:                                         } else {
23012:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
23013:                                         }
23014:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
23015:                                     }
23016:                                 }
23017:                             }
23018:                         }
23019:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
23020:                             if ($currcookies{$balancer}) {
23021:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
23022:                                                           $balancer).'</li>';
23023:                             } else {
23024:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
23025:                                                           $balancer).'</li>';
23026:                             }
23027:                         }
23028:                     }
23029:                 }
23030:                 if (keys(%toupdate)) {
23031:                     my %thismachine;
23032:                     my $updatedhere;
23033:                     my $cachetime = 60*60*24;
23034:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23035:                     foreach my $lonhost (keys(%toupdate)) {
23036:                         if ($thismachine{$lonhost}) {
23037:                             unless ($updatedhere) {
23038:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
23039:                                                               $defaultshash{'loadbalancing'},
23040:                                                               $cachetime);
23041:                                 $updatedhere = 1;
23042:                             }
23043:                         } else {
23044:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
23045:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
23046:                         }
23047:                     }
23048:                 }
23049:                 if ($resulttext ne '') {
23050:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
23051:                 } else {
23052:                     $resulttext = $nochgmsg;
23053:                 }
23054:             } else {
23055:                 $resulttext = $nochgmsg;
23056:             }
23057:         } else {
23058:             $resulttext = '<span class="LC_error">'.
23059:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23060:         }
23061:     } else {
23062:         $resulttext = $nochgmsg;
23063:     }
23064:     return $resulttext;
23065: }
23066: 
23067: sub recurse_check {
23068:     my ($chkcats,$categories,$depth,$name) = @_;
23069:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
23070:         my $chg = 0;
23071:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
23072:             my $category = $chkcats->[$depth]{$name}[$j];
23073:             my $item;
23074:             if ($category eq '') {
23075:                 $chg ++;
23076:             } else {
23077:                 my $deeper = $depth + 1;
23078:                 $item = &escape($category).':'.&escape($name).':'.$depth;
23079:                 if ($chg) {
23080:                     $categories->{$item} -= $chg;
23081:                 }
23082:                 &recurse_check($chkcats,$categories,$deeper,$category);
23083:                 $deeper --;
23084:             }
23085:         }
23086:     }
23087:     return;
23088: }
23089: 
23090: sub recurse_cat_deletes {
23091:     my ($item,$coursecategories,$deletions) = @_;
23092:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
23093:     my $subdepth = $depth + 1;
23094:     if (ref($coursecategories) eq 'HASH') {
23095:         foreach my $subitem (keys(%{$coursecategories})) {
23096:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
23097:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
23098:                 delete($coursecategories->{$subitem});
23099:                 $deletions->{$subitem} = 1;
23100:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
23101:             }
23102:         }
23103:     }
23104:     return;
23105: }
23106: 
23107: sub active_dc_picker {
23108:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
23109:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
23110:     my @domcoord = keys(%domcoords);
23111:     if (keys(%currhash)) {
23112:         foreach my $dc (keys(%currhash)) {
23113:             unless (exists($domcoords{$dc})) {
23114:                 push(@domcoord,$dc);
23115:             }
23116:         }
23117:     }
23118:     @domcoord = sort(@domcoord);
23119:     my $numdcs = scalar(@domcoord);
23120:     my $rows = 0;
23121:     my $table;
23122:     if ($numdcs > 1) {
23123:         $table = '<table>';
23124:         for (my $i=0; $i<@domcoord; $i++) {
23125:             my $rem = $i%($numinrow);
23126:             if ($rem == 0) {
23127:                 if ($i > 0) {
23128:                     $table .= '</tr>';
23129:                 }
23130:                 $table .= '<tr>';
23131:                 $rows ++;
23132:             }
23133:             my $check = '';
23134:             if ($inputtype eq 'radio') {
23135:                 if (keys(%currhash) == 0) {
23136:                     if (!$i) {
23137:                         $check = ' checked="checked"';
23138:                     }
23139:                 } elsif (exists($currhash{$domcoord[$i]})) {
23140:                     $check = ' checked="checked"';
23141:                 }
23142:             } else {
23143:                 if (exists($currhash{$domcoord[$i]})) {
23144:                     $check = ' checked="checked"';
23145:                 }
23146:             }
23147:             if ($i == @domcoord - 1) {
23148:                 my $colsleft = $numinrow - $rem;
23149:                 if ($colsleft > 1) {
23150:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
23151:                 } else {
23152:                     $table .= '<td class="LC_left_item">';
23153:                 }
23154:             } else {
23155:                 $table .= '<td class="LC_left_item">';
23156:             }
23157:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
23158:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23159:             $table .= '<span class="LC_nobreak"><label>'.
23160:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
23161:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
23162:             if ($user ne $dcname.':'.$dcdom) {
23163:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23164:             }
23165:             $table .= '</label></span></td>';
23166:         }
23167:         $table .= '</tr></table>';
23168:     } elsif ($numdcs == 1) {
23169:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
23170:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23171:         if ($inputtype eq 'radio') {
23172:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
23173:             if ($user ne $dcname.':'.$dcdom) {
23174:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23175:             }
23176:         } else {
23177:             my $check;
23178:             if (exists($currhash{$domcoord[0]})) {
23179:                 $check = ' checked="checked"';
23180:             }
23181:             $table = '<span class="LC_nobreak"><label>'.
23182:                      '<input type="checkbox" name="'.$name.'" '.
23183:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
23184:             if ($user ne $dcname.':'.$dcdom) {
23185:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23186:             }
23187:             $table .= '</label></span>';
23188:             $rows ++;
23189:         }
23190:     }
23191:     return ($numdcs,$table,$rows);
23192: }
23193: 
23194: sub usersession_titles {
23195:     return &Apache::lonlocal::texthash(
23196:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
23197:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
23198:                spares => 'Servers offloaded to, when busy',
23199:                version => 'LON-CAPA version requirement',
23200:                excludedomain => 'Allow all, but exclude specific domains',
23201:                includedomain => 'Deny all, but include specific domains',
23202:                primary => 'Primary (checked first)',
23203:                default => 'Default',
23204:            );
23205: }
23206: 
23207: sub id_for_thisdom {
23208:     my (%servers) = @_;
23209:     my %altids;
23210:     foreach my $server (keys(%servers)) {
23211:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
23212:         if ($serverhome ne $server) {
23213:             $altids{$serverhome} = $server;
23214:         }
23215:     }
23216:     return %altids;
23217: }
23218: 
23219: sub count_servers {
23220:     my ($currbalancer,%servers) = @_;
23221:     my (@spares,$numspares);
23222:     foreach my $lonhost (sort(keys(%servers))) {
23223:         next if ($currbalancer eq $lonhost);
23224:         push(@spares,$lonhost);
23225:     }
23226:     if ($currbalancer) {
23227:         $numspares = scalar(@spares);
23228:     } else {
23229:         $numspares = scalar(@spares) - 1;
23230:     }
23231:     return ($numspares,@spares);
23232: }
23233: 
23234: sub lonbalance_targets_js {
23235:     my ($dom,$types,$servers,$settings) = @_;
23236:     my $select = &mt('Select');
23237:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
23238:     if (ref($servers) eq 'HASH') {
23239:         $alltargets = join("','",sort(keys(%{$servers})));
23240:         my @homedoms;
23241:         foreach my $server (sort(keys(%{$servers}))) {
23242:             if (&Apache::lonnet::host_domain($server) eq $dom) {
23243:                 push(@homedoms,'1');
23244:             } else {
23245:                 push(@homedoms,'0');
23246:             }
23247:         }
23248:         $allishome = join("','",@homedoms);
23249:     }
23250:     if (ref($types) eq 'ARRAY') {
23251:         if (@{$types} > 0) {
23252:             @alltypes = @{$types};
23253:         }
23254:     }
23255:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
23256:     $allinsttypes = join("','",@alltypes);
23257:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23258:     if (ref($settings) eq 'HASH') {
23259:         %existing = %{$settings};
23260:     }
23261:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
23262:                               \%currtargets,\%currrules,\%currcookies);
23263:     my $balancers = join("','",sort(keys(%currbalancer)));
23264:     return <<"END";
23265: 
23266: <script type="text/javascript">
23267: // <![CDATA[
23268: 
23269: currBalancers = new Array('$balancers');
23270: 
23271: function toggleTargets(balnum) {
23272:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23273:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
23274:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
23275:     var prevbalancer = prevhostitem.value;
23276:     var baltotal = document.getElementById('loadbalancing_total').value;
23277:     prevhostitem.value = balancer;
23278:     if (prevbalancer != '') {
23279:         var prevIdx = currBalancers.indexOf(prevbalancer);
23280:         if (prevIdx != -1) {
23281:             currBalancers.splice(prevIdx,1);
23282:         }
23283:     }
23284:     if (balancer == '') {
23285:         hideSpares(balnum);
23286:     } else {
23287:         var currIdx = currBalancers.indexOf(balancer);
23288:         if (currIdx == -1) {
23289:             currBalancers.push(balancer);
23290:         }
23291:         var homedoms = new Array('$allishome');
23292:         var ishomedom = homedoms[lonhostitem.selectedIndex];
23293:         showSpares(balancer,ishomedom,balnum);
23294:     }
23295:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
23296:     return;
23297: }
23298: 
23299: function showSpares(balancer,ishomedom,balnum) {
23300:     var alltargets = new Array('$alltargets');
23301:     var insttypes = new Array('$allinsttypes');
23302:     var offloadtypes = new Array('primary','default');
23303: 
23304:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
23305:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
23306:  
23307:     for (var i=0; i<offloadtypes.length; i++) {
23308:         var count = 0;
23309:         for (var j=0; j<alltargets.length; j++) {
23310:             if (alltargets[j] != balancer) {
23311:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
23312:                 item.value = alltargets[j];
23313:                 item.style.textAlign='left';
23314:                 item.style.textFace='normal';
23315:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
23316:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
23317:                     item.disabled = '';
23318:                 } else {
23319:                     item.disabled = 'disabled';
23320:                     item.checked = false;
23321:                 }
23322:                 count ++;
23323:             }
23324:         }
23325:     }
23326:     for (var k=0; k<insttypes.length; k++) {
23327:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
23328:             if (ishomedom == 1) {
23329:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23330:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23331:             } else {
23332:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23333:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23334:             }
23335:         } else {
23336:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23337:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23338:         }
23339:         if ((insttypes[k] != '_LC_external') && 
23340:             ((insttypes[k] != '_LC_internetdom') ||
23341:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
23342:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
23343:             item.options.length = 0;
23344:             item.options[0] = new Option("","",true,true);
23345:             var idx = 0;
23346:             for (var m=0; m<alltargets.length; m++) {
23347:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
23348:                     idx ++;
23349:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23350:                 }
23351:             }
23352:         }
23353:     }
23354:     return;
23355: }
23356: 
23357: function hideSpares(balnum) {
23358:     var alltargets = new Array('$alltargets');
23359:     var insttypes = new Array('$allinsttypes');
23360:     var offloadtypes = new Array('primary','default');
23361: 
23362:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
23363:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
23364: 
23365:     var total = alltargets.length - 1;
23366:     for (var i=0; i<offloadtypes; i++) {
23367:         for (var j=0; j<total; j++) {
23368:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
23369:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
23370:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
23371:         }
23372:     }
23373:     for (var k=0; k<insttypes.length; k++) {
23374:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23375:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23376:         if (insttypes[k] != '_LC_external') {
23377:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
23378:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
23379:         }
23380:     }
23381:     return;
23382: }
23383: 
23384: function checkOffloads(item,balnum,type) {
23385:     var alltargets = new Array('$alltargets');
23386:     var offloadtypes = new Array('primary','default');
23387:     if (item.checked) {
23388:         var total = alltargets.length - 1;
23389:         var other;
23390:         if (type == offloadtypes[0]) {
23391:             other = offloadtypes[1];
23392:         } else {
23393:             other = offloadtypes[0];
23394:         }
23395:         for (var i=0; i<total; i++) {
23396:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
23397:             if (server == item.value) {
23398:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
23399:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
23400:                 }
23401:             }
23402:         }
23403:     }
23404:     return;
23405: }
23406: 
23407: function singleServerToggle(balnum,type) {
23408:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
23409:     if (offloadtoSelIdx == 0) {
23410:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
23411:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23412: 
23413:     } else {
23414:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
23415:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23416:     }
23417:     return;
23418: }
23419: 
23420: function balanceruleChange(formname,balnum,type) {
23421:     if (type == '_LC_external') {
23422:         return;
23423:     }
23424:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
23425:     for (var i=0; i<typesRules.length; i++) {
23426:         if (formname.elements[typesRules[i]].checked) {
23427:             if (formname.elements[typesRules[i]].value != 'specific') {
23428:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
23429:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23430:             } else {
23431:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23432:             }
23433:         }
23434:     }
23435:     return;
23436: }
23437: 
23438: function balancerDeleteChange(balnum) {
23439:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23440:     var baltotal = document.getElementById('loadbalancing_total').value;
23441:     var addtarget;
23442:     var removetarget;
23443:     var action = 'delete';
23444:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
23445:         var lonhost = hostitem.value;
23446:         var currIdx = currBalancers.indexOf(lonhost);
23447:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
23448:             if (currIdx != -1) {
23449:                 currBalancers.splice(currIdx,1);
23450:             }
23451:             addtarget = lonhost;
23452:         } else {
23453:             if (currIdx == -1) {
23454:                 currBalancers.push(lonhost);
23455:             }
23456:             removetarget = lonhost;
23457:             action = 'undelete';
23458:         }
23459:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
23460:     }
23461:     return;
23462: }
23463: 
23464: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
23465:     if (baltotal > 1) {
23466:         var offloadtypes = new Array('primary','default');
23467:         var alltargets = new Array('$alltargets');
23468:         var insttypes = new Array('$allinsttypes');
23469:         for (var i=0; i<baltotal; i++) {
23470:             if (i != balnum) {
23471:                 for (var j=0; j<offloadtypes.length; j++) {
23472:                     var total = alltargets.length - 1;
23473:                     for (var k=0; k<total; k++) {
23474:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
23475:                         var server = serveritem.value;
23476:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
23477:                             if (server == addtarget) {
23478:                                 serveritem.disabled = '';
23479:                             }
23480:                         }
23481:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23482:                             if (server == removetarget) {
23483:                                 serveritem.disabled = 'disabled';
23484:                                 serveritem.checked = false;
23485:                             }
23486:                         }
23487:                     }
23488:                 }
23489:                 for (var j=0; j<insttypes.length; j++) {
23490:                     if (insttypes[j] != '_LC_external') {
23491:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
23492:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
23493:                             var currSel = singleserver.selectedIndex;
23494:                             var currVal = singleserver.options[currSel].value;
23495:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
23496:                                 var numoptions = singleserver.options.length;
23497:                                 var needsnew = 1;
23498:                                 for (var k=0; k<numoptions; k++) {
23499:                                     if (singleserver.options[k] == addtarget) {
23500:                                         needsnew = 0;
23501:                                         break;
23502:                                     }
23503:                                 }
23504:                                 if (needsnew == 1) {
23505:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
23506:                                 }
23507:                             }
23508:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23509:                                 singleserver.options.length = 0;
23510:                                 if ((currVal) && (currVal != removetarget)) {
23511:                                     singleserver.options[0] = new Option("","",false,false);
23512:                                 } else {
23513:                                     singleserver.options[0] = new Option("","",true,true);
23514:                                 }
23515:                                 var idx = 0;
23516:                                 for (var m=0; m<alltargets.length; m++) {
23517:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
23518:                                         idx ++;
23519:                                         if (currVal == alltargets[m]) {
23520:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
23521:                                         } else {
23522:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23523:                                         }
23524:                                     }
23525:                                 }
23526:                             }
23527:                         }
23528:                     }
23529:                 }
23530:             }
23531:         }
23532:     }
23533:     return;
23534: }
23535: 
23536: // ]]>
23537: </script>
23538: 
23539: END
23540: }
23541: 
23542: sub new_spares_js {
23543:     my @sparestypes = ('primary','default');
23544:     my $types = join("','",@sparestypes);
23545:     my $select = &mt('Select');
23546:     return <<"END";
23547: 
23548: <script type="text/javascript">
23549: // <![CDATA[
23550: 
23551: function updateNewSpares(formname,lonhost) {
23552:     var types = new Array('$types');
23553:     var include = new Array();
23554:     var exclude = new Array();
23555:     for (var i=0; i<types.length; i++) {
23556:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23557:         for (var j=0; j<spareboxes.length; j++) {
23558:             if (formname.elements[spareboxes[j]].checked) {
23559:                 exclude.push(formname.elements[spareboxes[j]].value);
23560:             } else {
23561:                 include.push(formname.elements[spareboxes[j]].value);
23562:             }
23563:         }
23564:     }
23565:     for (var i=0; i<types.length; i++) {
23566:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23567:         var selIdx = newSpare.selectedIndex;
23568:         var currnew = newSpare.options[selIdx].value;
23569:         var okSpares = new Array();
23570:         for (var j=0; j<newSpare.options.length; j++) {
23571:             var possible = newSpare.options[j].value;
23572:             if (possible != '') {
23573:                 if (exclude.indexOf(possible) == -1) {
23574:                     okSpares.push(possible);
23575:                 } else {
23576:                     if (currnew == possible) {
23577:                         selIdx = 0;
23578:                     }
23579:                 }
23580:             }
23581:         }
23582:         for (var k=0; k<include.length; k++) {
23583:             if (okSpares.indexOf(include[k]) == -1) {
23584:                 okSpares.push(include[k]);
23585:             }
23586:         }
23587:         okSpares.sort();
23588:         newSpare.options.length = 0;
23589:         if (selIdx == 0) {
23590:             newSpare.options[0] = new Option("$select","",true,true);
23591:         } else {
23592:             newSpare.options[0] = new Option("$select","",false,false);
23593:         }
23594:         for (var m=0; m<okSpares.length; m++) {
23595:             var idx = m+1;
23596:             var selThis = 0;
23597:             if (selIdx != 0) {
23598:                 if (okSpares[m] == currnew) {
23599:                     selThis = 1;
23600:                 }
23601:             }
23602:             if (selThis == 1) {
23603:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23604:             } else {
23605:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23606:             }
23607:         }
23608:     }
23609:     return;
23610: }
23611: 
23612: function checkNewSpares(lonhost,type) {
23613:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23614:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23615:     if (chosen != '') {
23616:         var othertype;
23617:         var othernewSpare;
23618:         if (type == 'primary') {
23619:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23620:         }
23621:         if (type == 'default') {
23622:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23623:         }
23624:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23625:             othernewSpare.selectedIndex = 0;
23626:         }
23627:     }
23628:     return;
23629: }
23630: 
23631: // ]]>
23632: </script>
23633: 
23634: END
23635: 
23636: }
23637: 
23638: sub common_domprefs_js {
23639:     return <<"END";
23640: 
23641: <script type="text/javascript">
23642: // <![CDATA[
23643: 
23644: function getIndicesByName(formname,item) {
23645:     var group = new Array();
23646:     for (var i=0;i<formname.elements.length;i++) {
23647:         if (formname.elements[i].name == item) {
23648:             group.push(formname.elements[i].id);
23649:         }
23650:     }
23651:     return group;
23652: }
23653: 
23654: // ]]>
23655: </script>
23656: 
23657: END
23658: 
23659: }
23660: 
23661: sub recaptcha_js {
23662:     my %lt = &captcha_phrases();
23663:     return <<"END";
23664: 
23665: <script type="text/javascript">
23666: // <![CDATA[
23667: 
23668: function updateCaptcha(caller,context) {
23669:     var privitem;
23670:     var pubitem;
23671:     var privtext;
23672:     var pubtext;
23673:     var versionitem;
23674:     var versiontext;
23675:     if (document.getElementById(context+'_recaptchapub')) {
23676:         pubitem = document.getElementById(context+'_recaptchapub');
23677:     } else {
23678:         return;
23679:     }
23680:     if (document.getElementById(context+'_recaptchapriv')) {
23681:         privitem = document.getElementById(context+'_recaptchapriv');
23682:     } else {
23683:         return;
23684:     }
23685:     if (document.getElementById(context+'_recaptchapubtxt')) {
23686:         pubtext = document.getElementById(context+'_recaptchapubtxt');
23687:     } else {
23688:         return;
23689:     }
23690:     if (document.getElementById(context+'_recaptchaprivtxt')) {
23691:         privtext = document.getElementById(context+'_recaptchaprivtxt');
23692:     } else {
23693:         return;
23694:     }
23695:     if (document.getElementById(context+'_recaptchaversion')) {
23696:         versionitem = document.getElementById(context+'_recaptchaversion');
23697:     } else {
23698:         return;
23699:     }
23700:     if (document.getElementById(context+'_recaptchavertxt')) {
23701:         versiontext = document.getElementById(context+'_recaptchavertxt');
23702:     } else {
23703:         return;
23704:     }
23705:     if (caller.checked) {
23706:         if (caller.value == 'recaptcha') {
23707:             pubitem.type = 'text';
23708:             privitem.type = 'text';
23709:             pubitem.size = '40';
23710:             privitem.size = '40';
23711:             pubtext.innerHTML = "$lt{'pub'}";
23712:             privtext.innerHTML = "$lt{'priv'}";
23713:             versionitem.type = 'text';
23714:             versionitem.size = '3';
23715:             versiontext.innerHTML = "$lt{'ver'}";
23716:         } else {
23717:             pubitem.type = 'hidden';
23718:             privitem.type = 'hidden';
23719:             versionitem.type = 'hidden';
23720:             pubtext.innerHTML = '';
23721:             privtext.innerHTML = '';
23722:             versiontext.innerHTML = '';
23723:         }
23724:     }
23725:     return;
23726: }
23727: 
23728: // ]]>
23729: </script>
23730: 
23731: END
23732: 
23733: }
23734: 
23735: sub toggle_display_js {
23736:     return <<"END";
23737: 
23738: <script type="text/javascript">
23739: // <![CDATA[
23740: 
23741: function toggleDisplay(domForm,caller) {
23742:     if (document.getElementById(caller)) {
23743:         var divitem = document.getElementById(caller);
23744:         var optionsElement = domForm.coursecredits;
23745:         var checkval = 1;
23746:         var dispval = 'block';
23747:         var selfcreateRegExp = /^cancreate_emailverified/;
23748:         if (caller == 'emailoptions') {
23749:             optionsElement = domForm.cancreate_email;
23750:         }
23751:         if (caller == 'studentsubmission') {
23752:             optionsElement = domForm.postsubmit;
23753:         }
23754:         if (caller == 'cloneinstcode') {
23755:             optionsElement = domForm.canclone;
23756:             checkval = 'instcode';
23757:         }
23758:         if (selfcreateRegExp.test(caller)) {
23759:             optionsElement = domForm.elements[caller];
23760:             checkval = 'other';
23761:             dispval = 'inline'
23762:         }
23763:         if (optionsElement.length) {
23764:             var currval;
23765:             for (var i=0; i<optionsElement.length; i++) {
23766:                 if (optionsElement[i].checked) {
23767:                    currval = optionsElement[i].value;
23768:                 }
23769:             }
23770:             if (currval == checkval) {
23771:                 divitem.style.display = dispval;
23772:             } else {
23773:                 divitem.style.display = 'none';
23774:             }
23775:         }
23776:     }
23777:     return;
23778: }
23779: 
23780: // ]]>
23781: </script>
23782: 
23783: END
23784: 
23785: }
23786: 
23787: sub captcha_phrases {
23788:     return &Apache::lonlocal::texthash (
23789:                  priv => 'Private key',
23790:                  pub  => 'Public key',
23791:                  original  => 'original (CAPTCHA)',
23792:                  recaptcha => 'successor (ReCAPTCHA)',
23793:                  notused   => 'unused',
23794:                  ver => 'ReCAPTCHA version (1 or 2)',
23795:     );
23796: }
23797: 
23798: sub devalidate_remote_domconfs {
23799:     my ($dom,$cachekeys) = @_;
23800:     return unless (ref($cachekeys) eq 'HASH');
23801:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23802:     my %thismachine;
23803:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23804:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
23805:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
23806:                       'ipaccess','trust');
23807:     my %cache_by_lonhost;
23808:     if (exists($cachekeys->{'samllanding'})) {
23809:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
23810:             my %landing = %{$cachekeys->{'samllanding'}};
23811:             my %domservers = &Apache::lonnet::get_servers($dom);
23812:             if (keys(%domservers)) {
23813:                 foreach my $server (keys(%domservers)) {
23814:                     my @cached;
23815:                     next if ($thismachine{$server});
23816:                     if ($landing{$server}) {
23817:                         push(@cached,&escape('samllanding').':'.&escape($server));
23818:                     }
23819:                     if (@cached) {
23820:                         $cache_by_lonhost{$server} = \@cached;
23821:                     }
23822:                 }
23823:             }
23824:         }
23825:     }
23826:     if (keys(%servers)) {
23827:         foreach my $server (keys(%servers)) {
23828:             next if ($thismachine{$server});
23829:             my @cached;
23830:             foreach my $name (@posscached) {
23831:                 if ($cachekeys->{$name}) {
23832:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
23833:                         if (ref($cachekeys->{$name}) eq 'HASH') {
23834:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
23835:                                 push(@cached,&escape($name).':'.&escape($key));
23836:                             }
23837:                         }
23838:                     } else {
23839:                         push(@cached,&escape($name).':'.&escape($dom));
23840:                     }
23841:                 }
23842:             }
23843:             if ((exists($cache_by_lonhost{$server})) &&
23844:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
23845:                 push(@cached,@{$cache_by_lonhost{$server}});
23846:             }
23847:             if (@cached) {
23848:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
23849:             }
23850:         }
23851:     }
23852:     return;
23853: }
23854: 
23855: 1;

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