File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.450: download - view: text, annotated - select for diffs
Tue Jan 14 00:49:34 2025 UTC (5 weeks, 3 days ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Domain configuration for availability of "directory" and "non-directory"
  user information. When one institutional status type changes from having no
  data available to some data available, that should indicate change occurred.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.450 2025/01/14 00:49:34 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course.
   99: 
  100: Outputs: 1
  101: 
  102: $datatable  - HTML containing form elements which allow settings to be changed. 
  103: 
  104: In the case of course requests, radio buttons are displayed for each institutional
  105: affiliate type (and also default, and _LC_adv) for each of the course types 
  106: (official, unofficial, community, textbook, placement, and lti).  
  107: In each case the radio buttons allow the selection of one of four values:
  108: 
  109: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  110: which have the following effects:
  111: 
  112: 0
  113: 
  114: =over
  115: 
  116: - course requests are not allowed for this course types/affiliation
  117: 
  118: =back
  119: 
  120: approval 
  121: 
  122: =over 
  123: 
  124: - course requests must be approved by a Doman Coordinator in the 
  125: course's domain
  126: 
  127: =back
  128: 
  129: validate 
  130: 
  131: =over
  132: 
  133: - an institutional validation (e.g., check requestor is instructor
  134: of record) needs to be passed before the course will be created.  The required
  135: validation is in localenroll.pm on the primary library server for the course 
  136: domain.
  137: 
  138: =back
  139: 
  140: autolimit 
  141: 
  142: =over
  143:  
  144: - course requests will be processed automatically up to a limit of
  145: N requests for the course type for the particular requestor.
  146: If N is undefined, there is no limit to the number of course requests
  147: which a course owner may submit and have processed automatically. 
  148: 
  149: =back
  150: 
  151: =item modify_quotas() 
  152: 
  153: =back
  154: 
  155: =cut
  156: 
  157: package Apache::domainprefs;
  158: 
  159: use strict;
  160: use Apache::Constants qw(:common :http);
  161: use Apache::lonnet;
  162: use Apache::loncommon();
  163: use Apache::lonhtmlcommon();
  164: use Apache::lonlocal;
  165: use Apache::lonmsg();
  166: use Apache::lonconfigsettings;
  167: use Apache::lonuserutils();
  168: use Apache::loncoursequeueadmin();
  169: use Apache::courseprefs();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: use Net::CIDR;
  180: use Crypt::CBC;
  181: 
  182: my $registered_cleanup;
  183: my $modified_urls;
  184: 
  185: sub handler {
  186:     my $r=shift;
  187:     if ($r->header_only) {
  188:         &Apache::loncommon::content_type($r,'text/html');
  189:         $r->send_http_header;
  190:         return OK;
  191:     }
  192: 
  193:     my $context = 'domain';
  194:     my $dom = $env{'request.role.domain'};
  195:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  196:     if (&Apache::lonnet::allowed('mau',$dom)) {
  197:         &Apache::loncommon::content_type($r,'text/html');
  198:         $r->send_http_header;
  199:     } else {
  200:         $env{'user.error.msg'}=
  201:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  202:         return HTTP_NOT_ACCEPTABLE;
  203:     }
  204: 
  205:     $registered_cleanup=0;
  206:     @{$modified_urls}=();
  207: 
  208:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  209:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  210:                                             ['phase','actions']);
  211:     my $phase = 'pickactions';
  212:     if ( exists($env{'form.phase'}) ) {
  213:         $phase = $env{'form.phase'};
  214:     }
  215:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  216:     my %domconfig =
  217:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  218:                 'quotas','autoenroll','autoupdate','autocreate',
  219:                 'directorysrch','usercreation','usermodification',
  220:                 'contacts','defaults','scantron','coursecategories',
  221:                 'serverstatuses','requestcourses','helpsettings',
  222:                 'coursedefaults','usersessions','loadbalancing',
  223:                 'requestauthor','selfenrollment','inststatus',
  224:                 'ltitools','toolsec','ssl','trust','lti','ltisec',
  225:                 'privacy','passwords','proctoring','wafproxy',
  226:                 'ipaccess','authordefaults'],$dom);
  227:     my %encconfig =
  228:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  229:     my ($checked_is_home,$is_home);
  230:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  231:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  232:             my $home = &Apache::lonnet::domain($dom,'primary');
  233:             unless (($home eq 'no_host') || ($home eq '')) {
  234:                 my @ids=&Apache::lonnet::current_machine_ids();
  235:                 if (grep(/^\Q$home\E$/,@ids)) {
  236:                     $is_home = 1;
  237:                 }
  238:             }
  239:             $checked_is_home = 1;
  240:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  241:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  243:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  244:                     if (($is_home) && ($phase eq 'process')) {
  245:                         $domconfig{'ltitools'}{$id}{'secret'} = $encconfig{'ltitools'}{$id}{'secret'};
  246:                     }
  247:                 }
  248:             }
  249:         }
  250:     }
  251:     if (ref($domconfig{'lti'}) eq 'HASH') {
  252:         if (ref($encconfig{'lti'}) eq 'HASH') {
  253:             unless ($checked_is_home) {
  254:                 my $home = &Apache::lonnet::domain($dom,'primary');
  255:                 unless (($home eq 'no_host') || ($home eq '')) {
  256:                     my @ids=&Apache::lonnet::current_machine_ids();
  257:                     if (grep(/^\Q$home\E$/,@ids)) {
  258:                         $is_home = 1;
  259:                     }
  260:                 }
  261:                 $checked_is_home = 1;
  262:             }
  263:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  264:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  265:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  266:                     $domconfig{'lti'}{$id}{'key'} = $encconfig{'lti'}{$id}{'key'};
  267:                     if (($is_home) && ($phase eq 'process')) {
  268:                         $domconfig{'lti'}{$id}{'secret'} = $encconfig{'lti'}{$id}{'secret'};
  269:                     }
  270:                 }
  271:             }
  272:         }
  273:     }
  274:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  275:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  276:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  277:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  278:                     unless ($id =~ /^\d+$/) {
  279:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  280:                     }
  281:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  282:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  283:                         foreach my $item ('key','secret') {
  284:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  285:                         }
  286:                     }
  287:                 }
  288:             }
  289:         }
  290:     }
  291:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  292:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  293:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  294:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  295:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  296:                     foreach my $item ('key','secret') {
  297:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  298:                     }
  299:                 }
  300:             }
  301:         }
  302:     }
  303:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  304:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  305:                        'contacts','privacy','usercreation','selfcreation',
  306:                        'usermodification','scantron','requestcourses','requestauthor',
  307:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  308:                        'authordefaults','ltitools','proctoring','selfenrollment',
  309:                        'usersessions','ssl','trust','lti');
  310:     my %existing;
  311:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  312:         %existing = %{$domconfig{'loadbalancing'}};
  313:     }
  314:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  315:         push(@prefs_order,'loadbalancing');
  316:     }
  317:     my %prefs = (
  318:         'rolecolors' =>
  319:                    { text => 'Default color schemes',
  320:                      help => 'Domain_Configuration_Color_Schemes',
  321:                      header => [{col1 => 'Student Settings',
  322:                                  col2 => '',},
  323:                                 {col1 => 'Coordinator Settings',
  324:                                  col2 => '',},
  325:                                 {col1 => 'Author Settings',
  326:                                  col2 => '',},
  327:                                 {col1 => 'Administrator Settings',
  328:                                  col2 => '',}],
  329:                       print => \&print_rolecolors,
  330:                       modify => \&modify_rolecolors,
  331:                     },
  332:         'login' =>
  333:                     { text => 'Log-in page options',
  334:                       help => 'Domain_Configuration_Login_Page',
  335:                       header => [{col1 => 'Log-in Page Items',
  336:                                   col2 => '',},
  337:                                  {col1 => 'Log-in Help',
  338:                                   col2 => 'Value'},
  339:                                  {col1 => 'Custom HTML in document head',
  340:                                   col2 => 'Value'},
  341:                                  {col1 => 'SSO',
  342:                                   col2 => 'Dual login: SSO and non-SSO options'},
  343:                                 ],
  344:                       print => \&print_login,
  345:                       modify => \&modify_login,
  346:                     },
  347:         'defaults' => 
  348:                     { text => 'Default authentication/language/timezone/portal/types',
  349:                       help => 'Domain_Configuration_LangTZAuth',
  350:                       header => [{col1 => 'Setting',
  351:                                   col2 => 'Value'},
  352:                                  {col1 => 'Institutional user types',
  353:                                   col2 => 'Name displayed'},
  354:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  355:                                   col2 => 'Rules in use'}],
  356:                       print => \&print_defaults,
  357:                       modify => \&modify_defaults,
  358:                     },
  359:         'wafproxy' =>
  360:                     { text => 'Web Application Firewall/Reverse Proxy',
  361:                       help => 'Domain_Configuration_WAF_Proxy',
  362:                       header => [{col1 => 'Domain(s)',
  363:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  364:                                  },
  365:                                  {col1 => 'Domain(s)',
  366:                                   col2 => 'WAF Configuration',}],
  367:                       print => \&print_wafproxy,
  368:                       modify => \&modify_wafproxy,
  369:                     },
  370:         'passwords' =>
  371:                     { text => 'Passwords (Internal authentication)',
  372:                       help => 'Domain_Configuration_Passwords',
  373:                       header => [{col1 => 'Resetting Forgotten Password',
  374:                                   col2 => 'Settings'},
  375:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  376:                                   col2 => 'Settings'},
  377:                                  {col1 => 'Rules for LON-CAPA Passwords',
  378:                                   col2 => 'Settings'},
  379:                                  {col1 => 'Course Owner Changing Student Passwords',
  380:                                   col2 => 'Settings'}],
  381:                       print => \&print_passwords,
  382:                       modify => \&modify_passwords,
  383:                     },
  384:         'quotas' => 
  385:                     { text => 'Blogs, personal pages/timezones, portfolio/quotas',
  386:                       help => 'Domain_Configuration_Quotas',
  387:                       header => [{col1 => 'User affiliation',
  388:                                   col2 => 'Available tools',
  389:                                   col3 => 'Portfolio quota (MB)',}],
  390:                       print => \&print_quotas,
  391:                       modify => \&modify_quotas,
  392:                     },
  393:         'autoenroll' =>
  394:                    { text => 'Auto-enrollment settings',
  395:                      help => 'Domain_Configuration_Auto_Enrollment',
  396:                      header => [{col1 => 'Configuration setting',
  397:                                  col2 => 'Value(s)'}],
  398:                      print => \&print_autoenroll,
  399:                      modify => \&modify_autoenroll,
  400:                    },
  401:         'autoupdate' => 
  402:                    { text => 'Auto-update settings',
  403:                      help => 'Domain_Configuration_Auto_Updates',
  404:                      header => [{col1 => 'Setting',
  405:                                  col2 => 'Value',},
  406:                                 {col1 => 'Setting',
  407:                                  col2 => 'Affiliation'},
  408:                                 {col1 => 'User population',
  409:                                  col2 => 'Updatable user data'}],
  410:                      print => \&print_autoupdate,
  411:                      modify => \&modify_autoupdate,
  412:                   },
  413:         'autocreate' => 
  414:                   { text => 'Auto-course creation settings',
  415:                      help => 'Domain_Configuration_Auto_Creation',
  416:                      header => [{col1 => 'Configuration Setting',
  417:                                  col2 => 'Value',}],
  418:                      print => \&print_autocreate,
  419:                      modify => \&modify_autocreate,
  420:                   },
  421:         'directorysrch' => 
  422:                   { text => 'Directory searches',
  423:                     help => 'Domain_Configuration_InstDirectory_Search',
  424:                     header => [{col1 => 'Institutional Directory Setting',
  425:                                 col2 => 'Value',},
  426:                                {col1 => 'LON-CAPA Directory Setting',
  427:                                 col2 => 'Value',}],
  428:                     print => \&print_directorysrch,
  429:                     modify => \&modify_directorysrch,
  430:                   },
  431:         'contacts' =>
  432:                   { text => 'E-mail addresses and helpform',
  433:                     help => 'Domain_Configuration_Contact_Info',
  434:                     header => [{col1 => 'Default e-mail addresses',
  435:                                 col2 => 'Value',},
  436:                                {col1 => 'Recipient(s) for notifications',
  437:                                 col2 => 'Value',},
  438:                                {col1 => 'Nightly status check e-mail',
  439:                                 col2 => 'Settings',},
  440:                                {col1 => 'Ask helpdesk form settings',
  441:                                 col2 => 'Value',},],
  442:                     print => \&print_contacts,
  443:                     modify => \&modify_contacts,
  444:                   },
  445:         'usercreation' => 
  446:                   { text => 'User creation',
  447:                     help => 'Domain_Configuration_User_Creation',
  448:                     header => [{col1 => 'Format rule type',
  449:                                 col2 => 'Format rules in force'},
  450:                                {col1 => 'User account creation',
  451:                                 col2 => 'Usernames which may be created',},
  452:                                {col1 => 'Context',
  453:                                 col2 => 'Assignable authentication types'}],
  454:                     print => \&print_usercreation,
  455:                     modify => \&modify_usercreation,
  456:                   },
  457:         'selfcreation' => 
  458:                   { text => 'Users self-creating accounts',
  459:                     help => 'Domain_Configuration_Self_Creation', 
  460:                     header => [{col1 => 'Self-creation with institutional username',
  461:                                 col2 => 'Enabled?'},
  462:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  463:                                 col2 => 'Information user can enter'},
  464:                                {col1 => 'Self-creation with e-mail verification',
  465:                                 col2 => 'Settings'}],
  466:                     print => \&print_selfcreation,
  467:                     modify => \&modify_selfcreation,
  468:                   },
  469:         'usermodification' =>
  470:                   { text => 'User modification',
  471:                     help => 'Domain_Configuration_User_Modification',
  472:                     header => [{col1 => 'Target user has role',
  473:                                 col2 => 'User information updatable in author context'},
  474:                                {col1 => 'Target user has role',
  475:                                 col2 => 'User information updatable by co-author manager'},
  476:                                {col1 => 'Target user has role',
  477:                                 col2 => 'User information updatable in course context'}],
  478:                     print => \&print_usermodification,
  479:                     modify => \&modify_usermodification,
  480:                   },
  481:         'scantron' =>
  482:                   { text => 'Bubblesheet format',
  483:                     help => 'Domain_Configuration_Scantron_Format',
  484:                     header => [ {col1 => 'Bubblesheet format file',
  485:                                  col2 => ''},
  486:                                 {col1 => 'Bubblesheet data upload formats',
  487:                                  col2 => 'Settings'}],
  488:                     print => \&print_scantron,
  489:                     modify => \&modify_scantron,
  490:                   },
  491:         'requestcourses' => 
  492:                  {text => 'Request creation of courses',
  493:                   help => 'Domain_Configuration_Request_Courses',
  494:                   header => [{col1 => 'User affiliation',
  495:                               col2 => 'Availability/Processing of requests',},
  496:                              {col1 => 'Setting',
  497:                               col2 => 'Value'},
  498:                              {col1 => 'Available textbooks',
  499:                               col2 => ''},
  500:                              {col1 => 'Available templates',
  501:                               col2 => ''},
  502:                              {col1 => 'Validation (not official courses)',
  503:                               col2 => 'Value'},],
  504:                   print => \&print_quotas,
  505:                   modify => \&modify_quotas,
  506:                  },
  507:         'requestauthor' =>
  508:                  {text => 'Request Authoring Space',
  509:                   help => 'Domain_Configuration_Request_Author',
  510:                   header => [{col1 => 'User affiliation',
  511:                               col2 => 'Availability/Processing of requests',},
  512:                              {col1 => 'Setting',
  513:                               col2 => 'Value'}],
  514:                   print => \&print_quotas,
  515:                   modify => \&modify_quotas,
  516:                  },
  517:         'coursecategories' =>
  518:                   { text => 'Cataloging of courses/communities',
  519:                     help => 'Domain_Configuration_Cataloging_Courses',
  520:                     header => [{col1 => 'Catalog type/availability',
  521:                                 col2 => '',},
  522:                                {col1 => 'Category settings for standard catalog',
  523:                                 col2 => '',},
  524:                                {col1 => 'Categories',
  525:                                 col2 => '',
  526:                                }],
  527:                     print => \&print_coursecategories,
  528:                     modify => \&modify_coursecategories,
  529:                   },
  530:         'serverstatuses' =>
  531:                  {text   => 'Access to server status pages',
  532:                   help   => 'Domain_Configuration_Server_Status',
  533:                   header => [{col1 => 'Status Page',
  534:                               col2 => 'Other named users',
  535:                               col3 => 'Specific IPs',
  536:                             }],
  537:                   print => \&print_serverstatuses,
  538:                   modify => \&modify_serverstatuses,
  539:                  },
  540:         'helpsettings' =>
  541:                  {text   => 'Support settings',
  542:                   help   => 'Domain_Configuration_Help_Settings',
  543:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  544:                               col2 => 'Value'},
  545:                              {col1 => 'Helpdesk Roles',
  546:                               col2 => 'Settings'},],
  547:                   print  => \&print_helpsettings,
  548:                   modify => \&modify_helpsettings,
  549:                  },
  550:         'coursedefaults' => 
  551:                  {text => 'Course/Community defaults',
  552:                   help => 'Domain_Configuration_Course_Defaults',
  553:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  554:                               col2 => 'Value',},
  555:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  556:                               col2 => 'Value',},],
  557:                   print => \&print_coursedefaults,
  558:                   modify => \&modify_coursedefaults,
  559:                  },
  560:         'selfenrollment' => 
  561:                  {text   => 'Self-enrollment in Course/Community',
  562:                   help   => 'Domain_Configuration_Selfenrollment',
  563:                   header => [{col1 => 'Configuration Rights',
  564:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  565:                              {col1 => 'Defaults',
  566:                               col2 => 'Value'},
  567:                              {col1 => 'Self-enrollment validation (optional)',
  568:                               col2 => 'Value'},],
  569:                   print => \&print_selfenrollment,
  570:                   modify => \&modify_selfenrollment,
  571:                  },
  572:         'privacy' => 
  573:                  {text   => 'Role assignments and user privacy',
  574:                   help   => 'Domain_Configuration_User_Privacy',
  575:                   header => [{col1 => 'Role assigned in different domain',
  576:                               col2 => 'Approval options'},
  577:                              {col1 => 'Role assigned in different domain to user of type',
  578:                               col2 => 'User information available in that domain'},
  579:                              {col1 => "Role assigned in user's domain",
  580:                               col2 => 'Information viewable by privileged user'},
  581:                              {col1 => "Role assigned in user's domain",
  582:                               col2 => 'Information viewable by unprivileged user'}],
  583:                   print => \&print_privacy,
  584:                   modify => \&modify_privacy,
  585:                  },
  586:         'usersessions' =>
  587:                  {text  => 'User session hosting/offloading',
  588:                   help  => 'Domain_Configuration_User_Sessions',
  589:                   header => [{col1 => 'Domain server',
  590:                               col2 => 'Servers to offload sessions to when busy'},
  591:                              {col1 => 'Hosting of users from other domains',
  592:                               col2 => 'Rules'},
  593:                              {col1 => "Hosting domain's own users elsewhere",
  594:                               col2 => 'Rules'}],
  595:                   print => \&print_usersessions,
  596:                   modify => \&modify_usersessions,
  597:                  },
  598:         'loadbalancing' =>
  599:                  {text  => 'Dedicated Load Balancer(s)',
  600:                   help  => 'Domain_Configuration_Load_Balancing',
  601:                   header => [{col1 => 'Balancers',
  602:                               col2 => 'Default destinations',
  603:                               col3 => 'User affiliation',
  604:                               col4 => 'Overrides'},
  605:                             ],
  606:                   print => \&print_loadbalancing,
  607:                   modify => \&modify_loadbalancing,
  608:                  },
  609:         'ltitools' =>
  610:                  {text => 'External Tools (LTI)',
  611:                   help => 'Domain_Configuration_LTI_Tools',
  612:                   header => [{col1 => 'Encryption of shared secrets',
  613:                               col2 => 'Settings'},
  614:                              {col1 => 'Rules for shared secrets',
  615:                               col2 => 'Settings'},
  616:                              {col1 => 'Providers',
  617:                               col2 => 'Settings',}],
  618:                   print => \&print_ltitools,
  619:                   modify => \&modify_ltitools,
  620:                  },
  621:         'proctoring' =>
  622:                  {text => 'Remote Proctoring Integration',
  623:                   help => 'Domain_Configuration_Proctoring',
  624:                   header => [{col1 => 'Name',
  625:                               col2 => 'Configuration'}],
  626:                   print => \&print_proctoring,
  627:                   modify => \&modify_proctoring,
  628:                  },
  629:         'ssl' =>
  630:                  {text  => 'LON-CAPA Network (SSL)',
  631:                   help  => 'Domain_Configuration_Network_SSL',
  632:                   header => [{col1 => 'Server',
  633:                               col2 => 'Certificate Status'},
  634:                              {col1 => 'Connections to other servers',
  635:                               col2 => 'Rules'},
  636:                              {col1 => 'Connections from other servers',
  637:                               col2 => 'Rules'},
  638:                              {col1 => "Replicating domain's published content",
  639:                               col2 => 'Rules'}],
  640:                   print => \&print_ssl,
  641:                   modify => \&modify_ssl,
  642:                  },
  643:         'trust' =>
  644:                  {text   => 'Trust Settings',
  645:                   help   => 'Domain_Configuration_Trust',
  646:                   header => [{col1 => "Access to this domain's content by others",
  647:                               col2 => 'Rules'},
  648:                              {col1 => "Access to other domain's content by this domain",
  649:                               col2 => 'Rules'},
  650:                              {col1 => "Enrollment in this domain's courses by others",
  651:                               col2 => 'Rules',},
  652:                              {col1 => "Co-author roles in this domain for others",
  653:                               col2 => 'Rules',},
  654:                              {col1 => "Co-author roles for this domain's users elsewhere",
  655:                               col2 => 'Rules',},
  656:                              {col1 => "Domain roles in this domain assignable to others",
  657:                               col2 => 'Rules'},
  658:                              {col1 => "Course catalog for this domain displayed elsewhere",
  659:                               col2 => 'Rules'},
  660:                              {col1 => "Requests for creation of courses in this domain by others",
  661:                               col2 => 'Rules'},
  662:                              {col1 => "Users in other domains can send messages to this domain",
  663:                               col2 => 'Rules'},],
  664:                   print => \&print_trust,
  665:                   modify => \&modify_trust,
  666:                  },
  667:         'lti' =>
  668:                  {text => 'LTI Link Protection and LTI Consumers',
  669:                   help => 'Domain_Configuration_LTI_Provider',
  670:                   header => [{col1 => 'Encryption of shared secrets',
  671:                               col2 => 'Settings'},
  672:                              {col1 => 'Rules for shared secrets',
  673:                               col2 => 'Settings'},
  674:                              {col1 => 'Link Protectors in Courses',
  675:                               col2 => 'Values'},
  676:                              {col1 => 'Link Protectors',
  677:                               col2 => 'Settings'},
  678:                              {col1 => 'Consumers',
  679:                               col2 => 'Settings'},],
  680:                   print => \&print_lti,
  681:                   modify => \&modify_lti,
  682:                  },
  683:         'ipaccess' =>
  684:                        {text => 'IP-based access control',
  685:                         help => 'Domain_Configuration_IP_Access',
  686:                         header => [{col1 => 'Setting',
  687:                                     col2 => 'Value'},],
  688:                         print  => \&print_ipaccess,
  689:                         modify => \&modify_ipaccess,
  690:                        },
  691:         'authordefaults' =>
  692:                             {text => 'Authoring Space defaults',
  693:                              help => 'Domain_Configuration_Author_Defaults',
  694:                              header => [{col1 => 'Defaults which can be overridden by Author',
  695:                                          col2 => 'Settings',},
  696:                                         {col1 => 'Defaults which can be overridden by a Dom. Coord.',
  697:                                          col2 => 'Settings',},],
  698:                              print => \&print_authordefaults,
  699:                              modify => \&modify_authordefaults,
  700:                             },
  701:     );
  702:     if (keys(%servers) > 1) {
  703:         $prefs{'login'}  = { text   => 'Log-in page options',
  704:                              help   => 'Domain_Configuration_Login_Page',
  705:                             header => [{col1 => 'Log-in Service',
  706:                                         col2 => 'Server Setting',},
  707:                                        {col1 => 'Log-in Page Items',
  708:                                         col2 => 'Settings'},
  709:                                        {col1 => 'Log-in Help',
  710:                                         col2 => 'Value'},
  711:                                        {col1 => 'Custom HTML in document head',
  712:                                         col2 => 'Value'},
  713:                                        {col1 => 'SSO',
  714:                                         col2 => 'Dual login: SSO and non-SSO options'},
  715:                                       ],
  716:                             print => \&print_login,
  717:                             modify => \&modify_login,
  718:                            };
  719:     }
  720: 
  721:     my @roles = ('student','coordinator','author','admin');
  722:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  723:     &Apache::lonhtmlcommon::add_breadcrumb
  724:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  725:       text=>"Settings to display/modify"});
  726:     my $confname = $dom.'-domainconfig';
  727: 
  728:     if ($phase eq 'process') {
  729:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  730:                                                               \%prefs,\%domconfig,$confname,\@roles);
  731:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  732:             $r->rflush();
  733:             &devalidate_remote_domconfs($dom,$result);
  734:         }
  735:     } elsif ($phase eq 'display') {
  736:         my $js = &recaptcha_js().
  737:                  &toggle_display_js();
  738:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  739:             my ($othertitle,$usertypes,$types) =
  740:                 &Apache::loncommon::sorted_inst_types($dom);
  741:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  742:                                           $domconfig{'loadbalancing'}).
  743:                    &new_spares_js().
  744:                    &common_domprefs_js().
  745:                    &Apache::loncommon::javascript_array_indexof();
  746:         }
  747:         if (grep(/^requestcourses$/,@actions)) {
  748:             my $javascript_validations;
  749:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  750:             $js .= <<END;
  751: <script type="text/javascript">
  752: $javascript_validations
  753: </script>
  754: $coursebrowserjs
  755: END
  756:         } elsif (grep(/^ipaccess$/,@actions)) {
  757:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  758:         }
  759:         if (grep(/^selfcreation$/,@actions)) {
  760:             $js .= &selfcreate_javascript();
  761:         }
  762:         if (grep(/^contacts$/,@actions)) {
  763:             $js .= &contacts_javascript();
  764:         }
  765:         if (grep(/^scantron$/,@actions)) {
  766:             $js .= &scantron_javascript();
  767:         }
  768:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  769:     } else {
  770: # check if domconfig user exists for the domain.
  771:         my $servadm = $r->dir_config('lonAdmEMail');
  772:         my ($configuserok,$author_ok,$switchserver) =
  773:             &config_check($dom,$confname,$servadm);
  774:         unless ($configuserok eq 'ok') {
  775:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  776:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  777:                           $confname).
  778:                       '<br />'
  779:             );
  780:             if ($switchserver) {
  781:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  782:                           '<br />'.
  783:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  784:                           '<br />'.
  785:                           &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).
  786:                           '<br />'.
  787:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  788:                 );
  789:             } else {
  790:                 $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.').
  791:                           '<br />'.
  792:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  793:                 );
  794:             }
  795:             $r->print(&Apache::loncommon::end_page());
  796:             return OK;
  797:         }
  798:         if (keys(%domconfig) == 0) {
  799:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  800:             my @ids=&Apache::lonnet::current_machine_ids();
  801:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  802:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  803:                 my @loginimages = ('img','logo','domlogo','login');
  804:                 my $custom_img_count = 0;
  805:                 foreach my $img (@loginimages) {
  806:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  807:                         $custom_img_count ++;
  808:                     }
  809:                 }
  810:                 foreach my $role (@roles) {
  811:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  812:                         $custom_img_count ++;
  813:                     }
  814:                 }
  815:                 if ($custom_img_count > 0) {
  816:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  817:                     my $switch_server = &check_switchserver($dom,$confname);
  818:                     $r->print(
  819:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  820:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  821:     &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 />'.
  822:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  823:                     if ($switch_server) {
  824:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  825:                     }
  826:                     $r->print(&Apache::loncommon::end_page());
  827:                     return OK;
  828:                 }
  829:             }
  830:         }
  831:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  832:     }
  833:     return OK;
  834: }
  835: 
  836: sub process_changes {
  837:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  838:     my %domconfig;
  839:     if (ref($values) eq 'HASH') {
  840:         %domconfig = %{$values};
  841:     }
  842:     my $output;
  843:     if ($action eq 'login') {
  844:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  845:     } elsif ($action eq 'rolecolors') {
  846:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  847:                                      $lastactref,%domconfig);
  848:     } elsif ($action eq 'quotas') {
  849:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  850:     } elsif ($action eq 'autoenroll') {
  851:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  852:     } elsif ($action eq 'autoupdate') {
  853:         $output = &modify_autoupdate($dom,%domconfig);
  854:     } elsif ($action eq 'autocreate') {
  855:         $output = &modify_autocreate($dom,%domconfig);
  856:     } elsif ($action eq 'directorysrch') {
  857:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  858:     } elsif ($action eq 'usercreation') {
  859:         $output = &modify_usercreation($dom,%domconfig);
  860:     } elsif ($action eq 'selfcreation') {
  861:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  862:     } elsif ($action eq 'usermodification') {
  863:         $output = &modify_usermodification($dom,%domconfig);
  864:     } elsif ($action eq 'contacts') {
  865:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  866:     } elsif ($action eq 'defaults') {
  867:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  868:     } elsif ($action eq 'scantron') {
  869:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  870:     } elsif ($action eq 'coursecategories') {
  871:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  872:     } elsif ($action eq 'serverstatuses') {
  873:         $output = &modify_serverstatuses($dom,%domconfig);
  874:     } elsif ($action eq 'requestcourses') {
  875:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  876:     } elsif ($action eq 'requestauthor') {
  877:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  878:     } elsif ($action eq 'helpsettings') {
  879:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  880:     } elsif ($action eq 'coursedefaults') {
  881:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  882:     } elsif ($action eq 'selfenrollment') {
  883:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  884:     } elsif ($action eq 'usersessions') {
  885:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  886:     } elsif ($action eq 'loadbalancing') {
  887:         $output = &modify_loadbalancing($dom,%domconfig);
  888:     } elsif ($action eq 'ltitools') {
  889:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  890:     } elsif ($action eq 'proctoring') {
  891:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  892:     } elsif ($action eq 'ssl') {
  893:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  894:     } elsif ($action eq 'trust') {
  895:         $output = &modify_trust($dom,$lastactref,%domconfig);
  896:     } elsif ($action eq 'lti') {
  897:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  898:     } elsif ($action eq 'privacy') {
  899:         $output = &modify_privacy($dom,$lastactref,%domconfig);
  900:     } elsif ($action eq 'passwords') {
  901:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  902:     } elsif ($action eq 'wafproxy') {
  903:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  904:     } elsif ($action eq 'ipaccess') {
  905:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  906:     } elsif ($action eq 'authordefaults') {
  907:         $output = &modify_authordefaults($dom,$lastactref,%domconfig);
  908:     }
  909:     return $output;
  910: }
  911: 
  912: sub print_config_box {
  913:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  914:     my $rowtotal = 0;
  915:     my $output;
  916:     if ($action eq 'coursecategories') {
  917:         $output = &coursecategories_javascript($settings);
  918:     } elsif ($action eq 'defaults') {
  919:         $output = &defaults_javascript($settings); 
  920:     } elsif ($action eq 'passwords') {
  921:         $output = &passwords_javascript($action);
  922:     } elsif ($action eq 'helpsettings') {
  923:         my (%privs,%levelscurrent);
  924:         my %full=();
  925:         my %levels=(
  926:                      course => {},
  927:                      domain => {},
  928:                      system => {},
  929:                    );
  930:         my $context = 'domain';
  931:         my $crstype = 'Course';
  932:         my $formname = 'display';
  933:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  934:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  935:         $output =
  936:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full,
  937:                                                       \@templateroles);
  938:     } elsif ($action eq 'ltitools') {
  939:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  940:     } elsif ($action eq 'lti') {
  941:         $output .= &passwords_javascript('ltisecrets')."\n".
  942:                    &lti_javascript($dom,$settings);
  943:     } elsif ($action eq 'proctoring') {
  944:         $output .= &proctoring_javascript($settings);
  945:     } elsif ($action eq 'wafproxy') {
  946:         $output .= &wafproxy_javascript($dom);
  947:     } elsif ($action eq 'autoupdate') {
  948:         $output .= &autoupdate_javascript();
  949:     } elsif ($action eq 'autoenroll') {
  950:         $output .= &autoenroll_javascript();
  951:     } elsif ($action eq 'login') {
  952:         $output .= &saml_javascript();
  953:     } elsif ($action eq 'ipaccess') {
  954:         $output .= &ipaccess_javascript($settings);
  955:     } elsif ($action eq 'authordefaults') {
  956:         $output .= &authordefaults_javascript();
  957:     }
  958:     $output .=
  959:          '<table class="LC_nested_outer">
  960:           <tr>
  961:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  962:            &mt($item->{text}).'&nbsp;'.
  963:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  964:           '</tr>';
  965:     $rowtotal ++;
  966:     my $numheaders = 1;
  967:     if (ref($item->{'header'}) eq 'ARRAY') {
  968:         $numheaders = scalar(@{$item->{'header'}});
  969:     }
  970:     if ($numheaders > 1) {
  971:         my $colspan = '';
  972:         my $rightcolspan = '';
  973:         my $leftnobr = '';
  974:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  975:             ($action eq 'directorysrch') ||
  976:             (($action eq 'login') && ($numheaders < 5))) {
  977:             $colspan = ' colspan="2"';
  978:         }
  979:         if ($action eq 'usersessions') {
  980:             $rightcolspan = ' colspan="3"'; 
  981:         }
  982:         if ($action eq 'passwords') {
  983:             $leftnobr = ' LC_nobreak';
  984:         }
  985:         $output .= '
  986:           <tr>
  987:            <td>
  988:             <table class="LC_nested">
  989:              <tr class="LC_info_row">
  990:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  991:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  992:              </tr>';
  993:         $rowtotal ++;
  994:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  995:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  996:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  997:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  998:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  999:             ($action eq 'lti') || ($action eq 'ltitools') || ($action eq 'authordefaults')) {
 1000:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
 1001:         } elsif ($action eq 'passwords') {
 1002:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
 1003:         } elsif ($action eq 'coursecategories') {
 1004:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
 1005:         } elsif ($action eq 'scantron') {
 1006:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
 1007:         } elsif ($action eq 'login') {
 1008:             if ($numheaders == 5) {
 1009:                 $colspan = ' colspan="2"';
 1010:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
 1011:             } else {
 1012:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
 1013:             }
 1014:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 1015:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1016:         } elsif ($action eq 'rolecolors') {
 1017:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
 1018:         }
 1019:         $output .= '
 1020:            </table>
 1021:           </td>
 1022:          </tr>
 1023:          <tr>
 1024:            <td>
 1025:             <table class="LC_nested">
 1026:              <tr class="LC_info_row">
 1027:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
 1028:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
 1029:              </tr>';
 1030:             $rowtotal ++;
 1031:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
 1032:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
 1033:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
 1034:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
 1035:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
 1036:             ($action eq 'ltitools') || ($action eq 'usermodification')) {
 1037:             if ($action eq 'coursecategories') {
 1038:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
 1039:                 $colspan = ' colspan="2"';
 1040:             } elsif ($action eq 'trust') {
 1041:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1042:             } elsif ($action eq 'passwords') {
 1043:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1044:             } elsif ($action eq 'lti') {
 1045:                 $output .= $item->{'print'}->('upper',$dom,$settings,\$rowtotal).'
 1046:                            </table>
 1047:                           </td>
 1048:                          </tr>
 1049:                          <tr>
 1050:                           <td>
 1051:                           <table class="LC_nested">
 1052:                            <tr class="LC_info_row">
 1053:                             <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1054:                             <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1055:                            </tr>'."\n".
 1056:                            $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1057:             } else {
 1058:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1059:             }
 1060:             if ($action eq 'trust') {
 1061:                 $output .= '
 1062:             </table>
 1063:           </td>
 1064:          </tr>';
 1065:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1066:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1067:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1068:                     $output .= '
 1069:          <tr>
 1070:            <td>
 1071:             <table class="LC_nested">
 1072:              <tr class="LC_info_row">
 1073:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1074:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1075:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1076:             </table>
 1077:           </td>
 1078:          </tr>';
 1079:                 }
 1080:                 $output .= '
 1081:          <tr>
 1082:            <td>
 1083:             <table class="LC_nested">
 1084:              <tr class="LC_info_row">
 1085:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1086:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1087:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1088:             } else {
 1089:                 my $hdridx = 2;
 1090:                 if ($action eq 'lti') {
 1091:                     $hdridx = 3;
 1092:                 }
 1093:                 $output .= '
 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"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1102:               <td class="LC_right_item">'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td>
 1103:              </tr>'."\n";
 1104:                 if ($action eq 'coursecategories') {
 1105:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1106:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1107:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1108:                     if ($action eq 'passwords') {
 1109:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1110:                     } else {
 1111:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1112:                     }
 1113:                     $hdridx ++;
 1114:                     $output .= '
 1115:              </tr>
 1116:             </table>
 1117:            </td>
 1118:           </tr>
 1119:           <tr>
 1120:            <td>
 1121:             <table class="LC_nested">
 1122:              <tr class="LC_info_row">
 1123:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col1'}).'</td>
 1124:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$hdridx]->{'col2'}).'</td></tr>'."\n";
 1125:                     if ($action eq 'passwords') {
 1126:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1127:                     } else {
 1128:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1129:                     }
 1130:                     $output .= '
 1131:             </table>
 1132:           </td>
 1133:          </tr>
 1134:          <tr>';
 1135:                 } else {
 1136:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1137:                 }
 1138:             }
 1139:             $rowtotal ++;
 1140:         } elsif (($action eq 'coursedefaults') || ($action eq 'authordefaults') ||
 1141:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1142:                  ($action eq 'wafproxy')) {
 1143:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1144:         } elsif ($action eq 'scantron') {
 1145:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1146:         } elsif ($action eq 'ssl') {
 1147:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1148:             </table>
 1149:           </td>
 1150:          </tr>
 1151:          <tr>
 1152:            <td>
 1153:             <table class="LC_nested">
 1154:              <tr class="LC_info_row">
 1155:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1156:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1157:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1158:             </table>
 1159:           </td>
 1160:          </tr>
 1161:          <tr>
 1162:            <td>
 1163:             <table class="LC_nested">
 1164:              <tr class="LC_info_row">
 1165:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1166:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1167:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1168:         } elsif ($action eq 'login') {
 1169:             if ($numheaders == 5) {
 1170:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1171:            </table>
 1172:           </td>
 1173:          </tr>
 1174:          <tr>
 1175:            <td>
 1176:             <table class="LC_nested">
 1177:              <tr class="LC_info_row">
 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></tr>'.
 1180:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1181:                 $rowtotal ++;
 1182:             } else {
 1183:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1184:             }
 1185:             $output .= '
 1186:            </table>
 1187:           </td>
 1188:          </tr>
 1189:          <tr>
 1190:            <td>
 1191:             <table class="LC_nested">
 1192:              <tr class="LC_info_row">';
 1193:             if ($numheaders == 5) {
 1194:                 $output .= '
 1195:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1196:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1197:              </tr>';
 1198:             } else {
 1199:                 $output .= '
 1200:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1201:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1202:              </tr>';
 1203:             }
 1204:             $rowtotal ++;
 1205:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1206:            </table>
 1207:           </td>
 1208:          </tr>
 1209:          <tr>
 1210:            <td>
 1211:             <table class="LC_nested">
 1212:              <tr class="LC_info_row">';
 1213:             if ($numheaders == 5) {
 1214:                 $output .= '
 1215:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1216:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1217:              </tr>';
 1218:             } else {
 1219:                 $output .= '
 1220:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1221:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1222:              </tr>';
 1223:             }
 1224:             $rowtotal ++;
 1225:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1226:         } elsif ($action eq 'requestcourses') {
 1227:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1228:             $rowtotal ++;
 1229:             $output .= &print_studentcode($settings,\$rowtotal).'
 1230:            </table>
 1231:           </td>
 1232:          </tr>
 1233:          <tr>
 1234:            <td>
 1235:             <table class="LC_nested">
 1236:              <tr class="LC_info_row">
 1237:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1238:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1239:                        &textbookcourses_javascript($settings).
 1240:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1241:             </table>
 1242:            </td>
 1243:           </tr>
 1244:          <tr>
 1245:            <td>
 1246:             <table class="LC_nested">
 1247:              <tr class="LC_info_row">
 1248:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1249:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1250:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1251:             </table>
 1252:            </td>
 1253:           </tr>
 1254:           <tr>
 1255:            <td>
 1256:             <table class="LC_nested">
 1257:              <tr class="LC_info_row">
 1258:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1259:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1260:              </tr>'.
 1261:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1262:         } elsif ($action eq 'requestauthor') {
 1263:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1264:             $rowtotal ++;
 1265:         } elsif ($action eq 'rolecolors') {
 1266:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1267:            </table>
 1268:           </td>
 1269:          </tr>
 1270:          <tr>
 1271:            <td>
 1272:             <table class="LC_nested">
 1273:              <tr class="LC_info_row">
 1274:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1275:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1276:               <td class="LC_right_item" style="vertical-align: top">'.
 1277:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1278:              </tr>'.
 1279:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1280:            </table>
 1281:           </td>
 1282:          </tr>
 1283:          <tr>
 1284:            <td>
 1285:             <table class="LC_nested">
 1286:              <tr class="LC_info_row">
 1287:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1288:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1289:              </tr>'.
 1290:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1291:             $rowtotal += 2;
 1292:         }
 1293:     } else {
 1294:         $output .= '
 1295:           <tr>
 1296:            <td>
 1297:             <table class="LC_nested">
 1298:              <tr class="LC_info_row">';
 1299:         if ($action eq 'login') {
 1300:             $output .= '  
 1301:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1302:         } elsif ($action eq 'serverstatuses') {
 1303:             $output .= '
 1304:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1305:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1306: 
 1307:         } else {
 1308:             $output .= '
 1309:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1310:         }
 1311:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1312:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1313:                        &mt($item->{'header'}->[0]->{'col2'});
 1314:             if ($action eq 'serverstatuses') {
 1315:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1316:             } 
 1317:         } else {
 1318:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1319:                        &mt($item->{'header'}->[0]->{'col2'});
 1320:         }
 1321:         $output .= '</td>';
 1322:         if ($item->{'header'}->[0]->{'col3'}) {
 1323:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1324:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1325:                             &mt($item->{'header'}->[0]->{'col3'});
 1326:             } else {
 1327:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1328:                            &mt($item->{'header'}->[0]->{'col3'});
 1329:             }
 1330:             if ($action eq 'serverstatuses') {
 1331:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1332:             }
 1333:             $output .= '</td>';
 1334:         }
 1335:         if ($item->{'header'}->[0]->{'col4'}) {
 1336:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1337:                        &mt($item->{'header'}->[0]->{'col4'});
 1338:         }
 1339:         $output .= '</tr>';
 1340:         $rowtotal ++;
 1341:         if ($action eq 'quotas') {
 1342:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1343:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1344:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1345:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1346:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1347:         }
 1348:     }
 1349:     $output .= '
 1350:    </table>
 1351:   </td>
 1352:  </tr>
 1353: </table><br />';
 1354:     return ($output,$rowtotal);
 1355: }
 1356: 
 1357: sub print_login {
 1358:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1359:     my ($css_class,$datatable,$switchserver,%lt);
 1360:     my %choices = &login_choices();
 1361:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1362:         %lt = &login_file_options();
 1363:         $switchserver = &check_switchserver($dom,$confname);
 1364:     }
 1365:     if ($caller eq 'service') {
 1366:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1367:         my $choice = $choices{'disallowlogin'};
 1368:         $css_class = ' class="LC_odd_row"';
 1369:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1370:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1371:                       '<th>'.$choices{'server'}.'</th>'.
 1372:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1373:                       '<th>'.$choices{'custompath'}.'</th>'.
 1374:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1375:         my %disallowed;
 1376:         if (ref($settings) eq 'HASH') {
 1377:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1378:                %disallowed = %{$settings->{'loginvia'}};
 1379:             }
 1380:         }
 1381:         foreach my $lonhost (sort(keys(%servers))) {
 1382:             my $direct = 'selected="selected"';
 1383:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1384:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1385:                     $direct = '';
 1386:                 }
 1387:             }
 1388:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1389:                           '<td><select name="'.$lonhost.'_server">'.
 1390:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1391:                           '</option>';
 1392:             foreach my $hostid (sort(keys(%servers))) {
 1393:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1394:                 my $selected = '';
 1395:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1396:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1397:                         $selected = 'selected="selected"';
 1398:                     }
 1399:                 }
 1400:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1401:                               $servers{$hostid}.'</option>';
 1402:             }
 1403:             $datatable .= '</select></td>'.
 1404:                           '<td><select name="'.$lonhost.'_serverpath">';
 1405:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1406:                 my $pathname = $path;
 1407:                 if ($path eq 'custom') {
 1408:                     $pathname = &mt('Custom Path').' ->';
 1409:                 }
 1410:                 my $selected = '';
 1411:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1412:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1413:                         $selected = 'selected="selected"';
 1414:                     }
 1415:                 } elsif ($path eq '') {
 1416:                     $selected = 'selected="selected"';
 1417:                 }
 1418:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1419:             }
 1420:             $datatable .= '</select></td>';
 1421:             my ($custom,$exempt);
 1422:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1423:                 $custom = $disallowed{$lonhost}{'custompath'};
 1424:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1425:             }
 1426:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1427:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1428:                           '</tr>';
 1429:         }
 1430:         $datatable .= '</table></td></tr>';
 1431:         return $datatable;
 1432:     } elsif ($caller eq 'page') {
 1433:         my %defaultchecked = ( 
 1434:                                'coursecatalog' => 'on',
 1435:                                'helpdesk'      => 'on',
 1436:                                'adminmail'     => 'off',
 1437:                                'newuser'       => 'off',
 1438:                              );
 1439:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1440:         my (%checkedon,%checkedoff);
 1441:         foreach my $item (@toggles) {
 1442:             if ($defaultchecked{$item} eq 'on') { 
 1443:                 $checkedon{$item} = ' checked="checked" ';
 1444:                 $checkedoff{$item} = ' ';
 1445:             } elsif ($defaultchecked{$item} eq 'off') {
 1446:                 $checkedoff{$item} = ' checked="checked" ';
 1447:                 $checkedon{$item} = ' ';
 1448:             }
 1449:         }
 1450:         my @images = ('img','logo','domlogo','login');
 1451:         my @alttext = ('img','logo','domlogo');
 1452:         my @logintext = ('textcol','bgcol');
 1453:         my @bgs = ('pgbg','mainbg','sidebg');
 1454:         my @links = ('link','alink','vlink');
 1455:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1456:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1457:         my (%is_custom,%designs);
 1458:         my %defaults = (
 1459:                        font => $defaultdesign{'login.font'},
 1460:                        );
 1461:         foreach my $item (@images) {
 1462:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1463:             $defaults{'showlogo'}{$item} = 1;
 1464:         }
 1465:         foreach my $item (@bgs) {
 1466:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1467:         }
 1468:         foreach my $item (@logintext) {
 1469:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1470:         }
 1471:         foreach my $item (@links) {
 1472:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1473:         }
 1474:         if (ref($settings) eq 'HASH') {
 1475:             foreach my $item (@toggles) {
 1476:                 if ($settings->{$item} eq '1') {
 1477:                     $checkedon{$item} =  ' checked="checked" ';
 1478:                     $checkedoff{$item} = ' ';
 1479:                 } elsif ($settings->{$item} eq '0') {
 1480:                     $checkedoff{$item} =  ' checked="checked" ';
 1481:                     $checkedon{$item} = ' ';
 1482:                 }
 1483:             }
 1484:             foreach my $item (@images) {
 1485:                 if (defined($settings->{$item})) {
 1486:                     $designs{$item} = $settings->{$item};
 1487:                     $is_custom{$item} = 1;
 1488:                 }
 1489:                 if (defined($settings->{'showlogo'}{$item})) {
 1490:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1491:                 }
 1492:             }
 1493:             foreach my $item (@alttext) {
 1494:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1495:                     if ($settings->{'alttext'}->{$item} ne '') {
 1496:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1497:                     }
 1498:                 }
 1499:             }
 1500:             foreach my $item (@logintext) {
 1501:                 if ($settings->{$item} ne '') {
 1502:                     $designs{'logintext'}{$item} = $settings->{$item};
 1503:                     $is_custom{$item} = 1;
 1504:                 }
 1505:             }
 1506:             if ($settings->{'font'} ne '') {
 1507:                 $designs{'font'} = $settings->{'font'};
 1508:                 $is_custom{'font'} = 1;
 1509:             }
 1510:             foreach my $item (@bgs) {
 1511:                 if ($settings->{$item} ne '') {
 1512:                     $designs{'bgs'}{$item} = $settings->{$item};
 1513:                     $is_custom{$item} = 1;
 1514:                 }
 1515:             }
 1516:             foreach my $item (@links) {
 1517:                 if ($settings->{$item} ne '') {
 1518:                     $designs{'links'}{$item} = $settings->{$item};
 1519:                     $is_custom{$item} = 1;
 1520:                 }
 1521:             }
 1522:         } else {
 1523:             if ($designhash{$dom.'.login.font'} ne '') {
 1524:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1525:                 $is_custom{'font'} = 1;
 1526:             }
 1527:             foreach my $item (@images) {
 1528:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1529:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1530:                     $is_custom{$item} = 1;
 1531:                 }
 1532:             }
 1533:             foreach my $item (@bgs) {
 1534:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1535:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1536:                     $is_custom{$item} = 1;
 1537:                 }
 1538:             }
 1539:             foreach my $item (@links) {
 1540:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1541:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1542:                     $is_custom{$item} = 1;
 1543:                 }
 1544:             }
 1545:         }
 1546:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1547:                                                       logo => 'Institution Logo',
 1548:                                                       domlogo => 'Domain Logo',
 1549:                                                       login => 'Login box');
 1550:         my $itemcount = 1;
 1551:         foreach my $item (@toggles) {
 1552:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1553:             $datatable .=  
 1554:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1555:                 '</td><td>'.
 1556:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1557:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1558:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1559:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1560:                 '</tr>';
 1561:             $itemcount ++;
 1562:         }
 1563:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1564:         $datatable .= '</tr></table></td></tr>';
 1565:     } elsif ($caller eq 'help') {
 1566:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1567:         my $itemcount = 1;
 1568:         $defaulturl = '/adm/loginproblems.html';
 1569:         $defaulttype = 'default';
 1570:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1571:         my @currlangs;
 1572:         if (ref($settings) eq 'HASH') {
 1573:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1574:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1575:                     next if ($settings->{'helpurl'}{$key} eq '');
 1576:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1577:                     $type{$key} = 'custom';
 1578:                     unless ($key eq 'nolang') {
 1579:                         push(@currlangs,$key);
 1580:                     }
 1581:                 }
 1582:             } elsif ($settings->{'helpurl'} ne '') {
 1583:                 $type{'nolang'} = 'custom';
 1584:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1585:             }
 1586:         }
 1587:         foreach my $lang ('nolang',sort(@currlangs)) {
 1588:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1589:             $datatable .= '<tr'.$css_class.'>';
 1590:             if ($url{$lang} eq '') {
 1591:                 $url{$lang} = $defaulturl;
 1592:             }
 1593:             if ($type{$lang} eq '') {
 1594:                 $type{$lang} = $defaulttype;
 1595:             }
 1596:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1597:             if ($lang eq 'nolang') {
 1598:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1599:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1600:             } else {
 1601:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1602:                                   $langchoices{$lang},
 1603:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1604:             }
 1605:             $datatable .= '</span></td>'."\n".
 1606:                           '<td class="LC_left_item">';
 1607:             if ($type{$lang} eq 'custom') {
 1608:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1609:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1610:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1611:             } else {
 1612:                 $datatable .= $lt{'upl'};
 1613:             }
 1614:             $datatable .='<br />';
 1615:             if ($switchserver) {
 1616:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1617:             } else {
 1618:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1619:             }
 1620:             $datatable .= '</td></tr>';
 1621:             $itemcount ++;
 1622:         }
 1623:         my @addlangs;
 1624:         foreach my $lang (sort(keys(%langchoices))) {
 1625:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1626:             push(@addlangs,$lang);
 1627:         }
 1628:         if (@addlangs > 0) {
 1629:             my %toadd;
 1630:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1631:             $toadd{''} = &mt('Select');
 1632:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1633:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1634:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1635:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1636:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1637:             if ($switchserver) {
 1638:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1639:             } else {
 1640:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1641:             }
 1642:             $datatable .= '</td></tr>';
 1643:             $itemcount ++;
 1644:         }
 1645:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1646:     } elsif ($caller eq 'headtag') {
 1647:         my %domservers = &Apache::lonnet::get_servers($dom);
 1648:         my $choice = $choices{'headtag'};
 1649:         $css_class = ' class="LC_odd_row"';
 1650:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1651:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1652:                       '<th>'.$choices{'current'}.'</th>'.
 1653:                       '<th>'.$choices{'action'}.'</th>'.
 1654:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1655:         my (%currurls,%currexempt);
 1656:         if (ref($settings) eq 'HASH') {
 1657:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1658:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1659:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1660:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1661:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1662:                     }
 1663:                 }
 1664:             }
 1665:         }
 1666:         foreach my $lonhost (sort(keys(%domservers))) {
 1667:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1668:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1669:             if ($currurls{$lonhost}) {
 1670:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1671:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1672:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1673:                               '">'.$lt{'curr'}.'</a></td>'.
 1674:                               '<td><span class="LC_nobreak"><label>'.
 1675:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1676:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1677:             } else {
 1678:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1679:             }
 1680:             $datatable .='<br />';
 1681:             if ($switchserver) {
 1682:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1683:             } else {
 1684:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1685:             }
 1686:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1687:         }
 1688:         $datatable .= '</table></td></tr>';
 1689:     } elsif ($caller eq 'saml') {
 1690:         my %domservers = &Apache::lonnet::get_servers($dom);
 1691:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1692:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1693:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1694:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1695:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1696:         foreach my $lonhost (keys(%domservers)) {
 1697:             $samlurl{$lonhost} = '/adm/sso';
 1698:             $styleon{$lonhost} = 'display:none';
 1699:             $styleoff{$lonhost} = '';
 1700:         }
 1701:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1702:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1703:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1704:                     $saml{$lonhost} = 1;
 1705:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1706:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1707:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1708:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1709:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1710:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1711:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1712:                     $styleon{$lonhost} = '';
 1713:                     $styleoff{$lonhost} = 'display:none';
 1714:                 } else {
 1715:                     $styleon{$lonhost} = 'display:none';
 1716:                     $styleoff{$lonhost} = '';
 1717:                 }
 1718:             }
 1719:         }
 1720:         my $itemcount = 1;
 1721:         foreach my $lonhost (sort(keys(%domservers))) {
 1722:             my $samlon = ' ';
 1723:             my $samloff = ' checked="checked" ';
 1724:             if ($saml{$lonhost}) {
 1725:                 $samlon = $samloff;
 1726:                 $samloff = ' ';
 1727:             }
 1728:             my $samlwinon = '';
 1729:             my $samlwinoff = ' checked="checked"';
 1730:             if ($samlwindow{$lonhost}) {
 1731:                 $samlwinon = $samlwinoff;
 1732:                 $samlwinoff = '';
 1733:             }
 1734:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1735:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1736:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1737:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1738:                           &mt('No').'</label>'.('&nbsp;'x2).
 1739:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1740:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1741:                           &mt('Yes').'</label></span></td>'.
 1742:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1743:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1744:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1745:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1746:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1747:                           $samltext{$lonhost}.'" /></td><td>';
 1748:             if ($samlimg{$lonhost}) {
 1749:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1750:                               '<span class="LC_nobreak"><label>'.
 1751:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1752:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1753:             } else {
 1754:                 $datatable .= $lt{'upl'};
 1755:             }
 1756:             $datatable .='<br />';
 1757:             if ($switchserver) {
 1758:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1759:             } else {
 1760:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1761:             }
 1762:             $datatable .= '</td>'.
 1763:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1764:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1765:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1766:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1767:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1768:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1769:                           '<tr'.$css_class.'>'.
 1770:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1771:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1772:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1773:                           $samltitle{$lonhost}.'</textarea></td>'.
 1774:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1775:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1776:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1777:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1778:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1779:                           '</table></td>'.
 1780:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1781:            $itemcount ++;
 1782:         }
 1783:         $datatable .= '</table></td></tr>';
 1784:     }
 1785:     return $datatable;
 1786: }
 1787: 
 1788: sub login_choices {
 1789:     my %choices =
 1790:         &Apache::lonlocal::texthash (
 1791:             coursecatalog => 'Display Course/Community Catalog link?',
 1792:             adminmail     => "Display Administrator's E-mail Address?",
 1793:             helpdesk      => 'Display "Contact Helpdesk" link',
 1794:             disallowlogin => "Login page requests redirected",
 1795:             hostid        => "Server",
 1796:             server        => "Redirect to:",
 1797:             serverpath    => "Path",
 1798:             custompath    => "Custom", 
 1799:             exempt        => "Exempt IP(s)",
 1800:             directlogin   => "No redirect",
 1801:             newuser       => "Link to create a user account",
 1802:             img           => "Header",
 1803:             logo          => "Main Logo",
 1804:             domlogo       => "Domain Logo",
 1805:             login         => "Log-in Header", 
 1806:             textcol       => "Text color",
 1807:             bgcol         => "Box color",
 1808:             bgs           => "Background colors",
 1809:             links         => "Link colors",
 1810:             font          => "Font color",
 1811:             pgbg          => "Header",
 1812:             mainbg        => "Page",
 1813:             sidebg        => "Login box",
 1814:             link          => "Link",
 1815:             alink         => "Active link",
 1816:             vlink         => "Visited link",
 1817:             headtag       => "Custom markup",
 1818:             action        => "Action",
 1819:             current       => "Current",
 1820:             samllanding   => "Dual login?",
 1821:             samloptions   => "Options",
 1822:             alttext       => "Alt text",
 1823:         );
 1824:     return %choices;
 1825: }
 1826: 
 1827: sub login_file_options {
 1828:       return &Apache::lonlocal::texthash(
 1829:                                            del     => 'Delete?',
 1830:                                            rep     => 'Replace:',
 1831:                                            upl     => 'Upload:',
 1832:                                            curr    => 'View contents',
 1833:                                            default => 'Default',
 1834:                                            custom  => 'Custom',
 1835:                                            none    => 'None',
 1836:       );
 1837: }
 1838: 
 1839: sub print_ipaccess {
 1840:     my ($dom,$settings,$rowtotal) = @_;
 1841:     my $css_class;
 1842:     my $itemcount = 0;
 1843:     my $datatable;
 1844:     my %ordered;
 1845:     if (ref($settings) eq 'HASH') {
 1846:         foreach my $item (keys(%{$settings})) {
 1847:             if (ref($settings->{$item}) eq 'HASH') {
 1848:                 my $num = $settings->{$item}{'order'};
 1849:                 if ($num eq '') {
 1850:                     $num = scalar(keys(%{$settings}));
 1851:                 }
 1852:                 $ordered{$num} = $item;
 1853:             }
 1854:         }
 1855:     }
 1856:     my $maxnum = scalar(keys(%ordered));
 1857:     if (keys(%ordered)) {
 1858:         my @items = sort { $a <=> $b } keys(%ordered);
 1859:         for (my $i=0; $i<@items; $i++) {
 1860:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1861:             my $item = $ordered{$items[$i]};
 1862:             my ($name,$ipranges,%commblocks,%courses);
 1863:             if (ref($settings->{$item}) eq 'HASH') {
 1864:                 $name = $settings->{$item}->{'name'};
 1865:                 $ipranges = $settings->{$item}->{'ip'};
 1866:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1867:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1868:                 }
 1869:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1870:                     %courses = %{$settings->{$item}->{'courses'}};
 1871:                 }
 1872:             }
 1873:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1874:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1875:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1876:             for (my $k=0; $k<=$maxnum; $k++) {
 1877:                 my $vpos = $k+1;
 1878:                 my $selstr;
 1879:                 if ($k == $i) {
 1880:                     $selstr = ' selected="selected" ';
 1881:                 }
 1882:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1883:             }
 1884:             $datatable .= '</select>'.('&nbsp;'x2).
 1885:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1886:                 &mt('Delete?').'</label></span></td>'.
 1887:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1888:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1889:                 '</td></tr>';
 1890:             $itemcount ++;
 1891:         }
 1892:     }
 1893:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1894:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1895:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1896:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1897:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1898:     for (my $k=0; $k<$maxnum+1; $k++) {
 1899:         my $vpos = $k+1;
 1900:         my $selstr;
 1901:         if ($k == $maxnum) {
 1902:             $selstr = ' selected="selected" ';
 1903:         }
 1904:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1905:     }
 1906:     $datatable .= '</select>&nbsp;'."\n".
 1907:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1908:                   '<td colspan="2">'.
 1909:                   &ipaccess_options('add',$itemcount,$dom).
 1910:                   '</td>'."\n".
 1911:                   '</tr>'."\n";
 1912:     $$rowtotal ++;
 1913:     return $datatable;
 1914: }
 1915: 
 1916: sub ipaccess_options {
 1917:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1918:     my (%currblocks,%currcourses,$output);
 1919:     if (ref($blocksref) eq 'HASH') {
 1920:         %currblocks = %{$blocksref};
 1921:     }
 1922:     if (ref($coursesref) eq 'HASH') {
 1923:         %currcourses = %{$coursesref};
 1924:     }
 1925:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1926:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1927:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1928:               '</span></fieldset>'.
 1929:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1930:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1931:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1932:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1933:               $ipranges.'</textarea></fieldset>'.
 1934:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1935:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1936:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1937:               '<table>';
 1938:     foreach my $cid (sort(keys(%currcourses))) {
 1939:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1940:         $output .= '<tr><td><span class="LC_nobreak">'.
 1941:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1942:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1943:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1944:     }
 1945:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1946:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1947:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1948:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1949:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1950:                '</span></td></tr></table>'."\n".
 1951:                '</fieldset>';
 1952:     return $output;
 1953: }
 1954: 
 1955: sub blocker_checkboxes {
 1956:     my ($num,$blocks) = @_;
 1957:     my ($typeorder,$types) = &commblocktype_text();
 1958:     my $numinrow = 6;
 1959:     my $output = '<table>';
 1960:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1961:         my $block = $typeorder->[$i];
 1962:         my $blockstatus;
 1963:         if (ref($blocks) eq 'HASH') {
 1964:             if ($blocks->{$block} eq 'on') {
 1965:                 $blockstatus = 'checked="checked"';
 1966:             }
 1967:         }
 1968:         my $rem = $i%($numinrow);
 1969:         if ($rem == 0) {
 1970:             if ($i > 0) {
 1971:                 $output .= '</tr>';
 1972:             }
 1973:             $output .= '<tr>';
 1974:         }
 1975:         if ($i == scalar(@{$typeorder})-1) {
 1976:             my $colsleft = $numinrow-$rem;
 1977:             if ($colsleft > 1) {
 1978:                 $output .= '<td colspan="'.$colsleft.'">';
 1979:             } else {
 1980:                 $output .= '<td>';
 1981:             }
 1982:         } else {
 1983:             $output .= '<td>';
 1984:         }
 1985:         my $item = 'ipaccess_block_'.$num;
 1986:         if ($blockstatus) {
 1987:             $blockstatus = ' '.$blockstatus;
 1988:         }
 1989:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1990:                    '<input type="checkbox" name="'.$item.'"'.
 1991:                    $blockstatus.' value="'.$block.'"'.' />'.
 1992:                    $types->{$block}.'</label></span>'."\n".
 1993:                    '<br /></td>';
 1994:     }
 1995:     $output .= '</tr></table>';
 1996:     return $output;
 1997: }
 1998: 
 1999: sub commblocktype_text {
 2000:     my %types = &Apache::lonlocal::texthash(
 2001:         'com' => 'Messaging',
 2002:         'chat' => 'Chat Room',
 2003:         'boards' => 'Discussion',
 2004:         'port' => 'Portfolio',
 2005:         'groups' => 'Groups',
 2006:         'blogs' => 'Blogs',
 2007:         'about' => 'User Information',
 2008:         'printout' => 'Printouts',
 2009:         'passwd' => 'Change Password',
 2010:         'grades' => 'Gradebook',
 2011:         'search' => 'Course search',
 2012:         'index'  => 'Course content index',
 2013:         'wishlist' => 'Stored links',
 2014:         'annotate' => 'Annotations',
 2015:     );
 2016:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','index','annotate','passwd'];
 2017:     return ($typeorder,\%types);
 2018: }
 2019: 
 2020: sub print_rolecolors {
 2021:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 2022:     my %choices = &color_font_choices();
 2023:     my @bgs = ('pgbg','tabbg','sidebg');
 2024:     my @links = ('link','alink','vlink');
 2025:     my @images = ();
 2026:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 2027:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 2028:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2029:     my (%is_custom,%designs);
 2030:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 2031:     if (ref($settings) eq 'HASH') {
 2032:         if (ref($settings->{$role}) eq 'HASH') {
 2033:             if ($settings->{$role}->{'font'} ne '') {
 2034:                 $designs{'font'} = $settings->{$role}->{'font'};
 2035:                 $is_custom{'font'} = 1;
 2036:             }
 2037:             if ($settings->{$role}->{'fontmenu'} ne '') {
 2038:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 2039:                 $is_custom{'fontmenu'} = 1;
 2040:             }
 2041:             foreach my $item (@bgs) {
 2042:                 if ($settings->{$role}->{$item} ne '') {
 2043:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 2044:                     $is_custom{$item} = 1;
 2045:                 }
 2046:             }
 2047:             foreach my $item (@links) {
 2048:                 if ($settings->{$role}->{$item} ne '') {
 2049:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 2050:                     $is_custom{$item} = 1;
 2051:                 }
 2052:             }
 2053:         }
 2054:     } else {
 2055:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2056:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2057:             $is_custom{'fontmenu'} = 1; 
 2058:         }
 2059:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2060:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2061:             $is_custom{'font'} = 1;
 2062:         }
 2063:         foreach my $item (@bgs) {
 2064:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2065:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2066:                 $is_custom{$item} = 1;
 2067:             
 2068:             }
 2069:         }
 2070:         foreach my $item (@links) {
 2071:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2072:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2073:                 $is_custom{$item} = 1;
 2074:             }
 2075:         }
 2076:     }
 2077:     my $itemcount = 1;
 2078:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2079:     $datatable .= '</tr></table></td></tr>';
 2080:     return $datatable;
 2081: }
 2082: 
 2083: sub role_defaults {
 2084:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2085:     my %defaults;
 2086:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2087:         return %defaults;
 2088:     }
 2089:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2090:     if ($role eq 'login') {
 2091:         %defaults = (
 2092:                        font => $defaultdesign{$role.'.font'},
 2093:                     );
 2094:         if (ref($logintext) eq 'ARRAY') {
 2095:             foreach my $item (@{$logintext}) {
 2096:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2097:             }
 2098:         }
 2099:         foreach my $item (@{$images}) {
 2100:             $defaults{'showlogo'}{$item} = 1;
 2101:         }
 2102:     } else {
 2103:         %defaults = (
 2104:                        font => $defaultdesign{$role.'.font'},
 2105:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2106:                     );
 2107:     }
 2108:     foreach my $item (@{$bgs}) {
 2109:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2110:     }
 2111:     foreach my $item (@{$links}) {
 2112:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2113:     }
 2114:     foreach my $item (@{$images}) {
 2115:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2116:     }
 2117:     return %defaults;
 2118: }
 2119: 
 2120: sub display_color_options {
 2121:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2122:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2123:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2124:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2125:     my $datatable = '<tr'.$css_class.'>'.
 2126:         '<td>'.$choices->{'font'}.'</td>';
 2127:     if (!$is_custom->{'font'}) {
 2128:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2129:     } else {
 2130:         $datatable .= '<td>&nbsp;</td>';
 2131:     }
 2132:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2133: 
 2134:     $datatable .= '<td><span class="LC_nobreak">'.
 2135:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2136:                   ' value="'.$current_color.'" />&nbsp;'.
 2137:                   '&nbsp;</span></td></tr>';
 2138:     unless ($role eq 'login') { 
 2139:         $datatable .= '<tr'.$css_class.'>'.
 2140:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2141:         if (!$is_custom->{'fontmenu'}) {
 2142:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2143:         } else {
 2144:             $datatable .= '<td>&nbsp;</td>';
 2145:         }
 2146: 	$current_color = $designs->{'fontmenu'} ?
 2147: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2148:         $datatable .= '<td><span class="LC_nobreak">'.
 2149:                       '<input class="colorchooser" type="text" size="10" name="'
 2150: 		      .$role.'_fontmenu"'.
 2151:                       ' value="'.$current_color.'" />&nbsp;'.
 2152:                       '&nbsp;</span></td></tr>';
 2153:     }
 2154:     my $switchserver = &check_switchserver($dom,$confname);
 2155:     foreach my $img (@{$images}) {
 2156: 	$itemcount ++;
 2157:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2158:         $datatable .= '<tr'.$css_class.'>'.
 2159:                       '<td>'.$choices->{$img};
 2160:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2161:         if ($role eq 'login') {
 2162:             if ($img eq 'login') {
 2163:                 $login_hdr_pick =
 2164:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2165:                 $logincolors =
 2166:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2167:                                        $designs,$defaults);
 2168:             } else {
 2169:                 if ($img ne 'domlogo') {
 2170:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2171:                 }
 2172:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2173:                     $alttext = $designs->{'alttext'}{$img};
 2174:                 }
 2175:             }
 2176:         }
 2177:         $datatable .= '</td>';
 2178:         if ($designs->{$img} ne '') {
 2179:             $imgfile = $designs->{$img};
 2180: 	    $img_import = ($imgfile =~ m{^/adm/});
 2181:         } else {
 2182:             $imgfile = $defaults->{$img};
 2183:         }
 2184:         if ($imgfile) {
 2185:             my ($showfile,$fullsize);
 2186:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2187:                 my $urldir = $1;
 2188:                 my $filename = $2;
 2189:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2190:                 if (@info) {
 2191:                     my $thumbfile = 'tn-'.$filename;
 2192:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2193:                     if (@thumb) {
 2194:                         $showfile = $urldir.'/'.$thumbfile;
 2195:                     } else {
 2196:                         $showfile = $imgfile;
 2197:                     }
 2198:                 } else {
 2199:                     $showfile = '';
 2200:                 }
 2201:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2202:                 $showfile = $imgfile;
 2203:                 my $imgdir = $1;
 2204:                 my $filename = $2;
 2205:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2206:                     $showfile = "/$imgdir/tn-".$filename;
 2207:                 } else {
 2208:                     my $input = $londocroot.$imgfile;
 2209:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2210:                     if (!-e $output) {
 2211:                         my ($width,$height) = &thumb_dimensions();
 2212:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2213:                         if ($fullwidth ne '' && $fullheight ne '') {
 2214:                             if ($fullwidth > $width && $fullheight > $height) { 
 2215:                                 my $size = $width.'x'.$height;
 2216:                                 my @args = ('convert','-sample',$size,$input,$output);
 2217:                                 system({$args[0]} @args);
 2218:                                 $showfile = "/$imgdir/tn-".$filename;
 2219:                             }
 2220:                         }
 2221:                     }
 2222:                 }
 2223:             }
 2224:             if ($showfile) {
 2225:                 if ($showfile =~ m{^/(adm|res)/}) {
 2226:                     if ($showfile =~ m{^/res/}) {
 2227:                         my $local_showfile =
 2228:                             &Apache::lonnet::filelocation('',$showfile);
 2229:                         &Apache::lonnet::repcopy($local_showfile);
 2230:                     }
 2231:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2232:                 }
 2233:                 if ($imgfile) {
 2234:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2235:                         if ($imgfile =~ m{^/res/}) {
 2236:                             my $local_imgfile =
 2237:                                 &Apache::lonnet::filelocation('',$imgfile);
 2238:                             &Apache::lonnet::repcopy($local_imgfile);
 2239:                         }
 2240:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2241:                     } else {
 2242:                         $fullsize = $imgfile;
 2243:                     }
 2244:                 }
 2245:                 $datatable .= '<td>';
 2246:                 if ($img eq 'login') {
 2247:                     $datatable .= $login_hdr_pick;
 2248:                 } 
 2249:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2250:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2251:             } else {
 2252:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2253:                               &mt('Upload:').'<br />';
 2254:             }
 2255:         } else {
 2256:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2257:                           &mt('Upload:').'<br />';
 2258:         }
 2259:         if ($switchserver) {
 2260:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2261:         } else {
 2262:             if ($img ne 'login') { # suppress file selection for Log-in header
 2263:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2264:             }
 2265:         }
 2266:         if (($role eq 'login') && ($img ne 'login')) {
 2267:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2268:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2269:                           '</label></span>';
 2270:         }
 2271:         $datatable .= '</td></tr>';
 2272:     }
 2273:     $itemcount ++;
 2274:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2275:     $datatable .= '<tr'.$css_class.'>'.
 2276:                   '<td>'.$choices->{'bgs'}.'</td>';
 2277:     my $bgs_def;
 2278:     foreach my $item (@{$bgs}) {
 2279:         if (!$is_custom->{$item}) {
 2280:             $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>';
 2281:         }
 2282:     }
 2283:     if ($bgs_def) {
 2284:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2285:     } else {
 2286:         $datatable .= '<td>&nbsp;</td>';
 2287:     }
 2288:     $datatable .= '<td class="LC_right_item">'.
 2289:                   '<table border="0"><tr>';
 2290: 
 2291:     foreach my $item (@{$bgs}) {
 2292:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2293: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2294:         if ($designs->{'bgs'}{$item}) {
 2295:             $datatable .= '&nbsp;';
 2296:         }
 2297:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2298:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2299:     }
 2300:     $datatable .= '</tr></table></td></tr>';
 2301:     $itemcount ++;
 2302:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2303:     $datatable .= '<tr'.$css_class.'>'.
 2304:                   '<td>'.$choices->{'links'}.'</td>';
 2305:     my $links_def;
 2306:     foreach my $item (@{$links}) {
 2307:         if (!$is_custom->{$item}) {
 2308:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2309:         }
 2310:     }
 2311:     if ($links_def) {
 2312:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2313:     } else {
 2314:         $datatable .= '<td>&nbsp;</td>';
 2315:     }
 2316:     $datatable .= '<td class="LC_right_item">'.
 2317:                   '<table border="0"><tr>';
 2318:     foreach my $item (@{$links}) {
 2319: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2320:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2321:         if ($designs->{'links'}{$item}) {
 2322:             $datatable.='&nbsp;';
 2323:         }
 2324:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2325:                       '" /></td>';
 2326:     }
 2327:     $$rowtotal += $itemcount;
 2328:     return $datatable;
 2329: }
 2330: 
 2331: sub logo_display_options {
 2332:     my ($img,$defaults,$designs) = @_;
 2333:     my $checkedon;
 2334:     if (ref($defaults) eq 'HASH') {
 2335:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2336:             if ($defaults->{'showlogo'}{$img}) {
 2337:                 $checkedon = 'checked="checked" ';     
 2338:             }
 2339:         } 
 2340:     }
 2341:     if (ref($designs) eq 'HASH') {
 2342:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2343:             if (defined($designs->{'showlogo'}{$img})) {
 2344:                 if ($designs->{'showlogo'}{$img} == 0) {
 2345:                     $checkedon = '';
 2346:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2347:                     $checkedon = 'checked="checked" ';
 2348:                 }
 2349:             }
 2350:         }
 2351:     }
 2352:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2353:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2354:            &mt('show').'</label>'."\n";
 2355: }
 2356: 
 2357: sub login_header_options  {
 2358:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2359:     my $output = '';
 2360:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2361:         $output .= &mt('Text default(s):').'<br />';
 2362:         if (!$is_custom->{'textcol'}) {
 2363:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2364:                        '&nbsp;&nbsp;&nbsp;';
 2365:         }
 2366:         if (!$is_custom->{'bgcol'}) {
 2367:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2368:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2369:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2370:         }
 2371:         $output .= '<br />';
 2372:     }
 2373:     $output .='<br />';
 2374:     return $output;
 2375: }
 2376: 
 2377: sub login_text_colors {
 2378:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2379:     my $color_menu = '<table border="0"><tr>';
 2380:     foreach my $item (@{$logintext}) {
 2381:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2382:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2383:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2384:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2385:     }
 2386:     $color_menu .= '</tr></table><br />';
 2387:     return $color_menu;
 2388: }
 2389: 
 2390: sub image_changes {
 2391:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2392:     my $output;
 2393:     if ($img eq 'login') {
 2394:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2395:     } elsif (!$is_custom) {
 2396:         if ($img ne 'domlogo') {
 2397:             $output = &mt('Default image:').'<br />';
 2398:         } else {
 2399:             $output = &mt('Default in use:').'<br />';
 2400:         }
 2401:     }
 2402:     if ($img ne 'login') {
 2403:         if ($img_import) {
 2404:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2405:         }
 2406:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2407:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2408:         if ($is_custom) {
 2409:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2410:                        '<input type="checkbox" name="'.
 2411:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2412:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2413:         } else {
 2414:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2415:         }
 2416:     }
 2417:     return $output;
 2418: }
 2419: 
 2420: sub print_quotas {
 2421:     my ($dom,$settings,$rowtotal,$action) = @_;
 2422:     my $context;
 2423:     if ($action eq 'quotas') {
 2424:         $context = 'tools';
 2425:     } else {
 2426:         $context = $action;
 2427:     }
 2428:     my ($datatable,$defaultquota,@usertools,@options,%validations);
 2429:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2430:     my $typecount = 0;
 2431:     my ($css_class,%titles);
 2432:     if ($context eq 'requestcourses') {
 2433:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2434:         @options =('norequest','approval','validate','autolimit');
 2435:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2436:         %titles = &courserequest_titles();
 2437:     } elsif ($context eq 'requestauthor') {
 2438:         @usertools = ('author');
 2439:         @options = ('norequest','approval','automatic');
 2440:         %titles = &authorrequest_titles();
 2441:     } else {
 2442:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
 2443:         %titles = &tool_titles();
 2444:     }
 2445:     if (ref($types) eq 'ARRAY') {
 2446:         foreach my $type (@{$types}) {
 2447:             my $currdefquota;
 2448:             unless (($context eq 'requestcourses') ||
 2449:                     ($context eq 'requestauthor')) {
 2450:                 if (ref($settings) eq 'HASH') {
 2451:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2452:                         $currdefquota = $settings->{defaultquota}->{$type};
 2453:                     } else {
 2454:                         $currdefquota = $settings->{$type};
 2455:                     }
 2456:                 }
 2457:             }
 2458:             if (defined($usertypes->{$type})) {
 2459:                 $typecount ++;
 2460:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2461:                 $datatable .= '<tr'.$css_class.'>'.
 2462:                               '<td>'.$usertypes->{$type}.'</td>'.
 2463:                               '<td class="LC_left_item">';
 2464:                 if ($context eq 'requestcourses') {
 2465:                     $datatable .= '<table><tr>';
 2466:                 }
 2467:                 my %cell;  
 2468:                 foreach my $item (@usertools) {
 2469:                     if ($context eq 'requestcourses') {
 2470:                         my ($curroption,$currlimit);
 2471:                         if (ref($settings) eq 'HASH') {
 2472:                             if (ref($settings->{$item}) eq 'HASH') {
 2473:                                 $curroption = $settings->{$item}->{$type};
 2474:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2475:                                     $currlimit = $1; 
 2476:                                 }
 2477:                             }
 2478:                         }
 2479:                         if (!$curroption) {
 2480:                             $curroption = 'norequest';
 2481:                         }
 2482:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2483:                         foreach my $option (@options) {
 2484:                             my $val = $option;
 2485:                             if ($option eq 'norequest') {
 2486:                                 $val = 0;  
 2487:                             }
 2488:                             if ($option eq 'validate') {
 2489:                                 my $canvalidate = 0;
 2490:                                 if (ref($validations{$item}) eq 'HASH') { 
 2491:                                     if ($validations{$item}{$type}) {
 2492:                                         $canvalidate = 1;
 2493:                                     }
 2494:                                 }
 2495:                                 next if (!$canvalidate);
 2496:                             }
 2497:                             my $checked = '';
 2498:                             if ($option eq $curroption) {
 2499:                                 $checked = ' checked="checked"';
 2500:                             } elsif ($option eq 'autolimit') {
 2501:                                 if ($curroption =~ /^autolimit/) {
 2502:                                     $checked = ' checked="checked"';
 2503:                                 }                       
 2504:                             } 
 2505:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2506:                                   '<input type="radio" name="crsreq_'.$item.
 2507:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2508:                                   $titles{$option}.'</label>';
 2509:                             if ($option eq 'autolimit') {
 2510:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2511:                                                 $item.'_limit_'.$type.'" size="1" '.
 2512:                                                 'value="'.$currlimit.'" />';
 2513:                             }
 2514:                             $cell{$item} .= '</span> ';
 2515:                             if ($option eq 'autolimit') {
 2516:                                 $cell{$item} .= $titles{'unlimited'};
 2517:                             }
 2518:                         }
 2519:                     } elsif ($context eq 'requestauthor') {
 2520:                         my $curroption;
 2521:                         if (ref($settings) eq 'HASH') {
 2522:                             $curroption = $settings->{$type};
 2523:                         }
 2524:                         if (!$curroption) {
 2525:                             $curroption = 'norequest';
 2526:                         }
 2527:                         foreach my $option (@options) {
 2528:                             my $val = $option;
 2529:                             if ($option eq 'norequest') {
 2530:                                 $val = 0;
 2531:                             }
 2532:                             my $checked = '';
 2533:                             if ($option eq $curroption) {
 2534:                                 $checked = ' checked="checked"';
 2535:                             }
 2536:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2537:                                   '<input type="radio" name="authorreq_'.$type.
 2538:                                   '" value="'.$val.'"'.$checked.' />'.
 2539:                                   $titles{$option}.'</label></span>&nbsp; ';
 2540:                         }
 2541:                     } else {
 2542:                         my $checked = 'checked="checked" ';
 2543:                         if ($item eq 'timezone') {
 2544:                             $checked = '';
 2545:                         }
 2546:                         if (ref($settings) eq 'HASH') {
 2547:                             if (ref($settings->{$item}) eq 'HASH') {
 2548:                                 if (!$settings->{$item}->{$type}) {
 2549:                                     $checked = '';
 2550:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2551:                                     $checked =  'checked="checked" ';
 2552:                                 }
 2553:                             }
 2554:                         }
 2555:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2556:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2557:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2558:                                       '</label></span>&nbsp; ';
 2559:                     }
 2560:                 }
 2561:                 if ($context eq 'requestcourses') {
 2562:                     $datatable .= '</tr><tr>';
 2563:                     foreach my $item (@usertools) {
 2564:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2565:                     }
 2566:                     $datatable .= '</tr></table>';
 2567:                 }
 2568:                 $datatable .= '</td>';
 2569:                 unless (($context eq 'requestcourses') ||
 2570:                         ($context eq 'requestauthor')) {
 2571:                     $datatable .= 
 2572:                               '<td class="LC_right_item">'.
 2573:                               '<span class="LC_nobreak">'.
 2574:                               '<input type="text" name="quota_'.$type.
 2575:                               '" value="'.$currdefquota.
 2576:                               '" size="5" /></span></td>';
 2577:                 }
 2578:                 $datatable .= '</tr>';
 2579:             }
 2580:         }
 2581:     }
 2582:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2583:         $defaultquota = '20';
 2584:         if (ref($settings) eq 'HASH') {
 2585:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2586:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2587:             } elsif (defined($settings->{'default'})) {
 2588:                 $defaultquota = $settings->{'default'};
 2589:             }
 2590:         }
 2591:     }
 2592:     $typecount ++;
 2593:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2594:     $datatable .= '<tr'.$css_class.'>'.
 2595:                   '<td>'.$othertitle.'</td>'.
 2596:                   '<td class="LC_left_item">';
 2597:     if ($context eq 'requestcourses') {
 2598:         $datatable .= '<table><tr>';
 2599:     }
 2600:     my %defcell;
 2601:     foreach my $item (@usertools) {
 2602:         if ($context eq 'requestcourses') {
 2603:             my ($curroption,$currlimit);
 2604:             if (ref($settings) eq 'HASH') {
 2605:                 if (ref($settings->{$item}) eq 'HASH') {
 2606:                     $curroption = $settings->{$item}->{'default'};
 2607:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2608:                         $currlimit = $1;
 2609:                     }
 2610:                 }
 2611:             }
 2612:             if (!$curroption) {
 2613:                 $curroption = 'norequest';
 2614:             }
 2615:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2616:             foreach my $option (@options) {
 2617:                 my $val = $option;
 2618:                 if ($option eq 'norequest') {
 2619:                     $val = 0;
 2620:                 }
 2621:                 if ($option eq 'validate') {
 2622:                     my $canvalidate = 0;
 2623:                     if (ref($validations{$item}) eq 'HASH') {
 2624:                         if ($validations{$item}{'default'}) {
 2625:                             $canvalidate = 1;
 2626:                         }
 2627:                     }
 2628:                     next if (!$canvalidate);
 2629:                 }
 2630:                 my $checked = '';
 2631:                 if ($option eq $curroption) {
 2632:                     $checked = ' checked="checked"';
 2633:                 } elsif ($option eq 'autolimit') {
 2634:                     if ($curroption =~ /^autolimit/) {
 2635:                         $checked = ' checked="checked"';
 2636:                     }
 2637:                 }
 2638:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2639:                                   '<input type="radio" name="crsreq_'.$item.
 2640:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2641:                                   $titles{$option}.'</label>';
 2642:                 if ($option eq 'autolimit') {
 2643:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2644:                                        $item.'_limit_default" size="1" '.
 2645:                                        'value="'.$currlimit.'" />';
 2646:                 }
 2647:                 $defcell{$item} .= '</span> ';
 2648:                 if ($option eq 'autolimit') {
 2649:                     $defcell{$item} .= $titles{'unlimited'};
 2650:                 }
 2651:             }
 2652:         } elsif ($context eq 'requestauthor') {
 2653:             my $curroption;
 2654:             if (ref($settings) eq 'HASH') {
 2655:                 $curroption = $settings->{'default'};
 2656:             }
 2657:             if (!$curroption) {
 2658:                 $curroption = 'norequest';
 2659:             }
 2660:             foreach my $option (@options) {
 2661:                 my $val = $option;
 2662:                 if ($option eq 'norequest') {
 2663:                     $val = 0;
 2664:                 }
 2665:                 my $checked = '';
 2666:                 if ($option eq $curroption) {
 2667:                     $checked = ' checked="checked"';
 2668:                 }
 2669:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2670:                               '<input type="radio" name="authorreq_default"'.
 2671:                               ' value="'.$val.'"'.$checked.' />'.
 2672:                               $titles{$option}.'</label></span>&nbsp; ';
 2673:             }
 2674:         } else {
 2675:             my $checked = 'checked="checked" ';
 2676:             if (ref($settings) eq 'HASH') {
 2677:                 if (ref($settings->{$item}) eq 'HASH') {
 2678:                     if ($settings->{$item}->{'default'} == 0) {
 2679:                         $checked = '';
 2680:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2681:                         $checked = 'checked="checked" ';
 2682:                     }
 2683:                 }
 2684:             }
 2685:             $datatable .= '<span class="LC_nobreak"><label>'.
 2686:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2687:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2688:                           '</label></span>&nbsp; ';
 2689:         }
 2690:     }
 2691:     if ($context eq 'requestcourses') {
 2692:         $datatable .= '</tr><tr>';
 2693:         foreach my $item (@usertools) {
 2694:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2695:         }
 2696:         $datatable .= '</tr></table>';
 2697:     }
 2698:     $datatable .= '</td>';
 2699:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2700:         $datatable .= '<td class="LC_right_item">'.
 2701:                       '<span class="LC_nobreak">'.
 2702:                       '<input type="text" name="defaultquota" value="'.
 2703:                       $defaultquota.'" size="5" /></span></td>';
 2704:     }
 2705:     $datatable .= '</tr>';
 2706:     $typecount ++;
 2707:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2708:     $datatable .= '<tr'.$css_class.'>'.
 2709:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2710:     if ($context eq 'requestcourses') {
 2711:         $datatable .= &mt('(overrides affiliation, if set)').
 2712:                       '</td>'.
 2713:                       '<td class="LC_left_item">'.
 2714:                       '<table><tr>';
 2715:     } else {
 2716:         $datatable .= &mt('(overrides affiliation, if checked)').
 2717:                       '</td>'.
 2718:                       '<td class="LC_left_item" colspan="2">'.
 2719:                       '<br />';
 2720:     }
 2721:     my %advcell;
 2722:     foreach my $item (@usertools) {
 2723:         if ($context eq 'requestcourses') {
 2724:             my ($curroption,$currlimit);
 2725:             if (ref($settings) eq 'HASH') {
 2726:                 if (ref($settings->{$item}) eq 'HASH') {
 2727:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2728:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2729:                         $currlimit = $1;
 2730:                     }
 2731:                 }
 2732:             }
 2733:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2734:             my $checked = '';
 2735:             if ($curroption eq '') {
 2736:                 $checked = ' checked="checked"';
 2737:             }
 2738:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2739:                                '<input type="radio" name="crsreq_'.$item.
 2740:                                '__LC_adv" value=""'.$checked.' />'.
 2741:                                &mt('No override set').'</label></span>&nbsp; ';
 2742:             foreach my $option (@options) {
 2743:                 my $val = $option;
 2744:                 if ($option eq 'norequest') {
 2745:                     $val = 0;
 2746:                 }
 2747:                 if ($option eq 'validate') {
 2748:                     my $canvalidate = 0;
 2749:                     if (ref($validations{$item}) eq 'HASH') {
 2750:                         if ($validations{$item}{'_LC_adv'}) {
 2751:                             $canvalidate = 1;
 2752:                         }
 2753:                     }
 2754:                     next if (!$canvalidate);
 2755:                 }
 2756:                 my $checked = '';
 2757:                 if ($val eq $curroption) {
 2758:                     $checked = ' checked="checked"';
 2759:                 } elsif ($option eq 'autolimit') {
 2760:                     if ($curroption =~ /^autolimit/) {
 2761:                         $checked = ' checked="checked"';
 2762:                     }
 2763:                 }
 2764:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2765:                                   '<input type="radio" name="crsreq_'.$item.
 2766:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2767:                                   $titles{$option}.'</label>';
 2768:                 if ($option eq 'autolimit') {
 2769:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2770:                                        $item.'_limit__LC_adv" size="1" '.
 2771:                                        'value="'.$currlimit.'" />';
 2772:                 }
 2773:                 $advcell{$item} .= '</span> ';
 2774:                 if ($option eq 'autolimit') {
 2775:                     $advcell{$item} .= $titles{'unlimited'};
 2776:                 }
 2777:             }
 2778:         } elsif ($context eq 'requestauthor') {
 2779:             my $curroption;
 2780:             if (ref($settings) eq 'HASH') {
 2781:                 $curroption = $settings->{'_LC_adv'};
 2782:             }
 2783:             my $checked = '';
 2784:             if ($curroption eq '') {
 2785:                 $checked = ' checked="checked"';
 2786:             }
 2787:             $datatable .= '<span class="LC_nobreak"><label>'.
 2788:                           '<input type="radio" name="authorreq__LC_adv"'.
 2789:                           ' value=""'.$checked.' />'.
 2790:                           &mt('No override set').'</label></span>&nbsp; ';
 2791:             foreach my $option (@options) {
 2792:                 my $val = $option;
 2793:                 if ($option eq 'norequest') {
 2794:                     $val = 0;
 2795:                 }
 2796:                 my $checked = '';
 2797:                 if ($val eq $curroption) {
 2798:                     $checked = ' checked="checked"';
 2799:                 }
 2800:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2801:                               '<input type="radio" name="authorreq__LC_adv"'.
 2802:                               ' value="'.$val.'"'.$checked.' />'.
 2803:                               $titles{$option}.'</label></span>&nbsp; ';
 2804:             }
 2805:         } else {
 2806:             my $checked = 'checked="checked" ';
 2807:             if (ref($settings) eq 'HASH') {
 2808:                 if (ref($settings->{$item}) eq 'HASH') {
 2809:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2810:                         $checked = '';
 2811:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2812:                         $checked = 'checked="checked" ';
 2813:                     }
 2814:                 }
 2815:             }
 2816:             $datatable .= '<span class="LC_nobreak"><label>'.
 2817:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2818:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2819:                           '</label></span>&nbsp; ';
 2820:         }
 2821:     }
 2822:     if ($context eq 'requestcourses') {
 2823:         $datatable .= '</tr><tr>';
 2824:         foreach my $item (@usertools) {
 2825:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2826:         }
 2827:         $datatable .= '</tr></table>';
 2828:     }
 2829:     $datatable .= '</td></tr>';
 2830:     $$rowtotal += $typecount;
 2831:     return $datatable;
 2832: }
 2833: 
 2834: sub print_requestmail {
 2835:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2836:     my ($now,$datatable,%currapp);
 2837:     $now = time;
 2838:     if (ref($settings) eq 'HASH') {
 2839:         if (ref($settings->{'notify'}) eq 'HASH') {
 2840:             if ($settings->{'notify'}{'approval'} ne '') {
 2841:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2842:             }
 2843:         }
 2844:     }
 2845:     my $numinrow = 2;
 2846:     my $css_class;
 2847:     if ($$rowtotal%2) {
 2848:         $css_class = 'LC_odd_row';
 2849:     }
 2850:     if ($customcss) {
 2851:         $css_class .= " $customcss";
 2852:     }
 2853:     $css_class =~ s/^\s+//;
 2854:     if ($css_class) {
 2855:         $css_class = ' class="'.$css_class.'"';
 2856:     }
 2857:     if ($rowstyle) {
 2858:         $css_class .= ' style="'.$rowstyle.'"';
 2859:     }
 2860:     my $text;
 2861:     if ($action eq 'requestcourses') {
 2862:         $text = &mt('Receive notification of course requests requiring approval');
 2863:     } elsif ($action eq 'requestauthor') {
 2864:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2865:     } else {
 2866:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2867:     }
 2868:     $datatable = '<tr'.$css_class.'>'.
 2869:                  ' <td>'.$text.'</td>'.
 2870:                  ' <td class="LC_left_item">';
 2871:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2872:                                                  $action.'notifyapproval',%currapp);
 2873:     if ($numdc > 0) {
 2874:         $datatable .= $table;
 2875:     } else {
 2876:         $datatable .= &mt('There are no active Domain Coordinators');
 2877:     }
 2878:     $datatable .='</td></tr>';
 2879:     return $datatable;
 2880: }
 2881: 
 2882: sub print_studentcode {
 2883:     my ($settings,$rowtotal) = @_;
 2884:     my $rownum = 0; 
 2885:     my ($output,%current);
 2886:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2887:     if (ref($settings) eq 'HASH') {
 2888:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2889:             foreach my $type (@crstypes) {
 2890:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2891:             }
 2892:         }
 2893:     }
 2894:     $output .= '<tr>'.
 2895:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2896:                '<td class="LC_left_item">';
 2897:     foreach my $type (@crstypes) {
 2898:         my $check = ' ';
 2899:         if ($current{$type}) {
 2900:             $check = ' checked="checked" ';
 2901:         }
 2902:         $output .= '<span class="LC_nobreak"><label>'.
 2903:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2904:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2905:     }
 2906:     $output .= '</td></tr>';
 2907:     $$rowtotal ++;
 2908:     return $output;
 2909: }
 2910: 
 2911: sub print_textbookcourses {
 2912:     my ($dom,$type,$settings,$rowtotal) = @_;
 2913:     my $rownum = 0;
 2914:     my $css_class;
 2915:     my $itemcount = 1;
 2916:     my $maxnum = 0;
 2917:     my $bookshash;
 2918:     if (ref($settings) eq 'HASH') {
 2919:         $bookshash = $settings->{$type};
 2920:     }
 2921:     my %ordered;
 2922:     if (ref($bookshash) eq 'HASH') {
 2923:         foreach my $item (keys(%{$bookshash})) {
 2924:             if (ref($bookshash->{$item}) eq 'HASH') {
 2925:                 my $num = $bookshash->{$item}{'order'};
 2926:                 $ordered{$num} = $item;
 2927:             }
 2928:         }
 2929:     }
 2930:     my $confname = $dom.'-domainconfig';
 2931:     my $switchserver = &check_switchserver($dom,$confname);
 2932:     my $maxnum = scalar(keys(%ordered));
 2933:     my $datatable;
 2934:     if (keys(%ordered)) {
 2935:         my @items = sort { $a <=> $b } keys(%ordered);
 2936:         for (my $i=0; $i<@items; $i++) {
 2937:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2938:             my $key = $ordered{$items[$i]};
 2939:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2940:             my $coursetitle = $coursehash{'description'};
 2941:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2942:             if (ref($bookshash->{$key}) eq 'HASH') {
 2943:                 $subject = $bookshash->{$key}->{'subject'};
 2944:                 $title = $bookshash->{$key}->{'title'};
 2945:                 if ($type eq 'textbooks') {
 2946:                     $publisher = $bookshash->{$key}->{'publisher'};
 2947:                     $author = $bookshash->{$key}->{'author'};
 2948:                     $image = $bookshash->{$key}->{'image'};
 2949:                     if ($image ne '') {
 2950:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2951:                         my $imagethumb = "$path/tn-".$imagefile;
 2952:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2953:                     }
 2954:                 }
 2955:             }
 2956:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2957:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2958:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2959:             for (my $k=0; $k<=$maxnum; $k++) {
 2960:                 my $vpos = $k+1;
 2961:                 my $selstr;
 2962:                 if ($k == $i) {
 2963:                     $selstr = ' selected="selected" ';
 2964:                 }
 2965:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2966:             }
 2967:             $datatable .= '</select>'.('&nbsp;'x2).
 2968:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2969:                 &mt('Delete?').'</label></span></td>'.
 2970:                 '<td colspan="2">'.
 2971:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2972:                 ('&nbsp;'x2).
 2973:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2974:             if ($type eq 'textbooks') {
 2975:                 $datatable .= ('&nbsp;'x2).
 2976:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2977:                               ('&nbsp;'x2).
 2978:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2979:                               ('&nbsp;'x2).
 2980:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2981:                 if ($image) {
 2982:                     $datatable .= $imgsrc.
 2983:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2984:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2985:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2986:                 }
 2987:                 if ($switchserver) {
 2988:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2989:                 } else {
 2990:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2991:                 }
 2992:             }
 2993:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2994:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2995:                           $coursetitle.'</span></td></tr>'."\n";
 2996:             $itemcount ++;
 2997:         }
 2998:     }
 2999:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3000:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 3001:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3002:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 3003:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 3004:     for (my $k=0; $k<$maxnum+1; $k++) {
 3005:         my $vpos = $k+1;
 3006:         my $selstr;
 3007:         if ($k == $maxnum) {
 3008:             $selstr = ' selected="selected" ';
 3009:         }
 3010:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3011:     }
 3012:     $datatable .= '</select>&nbsp;'."\n".
 3013:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 3014:                   '<td colspan="2">'.
 3015:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 3016:                   ('&nbsp;'x2).
 3017:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 3018:                   ('&nbsp;'x2);
 3019:     if ($type eq 'textbooks') {
 3020:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 3021:                       ('&nbsp;'x2).
 3022:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 3023:                       ('&nbsp;'x2).
 3024:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 3025:         if ($switchserver) {
 3026:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3027:         } else {
 3028:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 3029:         }
 3030:         $datatable .= '</span>'."\n";
 3031:     }
 3032:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 3033:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 3034:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 3035:                   &Apache::loncommon::selectcourse_link
 3036:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 3037:                   '</span></td>'."\n".
 3038:                   '</tr>'."\n";
 3039:     $itemcount ++;
 3040:     return $datatable;
 3041: }
 3042: 
 3043: sub textbookcourses_javascript {
 3044:     my ($settings) = @_;
 3045:     return unless(ref($settings) eq 'HASH');
 3046:     my (%ordered,%total,%jstext);
 3047:     foreach my $type ('textbooks','templates') {
 3048:         $total{$type} = 0;
 3049:         if (ref($settings->{$type}) eq 'HASH') {
 3050:             foreach my $item (keys(%{$settings->{$type}})) {
 3051:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3052:                     my $num = $settings->{$type}->{$item}{'order'};
 3053:                     $ordered{$type}{$num} = $item;
 3054:                 }
 3055:             }
 3056:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3057:         }
 3058:         my @jsarray = ();
 3059:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3060:             push(@jsarray,$ordered{$type}{$item});
 3061:         }
 3062:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3063:     }
 3064:     return <<"ENDSCRIPT";
 3065: <script type="text/javascript">
 3066: // <![CDATA[
 3067: function reorderBooks(form,item,caller) {
 3068:     var changedVal;
 3069: $jstext{'textbooks'};
 3070: $jstext{'templates'};
 3071:     var newpos;
 3072:     var maxh;
 3073:     if (caller == 'textbooks') {  
 3074:         newpos = 'textbooks_addbook_pos';
 3075:         maxh = 1 + $total{'textbooks'};
 3076:     } else {
 3077:         newpos = 'templates_addbook_pos';
 3078:         maxh = 1 + $total{'templates'};
 3079:     }
 3080:     var current = new Array;
 3081:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3082:     if (item == newpos) {
 3083:         changedVal = newitemVal;
 3084:     } else {
 3085:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3086:         current[newitemVal] = newpos;
 3087:     }
 3088:     if (caller == 'textbooks') {
 3089:         for (var i=0; i<textbooks.length; i++) {
 3090:             var elementName = 'textbooks_'+textbooks[i];
 3091:             if (elementName != item) {
 3092:                 if (form.elements[elementName]) {
 3093:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3094:                     current[currVal] = elementName;
 3095:                 }
 3096:             }
 3097:         }
 3098:     }
 3099:     if (caller == 'templates') {
 3100:         for (var i=0; i<templates.length; i++) {
 3101:             var elementName = 'templates_'+templates[i];
 3102:             if (elementName != item) {
 3103:                 if (form.elements[elementName]) {
 3104:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3105:                     current[currVal] = elementName;
 3106:                 }
 3107:             }
 3108:         }
 3109:     }
 3110:     var oldVal;
 3111:     for (var j=0; j<maxh; j++) {
 3112:         if (current[j] == undefined) {
 3113:             oldVal = j;
 3114:         }
 3115:     }
 3116:     if (oldVal < changedVal) {
 3117:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3118:            var elementName = current[k];
 3119:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3120:         }
 3121:     } else {
 3122:         for (var k=changedVal; k<oldVal; k++) {
 3123:             var elementName = current[k];
 3124:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3125:         }
 3126:     }
 3127:     return;
 3128: }
 3129: 
 3130: // ]]>
 3131: </script>
 3132: 
 3133: ENDSCRIPT
 3134: }
 3135: 
 3136: sub ltitools_javascript {
 3137:     my ($settings) = @_;
 3138:     my $togglejs = &ltitools_toggle_js();
 3139:     unless (ref($settings) eq 'HASH') {
 3140:         return $togglejs;
 3141:     }
 3142:     my (%ordered,$total,%jstext);
 3143:     $total = 0;
 3144:     foreach my $item (keys(%{$settings})) {
 3145:         if (ref($settings->{$item}) eq 'HASH') {
 3146:             my $num = $settings->{$item}{'order'};
 3147:             $ordered{$num} = $item;
 3148:         }
 3149:     }
 3150:     $total = scalar(keys(%{$settings}));
 3151:     my @jsarray = ();
 3152:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3153:         push(@jsarray,$ordered{$item});
 3154:     }
 3155:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3156:     return <<"ENDSCRIPT";
 3157: <script type="text/javascript">
 3158: // <![CDATA[
 3159: function reorderLTITools(form,item) {
 3160:     var changedVal;
 3161: $jstext
 3162:     var newpos = 'ltitools_add_pos';
 3163:     var maxh = 1 + $total;
 3164:     var current = new Array;
 3165:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3166:     if (item == newpos) {
 3167:         changedVal = newitemVal;
 3168:     } else {
 3169:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3170:         current[newitemVal] = newpos;
 3171:     }
 3172:     for (var i=0; i<ltitools.length; i++) {
 3173:         var elementName = 'ltitools_'+ltitools[i];
 3174:         if (elementName != item) {
 3175:             if (form.elements[elementName]) {
 3176:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3177:                 current[currVal] = elementName;
 3178:             }
 3179:         }
 3180:     }
 3181:     var oldVal;
 3182:     for (var j=0; j<maxh; j++) {
 3183:         if (current[j] == undefined) {
 3184:             oldVal = j;
 3185:         }
 3186:     }
 3187:     if (oldVal < changedVal) {
 3188:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3189:            var elementName = current[k];
 3190:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3191:         }
 3192:     } else {
 3193:         for (var k=changedVal; k<oldVal; k++) {
 3194:             var elementName = current[k];
 3195:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3196:         }
 3197:     }
 3198:     return;
 3199: }
 3200: 
 3201: // ]]>
 3202: </script>
 3203: 
 3204: $togglejs
 3205: 
 3206: ENDSCRIPT
 3207: }
 3208: 
 3209: sub ltitools_toggle_js {
 3210:     return <<"ENDSCRIPT";
 3211: <script type="text/javascript">
 3212: // <![CDATA[
 3213: 
 3214: function toggleLTITools(form,setting,item) {
 3215:     var radioname = '';
 3216:     var divid = '';
 3217:     if ((setting == 'passback') || (setting == 'roster')) {
 3218:         radioname = 'ltitools_'+setting+'_'+item;
 3219:         divid = 'ltitools_'+setting+'time_'+item;
 3220:         var num = form.elements[radioname].length;
 3221:         if (num) {
 3222:             var setvis = '';
 3223:             for (var i=0; i<num; i++) {
 3224:                 if (form.elements[radioname][i].checked) {
 3225:                     if (form.elements[radioname][i].value == '1') {
 3226:                         if (document.getElementById(divid)) {
 3227:                             document.getElementById(divid).style.display = 'inline-block';
 3228:                         }
 3229:                         setvis = 1;
 3230:                     }
 3231:                     break;
 3232:                 }
 3233:             }
 3234:         }
 3235:         if (!setvis) {
 3236:             if (document.getElementById(divid)) {
 3237:                 document.getElementById(divid).style.display = 'none';
 3238:             }
 3239:         }
 3240:     }
 3241:     if (setting == 'user') {
 3242:         divid = 'ltitools_'+setting+'_div_'+item;
 3243:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3244:         if (document.getElementById(divid)) {
 3245:             if (document.getElementById(checkid)) {
 3246:                 if (document.getElementById(checkid).checked) {
 3247:                     document.getElementById(divid).style.display = 'inline-block';
 3248:                 } else {
 3249:                     document.getElementById(divid).style.display = 'none';
 3250:                 }
 3251:             }
 3252:         }
 3253:     }
 3254:     return;
 3255: }
 3256: // ]]>
 3257: </script>
 3258: 
 3259: ENDSCRIPT
 3260: }
 3261: 
 3262: sub wafproxy_javascript {
 3263:     my ($dom) = @_;
 3264:     return <<"ENDSCRIPT";
 3265: <script type="text/javascript">
 3266: // <![CDATA[
 3267: function updateWAF() {
 3268:     if (document.getElementById('wafproxy_remoteip')) {
 3269:         var wafremote = 0;
 3270:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3271:             wafremote = 1;
 3272:         }
 3273:         var fields = new Array('header','trust');
 3274:         for (var i=0; i<fields.length; i++) {
 3275:             if (document.getElementById('wafproxy_'+fields[i])) {
 3276:                 if (wafremote == 1) {
 3277:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3278:                 }
 3279:                 else {
 3280:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3281:                 }
 3282:             }
 3283:         }
 3284:         if (document.getElementById('wafproxyranges_$dom')) {
 3285:             if (wafremote == 1) {
 3286:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3287:             } else {
 3288:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3289:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3290:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3291:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3292:                         }
 3293:                     }
 3294:                 }
 3295:             }
 3296:         }
 3297:     }
 3298:     return;
 3299: }
 3300: 
 3301: function checkWAF() {
 3302:     if (document.getElementById('wafproxy_remoteip')) {
 3303:         var wafvpn = 0;
 3304:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3305:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3306:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3307:                     wafvpn = 1;
 3308:                 }
 3309:                 break;
 3310:             }
 3311:         }
 3312:         var vpn = new Array('vpnint','vpnext');
 3313:         for (var i=0; i<vpn.length; i++) {
 3314:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3315:                 if (wafvpn == 1) {
 3316:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3317:                 }
 3318:                 else {
 3319:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3320:                 }
 3321:             }
 3322:         }
 3323:         if (document.getElementById('wafproxyranges_$dom')) {
 3324:             if (wafvpn == 1) {
 3325:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3326:             }
 3327:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3328:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3329:             }
 3330:         }
 3331:     }
 3332:     return;
 3333: }
 3334: 
 3335: function toggleWAF() {
 3336:     if (document.getElementById('wafproxy_table')) {
 3337:         var wafproxy = 0;
 3338:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3339:              if (document.display.wafproxy_${dom}[i].checked) {
 3340:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3341:                      wafproxy = 1;
 3342:                      break;
 3343:                 }
 3344:             }
 3345:         }
 3346:         if (wafproxy == 1) {
 3347:             document.getElementById('wafproxy_table').style.display='inline';
 3348:         }
 3349:         else {
 3350:            document.getElementById('wafproxy_table').style.display='none';
 3351:         }
 3352:         if (document.getElementById('wafproxyrow_${dom}')) {
 3353:             if (wafproxy == 1) {
 3354:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3355:             }
 3356:             else {
 3357:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3358:             }
 3359:         }
 3360:         if (document.getElementById('nowafproxyrow_$dom')) {
 3361:             if (wafproxy == 1) {
 3362:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3363:             }
 3364:             else {
 3365:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3366:             }
 3367:         }
 3368:     }
 3369:     return;
 3370: }
 3371: // ]]>
 3372: </script>
 3373: 
 3374: ENDSCRIPT
 3375: }
 3376: 
 3377: sub proctoring_javascript {
 3378:     my ($settings) = @_;
 3379:     my (%ordered,$total,%jstext);
 3380:     $total = 0;
 3381:     if (ref($settings) eq 'HASH') {
 3382:         foreach my $item (keys(%{$settings})) {
 3383:             if (ref($settings->{$item}) eq 'HASH') {
 3384:                 my $num = $settings->{$item}{'order'};
 3385:                 $ordered{$num} = $item;
 3386:             }
 3387:         }
 3388:         $total = scalar(keys(%{$settings}));
 3389:     } else {
 3390:         %ordered = (
 3391:                        0 => 'proctorio',
 3392:                        1 => 'examity',
 3393:                    );
 3394:         $total = 2; 
 3395:     }
 3396:     my @jsarray = ();
 3397:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3398:         push(@jsarray,$ordered{$item});
 3399:     }
 3400:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3401:     return <<"ENDSCRIPT";
 3402: <script type="text/javascript">
 3403: // <![CDATA[
 3404: function reorderProctoring(form,item) {
 3405:     var changedVal;
 3406: $jstext
 3407:     var maxh = $total;
 3408:     var current = new Array;
 3409:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3410:     for (var i=0; i<proctors.length; i++) {
 3411:         var elementName = 'proctoring_pos_'+proctors[i];
 3412:         if (elementName != item) {
 3413:             if (form.elements[elementName]) {
 3414:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3415:                 current[currVal] = elementName;
 3416:             }
 3417:         }
 3418:     }
 3419:     var oldVal;
 3420:     for (var j=0; j<maxh; j++) {
 3421:         if (current[j] == undefined) {
 3422:             oldVal = j;
 3423:         }
 3424:     }
 3425:     if (oldVal < changedVal) {
 3426:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3427:            var elementName = current[k];
 3428:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3429:         }
 3430:     } else {
 3431:         for (var k=changedVal; k<oldVal; k++) {
 3432:             var elementName = current[k];
 3433:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3434:         }
 3435:     }
 3436:     return;
 3437: }
 3438: 
 3439: function toggleProctoring(form,item) {
 3440:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3441:     if (fieldsets.length) {
 3442:         var radioname = 'proctoring_available_'+item;
 3443:         var num = form.elements[radioname].length;
 3444:         if (num) {
 3445:             var setvis = '';
 3446:             for (var i=0; i<num; i++) {
 3447:                 if (form.elements[radioname][i].checked) {
 3448:                     if (form.elements[radioname][i].value == '1') {
 3449:                        setvis = 1;
 3450:                        break;
 3451:                     }
 3452:                 }
 3453:             }
 3454:             for (var j=0; j<fieldsets.length; j++) {
 3455:                 if (setvis) {
 3456:                     fieldsets[j].style.display = 'block';
 3457:                 } else {
 3458:                     fieldsets[j].style.display = 'none';
 3459:                 }
 3460:             }
 3461:         }
 3462:     }
 3463:     return;
 3464: }
 3465: 
 3466: // ]]>
 3467: </script>
 3468: 
 3469: ENDSCRIPT
 3470: }
 3471: 
 3472: 
 3473: sub lti_javascript {
 3474:     my ($dom,$settings) = @_;
 3475:     my $togglejs = &lti_toggle_js($dom);
 3476:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3477:     unless (ref($settings) eq 'HASH') {
 3478:         return $togglejs.'
 3479: <script type="text/javascript">
 3480: // <![CDATA[
 3481: 
 3482: '.$linkprot_js.'
 3483: 
 3484: // ]]>
 3485: </script>
 3486: ';
 3487:     }
 3488:     my (%ordered,$total,%jstext);
 3489:     $total = scalar(keys(%{$settings}));
 3490:     foreach my $item (keys(%{$settings})) {
 3491:         if (ref($settings->{$item}) eq 'HASH') {
 3492:             my $num = $settings->{$item}{'order'};
 3493:             if ($num eq '') {
 3494:                 $num = $total - 1;
 3495:             }
 3496:             $ordered{$num} = $item;
 3497:         }
 3498:     }
 3499:     my @jsarray = ();
 3500:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3501:         push(@jsarray,$ordered{$item});
 3502:     }
 3503:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3504:     return <<"ENDSCRIPT";
 3505: <script type="text/javascript">
 3506: // <![CDATA[
 3507: function reorderLTI(form,item) {
 3508:     var changedVal;
 3509: $jstext
 3510:     var newpos = 'lti_pos_add';
 3511:     var maxh = 1 + $total;
 3512:     var current = new Array;
 3513:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3514:     if (item == newpos) {
 3515:         changedVal = newitemVal;
 3516:     } else {
 3517:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3518:         current[newitemVal] = newpos;
 3519:     }
 3520:     for (var i=0; i<lti.length; i++) {
 3521:         var elementName = 'lti_pos_'+lti[i];
 3522:         if (elementName != item) {
 3523:             if (form.elements[elementName]) {
 3524:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3525:                 current[currVal] = elementName;
 3526:             }
 3527:         }
 3528:     }
 3529:     var oldVal;
 3530:     for (var j=0; j<maxh; j++) {
 3531:         if (current[j] == undefined) {
 3532:             oldVal = j;
 3533:         }
 3534:     }
 3535:     if (oldVal < changedVal) {
 3536:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3537:            var elementName = current[k];
 3538:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3539:         }
 3540:     } else {
 3541:         for (var k=changedVal; k<oldVal; k++) {
 3542:             var elementName = current[k];
 3543:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3544:         }
 3545:     }
 3546:     return;
 3547: }
 3548: 
 3549: $linkprot_js
 3550: 
 3551: // ]]>
 3552: </script>
 3553: 
 3554: $togglejs
 3555: 
 3556: ENDSCRIPT
 3557: }
 3558: 
 3559: sub lti_toggle_js {
 3560:     my ($dom) = @_;
 3561:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3562:                             localauth => 'Local auth argument',
 3563:                             krb       => 'Kerberos domain',
 3564:                          );
 3565:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3566:     &js_escape(\$crsincalert);
 3567:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3568:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3569:     my $course_servers = "'".join("','",keys(%servers))."'";
 3570:     return <<"ENDSCRIPT";
 3571: <script type="text/javascript">
 3572: // <![CDATA[
 3573: 
 3574: function toggleLTI(form,setting,item) {
 3575:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3576:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3577:         var setvis = '';
 3578:         var radioname = 'lti_requser_'+item;
 3579:         var num = form.elements[radioname].length;
 3580:         if (num) {
 3581:             for (var i=0; i<num; i++) {
 3582:                 if (form.elements[radioname][i].checked) {
 3583:                     if (form.elements[radioname][i].value == '1') {
 3584:                         setvis = 1;
 3585:                         break;
 3586:                     }
 3587:                 }
 3588:             }
 3589:         }
 3590:         if (usrfieldsets.length) {
 3591:             for (var j=0; j<usrfieldsets.length; j++) {
 3592:                 if (setvis) {
 3593:                     usrfieldsets[j].style.display = 'block';
 3594:                 } else {
 3595:                     usrfieldsets[j].style.display = 'none';
 3596:                 }
 3597:             }
 3598:         }
 3599:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3600:         if (crsfieldsets.length) {
 3601:             radioname = 'lti_crsinc_'+item;
 3602:             var num = form.elements[radioname].length;
 3603:             if (num) {
 3604:                 var crsvis = '';
 3605:                 for (var i=0; i<num; i++) {
 3606:                     if (form.elements[radioname][i].checked) {
 3607:                         if (form.elements[radioname][i].value == '1') {
 3608:                             if (setvis == '') {
 3609:                                 if (setting == 'crsinc'){
 3610:                                     alert("$crsincalert");
 3611:                                     form.elements[radioname][0].checked = true;
 3612:                                 }
 3613:                             } else {
 3614:                                 crsvis = 1;
 3615:                             }
 3616:                             break;
 3617:                         }
 3618:                     }
 3619:                 }
 3620:                 setvis = crsvis;
 3621:             }
 3622:             for (var j=0; j<crsfieldsets.length; j++) {
 3623:                 if (setvis) {
 3624:                     crsfieldsets[j].style.display = 'block';
 3625:                 } else {
 3626:                     crsfieldsets[j].style.display = 'none';
 3627:                 }
 3628:             }
 3629:         }
 3630:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3631:         var radioname = '';
 3632:         var divid = '';
 3633:         if (setting == 'user') {
 3634:             radioname = 'lti_mapuser_'+item;
 3635:             divid = 'lti_userfield_'+item;
 3636:         } else if (setting == 'crs') {
 3637:             radioname = 'lti_mapcrs_'+item;
 3638:             divid = 'lti_crsfield_'+item;
 3639:         } else if (setting == 'callback') {
 3640:             radioname = 'lti_callback_'+item;
 3641:             divid = 'lti_callbackfield_'+item;
 3642:         } else {
 3643:             radioname = 'lti_passback_'+item;
 3644:             divid =  'lti_passback_'+item;
 3645:         }
 3646:         var num = form.elements[radioname].length;
 3647:         if (num) {
 3648:             var setvis = '';
 3649:             for (var i=0; i<num; i++) {
 3650:                if (form.elements[radioname][i].checked) {
 3651:                    if ((setting == 'passback') || (setting == 'callback')) {
 3652:                        if (form.elements[radioname][i].value == '1') {
 3653:                            if (document.getElementById(divid)) {
 3654:                                document.getElementById(divid).style.display = 'inline-block';
 3655:                            }
 3656:                            setvis = 1;
 3657:                            break;
 3658:                        }
 3659:                    } else {
 3660:                        if (form.elements[radioname][i].value == 'other') {
 3661:                            if (document.getElementById(divid)) {
 3662:                                document.getElementById(divid).style.display = 'inline-block';
 3663:                            }
 3664:                            setvis = 1;
 3665:                            break;
 3666:                        }
 3667:                    }
 3668:                }
 3669:             }
 3670:             if (!setvis) {
 3671:                 if (document.getElementById(divid)) {
 3672:                     document.getElementById(divid).style.display = 'none';
 3673:                 }
 3674:             }
 3675:         }
 3676:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3677:         var numsec = form.elements['lti_crssec_'+item].length;
 3678:         if (numsec) {
 3679:             var setvis = '';
 3680:             for (var i=0; i<numsec; i++) {
 3681:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3682:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3683:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3684:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3685:                             setvis = 1;
 3686:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3687:                             if (numsrcsec) {
 3688:                                 var setsrcvis = '';
 3689:                                 for (var j=0; j<numsrcsec; j++) {
 3690:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3691:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3692:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3693:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3694:                                                 setsrcvis = 1;
 3695:                                             }
 3696:                                         }
 3697:                                     }
 3698:                                 }
 3699:                                 if (!setsrcvis) {
 3700:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3701:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3702:                                     }
 3703:                                 }
 3704:                             }
 3705:                         }
 3706:                     }
 3707:                 }
 3708:             }
 3709:             if (!setvis) {
 3710:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3711:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3712:                 }
 3713:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3714:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3715:                 }
 3716:             }
 3717:         }
 3718:     } else if (setting == 'lcauth') {
 3719:         var numauth = form.elements['lti_lcauth_'+item].length;
 3720:         if (numauth) {
 3721:             for (var i=0; i<numauth; i++) {
 3722:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3723:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3724:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3725:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3726:                         } else {
 3727:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3728:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3729:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3730:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3731:                                 } else {
 3732:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3733:                                 }
 3734:                             }
 3735:                         }
 3736:                     }
 3737:                 }
 3738:             }
 3739:         }
 3740:     } else if (setting == 'lcmenu') {
 3741:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3742:         var divid = 'lti_menufield_'+item;
 3743:         var setvis = '';
 3744:         for (var i=0; i<menus.length; i++) {
 3745:             var radioname = menus[i];
 3746:             var num = form.elements[radioname].length;
 3747:             if (num) {
 3748:                 for (var j=0; j<num; j++) {
 3749:                     if (form.elements[radioname][j].checked) {
 3750:                         if (form.elements[radioname][j].value == '1') {
 3751:                             if (document.getElementById(divid)) {
 3752:                                 document.getElementById(divid).style.display = 'inline-block';
 3753:                             }
 3754:                             setvis = 1;
 3755:                             break;
 3756:                         }
 3757:                     }
 3758:                 }
 3759:             }
 3760:             if (setvis == 1) {
 3761:                 break;
 3762:             }
 3763:         }
 3764:         if (!setvis) {
 3765:             if (document.getElementById(divid)) {
 3766:                 document.getElementById(divid).style.display = 'none';
 3767:             }
 3768:         }
 3769:     }
 3770:     return;
 3771: }
 3772: 
 3773: // ]]>
 3774: </script>
 3775: 
 3776: ENDSCRIPT
 3777: }
 3778: 
 3779: sub autoupdate_javascript {
 3780:     return <<"ENDSCRIPT";
 3781: <script type="text/javascript">
 3782: // <![CDATA[
 3783: function toggleLastActiveDays(form) {
 3784:     var radioname = 'lastactive';
 3785:     var divid = 'lastactive_div';
 3786:     var num = form.elements[radioname].length;
 3787:     if (num) {
 3788:         var setvis = '';
 3789:         for (var i=0; i<num; i++) {
 3790:             if (form.elements[radioname][i].checked) {
 3791:                 if (form.elements[radioname][i].value == '1') {
 3792:                     if (document.getElementById(divid)) {
 3793:                         document.getElementById(divid).style.display = 'inline-block';
 3794:                     }
 3795:                     setvis = 1;
 3796:                 }
 3797:                 break;
 3798:             }
 3799:         }
 3800:         if (!setvis) {
 3801:             if (document.getElementById(divid)) {
 3802:                 document.getElementById(divid).style.display = 'none';
 3803:             }
 3804:         }
 3805:     }
 3806:     return;
 3807: }
 3808: // ]]>
 3809: </script>
 3810: 
 3811: ENDSCRIPT
 3812: }
 3813: 
 3814: sub autoenroll_javascript {
 3815:     return <<"ENDSCRIPT";
 3816: <script type="text/javascript">
 3817: // <![CDATA[
 3818: function toggleFailsafe(form) {
 3819:     var radioname = 'autoenroll_failsafe';
 3820:     var divid = 'autoenroll_failsafe_div';
 3821:     var num = form.elements[radioname].length;
 3822:     if (num) {
 3823:         var setvis = '';
 3824:         for (var i=0; i<num; i++) {
 3825:             if (form.elements[radioname][i].checked) {
 3826:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3827:                     if (document.getElementById(divid)) {
 3828:                         document.getElementById(divid).style.display = 'inline-block';
 3829:                     }
 3830:                     setvis = 1;
 3831:                 }
 3832:                 break;
 3833:             }
 3834:         }
 3835:         if (!setvis) {
 3836:             if (document.getElementById(divid)) {
 3837:                 document.getElementById(divid).style.display = 'none';
 3838:             }
 3839:         }
 3840:     }
 3841:     return;
 3842: }
 3843: // ]]>
 3844: </script>
 3845: 
 3846: ENDSCRIPT
 3847: }
 3848: 
 3849: sub saml_javascript {
 3850:     return <<"ENDSCRIPT";
 3851: <script type="text/javascript">
 3852: // <![CDATA[
 3853: function toggleSamlOptions(form,hostid) {
 3854:     var radioname = 'saml_'+hostid;
 3855:     var tablecellon = 'samloptionson_'+hostid;
 3856:     var tablecelloff = 'samloptionsoff_'+hostid;
 3857:     var num = form.elements[radioname].length;
 3858:     if (num) {
 3859:         var setvis = '';
 3860:         for (var i=0; i<num; i++) {
 3861:             if (form.elements[radioname][i].checked) {
 3862:                 if (form.elements[radioname][i].value == '1') {
 3863:                     if (document.getElementById(tablecellon)) {
 3864:                         document.getElementById(tablecellon).style.display='';
 3865:                     }
 3866:                     if (document.getElementById(tablecelloff)) {
 3867:                         document.getElementById(tablecelloff).style.display='none';
 3868:                     }
 3869:                     setvis = 1;
 3870:                 }
 3871:                 break;
 3872:             }
 3873:         }
 3874:         if (!setvis) {
 3875:             if (document.getElementById(tablecellon)) {
 3876:                 document.getElementById(tablecellon).style.display='none';
 3877:             }
 3878:             if (document.getElementById(tablecelloff)) {
 3879:                 document.getElementById(tablecelloff).style.display='';
 3880:             }
 3881:         }
 3882:     }
 3883:     return;
 3884: }
 3885: // ]]>
 3886: </script>
 3887: 
 3888: ENDSCRIPT
 3889: }
 3890: 
 3891: sub ipaccess_javascript {
 3892:     my ($settings) = @_;
 3893:     my (%ordered,$total,%jstext);
 3894:     $total = 0;
 3895:     if (ref($settings) eq 'HASH') {
 3896:         foreach my $item (keys(%{$settings})) {
 3897:             if (ref($settings->{$item}) eq 'HASH') {
 3898:                 my $num = $settings->{$item}{'order'};
 3899:                 $ordered{$num} = $item;
 3900:             }
 3901:         }
 3902:         $total = scalar(keys(%{$settings}));
 3903:     }
 3904:     my @jsarray = ();
 3905:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3906:         push(@jsarray,$ordered{$item});
 3907:     }
 3908:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3909:     return <<"ENDSCRIPT";
 3910: <script type="text/javascript">
 3911: // <![CDATA[
 3912: function reorderIPaccess(form,item) {
 3913:     var changedVal;
 3914: $jstext
 3915:     var newpos = 'ipaccess_pos_add';
 3916:     var maxh = 1 + $total;
 3917:     var current = new Array;
 3918:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3919:     if (item == newpos) {
 3920:         changedVal = newitemVal;
 3921:     } else {
 3922:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3923:         current[newitemVal] = newpos;
 3924:     }
 3925:     for (var i=0; i<ipaccess.length; i++) {
 3926:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3927:         if (elementName != item) {
 3928:             if (form.elements[elementName]) {
 3929:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3930:                 current[currVal] = elementName;
 3931:             }
 3932:         }
 3933:     }
 3934:     var oldVal;
 3935:     for (var j=0; j<maxh; j++) {
 3936:         if (current[j] == undefined) {
 3937:             oldVal = j;
 3938:         }
 3939:     }
 3940:     if (oldVal < changedVal) {
 3941:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3942:            var elementName = current[k];
 3943:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3944:         }
 3945:     } else {
 3946:         for (var k=changedVal; k<oldVal; k++) {
 3947:             var elementName = current[k];
 3948:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3949:         }
 3950:     }
 3951:     return;
 3952: }
 3953: // ]]>
 3954: </script>
 3955: 
 3956: ENDSCRIPT
 3957: }
 3958: 
 3959: sub authordefaults_javascript {
 3960:     my %alert = &Apache::lonlocal::texthash (
 3961:                     reqd => 'Warning: at least one editor needs to be available.',
 3962:                     rest => 'Unchecking this editor disallowed while others unchecked.',
 3963:     );
 3964:     &js_escape(\%alert);
 3965:     return <<"ENDSCRIPT";
 3966: <script type="text/javascript">
 3967: // <![CDATA[
 3968: 
 3969: function checkEditors(form,checkbox,current) {
 3970:     if (form.elements[checkbox].length != undefined) {
 3971:         var count = 0;
 3972:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3973:             if (form.elements[checkbox][i].checked) {
 3974:                 count ++;
 3975:             }
 3976:         }
 3977:         if (count == 0) {
 3978:             if (current.type =='radio') {
 3979:                 current.checked = true;
 3980:                 alert('$alert{reqd}\\n$alert{rest}');
 3981:             }
 3982:         }
 3983:     }
 3984:     return;
 3985: }
 3986: // ]]>
 3987: </script>
 3988: 
 3989: ENDSCRIPT
 3990: }
 3991: 
 3992: sub print_autoenroll {
 3993:     my ($dom,$settings,$rowtotal) = @_;
 3994:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3995:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3996:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3997:     $failsafesty = 'none';
 3998:     %failsafechecked = (
 3999:         off => ' checked="checked"',
 4000:     );
 4001:     if (ref($settings) eq 'HASH') {
 4002:         if (exists($settings->{'run'})) {
 4003:             if ($settings->{'run'} eq '0') {
 4004:                 $runoff = ' checked="checked" ';
 4005:                 $runon = ' ';
 4006:             } else {
 4007:                 $runon = ' checked="checked" ';
 4008:                 $runoff = ' ';
 4009:             }
 4010:         } else {
 4011:             if ($autorun) {
 4012:                 $runon = ' checked="checked" ';
 4013:                 $runoff = ' ';
 4014:             } else {
 4015:                 $runoff = ' checked="checked" ';
 4016:                 $runon = ' ';
 4017:             }
 4018:         }
 4019:         if (exists($settings->{'co-owners'})) {
 4020:             if ($settings->{'co-owners'} eq '0') {
 4021:                 $coownersoff = ' checked="checked" ';
 4022:                 $coownerson = ' ';
 4023:             } else {
 4024:                 $coownerson = ' checked="checked" ';
 4025:                 $coownersoff = ' ';
 4026:             }
 4027:         } else {
 4028:             $coownersoff = ' checked="checked" ';
 4029:             $coownerson = ' ';
 4030:         }
 4031:         if (exists($settings->{'sender_domain'})) {
 4032:             $defdom = $settings->{'sender_domain'};
 4033:         }
 4034:         if (exists($settings->{'failsafe'})) {
 4035:             $failsafe = $settings->{'failsafe'};
 4036:             if ($failsafe eq 'zero') {
 4037:                 $failsafechecked{'zero'} = ' checked="checked"';
 4038:                 $failsafechecked{'off'} = '';
 4039:                 $failsafesty = 'inline-block';
 4040:             } elsif ($failsafe eq 'any') {
 4041:                 $failsafechecked{'any'} = ' checked="checked"';
 4042:                 $failsafechecked{'off'} = '';
 4043:             }
 4044:             $autofailsafe = $settings->{'autofailsafe'};
 4045:         } elsif (exists($settings->{'autofailsafe'})) {
 4046:             $autofailsafe = $settings->{'autofailsafe'};
 4047:             if ($autofailsafe ne '') {
 4048:                 $failsafechecked{'zero'} = ' checked="checked"';
 4049:                 $failsafe = 'zero';
 4050:                 $failsafechecked{'off'} = '';
 4051:             }
 4052:         }
 4053:     } else {
 4054:         if ($autorun) {
 4055:             $runon = ' checked="checked" ';
 4056:             $runoff = ' ';
 4057:         } else {
 4058:             $runoff = ' checked="checked" ';
 4059:             $runon = ' ';
 4060:         }
 4061:     }
 4062:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 4063:     my $notif_sender;
 4064:     if (ref($settings) eq 'HASH') {
 4065:         $notif_sender = $settings->{'sender_uname'};
 4066:     }
 4067:     my $datatable='<tr class="LC_odd_row">'.
 4068:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 4069:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4070:                   '<input type="radio" name="autoenroll_run"'.
 4071:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4072:                   '<label><input type="radio" name="autoenroll_run"'.
 4073:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4074:                   '</tr><tr>'.
 4075:                   '<td>'.&mt('Notification messages - sender').
 4076:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4077:                   &mt('username').':&nbsp;'.
 4078:                   '<input type="text" name="sender_uname" value="'.
 4079:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4080:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4081:                   '<tr class="LC_odd_row">'.
 4082:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4083:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4084:                   '<input type="radio" name="autoassign_coowners"'.
 4085:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4086:                   '<label><input type="radio" name="autoassign_coowners"'.
 4087:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4088:                   '</tr><tr>'.
 4089:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4090:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4091:                   '<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; '.
 4092:                   '<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 />'.
 4093:                   '<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>'.
 4094:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4095:                   '<span class="LC_nobreak">'.
 4096:                   &mt('Threshold for number of students in section to drop: [_1]',
 4097:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4098:                   '</span></div></td></tr>';
 4099:     $$rowtotal += 4;
 4100:     return $datatable;
 4101: }
 4102: 
 4103: sub print_autoupdate {
 4104:     my ($position,$dom,$settings,$rowtotal) = @_;
 4105:     my ($enable,$datatable);
 4106:     if ($position eq 'top') {
 4107:         my %choices = &Apache::lonlocal::texthash (
 4108:                           run        => 'Auto-update active?',
 4109:                           classlists => 'Update information in classlists?',
 4110:                           unexpired  => 'Skip updates for users without active or future roles?',
 4111:                           lastactive => 'Skip updates for inactive users?',
 4112:         );
 4113:         my $itemcount = 0;
 4114:         my $updateon = ' ';
 4115:         my $updateoff = ' checked="checked" ';
 4116:         if (ref($settings) eq 'HASH') {
 4117:             if ($settings->{'run'} eq '1') {
 4118:                 $updateon = $updateoff;
 4119:                 $updateoff = ' ';
 4120:             }
 4121:         }
 4122:         $enable = '<tr class="LC_odd_row">'.
 4123:                   '<td>'.$choices{'run'}.'</td>'.
 4124:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4125:                   '<input type="radio" name="autoupdate_run"'.
 4126:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4127:                   '<label><input type="radio" name="autoupdate_run"'.
 4128:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4129:                   '</tr>';
 4130:         my @toggles = ('classlists','unexpired');
 4131:         my %defaultchecked = ('classlists' => 'off',
 4132:                               'unexpired'  => 'off'
 4133:                               );
 4134:         $$rowtotal ++;
 4135:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4136:                                                      \%choices,$itemcount,'','','left','no');
 4137:         $datatable = $enable.$datatable;
 4138:         $$rowtotal += $itemcount;
 4139:         my $lastactiveon = ' ';
 4140:         my $lastactiveoff = ' checked="checked" ';
 4141:         my $lastactivestyle = 'none';
 4142:         my $lastactivedays;
 4143:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4144:         if (ref($settings) eq 'HASH') {
 4145:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4146:                 $lastactiveon = $lastactiveoff;
 4147:                 $lastactiveoff = ' ';
 4148:                 $lastactivestyle = 'inline-block';
 4149:                 $lastactivedays = $settings->{'lastactive'};
 4150:             }
 4151:         }
 4152:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4153:         $datatable .= '<tr'.$css_class.'>'.
 4154:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4155:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4156:                       '<input type="radio" name="lastactive"'.
 4157:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4158:                       '&nbsp;<label>'.
 4159:                       '<input type="radio" name="lastactive"'.
 4160:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4161:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4162:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4163:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4164:                           $lastactivedays.'" />').
 4165:                       '</span></td>'.
 4166:                       '</tr>';
 4167:         $$rowtotal ++;
 4168:     } elsif ($position eq 'middle') {
 4169:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4170:         my $numinrow = 3;
 4171:         my $locknamesettings;
 4172:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4173:                                      $dom,$numinrow,$othertitle,
 4174:                                     'lockablenames',$rowtotal);
 4175:         $$rowtotal ++;
 4176:     } else {
 4177:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4178:         my @fields = ('lastname','firstname','middlename','generation',
 4179:                       'permanentemail','id');
 4180:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4181:         my $numrows = 0;
 4182:         if (ref($types) eq 'ARRAY') {
 4183:             if (@{$types} > 0) {
 4184:                 $datatable = 
 4185:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4186:                                          \@fields,$types,\$numrows);
 4187:                     $$rowtotal += @{$types}; 
 4188:             }
 4189:         }
 4190:         $datatable .= 
 4191:             &usertype_update_row($settings,{'default' => $othertitle},
 4192:                                  \%fieldtitles,\@fields,['default'],
 4193:                                  \$numrows);
 4194:         $$rowtotal ++;     
 4195:     }
 4196:     return $datatable;
 4197: }
 4198: 
 4199: sub print_autocreate {
 4200:     my ($dom,$settings,$rowtotal) = @_;
 4201:     my (%createon,%createoff,%currhash);
 4202:     my @types = ('xml','req');
 4203:     if (ref($settings) eq 'HASH') {
 4204:         foreach my $item (@types) {
 4205:             $createoff{$item} = ' checked="checked" ';
 4206:             $createon{$item} = ' ';
 4207:             if (exists($settings->{$item})) {
 4208:                 if ($settings->{$item}) {
 4209:                     $createon{$item} = ' checked="checked" ';
 4210:                     $createoff{$item} = ' ';
 4211:                 }
 4212:             }
 4213:         }
 4214:         if ($settings->{'xmldc'} ne '') {
 4215:             $currhash{$settings->{'xmldc'}} = 1;
 4216:         }
 4217:     } else {
 4218:         foreach my $item (@types) {
 4219:             $createoff{$item} = ' checked="checked" ';
 4220:             $createon{$item} = ' ';
 4221:         }
 4222:     }
 4223:     $$rowtotal += 2;
 4224:     my $numinrow = 2;
 4225:     my $datatable='<tr class="LC_odd_row">'.
 4226:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4227:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4228:                   '<input type="radio" name="autocreate_xml"'.
 4229:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4230:                   '<label><input type="radio" name="autocreate_xml"'.
 4231:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4232:                   '</td></tr><tr>'.
 4233:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4234:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4235:                   '<input type="radio" name="autocreate_req"'.
 4236:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4237:                   '<label><input type="radio" name="autocreate_req"'.
 4238:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4239:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4240:                                                    'autocreate_xmldc',%currhash);
 4241:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4242:     if ($numdc > 1) {
 4243:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4244:                       '</td><td class="LC_left_item">';
 4245:     } else {
 4246:         $datatable .= &mt('Course creation processed as:').
 4247:                       '</td><td class="LC_right_item">';
 4248:     }
 4249:     $datatable .= $dctable.'</td></tr>';
 4250:     $$rowtotal += $rows;
 4251:     return $datatable;
 4252: }
 4253: 
 4254: sub print_directorysrch {
 4255:     my ($position,$dom,$settings,$rowtotal) = @_;
 4256:     my $datatable;
 4257:     if ($position eq 'top') {
 4258:         my $instsrchon = ' ';
 4259:         my $instsrchoff = ' checked="checked" ';
 4260:         my ($exacton,$containson,$beginson);
 4261:         my $instlocalon = ' ';
 4262:         my $instlocaloff = ' checked="checked" ';
 4263:         if (ref($settings) eq 'HASH') {
 4264:             if ($settings->{'available'} eq '1') {
 4265:                 $instsrchon = $instsrchoff;
 4266:                 $instsrchoff = ' ';
 4267:             }
 4268:             if ($settings->{'localonly'} eq '1') {
 4269:                 $instlocalon = $instlocaloff;
 4270:                 $instlocaloff = ' ';
 4271:             }
 4272:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4273:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4274:                     if ($type eq 'exact') {
 4275:                         $exacton = ' checked="checked" ';
 4276:                     } elsif ($type eq 'contains') {
 4277:                         $containson = ' checked="checked" ';
 4278:                     } elsif ($type eq 'begins') {
 4279:                         $beginson = ' checked="checked" ';
 4280:                     }
 4281:                 }
 4282:             } else {
 4283:                 if ($settings->{'searchtypes'} eq 'exact') {
 4284:                     $exacton = ' checked="checked" ';
 4285:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4286:                     $containson = ' checked="checked" ';
 4287:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4288:                     $exacton = ' checked="checked" ';
 4289:                     $containson = ' checked="checked" ';
 4290:                 }
 4291:             }
 4292:         }
 4293:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4294:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4295: 
 4296:         my $numinrow = 4;
 4297:         my $cansrchrow = 0;
 4298:         $datatable='<tr class="LC_odd_row">'.
 4299:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4300:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4301:                    '<input type="radio" name="dirsrch_available"'.
 4302:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4303:                    '<label><input type="radio" name="dirsrch_available"'.
 4304:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4305:                    '</tr><tr>'.
 4306:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4307:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4308:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4309:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4310:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4311:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4312:                    '</tr>';
 4313:         $$rowtotal += 2;
 4314:         if (ref($usertypes) eq 'HASH') {
 4315:             if (keys(%{$usertypes}) > 0) {
 4316:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4317:                                              $numinrow,$othertitle,'cansearch',
 4318:                                              $rowtotal);
 4319:                 $cansrchrow = 1;
 4320:             }
 4321:         }
 4322:         if ($cansrchrow) {
 4323:             $$rowtotal ++;
 4324:             $datatable .= '<tr>';
 4325:         } else {
 4326:             $datatable .= '<tr class="LC_odd_row">';
 4327:         }
 4328:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4329:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4330:         foreach my $title (@{$titleorder}) {
 4331:             if (defined($searchtitles->{$title})) {
 4332:                 my $check = ' ';
 4333:                 if (ref($settings) eq 'HASH') {
 4334:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4335:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4336:                             $check = ' checked="checked" ';
 4337:                         }
 4338:                     }
 4339:                 }
 4340:                 $datatable .= '<td class="LC_left_item">'.
 4341:                               '<span class="LC_nobreak"><label>'.
 4342:                               '<input type="checkbox" name="searchby" '.
 4343:                               'value="'.$title.'"'.$check.'/>'.
 4344:                               $searchtitles->{$title}.'</label></span></td>';
 4345:             }
 4346:         }
 4347:         $datatable .= '</tr></table></td></tr>';
 4348:         $$rowtotal ++;
 4349:         if ($cansrchrow) {
 4350:             $datatable .= '<tr class="LC_odd_row">';
 4351:         } else {
 4352:             $datatable .= '<tr>';
 4353:         }
 4354:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4355:                       '<td class="LC_left_item" colspan="2">'.
 4356:                       '<span class="LC_nobreak"><label>'.
 4357:                       '<input type="checkbox" name="searchtypes" '.
 4358:                       $exacton.' value="exact" />'.&mt('Exact match').
 4359:                       '</label>&nbsp;'.
 4360:                       '<label><input type="checkbox" name="searchtypes" '.
 4361:                       $beginson.' value="begins" />'.&mt('Begins with').
 4362:                       '</label>&nbsp;'.
 4363:                       '<label><input type="checkbox" name="searchtypes" '.
 4364:                       $containson.' value="contains" />'.&mt('Contains').
 4365:                       '</label></span></td></tr>';
 4366:         $$rowtotal ++;
 4367:     } else {
 4368:         my $domsrchon = ' checked="checked" ';
 4369:         my $domsrchoff = ' ';
 4370:         my $domlocalon = ' ';
 4371:         my $domlocaloff = ' checked="checked" ';
 4372:         if (ref($settings) eq 'HASH') {
 4373:             if ($settings->{'lclocalonly'} eq '1') {
 4374:                 $domlocalon = $domlocaloff;
 4375:                 $domlocaloff = ' ';
 4376:             }
 4377:             if ($settings->{'lcavailable'} eq '0') {
 4378:                 $domsrchoff = $domsrchon;
 4379:                 $domsrchon = ' ';
 4380:             }
 4381:         }
 4382:         $datatable='<tr class="LC_odd_row">'.
 4383:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4384:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4385:                       '<input type="radio" name="dirsrch_domavailable"'.
 4386:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4387:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4388:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4389:                       '</tr><tr>'.
 4390:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4391:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4392:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4393:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4394:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4395:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4396:                       '</tr>';
 4397:         $$rowtotal += 2;
 4398:     }
 4399:     return $datatable;
 4400: }
 4401: 
 4402: sub print_contacts {
 4403:     my ($position,$dom,$settings,$rowtotal) = @_;
 4404:     my $datatable;
 4405:     my @contacts = ('adminemail','supportemail');
 4406:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4407:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4408:     if ($position eq 'top') {
 4409:         if (ref($settings) eq 'HASH') {
 4410:             foreach my $item (@contacts) {
 4411:                 if (exists($settings->{$item})) {
 4412:                     $to{$item} = $settings->{$item};
 4413:                 }
 4414:             }
 4415:         }
 4416:     } elsif ($position eq 'middle') {
 4417:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4418:                      'updatesmail','idconflictsmail','hostipmail');
 4419:         foreach my $type (@mailings) {
 4420:             $otheremails{$type} = '';
 4421:         }
 4422:     } elsif ($position eq 'lower') {
 4423:         if (ref($settings) eq 'HASH') {
 4424:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4425:                 %lonstatus = %{$settings->{'lonstatus'}};
 4426:             }
 4427:         }
 4428:     } else {
 4429:         @mailings = ('helpdeskmail','otherdomsmail');
 4430:         foreach my $type (@mailings) {
 4431:             $otheremails{$type} = '';
 4432:         }
 4433:         $bccemails{'helpdeskmail'} = '';
 4434:         $bccemails{'otherdomsmail'} = '';
 4435:         $includestr{'helpdeskmail'} = '';
 4436:         $includestr{'otherdomsmail'} = '';
 4437:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4438:     }
 4439:     if (ref($settings) eq 'HASH') {
 4440:         unless (($position eq 'top') || ($position eq 'lower')) {
 4441:             foreach my $type (@mailings) {
 4442:                 if (exists($settings->{$type})) {
 4443:                     if (ref($settings->{$type}) eq 'HASH') {
 4444:                         foreach my $item (@contacts) {
 4445:                             if ($settings->{$type}{$item}) {
 4446:                                 $checked{$type}{$item} = ' checked="checked" ';
 4447:                             }
 4448:                         }
 4449:                         $otheremails{$type} = $settings->{$type}{'others'};
 4450:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 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:                     }
 4458:                 } elsif ($type eq 'lonstatusmail') {
 4459:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4460:                 }
 4461:             }
 4462:         }
 4463:         if ($position eq 'bottom') {
 4464:             foreach my $type (@mailings) {
 4465:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4466:                 if ($settings->{$type}{'include'} ne '') {
 4467:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4468:                     $includestr{$type} = &unescape($includestr{$type});
 4469:                 }
 4470:             }
 4471:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4472:                 if (ref($fields) eq 'ARRAY') {
 4473:                     foreach my $field (@{$fields}) {
 4474:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4475:                     }
 4476:                 }
 4477:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4478:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4479:                 } else {
 4480:                     $maxsize = '1.0';
 4481:                 }
 4482:             } else {
 4483:                 if (ref($fields) eq 'ARRAY') {
 4484:                     foreach my $field (@{$fields}) {
 4485:                         $currfield{$field} = 'yes';
 4486:                     }
 4487:                 }
 4488:                 $maxsize = '1.0';
 4489:             }
 4490:         }
 4491:     } else {
 4492:         if ($position eq 'top') {
 4493:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4494:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4495:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4496:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4497:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4498:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4499:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4500:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4501:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4502:         } elsif ($position eq 'bottom') {
 4503:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4504:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4505:             if (ref($fields) eq 'ARRAY') {
 4506:                 foreach my $field (@{$fields}) {
 4507:                     $currfield{$field} = 'yes';
 4508:                 }
 4509:             }
 4510:             $maxsize = '1.0';
 4511:         }
 4512:     }
 4513:     my ($titles,$short_titles) = &contact_titles();
 4514:     my $rownum = 0;
 4515:     my $css_class;
 4516:     if ($position eq 'top') {
 4517:         foreach my $item (@contacts) {
 4518:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4519:             $datatable .= '<tr'.$css_class.'>'. 
 4520:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4521:                           '</span></td><td class="LC_right_item">'.
 4522:                           '<input type="text" name="'.$item.'" value="'.
 4523:                           $to{$item}.'" /></td></tr>';
 4524:             $rownum ++;
 4525:         }
 4526:     } elsif ($position eq 'bottom') {
 4527:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4528:         $datatable .= '<tr'.$css_class.'>'.
 4529:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4530:                       &mt('(e-mail, subject, and description always shown)').
 4531:                       '</td><td class="LC_left_item">';
 4532:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4533:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4534:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4535:             foreach my $field (@{$fields}) {
 4536:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4537:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4538:                     $datatable .= ' '.&mt('(logged-in users)');
 4539:                 }
 4540:                 $datatable .='</td><td>';
 4541:                 my $clickaction;
 4542:                 if ($field eq 'screenshot') {
 4543:                     $clickaction = ' onclick="screenshotSize(this);"';
 4544:                 }
 4545:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4546:                     foreach my $option (@{$possoptions->{$field}}) {
 4547:                         my $checked;
 4548:                         if ($currfield{$field} eq $option) {
 4549:                             $checked = ' checked="checked"';
 4550:                         }
 4551:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4552:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4553:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4554:                                       '</label></span>'.('&nbsp;'x2);
 4555:                     }
 4556:                 }
 4557:                 if ($field eq 'screenshot') {
 4558:                     my $display;
 4559:                     if ($currfield{$field} eq 'no') {
 4560:                         $display = ' style="display:none"';
 4561:                     }
 4562:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4563:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4564:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4565:                 }
 4566:                 $datatable .= '</td></tr>';
 4567:             }
 4568:             $datatable .= '</table>';
 4569:         }
 4570:         $datatable .= '</td></tr>'."\n";
 4571:         $rownum ++;
 4572:     }
 4573:     unless (($position eq 'top') || ($position eq 'lower')) {
 4574:         foreach my $type (@mailings) {
 4575:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4576:             $datatable .= '<tr'.$css_class.'>'.
 4577:                           '<td><span class="LC_nobreak">'.
 4578:                           $titles->{$type}.': </span></td>'.
 4579:                           '<td class="LC_left_item">';
 4580:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4581:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4582:             }
 4583:             $datatable .= '<span class="LC_nobreak">';
 4584:             foreach my $item (@contacts) {
 4585:                 $datatable .= '<label>'.
 4586:                               '<input type="checkbox" name="'.$type.'"'.
 4587:                               $checked{$type}{$item}.
 4588:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4589:                               '</label>&nbsp;';
 4590:             }
 4591:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4592:                           '<input type="text" name="'.$type.'_others" '.
 4593:                           'value="'.$otheremails{$type}.'"  />';
 4594:             my %locchecked;
 4595:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4596:                 foreach my $loc ('s','b') {
 4597:                     if ($includeloc{$type} eq $loc) {
 4598:                         $locchecked{$loc} = ' checked="checked"';
 4599:                         last;
 4600:                     }
 4601:                 }
 4602:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4603:                               '<input type="text" name="'.$type.'_bcc" '.
 4604:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4605:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4606:                               &mt('Text automatically added to e-mail:').' '.
 4607:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4608:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4609:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4610:                               ('&nbsp;'x2).
 4611:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4612:                               '</span></fieldset>';
 4613:             }
 4614:             $datatable .= '</td></tr>'."\n";
 4615:             $rownum ++;
 4616:         }
 4617:     }
 4618:     if ($position eq 'middle') {
 4619:         my %choices;
 4620:         my $corelink = &core_link_msu();
 4621:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4622:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4623:                                         $corelink);
 4624:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4625:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4626:         my %defaultchecked = ('reporterrors'  => 'on',
 4627:                               'reportupdates' => 'on',
 4628:                               'reportstatus'  => 'on');
 4629:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4630:                                                    \%choices,$rownum);
 4631:         $datatable .= $reports;
 4632:     } elsif ($position eq 'lower') {
 4633:         my (%current,%excluded,%weights);
 4634:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4635:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4636:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4637:         } else {
 4638:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4639:         }
 4640:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4641:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4642:         } else {
 4643:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4644:         }
 4645:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4646:             foreach my $type ('E','W','N','U') {
 4647:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4648:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4649:                 } else {
 4650:                     $weights{$type} = $defaults->{$type};
 4651:                 }
 4652:             }
 4653:         } else {
 4654:             foreach my $type ('E','W','N','U') {
 4655:                 $weights{$type} = $defaults->{$type};
 4656:             }
 4657:         }
 4658:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4659:             if (@{$lonstatus{'excluded'}} > 0) {
 4660:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4661:             }
 4662:         }
 4663:         foreach my $item ('errorthreshold','errorsysmail') {
 4664:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4665:             $datatable .= '<tr'.$css_class.'>'.
 4666:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4667:                           $titles->{$item}.
 4668:                           '</span></td><td class="LC_left_item">'.
 4669:                           '<input type="text" name="'.$item.'" value="'.
 4670:                           $current{$item}.'" size="5" /></td></tr>';
 4671:             $rownum ++;
 4672:         }
 4673:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4674:         $datatable .= '<tr'.$css_class.'>'.
 4675:                       '<td class="LC_left_item">'.
 4676:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4677:                       '</span></td><td class="LC_left_item"><table><tr>';
 4678:         foreach my $type ('E','W','N','U') {
 4679:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4680:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4681:                           $weights{$type}.'" size="5" /></td>';
 4682:         }
 4683:         $datatable .= '</tr></table></tr>';
 4684:         $rownum ++;
 4685:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4686:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4687:                       $titles->{'errorexcluded'}.'</td>'.
 4688:                       '<td class="LC_left_item"><table>';
 4689:         my $numinrow = 4;
 4690:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4691:         for (my $i=0; $i<@ids; $i++) {
 4692:             my $rem = $i%($numinrow);
 4693:             if ($rem == 0) {
 4694:                 if ($i > 0) {
 4695:                     $datatable .= '</tr>';
 4696:                 }
 4697:                 $datatable .= '<tr>';
 4698:             }
 4699:             my $check;
 4700:             if ($excluded{$ids[$i]}) {
 4701:                 $check = ' checked="checked" ';
 4702:             }
 4703:             $datatable .= '<td class="LC_left_item">'.
 4704:                           '<span class="LC_nobreak"><label>'.
 4705:                           '<input type="checkbox" name="errorexcluded" '.
 4706:                           'value="'.$ids[$i].'"'.$check.' />'.
 4707:                           $ids[$i].'</label></span></td>';
 4708:         }
 4709:         my $colsleft = $numinrow - @ids%($numinrow);
 4710:         if ($colsleft > 1 ) {
 4711:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4712:                           '&nbsp;</td>';
 4713:         } elsif ($colsleft == 1) {
 4714:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4715:         }
 4716:         $datatable .= '</tr></table></td></tr>';
 4717:         $rownum ++;
 4718:     } elsif ($position eq 'bottom') {
 4719:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4720:         my (@posstypes,%usertypeshash);
 4721:         if (ref($types) eq 'ARRAY') {
 4722:             @posstypes = @{$types};
 4723:         }
 4724:         if (@posstypes) {
 4725:             if (ref($usertypes) eq 'HASH') {
 4726:                 %usertypeshash = %{$usertypes};
 4727:             }
 4728:             my @overridden;
 4729:             my $numinrow = 4;
 4730:             if (ref($settings) eq 'HASH') {
 4731:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4732:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4733:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4734:                             push(@overridden,$key);
 4735:                             foreach my $item (@contacts) {
 4736:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4737:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4738:                                 }
 4739:                             }
 4740:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4741:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4742:                             $includeloc{'override_'.$key} = '';
 4743:                             $includestr{'override_'.$key} = '';
 4744:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4745:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) =
 4746:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4747:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4748:                             }
 4749:                         }
 4750:                     }
 4751:                 }
 4752:             }
 4753:             my $customclass = 'LC_helpdesk_override';
 4754:             my $optionsprefix = 'LC_options_helpdesk_';
 4755: 
 4756:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4757:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4758:                                          $numinrow,$othertitle,'overrides',
 4759:                                          \$rownum,$onclicktypes,$customclass);
 4760:             $rownum ++;
 4761:             $usertypeshash{'default'} = $othertitle;
 4762:             foreach my $status (@posstypes) {
 4763:                 my $css_class;
 4764:                 if ($rownum%2) {
 4765:                     $css_class = 'LC_odd_row ';
 4766:                 }
 4767:                 $css_class .= $customclass;
 4768:                 my $rowid = $optionsprefix.$status;
 4769:                 my $hidden = 1;
 4770:                 my $currstyle = 'display:none';
 4771:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4772:                     $currstyle = 'display:table-row';
 4773:                     $hidden = 0;
 4774:                 }
 4775:                 my $key = 'override_'.$status;
 4776:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4777:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4778:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4779:                                                   \@contacts,$short_titles);
 4780:                 unless ($hidden) {
 4781:                     $rownum ++;
 4782:                 }
 4783:             }
 4784:         }
 4785:     }
 4786:     $$rowtotal += $rownum;
 4787:     return $datatable;
 4788: }
 4789: 
 4790: sub core_link_msu {
 4791:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4792:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4793: }
 4794: 
 4795: sub overridden_helpdesk {
 4796:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4797:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4798:     my $class = 'LC_left_item';
 4799:     if ($css_class) {
 4800:         $css_class = ' class="'.$css_class.'"';
 4801:     }
 4802:     if ($rowid) {
 4803:         $rowid = ' id="'.$rowid.'"';
 4804:     }
 4805:     if ($rowstyle) {
 4806:         $rowstyle = ' style="'.$rowstyle.'"';
 4807:     }
 4808:     my ($output,$description);
 4809:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4810:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4811:               "<td>$description</td>\n".
 4812:               '<td class="'.$class.'" colspan="2">'.
 4813:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4814:               '<span class="LC_nobreak">';
 4815:     if (ref($contacts) eq 'ARRAY') {
 4816:         foreach my $item (@{$contacts}) {
 4817:             my $check;
 4818:             if (ref($checked) eq 'HASH') {
 4819:                $check = $checked->{$item};
 4820:             }
 4821:             my $title;
 4822:             if (ref($short_titles) eq 'HASH') {
 4823:                 $title = $short_titles->{$item};
 4824:             }
 4825:             $output .= '<label>'.
 4826:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4827:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4828:         }
 4829:     }
 4830:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4831:                '<input type="text" name="override_'.$type.'_others" '.
 4832:                'value="'.$otheremails.'"  />';
 4833:     my %locchecked;
 4834:     foreach my $loc ('s','b') {
 4835:         if ($includeloc eq $loc) {
 4836:             $locchecked{$loc} = ' checked="checked"';
 4837:             last;
 4838:         }
 4839:     }
 4840:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4841:                '<input type="text" name="override_'.$type.'_bcc" '.
 4842:                'value="'.$bccemails.'"  /></fieldset>'.
 4843:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4844:                &mt('Text automatically added to e-mail:').' '.
 4845:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4846:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4847:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4848:                ('&nbsp;'x2).
 4849:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4850:                '</span></fieldset>'.
 4851:                '</td></tr>'."\n";
 4852:     return $output;
 4853: }
 4854: 
 4855: sub contacts_javascript {
 4856:     return <<"ENDSCRIPT";
 4857: 
 4858: <script type="text/javascript">
 4859: // <![CDATA[
 4860: 
 4861: function screenshotSize(field) {
 4862:     if (document.getElementById('help_screenshotsize')) {
 4863:         if (field.value == 'no') {
 4864:             document.getElementById('help_screenshotsize').style.display="none";
 4865:         } else {
 4866:             document.getElementById('help_screenshotsize').style.display="";
 4867:         }
 4868:     }
 4869:     return;
 4870: }
 4871: 
 4872: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4873:     if (form.elements[checkbox].length != undefined) {
 4874:         var count = 0;
 4875:         if (docount) {
 4876:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4877:                 if (form.elements[checkbox][i].checked) {
 4878:                     count ++;
 4879:                 }
 4880:             }
 4881:         }
 4882:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4883:             var type = form.elements[checkbox][i].value;
 4884:             if (document.getElementById(prefix+type)) {
 4885:                 if (form.elements[checkbox][i].checked) {
 4886:                     document.getElementById(prefix+type).style.display = 'table-row';
 4887:                     if (count % 2 == 1) {
 4888:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4889:                     } else {
 4890:                         document.getElementById(prefix+type).className = target;
 4891:                     }
 4892:                     count ++;
 4893:                 } else {
 4894:                     document.getElementById(prefix+type).style.display = 'none';
 4895:                 }
 4896:             }
 4897:         }
 4898:     }
 4899:     return;
 4900: }
 4901: 
 4902: // ]]>
 4903: </script>
 4904: 
 4905: ENDSCRIPT
 4906: }
 4907: 
 4908: sub print_helpsettings {
 4909:     my ($position,$dom,$settings,$rowtotal) = @_;
 4910:     my $confname = $dom.'-domainconfig';
 4911:     my $formname = 'display';
 4912:     my ($datatable,$itemcount);
 4913:     if ($position eq 'top') {
 4914:         $itemcount = 1;
 4915:         my (%choices,%defaultchecked,@toggles);
 4916:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4917:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4918:                                      &mt('LON-CAPA bug tracker'),600,500));
 4919:         %defaultchecked = ('submitbugs' => 'on');
 4920:         @toggles = ('submitbugs');
 4921:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4922:                                                      \%choices,$itemcount);
 4923:         $$rowtotal ++;
 4924:     } else {
 4925:         my $css_class;
 4926:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4927:         my (%customroles,%ordered,%current);
 4928:         if (ref($settings) eq 'HASH') {
 4929:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4930:                 %current = %{$settings->{'adhoc'}};
 4931:             }
 4932:         }
 4933:         my $count = 0;
 4934:         foreach my $key (sort(keys(%existing))) {
 4935:             if ($key=~/^rolesdef\_(\w+)$/) {
 4936:                 my $rolename = $1;
 4937:                 my (%privs,$order);
 4938:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4939:                 $customroles{$rolename} = \%privs;
 4940:                 if (ref($current{$rolename}) eq 'HASH') {
 4941:                     $order = $current{$rolename}{'order'};
 4942:                 }
 4943:                 if ($order eq '') {
 4944:                     $order = $count;
 4945:                 }
 4946:                 $ordered{$order} = $rolename;
 4947:                 $count++;
 4948:             }
 4949:         }
 4950:         my $maxnum = scalar(keys(%ordered));
 4951:         my @roles_by_num = ();
 4952:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4953:             push(@roles_by_num,$item);
 4954:         }
 4955:         my $context = 'domprefs';
 4956:         my $crstype = 'Course';
 4957:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4958:         my @accesstypes = ('all','dh','da','none');
 4959:         my ($numstatustypes,@jsarray);
 4960:         if (ref($types) eq 'ARRAY') {
 4961:             if (@{$types} > 0) {
 4962:                 $numstatustypes = scalar(@{$types});
 4963:                 push(@accesstypes,'status');
 4964:                 @jsarray = ('bystatus');
 4965:             }
 4966:         }
 4967:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4968:         if (keys(%domhelpdesk)) {
 4969:             push(@accesstypes,('inc','exc'));
 4970:             push(@jsarray,('notinc','notexc'));
 4971:         }
 4972:         my $hiddenstr = join("','",@jsarray);
 4973:         my $context = 'domprefs';
 4974:         my $crstype = 'Course';
 4975:         my $prefix = 'helproles_';
 4976:         my $add_class = 'LC_hidden';
 4977:         foreach my $num (@roles_by_num) {
 4978:             my $role = $ordered{$num};
 4979:             my ($desc,$access,@statuses);
 4980:             if (ref($current{$role}) eq 'HASH') {
 4981:                 $desc = $current{$role}{'desc'};
 4982:                 $access = $current{$role}{'access'};
 4983:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4984:                     @statuses = @{$current{$role}{'insttypes'}};
 4985:                 }
 4986:             }
 4987:             if ($desc eq '') {
 4988:                 $desc = $role;
 4989:             }
 4990:             my $identifier = 'custhelp'.$num;
 4991:             my %full=();
 4992:             my %levels= (
 4993:                          course => {},
 4994:                          domain => {},
 4995:                          system => {},
 4996:                         );
 4997:             my %levelscurrent=(
 4998:                                course => {},
 4999:                                domain => {},
 5000:                                system => {},
 5001:                               );
 5002:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 5003:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5004:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5005:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 5006:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 5007:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 5008:             for (my $k=0; $k<=$maxnum; $k++) {
 5009:                 my $vpos = $k+1;
 5010:                 my $selstr;
 5011:                 if ($k == $num) {
 5012:                     $selstr = ' selected="selected" ';
 5013:                 }
 5014:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5015:             }
 5016:             $datatable .= '</select>'.('&nbsp;'x2).
 5017:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 5018:                           '</td>'.
 5019:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5020:                           &mt('Name shown to users:').
 5021:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 5022:                           '</fieldset>'.
 5023:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 5024:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 5025:                           '<fieldset>'.
 5026:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 5027:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 5028:                                                                    \%levelscurrent,$identifier,
 5029:                                                                    'LC_hidden',$prefix.$num.'_privs').
 5030:                           '</fieldset></td>';
 5031:             $itemcount ++;
 5032:         }
 5033:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5034:         my $newcust = 'custhelp'.$count;
 5035:         my (%privs,%levelscurrent);
 5036:         my %full=();
 5037:         my %levels= (
 5038:                      course => {},
 5039:                      domain => {},
 5040:                      system => {},
 5041:                     );
 5042:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 5043:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 5044:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 5045:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 5046:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 5047:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 5048:         for (my $k=0; $k<$maxnum+1; $k++) {
 5049:             my $vpos = $k+1;
 5050:             my $selstr;
 5051:             if ($k == $maxnum) {
 5052:                 $selstr = ' selected="selected" ';
 5053:             }
 5054:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5055:         }
 5056:         $datatable .= '</select>&nbsp;'."\n".
 5057:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 5058:                       '</label></span></td>'.
 5059:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 5060:                       '<span class="LC_nobreak">'.
 5061:                       &mt('Internal name:').
 5062:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 5063:                       '</span>'.('&nbsp;'x4).
 5064:                       '<span class="LC_nobreak">'.
 5065:                       &mt('Name shown to users:').
 5066:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 5067:                       '</span></fieldset>'.
 5068:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5069:                                              $usertypes,$types,\%domhelpdesk).
 5070:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5071:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5072:                                                                 \@templateroles,$newcust).
 5073:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5074:                                                                \%levelscurrent,$newcust).
 5075:                       '</fieldset>'.
 5076:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5077:                       '</td></tr>';
 5078:         $count ++;
 5079:         $$rowtotal += $count;
 5080:     }
 5081:     return $datatable;
 5082: }
 5083: 
 5084: sub adhocbutton {
 5085:     my ($prefix,$num,$field,$visibility) = @_;
 5086:     my %lt = &Apache::lonlocal::texthash(
 5087:                                           show => 'Show details',
 5088:                                           hide => 'Hide details',
 5089:                                         );
 5090:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5091:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5092:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5093:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5094: }
 5095: 
 5096: sub helpsettings_javascript {
 5097:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5098:     return unless(ref($roles_by_num) eq 'ARRAY');
 5099:     my %html_js_lt = &Apache::lonlocal::texthash(
 5100:                                           show => 'Show details',
 5101:                                           hide => 'Hide details',
 5102:                                         );
 5103:     &html_escape(\%html_js_lt);
 5104:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5105:     return <<"ENDSCRIPT";
 5106: <script type="text/javascript">
 5107: // <![CDATA[
 5108: 
 5109: function reorderHelpRoles(form,item) {
 5110:     var changedVal;
 5111: $jstext
 5112:     var newpos = 'helproles_${total}_pos';
 5113:     var maxh = 1 + $total;
 5114:     var current = new Array();
 5115:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5116:     if (item == newpos) {
 5117:         changedVal = newitemVal;
 5118:     } else {
 5119:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5120:         current[newitemVal] = newpos;
 5121:     }
 5122:     for (var i=0; i<helproles.length; i++) {
 5123:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5124:         if (elementName != item) {
 5125:             if (form.elements[elementName]) {
 5126:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5127:                 current[currVal] = elementName;
 5128:             }
 5129:         }
 5130:     }
 5131:     var oldVal;
 5132:     for (var j=0; j<maxh; j++) {
 5133:         if (current[j] == undefined) {
 5134:             oldVal = j;
 5135:         }
 5136:     }
 5137:     if (oldVal < changedVal) {
 5138:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5139:            var elementName = current[k];
 5140:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5141:         }
 5142:     } else {
 5143:         for (var k=changedVal; k<oldVal; k++) {
 5144:             var elementName = current[k];
 5145:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5146:         }
 5147:     }
 5148:     return;
 5149: }
 5150: 
 5151: function helpdeskAccess(num) {
 5152:     var curraccess = null;
 5153:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5154:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5155:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5156:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5157:             }
 5158:         }
 5159:     }
 5160:     var shown = Array();
 5161:     var hidden = Array();
 5162:     if (curraccess == 'none') {
 5163:         hidden = Array('$hiddenstr');
 5164:     } else {
 5165:         if (curraccess == 'status') {
 5166:             shown = Array('bystatus');
 5167:             hidden = Array('notinc','notexc');
 5168:         } else {
 5169:             if (curraccess == 'exc') {
 5170:                 shown = Array('notexc');
 5171:                 hidden = Array('notinc','bystatus');
 5172:             }
 5173:             if (curraccess == 'inc') {
 5174:                 shown = Array('notinc');
 5175:                 hidden = Array('notexc','bystatus');
 5176:             }
 5177:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5178:                 hidden = Array('notinc','notexc','bystatus');
 5179:             }
 5180:         }
 5181:     }
 5182:     if (hidden.length > 0) {
 5183:         for (var i=0; i<hidden.length; i++) {
 5184:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5185:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5186:             }
 5187:         }
 5188:     }
 5189:     if (shown.length > 0) {
 5190:         for (var i=0; i<shown.length; i++) {
 5191:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5192:                 if (shown[i] == 'privs') {
 5193:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5194:                 } else {
 5195:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5196:                 }
 5197:             }
 5198:         }
 5199:     }
 5200:     return;
 5201: }
 5202: 
 5203: function toggleHelpdeskItem(num,field) {
 5204:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5205:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5206:             document.getElementById('helproles_'+num+'_'+field).className =
 5207:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5208:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5209:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5210:             }
 5211:         } else {
 5212:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5213:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5214:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5215:             }
 5216:         }
 5217:     }
 5218:     return;
 5219: }
 5220: 
 5221: // ]]>
 5222: </script>
 5223: 
 5224: ENDSCRIPT
 5225: }
 5226: 
 5227: sub helpdeskroles_access {
 5228:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5229:         $usertypes,$types,$domhelpdesk) = @_;
 5230:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5231:     my %lt = &Apache::lonlocal::texthash(
 5232:                     'rou'    => 'Role usage',
 5233:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5234:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5235:                     'dh'     => 'All with domain helpdesk role',
 5236:                     'da'     => 'All with domain helpdesk assistant role',
 5237:                     'none'   => 'None',
 5238:                     'status' => 'Determined based on institutional status',
 5239:                     'inc'    => 'Include all, but exclude specific personnel',
 5240:                     'exc'    => 'Exclude all, but include specific personnel',
 5241:                   );
 5242:     my %usecheck = (
 5243:                      all => ' checked="checked"',
 5244:                    );
 5245:     my %displaydiv = (
 5246:                       status => 'none',
 5247:                       inc    => 'none',
 5248:                       exc    => 'none',
 5249:                       priv   => 'block',
 5250:                      );
 5251:     my $output;
 5252:     if (ref($current) eq 'HASH') {
 5253:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5254:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5255:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5256:                 delete($usecheck{'all'});
 5257:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5258:                     my $access = $1;
 5259:                     $displaydiv{$access} = 'inline';
 5260:                 } elsif ($current->{access} eq 'none') {
 5261:                     $displaydiv{'priv'} = 'none';
 5262:                 }
 5263:             }
 5264:         }
 5265:     }
 5266:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5267:               '<p>'.$lt{'whi'}.'</p>';
 5268:     foreach my $access (@{$accesstypes}) {
 5269:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5270:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5271:                    $lt{$access}.'</label>';
 5272:         if ($access eq 'status') {
 5273:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5274:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5275:                                                                  $othertitle,$usertypes,$types).
 5276:                        '</div>';
 5277:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5278:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5279:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5280:                        '</div>';
 5281:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5282:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5283:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5284:                        '</div>';
 5285:         }
 5286:         $output .= '</p>';
 5287:     }
 5288:     $output .= '</fieldset>';
 5289:     return $output;
 5290: }
 5291: 
 5292: sub radiobutton_prefs {
 5293:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5294:         $additional,$align,$firstval) = @_;
 5295:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5296:                    (ref($choices) eq 'HASH'));
 5297: 
 5298:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5299: 
 5300:     foreach my $item (@{$toggles}) {
 5301:         if ($defaultchecked->{$item} eq 'on') {
 5302:             $checkedon{$item} = ' checked="checked" ';
 5303:             $checkedoff{$item} = ' ';
 5304:         } elsif ($defaultchecked->{$item} eq 'off') {
 5305:             $checkedoff{$item} = ' checked="checked" ';
 5306:             $checkedon{$item} = ' ';
 5307:         }
 5308:     }
 5309:     if (ref($settings) eq 'HASH') {
 5310:         foreach my $item (@{$toggles}) {
 5311:             if ($settings->{$item} eq '1') {
 5312:                 $checkedon{$item} =  ' checked="checked" ';
 5313:                 $checkedoff{$item} = ' ';
 5314:             } elsif ($settings->{$item} eq '0') {
 5315:                 $checkedoff{$item} =  ' checked="checked" ';
 5316:                 $checkedon{$item} = ' ';
 5317:             }
 5318:         }
 5319:     }
 5320:     if ($onclick) {
 5321:         $onclick = ' onclick="'.$onclick.'"';
 5322:     }
 5323:     foreach my $item (@{$toggles}) {
 5324:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5325:         $datatable .=
 5326:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5327:             '<span class="LC_nobreak">'.$choices->{$item}.
 5328:             '</span></td>';
 5329:         if ($align eq 'left') {
 5330:             $datatable .= '<td class="LC_left_item">';
 5331:         } else {
 5332:             $datatable .= '<td class="LC_right_item">';
 5333:         }
 5334:         $datatable .= '<span class="LC_nobreak">';
 5335:         if ($firstval eq 'no') {
 5336:             $datatable .=
 5337:                 '<label><input type="radio" name="'.
 5338:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5339:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5340:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5341:         } else {
 5342:             $datatable .=
 5343:                 '<label><input type="radio" name="'.
 5344:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5345:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5346:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5347:         }
 5348:         $datatable .= '</span>'.$additional.'</td></tr>';
 5349:         $itemcount ++;
 5350:     }
 5351:     return ($datatable,$itemcount);
 5352: }
 5353: 
 5354: sub print_ltitools {
 5355:     my ($position,$dom,$settings,$rowtotal) = @_;
 5356:     my (%rules,%encrypt,%privkeys,%linkprot);
 5357:     if (ref($settings) eq 'HASH') {
 5358:         if ($position eq 'top') {
 5359:             if (exists($settings->{'encrypt'})) {
 5360:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5361:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5362:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5363:                     }
 5364:                 }
 5365:             }
 5366:             if (exists($settings->{'private'})) {
 5367:                 if (ref($settings->{'private'}) eq 'HASH') {
 5368:                     if (ref($settings->{'private'}) eq 'HASH') {
 5369:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5370:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5371:                         }
 5372:                     }
 5373:                 }
 5374:             }
 5375:         } elsif ($position eq 'middle') {
 5376:             if (exists($settings->{'rules'})) {
 5377:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5378:                     %rules = %{$settings->{'rules'}};
 5379:                 }
 5380:             }
 5381:         } else {
 5382:             foreach my $key ('encrypt','private','rules') {
 5383:                 if (exists($settings->{$key})) {
 5384:                     delete($settings->{$key});
 5385:                 }
 5386:             }
 5387:         }
 5388:     }
 5389:     my $datatable;
 5390:     my $itemcount = 1;
 5391:     if ($position eq 'top') {
 5392:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5393:     } elsif ($position eq 'middle') {
 5394:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5395:         $$rowtotal += $itemcount;
 5396:     } else {
 5397:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5398:     }
 5399:     return $datatable;
 5400: }
 5401: 
 5402: sub ltitools_names {
 5403:     my %lt = &Apache::lonlocal::texthash(
 5404:                                           'title'          => 'Title',
 5405:                                           'version'        => 'Version',
 5406:                                           'msgtype'        => 'Message Type',
 5407:                                           'sigmethod'      => 'Signature Method',
 5408:                                           'url'            => 'URL',
 5409:                                           'key'            => 'Key',
 5410:                                           'lifetime'       => 'Nonce lifetime (s)',
 5411:                                           'secret'         => 'Secret',
 5412:                                           'icon'           => 'Icon',
 5413:                                           'user'           => 'User',
 5414:                                           'fullname'       => 'Full Name',
 5415:                                           'firstname'      => 'First Name',
 5416:                                           'lastname'       => 'Last Name',
 5417:                                           'email'          => 'E-mail',
 5418:                                           'roles'          => 'Role',
 5419:                                           'window'         => 'Window',
 5420:                                           'tab'            => 'Tab',
 5421:                                           'iframe'         => 'iFrame',
 5422:                                           'height'         => 'Height',
 5423:                                           'width'          => 'Width',
 5424:                                           'linktext'       => 'Default Link Text',
 5425:                                           'explanation'    => 'Default Explanation',
 5426:                                           'passback'       => 'Tool can return grades:',
 5427:                                           'roster'         => 'Tool can retrieve roster:',
 5428:                                           'crstarget'      => 'Display target',
 5429:                                           'crslabel'       => 'Course label',
 5430:                                           'crstitle'       => 'Course title',
 5431:                                           'crslinktext'    => 'Link Text',
 5432:                                           'crsexplanation' => 'Explanation',
 5433:                                           'crsappend'      => 'Provider URL',
 5434:                                         );
 5435:     return %lt;
 5436: }
 5437: 
 5438: sub secrets_form {
 5439:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5440:     my @ids=&Apache::lonnet::current_machine_ids();
 5441:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5442:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5443:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5444:     $itemcount = 0;
 5445:     foreach my $hostid (sort(keys(%servers))) {
 5446:         my ($showextra,$divsty,$switch);
 5447:         if ($hostid eq $primary) {
 5448:             if ($context eq 'ltisec') {
 5449:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5450:                     $showextra = 1;
 5451:                 }
 5452:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5453:                     $showextra = 1;
 5454:                 }
 5455:             } else {
 5456:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5457:                     $showextra = 1;
 5458:                 }
 5459:             }
 5460:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5461:                 $switch = 1;
 5462:             }
 5463:             if ($showextra) {
 5464:                 $numshown ++;
 5465:                 $divsty = 'display:inline-block';
 5466:             } else {
 5467:                 $divsty = 'display:none';
 5468:             }
 5469:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5470:                       '<legend>'.$hostid.'</legend>';
 5471:             if ($switch) {
 5472:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5473:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5474:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5475:                 if (exists($privkeys->{$hostid})) {
 5476:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5477:                               '<span class="LC_nobreak">'.
 5478:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5479:                               '<span class="LC_nobreak">'.&mt('Change?').
 5480:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5481:                               ('&nbsp;'x2).
 5482:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5483:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5484:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5485:                               '</span></div>';
 5486:                 } else {
 5487:                     $extra .= '<span class="LC_nobreak">'.
 5488:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5489:                               '</span>'."\n";
 5490:                 }
 5491:             } elsif (exists($privkeys->{$hostid})) {
 5492:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5493:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5494:                           '<span class="LC_nobreak">'.&mt('Change?').
 5495:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5496:                           ('&nbsp;'x2).
 5497:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5498:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5499:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5500:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5501:                           '<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>'.
 5502:                           '</span></div>';
 5503:             } else {
 5504:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5505:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5506:                           '<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>';
 5507:             }
 5508:             $extra .= '</fieldset>';
 5509:         }
 5510:     }
 5511:     my (%choices,@toggles,%defaultchecked);
 5512:     if ($context eq 'ltisec') {
 5513:         %choices = &Apache::lonlocal::texthash (
 5514:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5515:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5516:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5517:                                                );
 5518:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5519:         %defaultchecked = (
 5520:                            'ltisec_crslinkprot' => 'off',
 5521:                            'ltisec_domlinkprot' => 'off',
 5522:                            'ltisec_consumers'   => 'off',
 5523:                           );
 5524:     } else {
 5525:         %choices = &Apache::lonlocal::texthash (
 5526:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5527:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5528:                                                );
 5529:         @toggles = qw(toolsec_crs toolsec_dom);
 5530:         %defaultchecked = (
 5531:                            'toolsec_crs' => 'off',
 5532:                            'toolsec_dom' => 'off',
 5533:                           );
 5534:     }
 5535:     my ($onclick,$itemcount);
 5536:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5537:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5538:                                               \%choices,$itemcount,$onclick,'','left','no');
 5539: 
 5540:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5541:     my $noprivkeysty = 'display:inline-block';
 5542:     if ($numshown) {
 5543:         $noprivkeysty = 'display:none';
 5544:     }
 5545:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5546:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5547:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5548:                $extra.
 5549:                '</td></tr>';
 5550:     $itemcount ++;
 5551:     $$rowtotal += $itemcount;
 5552:     return $output;
 5553: }
 5554: 
 5555: sub print_proctoring {
 5556:     my ($dom,$settings,$rowtotal) = @_;
 5557:     my $itemcount = 1;
 5558:     my (%ordered,%providernames,%current,%currentdef);
 5559:     my $confname = $dom.'-domainconfig';
 5560:     my $switchserver = &check_switchserver($dom,$confname);
 5561:     if (ref($settings) eq 'HASH') {
 5562:         foreach my $item (keys(%{$settings})) {
 5563:             if (ref($settings->{$item}) eq 'HASH') {
 5564:                 my $num = $settings->{$item}{'order'};
 5565:                 $ordered{$num} = $item;
 5566:             }
 5567:         }
 5568:     } else {
 5569:         %ordered = (
 5570:                      1 => 'proctorio',
 5571:                      2 => 'examity',
 5572:                    );
 5573:     }
 5574:     %providernames = &proctoring_providernames();
 5575:     my $maxnum = scalar(keys(%ordered));
 5576:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5577:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5578:     if (ref($requref) eq 'HASH') {
 5579:         %requserfields = %{$requref};
 5580:     }
 5581:     if (ref($opturef) eq 'HASH') {
 5582:         %optuserfields = %{$opturef};
 5583:     }
 5584:     if (ref($defref) eq 'HASH') {
 5585:         %defaults = %{$defref};
 5586:     }
 5587:     if (ref($extref) eq 'HASH') {
 5588:         %extended = %{$extref};
 5589:     }
 5590:     if (ref($crsref) eq 'HASH') {
 5591:         %crsconf = %{$crsref};
 5592:     }
 5593:     if (ref($rolesref) eq 'ARRAY') {
 5594:         @courseroles = @{$rolesref};
 5595:     }
 5596:     if (ref($ltiref) eq 'ARRAY') {
 5597:         @ltiroles = @{$ltiref};
 5598:     }
 5599:     my $datatable;
 5600:     my $css_class;
 5601:     if (keys(%ordered)) {
 5602:         my @items = sort { $a <=> $b } keys(%ordered);
 5603:         for (my $i=0; $i<@items; $i++) {
 5604:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5605:             my $provider = $ordered{$items[$i]};
 5606:             my $optionsty = 'none';
 5607:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5608:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5609:             if (ref($settings) eq 'HASH') {
 5610:                 if (ref($settings->{$provider}) eq 'HASH') {
 5611:                     %current = %{$settings->{$provider}};
 5612:                     if ($current{'available'}) {
 5613:                         $optionsty = 'block';
 5614:                         $available = 1;
 5615:                     }
 5616:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5617:                         $lifetime = $current{'lifetime'};
 5618:                     }
 5619:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5620:                         $version = $current{'version'};
 5621:                     }
 5622:                     if ($current{'image'} ne '') {
 5623:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5624:                     }
 5625:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5626:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5627:                     }
 5628:                     $userincdom = $current{'incdom'};
 5629:                     if (ref($current{'roles'}) eq 'HASH') {
 5630:                         %rolemaps = %{$current{'roles'}};
 5631:                         $checkedfields{'roles'} = 1;
 5632:                     }
 5633:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5634:                         foreach my $val (@{$current{'defaults'}}) {
 5635:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5636:                                 $inuse{$val} = 1;
 5637:                             } else {
 5638:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5639:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5640:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5641:                                             $inuse{$poss} = $val;
 5642:                                             last;
 5643:                                         }
 5644:                                     }
 5645:                                 }
 5646:                             }
 5647:                         }
 5648:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5649:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5650:                             my $currval = $current{'defaults'}{$key}; 
 5651:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5652:                                 $inuse{$key} = 1;
 5653:                             } else {
 5654:                                 my $match;
 5655:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5656:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5657:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5658:                                             $inuse{$poss} = $key;
 5659:                                             last;
 5660:                                         }
 5661:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5662:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5663:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5664:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5665:                                                     $currentdef{$inner} = $currval;
 5666:                                                     $match = 1;
 5667:                                                     last;
 5668:                                                 }
 5669:                                             } elsif ($inner eq $key) {
 5670:                                                 $currentdef{$key} = $currval;
 5671:                                                 $match = 1;
 5672:                                                 last;
 5673:                                             }
 5674:                                         }
 5675:                                     }
 5676:                                     last if ($match);
 5677:                                 }
 5678:                             }
 5679:                         }
 5680:                     }
 5681:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5682:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5683:                     }
 5684:                 }
 5685:             }
 5686:             my %lt = &proctoring_titles($provider);
 5687:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5688:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5689:             my %checkedavailable = (
 5690:                                    yes => '',
 5691:                                    no  => ' checked="checked"',
 5692:                                  );
 5693:             if ($available) {
 5694:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5695:                 $checkedavailable{'no'} = '';
 5696:             }
 5697:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5698:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5699:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5700:             for (my $k=0; $k<$maxnum; $k++) {
 5701:                 my $vpos = $k+1;
 5702:                 my $selstr;
 5703:                 if ($k == $i) {
 5704:                     $selstr = ' selected="selected" ';
 5705:                 }
 5706:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5707:             }
 5708:             if ($version eq '') {
 5709:                 if ($provider eq 'proctorio') {
 5710:                     $version = '1.0';
 5711:                 } elsif ($provider eq 'examity') {
 5712:                     $version = '1.1';
 5713:                 }
 5714:             }
 5715:             if ($lifetime eq '') {
 5716:                 $lifetime = '300';
 5717:             }
 5718:             $datatable .=
 5719:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5720:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5721:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5722:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5723:                 '</td>'.
 5724:                 '<td colspan="2">'.
 5725:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5726:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5727:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5728:                 ('&nbsp;'x2).
 5729:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5730:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5731:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5732:                 ('&nbsp;'x2).
 5733:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5734:                 '<br />'.
 5735:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5736:                 '<br />'.
 5737:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5738:                 ('&nbsp;'x2).
 5739:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5740:                 '<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";
 5741:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5742:             if ($imgsrc) {
 5743:                 $datatable .= $imgsrc.
 5744:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5745:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5746:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5747:             }
 5748:             $datatable .= '&nbsp;';
 5749:             if ($switchserver) {
 5750:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5751:             } else {
 5752:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5753:             }
 5754:             unless ($imgsrc) {
 5755:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5756:             }
 5757:             $datatable .= '</fieldset>'."\n";
 5758:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5759:                 if (@{$requserfields{$provider}} > 0) {
 5760:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5761:                     foreach my $field (@{$requserfields{$provider}}) {
 5762:                         $datatable .= '<span class="LC_nobreak">'.
 5763:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5764:                                       $lt{$field}.'</label>';
 5765:                         if ($field eq 'user') {
 5766:                             my $seluserdom = '';
 5767:                             my $unseluserdom = ' selected="selected"';
 5768:                             if ($userincdom) {
 5769:                                 $seluserdom = $unseluserdom;
 5770:                                 $unseluserdom = '';
 5771:                             }
 5772:                             $datatable .= ':&nbsp;'.
 5773:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5774:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5775:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5776:                                 '</select>&nbsp;';
 5777:                         } else {
 5778:                             $datatable .= '&nbsp;';
 5779:                             if ($field eq 'roles') {
 5780:                                 $showroles = 1; 
 5781:                             } 
 5782:                         }
 5783:                         $datatable .= '</span> ';
 5784:                     }
 5785:                 }
 5786:                 $datatable .= '</fieldset>'."\n";
 5787:             }
 5788:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5789:                 if (@{$optuserfields{$provider}} > 0) {
 5790:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5791:                     foreach my $field (@{$optuserfields{$provider}}) {
 5792:                         my $checked;
 5793:                         if ($checkedfields{$field}) {
 5794:                             $checked = ' checked="checked"';
 5795:                         }
 5796:                         $datatable .= '<span class="LC_nobreak">'.
 5797:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5798:                     }
 5799:                     $datatable .= '</fieldset>'."\n";
 5800:                 }
 5801:             }
 5802:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5803:                 if (@{$defaults{$provider}}) {
 5804:                     my (%options,@selectboxes);
 5805:                     if (ref($extended{$provider}) eq 'HASH') {
 5806:                         %options = %{$extended{$provider}};
 5807:                     }
 5808:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5809:                     my ($rem,$numinrow,$dropdowns);
 5810:                     if ($provider eq 'proctorio') { 
 5811:                         $datatable .= '<table>';
 5812:                         $numinrow = 4;
 5813:                     }
 5814:                     my $i = 0;
 5815:                     foreach my $field (@{$defaults{$provider}}) {
 5816:                         my $checked;
 5817:                         if ($inuse{$field}) {
 5818:                             $checked = ' checked="checked"';
 5819:                         }
 5820:                         if ($provider eq 'examity') {
 5821:                             if ($field eq 'display') {
 5822:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5823:                                 foreach my $option ('iframe','tab','window') {
 5824:                                     my $checkdisp;
 5825:                                     if ($currentdef{'target'} eq $option) {
 5826:                                         $checkdisp = ' checked="checked"';
 5827:                                     }
 5828:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5829:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5830:                                 }
 5831:                                 $datatable .= ('&nbsp;'x4);
 5832:                                 foreach my $dimen ('width','height') {
 5833:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5834:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5835:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5836:                                                   ('&nbsp;'x2);
 5837:                                 }
 5838:                                 $datatable .= '</span><br />'.
 5839:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5840:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5841:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5842:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5843:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5844:                                               $currentdef{'explanation'}.
 5845:                                               '</textarea></div><div style=""></div><br />';
 5846:                             }
 5847:                         } else {
 5848:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5849:                                 my ($output,$selnone);
 5850:                                 unless ($checked) {
 5851:                                     $selnone = ' selected="selected"';
 5852:                                 }
 5853:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5854:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5855:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5856:                                 foreach my $option (@{$options{$field}}) {
 5857:                                     my $sel; 
 5858:                                     if ($inuse{$field} eq $option) {
 5859:                                         $sel = ' selected="selected"';
 5860:                                     }
 5861:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5862:                                 }
 5863:                                 $output .= '</select></span>';
 5864:                                 push(@selectboxes,$output);
 5865:                             } else {
 5866:                                 $rem = $i%($numinrow);
 5867:                                 if ($rem == 0) {
 5868:                                     if ($i > 0) {
 5869:                                         $datatable .= '</tr>';
 5870:                                     }
 5871:                                     $datatable .= '<tr>';
 5872:                                 }
 5873:                                 $datatable .= '<td class="LC_left_item">'.
 5874:                                               '<span class="LC_nobreak">'.
 5875:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5876:                                               $fieldtitles{$field}.'</label></span></td>';
 5877:                                 $i++;
 5878:                             }
 5879:                         }
 5880:                     }
 5881:                     if ($provider eq 'proctorio') {
 5882:                         if ($numinrow) {
 5883:                             $rem = $i%$numinrow;
 5884:                         }
 5885:                         my $colsleft = $numinrow - $rem;
 5886:                         if ($colsleft > 1) {
 5887:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5888:                         } else {
 5889:                             $datatable .= '<td class="LC_left_item">';
 5890:                         }
 5891:                         $datatable .= '&nbsp;'.
 5892:                                       '</td></tr></table>';
 5893:                         if (@selectboxes) {
 5894:                             $datatable .= '<hr /><table>';
 5895:                             $numinrow = 2;
 5896:                             for (my $i=0; $i<@selectboxes; $i++) {
 5897:                                 $rem = $i%($numinrow);
 5898:                                 if ($rem == 0) {
 5899:                                     if ($i > 0) {
 5900:                                         $datatable .= '</tr>';
 5901:                                     }
 5902:                                     $datatable .= '<tr>';
 5903:                                 }
 5904:                                 $datatable .= '<td class="LC_left_item">'.
 5905:                                               $selectboxes[$i].'</td>';
 5906:                             }
 5907:                             if ($numinrow) {
 5908:                                 $rem = $i%$numinrow;
 5909:                             }
 5910:                             $colsleft = $numinrow - $rem;
 5911:                             if ($colsleft > 1) {
 5912:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5913:                             } else {
 5914:                                 $datatable .= '<td class="LC_left_item">';
 5915:                             }
 5916:                             $datatable .= '&nbsp;'.
 5917:                                           '</td></tr></table>';
 5918:                         }
 5919:                     }
 5920:                     $datatable .= '</fieldset>';
 5921:                 }
 5922:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5923:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5924:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5925:                     my ($rem,$numinrow);
 5926:                     if ($provider eq 'proctorio') {
 5927:                         $datatable .= '<table>';
 5928:                         $numinrow = 4;
 5929:                     }
 5930:                     my $i = 0;
 5931:                     foreach my $item (@{$crsconf{$provider}}) {
 5932:                         my $name;
 5933:                         if ($provider eq 'examity') {
 5934:                             $name = $lt{'crs'.$item};
 5935:                         } elsif ($provider eq 'proctorio') {
 5936:                             $name = $fieldtitles{$item};
 5937:                             $rem = $i%($numinrow);
 5938:                             if ($rem == 0) {
 5939:                                 if ($i > 0) {
 5940:                                     $datatable .= '</tr>';
 5941:                                 }
 5942:                                 $datatable .= '<tr>';
 5943:                             }
 5944:                             $datatable .= '<td class="LC_left_item>';
 5945:                         }
 5946:                         my $checked;
 5947:                         if ($crsconfig{$item}) {
 5948:                             $checked = ' checked="checked"';
 5949:                         }
 5950:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5951:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5952:                                       $name.'</label></span>';
 5953:                         if ($provider eq 'examity') {
 5954:                             $datatable .= '&nbsp; ';
 5955:                         }
 5956:                         $datatable .= "\n";
 5957:                         $i++;
 5958:                     }
 5959:                     if ($provider eq 'proctorio') {
 5960:                         if ($numinrow) {
 5961:                             $rem = $i%$numinrow;
 5962:                         }
 5963:                         my $colsleft = $numinrow - $rem;
 5964:                         if ($colsleft > 1) {
 5965:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5966:                         } else {
 5967:                             $datatable .= '<td class="LC_left_item">';
 5968:                         }
 5969:                         $datatable .= '&nbsp;'.
 5970:                                       '</td></tr></table>';
 5971:                     }
 5972:                     $datatable .= '</fieldset>';
 5973:                 }
 5974:                 if ($showroles) {
 5975:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5976:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5977:                     foreach my $role (@courseroles) {
 5978:                         my ($selected,$selectnone);
 5979:                         if (!$rolemaps{$role}) {
 5980:                             $selectnone = ' selected="selected"';
 5981:                         }
 5982:                         $datatable .= '<td style="text-align: center">'.
 5983:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5984:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5985:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5986:                         foreach my $ltirole (@ltiroles) {
 5987:                             unless ($selectnone) {
 5988:                                 if ($rolemaps{$role} eq $ltirole) {
 5989:                                     $selected = ' selected="selected"';
 5990:                                 } else {
 5991:                                     $selected = '';
 5992:                                 }
 5993:                             }
 5994:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5995:                         }
 5996:                         $datatable .= '</select></td>';
 5997:                     }
 5998:                     $datatable .= '</tr></table></fieldset>'.
 5999:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 6000:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 6001:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 6002:                                   '<tr><td></td><td>lms</td>'.
 6003:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 6004:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 6005:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 6006:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 6007:                         my %custom = %{$settings->{$provider}->{'custom'}};
 6008:                         if (keys(%custom) > 0) {
 6009:                             foreach my $key (sort(keys(%custom))) {
 6010:                                 next if ($key eq 'lms');
 6011:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 6012:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 6013:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 6014:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 6015:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 6016:                             }
 6017:                         }
 6018:                     }
 6019:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 6020:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 6021:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 6022:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 6023:                                   '</table></fieldset></td></tr>'."\n";
 6024:                 }
 6025:                 $datatable .= '</td></tr>';
 6026:             }
 6027:             $itemcount ++;
 6028:         }
 6029:     }
 6030:     return $datatable;
 6031: }
 6032: 
 6033: sub proctoring_data {
 6034:     my $requserfields = {
 6035:                       proctorio => ['user'],
 6036:                       examity   => ['roles','user'],
 6037:                      };
 6038:     my $optuserfields = {
 6039:                         proctorio => ['fullname'],
 6040:                         examity   => ['fullname','firstname','lastname','email'],
 6041:                      };
 6042:     my $defaults = {
 6043:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6044:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6045:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6046:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 6047:                                 'reentry','calculator','whiteboard'],
 6048:                   examity   => ['display'],
 6049:                 };
 6050:     my $extended = { 
 6051:                   proctorio => {
 6052:                                  verifyid => ['verifyidauto','verifyidlive'],
 6053:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 6054:                                  tabslinks => ['notabs','linksonly'],
 6055:                                  reentry => ['noreentry','agentreentry'],
 6056:                                  calculator => ['calculatorbasic','calculatorsci'],
 6057:                                },
 6058:                   examity => {
 6059:                                display => {
 6060:                                            target      => ['iframe','tab','window'],
 6061:                                            width       => '',
 6062:                                            height      => '',
 6063:                                            linktext    => '',
 6064:                                            explanation => '',
 6065:                                           },
 6066:                              },
 6067:                 };
 6068:     my $crsconf = {
 6069:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6070:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6071:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6072:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6073:                                'reentry','calculator','whiteboard'],
 6074:                  examity => ['label','title','target','linktext','explanation','append'],
 6075:                };
 6076:     my $courseroles = ['cc','in','ta','ep','st'];
 6077:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6078:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6079: }
 6080: 
 6081: sub proctoring_titles {
 6082:     my ($item) = @_;
 6083:     my (%common_lt,%custom_lt);
 6084:     %common_lt = &Apache::lonlocal::texthash (
 6085:                      'avai'      => 'Available?',
 6086:                      'base'      => 'Basic Settings',
 6087:                      'requ'      => 'User data required to be sent on launch',
 6088:                      'optu'      => 'User data optionally sent on launch',
 6089:                      'udsl'      => 'User data sent on launch',
 6090:                      'defa'      => 'Defaults for items configurable in course',
 6091:                      'sigmethod' => 'Signature Method',
 6092:                      'key'       => 'Key',
 6093:                      'lifetime'  => 'Nonce lifetime (s)',
 6094:                      'secret'    => 'Secret',
 6095:                      'icon'      => 'Icon',
 6096:                      'fullname'  => 'Full Name',
 6097:                      'visible'   => 'Visible input',
 6098:                      'username'  => 'username',
 6099:                      'user'      => 'User',
 6100:                  );
 6101:     if ($item eq 'proctorio') {
 6102:         %custom_lt = &Apache::lonlocal::texthash (
 6103:                          'version'        => 'OAuth version',
 6104:                          'url'            => 'API URL',
 6105:                          'uname:dom'      => 'username-domain',
 6106:         );
 6107:     } elsif ($item eq 'examity') {
 6108:         %custom_lt = &Apache::lonlocal::texthash (
 6109:                          'version'        => 'LTI Version',
 6110:                          'url'            => 'URL',
 6111:                          'uname:dom'      => 'username:domain',
 6112:                          'msgtype'        => 'Message Type',
 6113:                          'firstname'      => 'First Name',
 6114:                          'lastname'       => 'Last Name',
 6115:                          'email'          => 'E-mail',
 6116:                          'roles'          => 'Role',
 6117:                          'crstarget'      => 'Display target',
 6118:                          'crslabel'       => 'Course label',
 6119:                          'crstitle'       => 'Course title', 
 6120:                          'crslinktext'    => 'Link Text',
 6121:                          'crsexplanation' => 'Explanation',
 6122:                          'crsappend'      => 'Provider URL',
 6123:         );
 6124:     }
 6125:     my %lt = (%common_lt,%custom_lt);
 6126:     return %lt;
 6127: }
 6128: 
 6129: sub proctoring_fieldtitles {
 6130:     my ($item) = @_;
 6131:     if ($item eq 'proctorio') {
 6132:         return &Apache::lonlocal::texthash (
 6133:                   'recordvideo' => 'Record video',
 6134:                   'recordaudio' => 'Record audio',
 6135:                   'recordscreen' => 'Record screen',
 6136:                   'recordwebtraffic' => 'Record web traffic',
 6137:                   'recordroomstart' => 'Record room scan',
 6138:                   'verifyvideo' => 'Verify webcam',
 6139:                   'verifyaudio' => 'Verify microphone',
 6140:                   'verifydesktop' => 'Verify desktop recording',
 6141:                   'verifyid' => 'Photo ID verification',
 6142:                   'verifysignature' => 'Require signature',
 6143:                   'fullscreen' => 'Fullscreen',
 6144:                   'clipboard' => 'Disable copy/paste',
 6145:                   'tabslinks' => 'New tabs/windows',
 6146:                   'closetabs' => 'Close other tabs',
 6147:                   'onescreen' => 'Limit to single screen',
 6148:                   'print' => 'Disable Printing',
 6149:                   'downloads' => 'Disable Downloads',
 6150:                   'cache' => 'Empty cache after exam',
 6151:                   'rightclick' => 'Disable right click',
 6152:                   'reentry' => 'Re-entry to exam',
 6153:                   'calculator' => 'Onscreen calculator',
 6154:                   'whiteboard' => 'Onscreen whiteboard',
 6155:                   'verifyidauto' => 'Automated verification',
 6156:                   'verifyidlive' => 'Live agent verification',
 6157:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6158:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6159:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6160:                   'notabs' => 'Disaallowed',
 6161:                   'linksonly' => 'Allowed from links in exam',
 6162:                   'noreentry' => 'Disallowed',
 6163:                   'agentreentry' => 'Agent required for re-entry',
 6164:                   'calculatorbasic' => 'Basic',
 6165:                   'calculatorsci' => 'Scientific',
 6166:         );
 6167:     } elsif ($item eq 'examity') {
 6168:         return &Apache::lonlocal::texthash (
 6169:                 'target'         => 'Display target',   
 6170:                 'window'         => 'Window',
 6171:                 'tab'            => 'Tab',
 6172:                 'iframe'         => 'iFrame',
 6173:                 'height'         => 'Height (pixels)',
 6174:                 'width'          => 'Width (pixels)',
 6175:                 'linktext'       => 'Default Link Text',
 6176:                 'explanation'    => 'Default Explanation',
 6177:                 'append'         => 'Provider URL',
 6178:         );
 6179:     }
 6180: }
 6181: 
 6182: sub proctoring_providernames {
 6183:     return (
 6184:              proctorio => 'Proctorio',
 6185:              examity   => 'Examity',
 6186:            );
 6187: }
 6188: 
 6189: sub print_lti {
 6190:     my ($position,$dom,$settings,$rowtotal) = @_;
 6191:     my $itemcount = 1;
 6192:     my ($datatable,$css_class);
 6193:     my (%rules,%encrypt,%privkeys,%linkprot,%suggestions);
 6194:     if (ref($settings) eq 'HASH') {
 6195:         if ($position eq 'top') {
 6196:             if (exists($settings->{'encrypt'})) {
 6197:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6198:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6199:                         if ($key eq 'consumers') {
 6200:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6201:                         } else {
 6202:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6203:                         }
 6204:                     }
 6205:                 }
 6206:             }
 6207:             if (exists($settings->{'private'})) {
 6208:                 if (ref($settings->{'private'}) eq 'HASH') {
 6209:                     if (ref($settings->{'private'}) eq 'HASH') {
 6210:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6211:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6212:                         }
 6213:                     }
 6214:                 }
 6215:             }
 6216:         } elsif ($position eq 'upper') {
 6217:             if (exists($settings->{'rules'})) {
 6218:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6219:                     %rules = %{$settings->{'rules'}};
 6220:                 }
 6221:             }
 6222:         } elsif ($position eq 'middle') {
 6223:             if (exists($settings->{'suggested'})) {
 6224:                 if (ref($settings->{'suggested'}) eq 'HASH') {
 6225:                     %suggestions = %{$settings->{'suggested'}};
 6226:                 }
 6227:             }
 6228:         } elsif ($position eq 'lower') {
 6229:             if (exists($settings->{'linkprot'})) {
 6230:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6231:                     %linkprot = %{$settings->{'linkprot'}};
 6232:                     if ($linkprot{'lock'}) {
 6233:                         delete($linkprot{'lock'});
 6234:                     }
 6235:                 }
 6236:             }
 6237:         } else {
 6238:             foreach my $key ('encrypt','private','rules','linkprot','suggestions') {
 6239:                 if (exists($settings->{$key})) {
 6240:                     delete($settings->{$key});
 6241:                 }
 6242:             }
 6243:         }
 6244:     }
 6245:     if ($position eq 'top') {
 6246:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6247:     } elsif ($position eq 'upper') {
 6248:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6249:         $$rowtotal += $itemcount;
 6250:     } elsif ($position eq 'middle') {
 6251:         $datatable = &linkprot_suggestions(\%suggestions,\$itemcount);
 6252:         $$rowtotal += $itemcount;
 6253:     } elsif ($position eq 'lower') {
 6254:         $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6255:     } else {
 6256:         my ($switchserver,$switchmessage);
 6257:         $switchserver = &check_switchserver($dom);
 6258:         $switchmessage = &mt("submit from domain's primary library server: [_1].",$switchserver);
 6259:         my $maxnum = 0;
 6260:         my %ordered;
 6261:         if (ref($settings) eq 'HASH') {
 6262:             foreach my $item (keys(%{$settings})) {
 6263:                 if (ref($settings->{$item}) eq 'HASH') {
 6264:                     my $num = $settings->{$item}{'order'};
 6265:                     if ($num eq '') {
 6266:                         $num = scalar(keys(%{$settings}));
 6267:                     }
 6268:                     $ordered{$num} = $item;
 6269:                 }
 6270:             }
 6271:         }
 6272:         $maxnum = scalar(keys(%ordered));
 6273:         my %lt = &lti_names();
 6274:         if (keys(%ordered)) {
 6275:             my @items = sort { $a <=> $b } keys(%ordered);
 6276:             for (my $i=0; $i<@items; $i++) {
 6277:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6278:                 my $item = $ordered{$items[$i]};
 6279:                 my ($key,$secret,$usable,$lifetime,$consumer,$requser,$crsinc,$current);
 6280:                 if (ref($settings->{$item}) eq 'HASH') {
 6281:                     $key = $settings->{$item}->{'key'};
 6282:                     $usable = $settings->{$item}->{'usable'};
 6283:                     $lifetime = $settings->{$item}->{'lifetime'};
 6284:                     $consumer = $settings->{$item}->{'consumer'};
 6285:                     $requser = $settings->{$item}->{'requser'};
 6286:                     $crsinc = $settings->{$item}->{'crsinc'};
 6287:                     $current = $settings->{$item};
 6288:                 }
 6289:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6290:                 my %checkedrequser = (
 6291:                                        yes => ' checked="checked"',
 6292:                                        no  => '',
 6293:                                      );
 6294:                 if (!$requser) {
 6295:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6296:                     $checkedrequser{'yes'} = '';
 6297:                 }
 6298:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6299:                 my %checkedcrsinc = (
 6300:                                       yes => ' checked="checked"',
 6301:                                       no  => '',
 6302:                                     );
 6303:                 if (!$crsinc) {
 6304:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6305:                     $checkedcrsinc{'yes'} = '';
 6306:                 }
 6307:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6308:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6309:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6310:                 for (my $k=0; $k<=$maxnum; $k++) {
 6311:                     my $vpos = $k+1;
 6312:                     my $selstr;
 6313:                     if ($k == $i) {
 6314:                         $selstr = ' selected="selected" ';
 6315:                     }
 6316:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6317:                 }
 6318:                 $datatable .= '</select>'.('&nbsp;'x2).
 6319:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6320:                     &mt('Delete?').'</label></span></td>'.
 6321:                     '<td colspan="2">'.
 6322:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6323:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6324:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6325:                     ('&nbsp;'x2).
 6326:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6327:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6328:                     ('&nbsp;'x2).
 6329:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6330:                     'value="'.$lifetime.'" size="3" /></span><br /><br />';
 6331:                 if ($key ne '') {
 6332:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'};
 6333:                     if ($switchserver) {
 6334:                         $datatable .= ': ['.&mt('[_1] to view/edit',$switchserver).']';
 6335:                     } else {
 6336:                         $datatable .= ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />';
 6337:                     }
 6338:                     $datatable .= '</span> '.('&nbsp;'x2);
 6339:                 } elsif (!$switchserver) {
 6340:                     $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':'.
 6341:                                   '<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" autocomplete="off" />'.
 6342:                                   '</span> '.('&nbsp;'x2);
 6343:                 }
 6344:                 if ($switchserver) {
 6345:                     if ($usable ne '') {
 6346:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6347:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6348:                                       '<span class="LC_nobreak">'.&mt('Change secret?').
 6349:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6350:                                       ('&nbsp;'x2).
 6351:                                      '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').'</label>'.('&nbsp;'x2).
 6352:                                       '</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6353:                                       '<span class="LC_nobreak"> - '.$switchmessage.'</span>'.
 6354:                                       '</div>';
 6355:                     } elsif ($key eq '') {
 6356:                         $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6357:                     } else {
 6358:                         $datatable .= '<span class="LC_nobreak">'.&mt('Secret required').' - '.$switchmessage.'</span>'."\n";
 6359:                     }
 6360:                 } else {
 6361:                     if ($usable ne '') {
 6362:                         $datatable .= '<div id="lti_divcurrsecret_'.$i.'" style="display:inline-block" /><span class="LC_nobreak">'.
 6363:                                       $lt{'secret'}.': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 6364:                                       '<span class="LC_nobreak">'.&mt('Change?').
 6365:                                       '<label><input type="radio" value="0" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" checked="checked" />'.&mt('No').'</label>'.
 6366:                                       ('&nbsp;'x2).
 6367:                                       '<label><input type="radio" value="1" name="lti_changesecret_'.$i.'" onclick="javascript:toggleChgSecret(this.form,'."'$i','secret','lti'".');" />'.&mt('Yes').
 6368:                                       '</label>&nbsp;&nbsp;</span><div id="lti_divchgsecret_'.$i.'" style="display:none" />'.
 6369:                                       '<span class="LC_nobreak">'.&mt('New Secret').':'.
 6370:                                       '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6371:                                       '<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>';
 6372:                     } else {
 6373:                         $datatable .=
 6374:                             '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6375:                             '<input type="password" size="20" name="lti_secret_'.$i.'" value="" autocomplete="new-password" />'.
 6376:                             '<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>';
 6377:                     }
 6378:                 }
 6379:                 $datatable .= '<br /><br />'.
 6380:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6381:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6382:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6383:                     '<br /><br />'.
 6384:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6385:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6386:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6387:                     ('&nbsp;'x4).
 6388:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6389:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6390:                 $itemcount ++;
 6391:             }
 6392:         }
 6393:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6394:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6395:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6396:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6397:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6398:         for (my $k=0; $k<$maxnum+1; $k++) {
 6399:             my $vpos = $k+1;
 6400:             my $selstr;
 6401:             if ($k == $maxnum) {
 6402:                 $selstr = ' selected="selected" ';
 6403:             }
 6404:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6405:         }
 6406:         $datatable .= '</select>&nbsp;'."\n".
 6407:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6408:                       '<td colspan="2">'.
 6409:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6410:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6411:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6412:                       ('&nbsp;'x2).
 6413:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6414:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6415:                       ('&nbsp;'x2).
 6416:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span><br /><br />'."\n";
 6417:         if ($switchserver) {
 6418:             $datatable .= '<span class="LC_nobreak">'.&mt('Key and Secret are required').' - '.$switchmessage.'</span>'."\n";
 6419:         } else {
 6420:             $datatable .= '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" autocomplete="off" /></span> '."\n".
 6421:                           ('&nbsp;'x2).
 6422:                           '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" autocomplete="new-password" />'.
 6423:                           '<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";
 6424:         }
 6425:         $datatable .= '<br /><br />'.
 6426:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6427:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6428:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6429:                       '<br /><br />'.
 6430:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6431:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6432:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6433:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6434:                       '</td>'."\n".
 6435:                       '</tr>'."\n";
 6436:         $itemcount ++;
 6437:     }
 6438:     $$rowtotal += $itemcount;
 6439:     return $datatable;
 6440: }
 6441: 
 6442: sub lti_names {
 6443:     my %lt = &Apache::lonlocal::texthash(
 6444:                                           'version'   => 'LTI Version',
 6445:                                           'url'       => 'URL',
 6446:                                           'key'       => 'Key',
 6447:                                           'lifetime'  => 'Nonce lifetime (s)',
 6448:                                           'consumer'  => 'Consumer',
 6449:                                           'secret'    => 'Secret',
 6450:                                           'requser'   => "User's identity sent",
 6451:                                           'crsinc'    => "Course's identity sent",
 6452:                                           'email'     => 'Email address',
 6453:                                           'sourcedid' => 'User ID',
 6454:                                           'other'     => 'Other',
 6455:                                           'passback'  => 'Can return grades to Consumer:',
 6456:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6457:                                           'topmenu'   => 'Display LON-CAPA page header',
 6458:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6459:                                         );
 6460:     return %lt;
 6461: }
 6462: 
 6463: sub lti_options {
 6464:     my ($num,$current,$itemcount,%lt) = @_;
 6465:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6466:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6467:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6468:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6469:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6470:     $checked{'mapcrstype'} = {};
 6471:     $checked{'makeuser'} = {};
 6472:     $checked{'selfenroll'} = {};
 6473:     $checked{'crssec'} = {};
 6474:     $checked{'crssecsrc'} = {};
 6475:     $checked{'lcauth'} = {};
 6476:     $checked{'menuitem'} = {};
 6477:     if ($num eq 'add') {
 6478:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6479:     }
 6480:     my $userfieldsty = 'none';
 6481:     my $crsfieldsty = 'none';
 6482:     my $crssecfieldsty = 'none';
 6483:     my $secsrcfieldsty = 'none';
 6484:     my $callbacksty = 'none';
 6485:     my $passbacksty = 'none';
 6486:     my $optionsty = 'block';
 6487:     my $crssty = 'block';
 6488:     my $lcauthparm;
 6489:     my $lcauthparmstyle = 'display:none';
 6490:     my $lcauthparmtext;
 6491:     my $menusty;
 6492:     my $numinrow = 4;
 6493:     my %menutitles = &ltimenu_titles();
 6494: 
 6495:     if (ref($current) eq 'HASH') {
 6496:         if (!$current->{'requser'}) {
 6497:             $optionsty = 'none';
 6498:             $crssty = 'none';
 6499:         } elsif (!$current->{'crsinc'}) {
 6500:             $crssty = 'none';
 6501:         }
 6502:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6503:             $checked{'mapuser'}{'sourcedid'} = '';
 6504:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6505:                 $checked{'mapuser'}{'email'} = ' checked="checked"';
 6506:             } else {
 6507:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6508:                 $userfield = $current->{'mapuser'};
 6509:                 $userfieldsty = 'inline-block';
 6510:             }
 6511:         }
 6512:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6513:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6514:             if ($current->{'mapcrs'} eq 'context_id') {
 6515:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"';
 6516:             } else {
 6517:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6518:                 $cidfield = $current->{'mapcrs'};
 6519:                 $crsfieldsty = 'inline-block';
 6520:             }
 6521:         }
 6522:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6523:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6524:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6525:             }
 6526:         }
 6527:         if (!$current->{'storecrs'}) {
 6528:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6529:             $checked{'storecrs'}{'Y'} = '';
 6530:         }
 6531:         if ($current->{'makecrs'}) {
 6532:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6533:         }
 6534:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6535:             foreach my $role (@{$current->{'makeuser'}}) {
 6536:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6537:             }
 6538:         }
 6539:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6540:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6541:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6542:                 $lcauthparm = $current->{'lcauthparm'};
 6543:                 $lcauthparmstyle = 'display:table-row';
 6544:                 if ($current->{'lcauth'} eq 'localauth') {
 6545:                     $lcauthparmtext = &mt('Local auth argument');
 6546:                 } else {
 6547:                     $lcauthparmtext = &mt('Kerberos domain');
 6548:                 }
 6549:             }
 6550:         }
 6551:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6552:             foreach my $role (@{$current->{'selfenroll'}}) {
 6553:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6554:             }
 6555:         }
 6556:         if (ref($current->{'maproles'}) eq 'HASH') {
 6557:             %rolemaps = %{$current->{'maproles'}};
 6558:         }
 6559:         if ($current->{'section'} ne '') {
 6560:             $checked{'crssec'}{'Y'} = '  checked="checked"';
 6561:             $crssecfieldsty = 'inline-block';
 6562:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6563:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6564:             } else {
 6565:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6566:                 $crssecsrc = $current->{'section'};
 6567:                 $secsrcfieldsty = 'inline-block';
 6568:             }
 6569:         } else {
 6570:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6571:         }
 6572:         if ($current->{'callback'} ne '') {
 6573:             $callback = $current->{'callback'};
 6574:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6575:             $callbacksty = 'inline-block';
 6576:         } else {
 6577:             $checked{'callback'}{'N'} = ' checked="checked"';
 6578:         }
 6579:         if ($current->{'topmenu'}) {
 6580:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6581:         } else {
 6582:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6583:         }
 6584:         if ($current->{'inlinemenu'}) {
 6585:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6586:         } else {
 6587:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6588:         }
 6589:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6590:             $menusty = 'inline-block';
 6591:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6592:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6593:                     if (exists($menutitles{$item})) {
 6594:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6595:                     }
 6596:                 }
 6597:             }
 6598:         } else {
 6599:             $menusty = 'none';
 6600:         }
 6601:     } else {
 6602:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6603:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6604:         $checked{'callback'}{'N'} = ' checked="checked"';
 6605:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6606:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6607:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6608:         $menusty = 'inline-block';
 6609:     }
 6610:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6611:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6612:                                official   => 'Official',
 6613:                                unofficial => 'Unofficial',
 6614:                                community  => 'Community',
 6615:                                textbook   => 'Textbook',
 6616:                                placement  => 'Placement Test',
 6617:                                lti        => 'LTI Provider',
 6618:     );
 6619:     my @authtypes = ('internal','krb4','krb5','localauth');
 6620:     my %shortauth = (
 6621:                      internal => 'int',
 6622:                      krb4 => 'krb4',
 6623:                      krb5 => 'krb5',
 6624:                      localauth  => 'loc'
 6625:                     );
 6626:     my %authnames = &authtype_names();
 6627:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6628:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6629:     my @courseroles = ('cc','in','ta','ep','st');
 6630:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6631:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6632:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6633:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6634:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6635:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6636:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6637:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6638:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6639:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6640:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6641:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6642:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6643:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6644:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6645:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6646:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6647:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6648:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6649:     foreach my $option ('sourcedid','email','other') {
 6650:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6651:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6652:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6653:     }
 6654:     $output .= '</span></div>'.
 6655:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6656:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6657:                'value="'.$userfield.'" /></div></fieldset>'.
 6658:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6659:     foreach my $ltirole (@ltiroles) {
 6660:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6661:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';
 6662:     }
 6663:     $output .= '</fieldset>'.
 6664:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6665:                '<table>'.
 6666:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6667:                '</table>'.
 6668:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6669:                '<td class="LC_left_item">';
 6670:     foreach my $auth ('lti',@authtypes) {
 6671:         my $authtext;
 6672:         if ($auth eq 'lti') {
 6673:             $authtext = &mt('None');
 6674:         } else {
 6675:             $authtext = $authnames{$shortauth{$auth}};
 6676:         }
 6677:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6678:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6679:                    $authtext.'</label></span> &nbsp;';
 6680:     }
 6681:     $output .= '</td></tr>'.
 6682:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6683:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6684:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6685:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6686:                '</table></fieldset>'.
 6687:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6688:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6689:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6690:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6691:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6692:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6693:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6694:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6695:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6696:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6697:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6698:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6699:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6700:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6701:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6702:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6703:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6704:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6705:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6706:                    ('&nbsp;'x2);
 6707:     }
 6708:     $output .= '</span></div></fieldset>'.
 6709:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6710:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6711:                &mt('Unique course identifier').':&nbsp;';
 6712:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6713:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6714:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6715:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6716:     }
 6717:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6718:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6719:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6720:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6721:     foreach my $type (@coursetypes) {
 6722:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6723:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6724:                    ('&nbsp;'x2);
 6725:     }
 6726:     $output .= '</span><br /><br />'.
 6727:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6728:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6729:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6730:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6731:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6732:                '</fieldset>'.
 6733:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6734:     foreach my $ltirole (@lticourseroles) {
 6735:         my ($selected,$selectnone);
 6736:         if ($rolemaps{$ltirole} eq '') {
 6737:             $selectnone = ' selected="selected"';
 6738:         }
 6739:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6740:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6741:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6742:         foreach my $role (@courseroles) {
 6743:             unless ($selectnone) {
 6744:                 if ($rolemaps{$ltirole} eq $role) {
 6745:                     $selected = ' selected="selected"';
 6746:                 } else {
 6747:                     $selected = '';
 6748:                 }
 6749:             }
 6750:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6751:                        &Apache::lonnet::plaintext($role,'Course').
 6752:                        '</option>';
 6753:         }
 6754:         $output .= '</select></td>';
 6755:     }
 6756:     $output .= '</tr></table></fieldset>'.
 6757:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6758:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6759:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6760:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6761:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6762:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6763:                '</fieldset>'.
 6764:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6765:     foreach my $lticrsrole (@lticourseroles) {
 6766:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6767:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6768:     }
 6769:     $output .= '</fieldset>'.
 6770:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6771:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6772:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6773:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6774:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6775:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6776:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6777:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6778:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6779:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6780:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6781:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6782:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6783:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6784:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6785:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6786:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6787:     foreach my $extra ('roster','passback') {
 6788:         my $checkedon = '';
 6789:         my $checkedoff = ' checked="checked"';
 6790:         if ($extra eq 'passback') {
 6791:             $pb1p1chk = ' checked="checked"';
 6792:             $pb1p0chk = '';
 6793:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"';
 6794:         } else {
 6795:             $onclickpb = '';
 6796:         }
 6797:         if (ref($current) eq 'HASH') {
 6798:             if (($current->{$extra})) {
 6799:                 $checkedon = $checkedoff;
 6800:                 $checkedoff = '';
 6801:                 if ($extra eq 'passback') {
 6802:                     $passbacksty = 'inline-block';
 6803:                 }
 6804:                 if ($current->{'passbackformat'} eq '1.0') {
 6805:                     $pb1p0chk =  ' checked="checked"';
 6806:                     $pb1p1chk = '';
 6807:                 }
 6808:             }
 6809:         }
 6810:         $output .= $lt{$extra}.'&nbsp;'.
 6811:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6812:                    &mt('No').'</label>'.('&nbsp;'x2).
 6813:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6814:                    &mt('Yes').'</label><br />';
 6815:     }
 6816:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6817:                '<span class="LC_nobreak">'.&mt('Grade format').
 6818:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6819:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6820:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6821:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6822:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6823:     $output .= '</span></div></fieldset>';
 6824: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6825: #
 6826: #    $output .= '</fieldset>'.
 6827: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6828:     return $output;
 6829: }
 6830: 
 6831: sub ltimenu_titles {
 6832:     return &Apache::lonlocal::texthash(
 6833:                                         fullname    => 'Full name',
 6834:                                         coursetitle => 'Course title',
 6835:                                         role        => 'Role',
 6836:                                         logout      => 'Logout',
 6837:                                         grades      => 'Grades',
 6838:     );
 6839: }
 6840: 
 6841: sub linkprot_suggestions {
 6842:     my ($suggested,$itemcount) = @_;
 6843:     my $count = 0;
 6844:     my $next = 1;
 6845:     my %lt = &Apache::lonlocal::texthash(
 6846:                                           'name' => 'Suggested Launcher',
 6847:                                           'info' => 'Recommendations',
 6848:                                         );
 6849:     my ($datatable,$css_class,$dest);
 6850:     if (ref($suggested) eq 'HASH') {
 6851:         my @current = sort { $a <=> $b } keys(%{$suggested});
 6852:         $next += $current[-1];
 6853:         for (my $i=0; $i<@current; $i++) {
 6854:             my $num = $current[$i];
 6855:             my %values;
 6856:             if (ref($suggested->{$num}) eq 'HASH') {
 6857:                 %values = %{$suggested->{$num}};
 6858:             } else {
 6859:                 next;
 6860:             }
 6861:             $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6862:             $datatable .=
 6863:                 '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6864:                 '<label><input type="checkbox" name="linkprot_suggested_del" value="'.$i.'" />'."\n".
 6865:                 &mt('Delete?').'</label></span></td><td>'."\n".
 6866:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6867:                 '<input type="text" size="15" name="linkprot_suggested_name_'.$i.'" value="'.$values{'name'}.'" autocomplete="off" />'."\n".
 6868:                 '</fieldset></div>'.
 6869:                 '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6870:                 '<textarea cols="55" rows="5" name="linkprot_suggested_info_'.$i.'">'.$values{'info'}.'</textarea>'.
 6871:                 '</fieldset></div>'.
 6872:                 '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6873:                 '<input type="hidden" name="linkprot_suggested_id_'.$i.'" value="'.$num.'" /></td></tr>'."\n";
 6874:             $$itemcount ++;
 6875:         }
 6876:     }
 6877:     $css_class = $$itemcount%2?' class="LC_odd_row"':'';
 6878:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6879:                   '<input type="hidden" name="linkprot_suggested_maxnum" value="'.$next.'" />'."\n".
 6880:                   '<input type="checkbox" name="linkprot_suggested_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6881:                   '<td>'."\n".
 6882:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'name'}.'</legend>'."\n".
 6883:                   '<input type="text" size="15" name="linkprot_suggested_name_add" value="" autocomplete="off" />'."\n".
 6884:                   '</fieldset></div>'.
 6885:                   '<div class="LC_floatleft"><fieldset><legend>'.$lt{'info'}.'</legend>'."\n".
 6886:                   '<textarea cols="55" rows="5" name="linkprot_suggested_info_add"></textarea>'.
 6887:                   '</fieldset></div>'.
 6888:                   '<div style="padding:0;clear:both;margin:0;border:0"></div>'."\n".
 6889:                   '</td></tr>'."\n";
 6890:     return $datatable;
 6891: }
 6892: 
 6893: sub print_coursedefaults {
 6894:     my ($position,$dom,$settings,$rowtotal) = @_;
 6895:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6896:     my $itemcount = 1;
 6897:     my %choices =  &Apache::lonlocal::texthash (
 6898:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6899:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6900:         coursequota          => 'Default cumulative quota for all group portfolio spaces in course (MB)',
 6901:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6902:         coursecredits        => 'Credits can be specified for courses',
 6903:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6904:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6905:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6906:         texengine            => 'Default method to display mathematics',
 6907:         postsubmit           => 'Disable submit button/keypress following student submission',
 6908:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6909:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6910:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6911:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6912:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6913:         crsauthor            => 'Standard LON-CAPA problems can be created within a course/community (by type)',
 6914:         crseditors           => 'Available editors for web pages and/or problems created in a course/community',
 6915:     );
 6916:     my %staticdefaults = (
 6917:                            anonsurvey_threshold => 10,
 6918:                            uploadquota          => 500,
 6919:                            coursequota          => 20,
 6920:                            postsubmit           => 60,
 6921:                            mysqltables          => 172800,
 6922:                            domexttool           => 1,
 6923:                            exttool              => 0,
 6924:                            crsauthor            => 1,
 6925:                            crseditors           => ['edit','xml'],  
 6926:                          );
 6927:     if ($position eq 'top') {
 6928:         %defaultchecked = (
 6929:                             'canuse_pdfforms' => 'off',
 6930:                             'uselcmath'       => 'on',
 6931:                             'usejsme'         => 'on',
 6932:                             'inline_chem'     => 'on',
 6933:                             'canclone'        => 'none',
 6934:                           );
 6935:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6936:         my $deftex = $Apache::lonnet::deftex;
 6937:         if (ref($settings) eq 'HASH') {
 6938:             if ($settings->{'texengine'}) {
 6939:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6940:                     $deftex = $settings->{'texengine'};
 6941:                 }
 6942:             }
 6943:         }
 6944:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6945:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6946:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6947:                        '</span></td><td class="LC_right_item">'.
 6948:                        '<select name="texengine">'."\n";
 6949:         my %texoptions = (
 6950:                             MathJax  => 'MathJax',
 6951:                             mimetex  => &mt('Convert to Images'),
 6952:                             tth      => &mt('TeX to HTML'),
 6953:                          );
 6954:         foreach my $renderer ('MathJax','mimetex','tth') {
 6955:             my $selected = '';
 6956:             if ($renderer eq $deftex) {
 6957:                 $selected = ' selected="selected"';
 6958:             }
 6959:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6960:         }
 6961:         $mathdisp .= '</select></td></tr>'."\n";
 6962:         $itemcount ++;
 6963:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6964:                                                      \%choices,$itemcount);
 6965:         $datatable = $mathdisp.$datatable;
 6966:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6967:         $datatable .=
 6968:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6969:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6970:             '</span></td><td class="LC_left_item">';
 6971:         my $currcanclone = 'none';
 6972:         my $onclick;
 6973:         my @cloneoptions = ('none','domain');
 6974:         my %clonetitles = &Apache::lonlocal::texthash (
 6975:                              none     => 'No additional course requesters',
 6976:                              domain   => "Any course requester in course's domain",
 6977:                              instcode => 'Course requests for official courses ...',
 6978:                           );
 6979:         my (%codedefaults,@code_order,@posscodes);
 6980:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6981:                                                     \@code_order) eq 'ok') {
 6982:             if (@code_order > 0) {
 6983:                 push(@cloneoptions,'instcode');
 6984:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6985:             }
 6986:         }
 6987:         if (ref($settings) eq 'HASH') {
 6988:             if ($settings->{'canclone'}) {
 6989:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6990:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6991:                         if (@code_order > 0) {
 6992:                             $currcanclone = 'instcode';
 6993:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6994:                         }
 6995:                     }
 6996:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6997:                     $currcanclone = $settings->{'canclone'};
 6998:                 }
 6999:             }
 7000:         }
 7001:         foreach my $option (@cloneoptions) {
 7002:             my ($checked,$additional);
 7003:             if ($currcanclone eq $option) {
 7004:                 $checked = ' checked="checked"';
 7005:             }
 7006:             if ($option eq 'instcode') {
 7007:                 if (@code_order) {
 7008:                     my $show = 'none';
 7009:                     if ($checked) {
 7010:                         $show = 'block';
 7011:                     }
 7012:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 7013:                                   &mt('Institutional codes for new and cloned course have identical:').
 7014:                                   '<br />';
 7015:                     foreach my $item (@code_order) {
 7016:                         my $codechk;
 7017:                         if ($checked) {
 7018:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 7019:                                 $codechk = ' checked="checked"';
 7020:                             }
 7021:                         }
 7022:                         $additional .= '<label>'.
 7023:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 7024:                                        $item.'</label>';
 7025:                     }
 7026:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 7027:                 }
 7028:             }
 7029:             $datatable .=
 7030:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 7031:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 7032:                 '</label>&nbsp;'.$additional.'</span><br />';
 7033:         }
 7034:         $datatable .= '</td>'.
 7035:                       '</tr>';
 7036:         $itemcount ++;
 7037:     } else {
 7038:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7039:         my ($currdefresponder,%defcredits,%curruploadquota,%currcoursequota,
 7040:             %deftimeout,%currmysql);
 7041:         my $currusecredits = 0;
 7042:         my $postsubmitclient = 1;
 7043:         my $ltiauth = 0;
 7044:         my %domexttool;
 7045:         my %exttool;
 7046:         my %crsauthor;
 7047:         my %crseditors;
 7048:         my @types = ('official','unofficial','community','textbook','placement');
 7049:         if (ref($settings) eq 'HASH') {
 7050:             if ($settings->{'ltiauth'}) {
 7051:                 $ltiauth = 1;
 7052:             }
 7053:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 7054:                 foreach my $type (@types) {
 7055:                     if ($settings->{'domexttool'}->{$type}) {
 7056:                         $domexttool{$type} = ' checked="checked"';
 7057:                     }
 7058:                 }
 7059:             } else {
 7060:                 foreach my $type (@types) {
 7061:                     if ($staticdefaults{'domexttool'}) {
 7062:                         $domexttool{$type} = ' checked="checked"';
 7063:                     }
 7064:                 }
 7065:             }
 7066:             if (ref($settings->{'exttool'}) eq 'HASH') {
 7067:                 foreach my $type (@types) {
 7068:                     if ($settings->{'exttool'}->{$type}) {
 7069:                         $exttool{$type} = ' checked="checked"';
 7070:                     }
 7071:                 }
 7072:             }
 7073:             if (ref($settings->{'crsauthor'}) eq 'HASH') {
 7074:                 foreach my $type (@types) {
 7075:                     if ($settings->{'crsauthor'}->{$type}) {
 7076:                         $crsauthor{$type} = ' checked="checked"';
 7077:                     }
 7078:                 }
 7079:             } else {
 7080:                 foreach my $type (@types) {
 7081:                     if ($staticdefaults{'crsauthor'}) {
 7082:                         $crsauthor{$type} = ' checked="checked"';
 7083:                     }
 7084:                 }
 7085:             }
 7086:             if (ref($settings->{'crseditors'}) eq 'ARRAY') {
 7087:                 foreach my $editor (@{$settings->{'crseditors'}}) {
 7088:                     $crseditors{$editor} = ' checked="checked"';
 7089:                 }
 7090:             } else {
 7091:                 foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7092:                     $crseditors{$editor} = ' checked="checked"';
 7093:                 }
 7094:             }
 7095:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 7096:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 7097:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 7098:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 7099:                 }
 7100:             }
 7101:             if (ref($settings->{'coursequota'}) eq 'HASH') {
 7102:                 foreach my $type (keys(%{$settings->{'coursequota'}})) {
 7103:                     $currcoursequota{$type} = $settings->{'coursequota'}{$type};
 7104:                 }
 7105:             }
 7106:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 7107:                 foreach my $type (@types) {
 7108:                     next if ($type eq 'community');
 7109:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 7110:                     if ($defcredits{$type} ne '') {
 7111:                         $currusecredits = 1;
 7112:                     }
 7113:                 }
 7114:             }
 7115:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 7116:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 7117:                     $postsubmitclient = 0;
 7118:                     foreach my $type (@types) {
 7119:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7120:                     }
 7121:                 } else {
 7122:                     foreach my $type (@types) {
 7123:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 7124:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 7125:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 7126:                             } else {
 7127:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7128:                             }
 7129:                         } else {
 7130:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7131:                         }
 7132:                     }
 7133:                 }
 7134:             } else {
 7135:                 foreach my $type (@types) {
 7136:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7137:                 }
 7138:             }
 7139:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 7140:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 7141:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 7142:                 }
 7143:             } else {
 7144:                 foreach my $type (@types) {
 7145:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 7146:                 }
 7147:             }
 7148:         } else {
 7149:             foreach my $type (@types) {
 7150:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 7151:                 if ($staticdefaults{'domexttool'}) {
 7152:                     $domexttool{$type} = ' checked="checked"';
 7153:                 }
 7154:                 if ($staticdefaults{'crsauthor'}) {
 7155:                     $crsauthor{$type} = ' checked="checked"';
 7156:                 }
 7157:             }
 7158:             foreach my $editor (@{$staticdefaults{'crseditors'}}) {
 7159:                 $crseditors{$editor} = ' checked="checked"';
 7160:             }
 7161:         }
 7162:         if (!$currdefresponder) {
 7163:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 7164:         } elsif ($currdefresponder < 1) {
 7165:             $currdefresponder = 1;
 7166:         }
 7167:         foreach my $type (@types) {
 7168:             if ($curruploadquota{$type} eq '') {
 7169:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 7170:             }
 7171:             if ($currcoursequota{$type} eq '') {
 7172:                 $currcoursequota{$type} = $staticdefaults{'coursequota'};
 7173:             }
 7174:         }
 7175:         $datatable .=
 7176:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7177:                 $choices{'anonsurvey_threshold'}.
 7178:                 '</span></td>'.
 7179:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 7180:                 '<input type="text" name="anonsurvey_threshold"'.
 7181:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 7182:                 '</td></tr>'."\n";
 7183:         $itemcount ++;
 7184:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7185:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7186:                       $choices{'uploadquota'}.
 7187:                       '</span></td>'.
 7188:                       '<td style="text-align: right" class="LC_right_item">'.
 7189:                       '<table><tr>';
 7190:         foreach my $type (@types) {
 7191:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7192:                            '<input type="text" name="uploadquota_'.$type.'"'.
 7193:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 7194:         }
 7195:         $datatable .= '</tr></table></td></tr>'."\n";
 7196:         $itemcount ++;
 7197:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7198:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7199:                       $choices{'coursequota'}.
 7200:                       '</span></td>'.
 7201:                       '<td style="text-align: right" class="LC_right_item">'.
 7202:                       '<table><tr>';
 7203:         foreach my $type (@types) {
 7204:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7205:                            '<input type="text" name="coursequota_'.$type.'"'.
 7206:                            ' value="'.$currcoursequota{$type}.'" size="5" /></td>';
 7207:         }
 7208:         $datatable .= '</tr></table></td></tr>'."\n";
 7209:         $itemcount ++;
 7210:         my $onclick = "toggleDisplay(this.form,'credits');";
 7211:         my $display = 'none';
 7212:         if ($currusecredits) {
 7213:             $display = 'block';
 7214:         }
 7215:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 7216:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 7217:         foreach my $type (@types) {
 7218:             next if ($type eq 'community');
 7219:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7220:                            '<input type="text" name="'.$type.'_credits"'.
 7221:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7222:         }
 7223:         $additional .= '</tr></table></div>'."\n";
 7224:         %defaultchecked = ('coursecredits' => 'off');
 7225:         @toggles = ('coursecredits');
 7226:         my $current = {
 7227:                         'coursecredits' => $currusecredits,
 7228:                       };
 7229:         (my $table,$itemcount) =
 7230:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7231:                                \%choices,$itemcount,$onclick,$additional,'left');
 7232:         $datatable .= $table;
 7233:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7234:         my $display = 'none';
 7235:         if ($postsubmitclient) {
 7236:             $display = 'block';
 7237:         }
 7238:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7239:                       &mt('Number of seconds submit is disabled').'<br />'.
 7240:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7241:                       '<table><tr>';
 7242:         foreach my $type (@types) {
 7243:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7244:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7245:                            $deftimeout{$type}.'" size="5" /></td>';
 7246:         }
 7247:         $additional .= '</tr></table></div>'."\n";
 7248:         %defaultchecked = ('postsubmit' => 'on');
 7249:         @toggles = ('postsubmit');
 7250:         $current = {
 7251:                        'postsubmit' => $postsubmitclient,
 7252:                    };
 7253:         ($table,$itemcount) =
 7254:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7255:                                \%choices,$itemcount,$onclick,$additional,'left');
 7256:         $datatable .= $table;
 7257:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7258:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7259:                       $choices{'mysqltables'}.
 7260:                       '</span></td>'.
 7261:                       '<td style="text-align: right" class="LC_right_item">'.
 7262:                       '<table><tr>';
 7263:         foreach my $type (@types) {
 7264:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7265:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7266:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7267:         }
 7268:         $datatable .= '</tr></table></td></tr>'."\n";
 7269:         $itemcount ++;
 7270:         %defaultchecked = ('ltiauth' => 'off');
 7271:         @toggles = ('ltiauth');
 7272:         $current = {
 7273:                        'ltiauth' => $ltiauth,
 7274:                    };
 7275:         ($table,$itemcount) =
 7276:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7277:                                \%choices,$itemcount,undef,undef,'left');
 7278:         $datatable .= $table;
 7279:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7280:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7281:                       $choices{'domexttool'}.
 7282:                       '</span></td>'.
 7283:                       '<td style="text-align: right" class="LC_right_item">'.
 7284:                       '<table><tr>';
 7285:         foreach my $type (@types) {
 7286:             $datatable .= '<td style="text-align: left">'.
 7287:                           '<span class="LC_nobreak">'.
 7288:                           '<label><input type="checkbox" name="domexttool"'.
 7289:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7290:                           &mt($type).'</label></span></td>'."\n";
 7291:         }
 7292:         $datatable .= '</tr></table></td></tr>'."\n";
 7293:         $itemcount ++;
 7294:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7295:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7296:                       $choices{'exttool'}.
 7297:                       '</span></td>'.
 7298:                       '<td style="text-align: right" class="LC_right_item">'.
 7299:                       '<table><tr>';
 7300:         foreach my $type (@types) {
 7301:             $datatable .= '<td style="text-align: left">'.
 7302:                           '<span class="LC_nobreak">'.
 7303:                           '<label><input type="checkbox" name="exttool"'.
 7304:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7305:                           &mt($type).'</label></span></td>'."\n";
 7306:         }
 7307:         $datatable .= '</tr></table></td></tr>'."\n";
 7308:         $itemcount ++;
 7309:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7310:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7311:                       $choices{'crsauthor'}.
 7312:                       '</span></td>'.
 7313:                       '<td style="text-align: right" class="LC_right_item">'.
 7314:                       '<table><tr>';
 7315:         foreach my $type (@types) {
 7316:             $datatable .= '<td style="text-align: left">'.
 7317:                           '<span class="LC_nobreak">'.
 7318:                           '<label><input type="checkbox" name="crsauthor"'.
 7319:                           ' value="'.$type.'"'.$crsauthor{$type}.' />'.
 7320:                           &mt($type).'</label></span></td>'."\n";
 7321:         }
 7322:         $datatable .= '</tr></table></td></tr>'."\n";
 7323:         $itemcount ++;
 7324:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7325:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7326:                       $choices{'crseditors'}.
 7327:                       '</span></td>'.
 7328:                       '<td style="text-align: right" class="LC_right_item">'.
 7329:                       '<table><tr>';
 7330:         my @editors = ('edit','xml','daxe');
 7331:         my %editornames = &crseditor_titles();
 7332:         foreach my $editor (@editors) {
 7333:             $datatable .= '<td style="text-align: left">'.
 7334:                           '<span class="LC_nobreak">'.
 7335:                           '<label><input type="checkbox" name="crseditors"'.
 7336:                           ' value="'.$editor.'"'.$crseditors{$editor}.' />'.
 7337:                           $editornames{$editor}.'</label></span></td>'."\n";
 7338:         }
 7339:         $datatable .= '</tr></table></td></tr>'."\n";
 7340:     }
 7341:     $$rowtotal += $itemcount;
 7342:     return $datatable;
 7343: }
 7344: 
 7345: sub crseditor_titles {
 7346:     return &Apache::lonlocal::texthash(
 7347:                edit  => 'Standard editor (Edit)',
 7348:                xml   => 'Text editor (EditXML)',
 7349:                daxe  => 'Daxe editor (Daxe)',
 7350:            );
 7351: }
 7352: 
 7353: sub print_authordefaults {
 7354:     my ($position,$dom,$settings,$rowtotal) = @_;
 7355:     my ($css_class,$datatable,%checkedon,%checkedoff);
 7356:     my $itemcount = 1;
 7357:     my %titles = &authordefaults_titles();
 7358:     if ($position eq 'top') {
 7359:         my %defaultchecked = (
 7360:                             'nocodemirror' => 'off',
 7361:                             'daxecollapse' => 'off',
 7362:                             'domcoordacc'  => 'on',
 7363:                           );
 7364:         my @toggles = ('nocodemirror','daxecollapse','domcoordacc');
 7365:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 7366:                                                      \%titles,$itemcount);
 7367:         my %staticdefaults = (
 7368:                                 'copyright'    => 'default',
 7369:                                 'sourceavail'  => 'closed',
 7370:                              );
 7371:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7372:         my %currrights;
 7373:         foreach my $item ('copyright','sourceavail') {
 7374:             $currrights{$item} = $staticdefaults{$item};
 7375:             if (ref($settings) eq 'HASH') {
 7376:                 if (exists($settings->{$item})) {
 7377:                     $currrights{$item} = $settings->{$item};
 7378:                 }
 7379:             }
 7380:         }
 7381:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7382:                       '<span class="LC_nobreak">'.$titles{'copyright'}.
 7383:                       '</span></td><td class="LC_right_item">'.
 7384:                       &selectbox('copyright',$currrights{'copyright'},'',
 7385:                                  \&Apache::loncommon::copyrightdescription,
 7386:                                  (grep !/^priv|custom$/,(&Apache::loncommon::copyrightids))).
 7387:                       '</td></tr>'."\n";
 7388:         $itemcount ++;
 7389:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7390:         $datatable .= '<tr'.$css_class.'><td style="vertical-align: top">'.
 7391:                       '<span class="LC_nobreak">'.$titles{'sourceavail'}.
 7392:                       '</span></td><td class="LC_right_item">'.
 7393:                       &selectbox('sourceavail',$currrights{'sourceavail'},'',
 7394:                                  \&Apache::loncommon::source_copyrightdescription,
 7395:                                  (&Apache::loncommon::source_copyrightids)).
 7396:                       '</td></tr>'."\n";        
 7397:     } else {
 7398:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7399:         my $curreditors;
 7400:         my %staticdefaults = (
 7401:                                 editors => ['edit','xml'],
 7402:                                 authorquota => 500,
 7403:                                 webdav => 0,
 7404:                              );
 7405:         my $curreditors = $staticdefaults{'editors'};
 7406:         if ((ref($settings) eq 'HASH') &&
 7407:             (ref($settings->{'editors'}) eq 'ARRAY')) {
 7408:             $curreditors = $settings->{'editors'};
 7409:         } else {
 7410:             $curreditors = $staticdefaults{'editors'};
 7411:         }
 7412:         my @editors = ('edit','xml','daxe');
 7413:         $datatable = '<tr'.$css_class.'>'."\n".
 7414:                      '<td>'.$titles{'editors'}.'</td>'."\n".
 7415:                      '<td class="LC_left_item">'."\n".
 7416:                      '<span class="LC_nobreak">';
 7417:         foreach my $editor (@editors) {
 7418:             my $checked;
 7419:             if (grep(/^\Q$editor\E$/,@{$curreditors})) {
 7420:                 $checked = ' checked="checked"';
 7421:             }
 7422:             $datatable .= '<label>'.
 7423:                           '<input type="checkbox" name="author_editors" '.
 7424:                           $checked.' value="'.$editor.'" '.
 7425:                           'onclick="javascript:checkEditors(this.form,'."'author_editors'".',this);" />'.
 7426:                           $titles{$editor}.'</label>&nbsp;';
 7427:         }
 7428:         $datatable .= '</span>'."\n".'</td>'."\n".'</tr>'."\n";
 7429:         $itemcount ++;
 7430:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7431:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7432:         my @insttypes;
 7433:         if (ref($types) eq 'ARRAY') {
 7434:             @insttypes = @{$types};
 7435:         }
 7436:         my $typecount = 0;
 7437:         my %domconf = &Apache::lonnet::get_dom('configuration',['quotas'],$dom);
 7438:         my @items = ('webdav','authorquota');
 7439:         my %quotas;
 7440:         if (ref($domconf{'quotas'}) eq 'HASH') {
 7441:             %quotas = %{$domconf{'quotas'}};
 7442:             foreach my $item (@items) {
 7443:                 if (ref($quotas{$item}) eq 'HASH') {
 7444:                     foreach my $type (@insttypes,'default') {
 7445:                         if ($item eq 'authorquota') {
 7446:                             if ($quotas{$item}{$type} !~ /^\d+$/) {
 7447:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7448:                             }
 7449:                         } elsif ($item eq 'webdav') {
 7450:                             if ($quotas{$item}{$type} !~ /^(0|1)$/) {
 7451:                                 $quotas{$item}{$type} = $staticdefaults{$item};
 7452:                             }
 7453:                         }
 7454:                     }
 7455:                 } else {
 7456:                     foreach my $type (@insttypes,'default') {
 7457:                         $quotas{$item}{$type} = $staticdefaults{$item};
 7458:                     }
 7459:                 }
 7460:             }
 7461:         } else {
 7462:             foreach my $item (@items) {
 7463:                 foreach my $type (@insttypes,'default') {
 7464:                     $quotas{$item}{$type} = $staticdefaults{$item};
 7465:                 }
 7466:             }
 7467:         }
 7468:         if (ref($usertypes) eq 'HASH') {
 7469:             my $numinrow = 4;
 7470:             my $onclick = '';
 7471:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7472:                                          $numinrow,$othertitle,'authorquota',
 7473:                                          \$itemcount,$onclick);
 7474:             $itemcount ++;
 7475:             $datatable .= &insttypes_row(\%quotas,$types,$usertypes,$dom,
 7476:                                          $numinrow,$othertitle,'webdav',
 7477:                                          \$itemcount);
 7478:             $itemcount ++;
 7479:         }
 7480:         my $checkedno = ' checked="checked"';
 7481:         my ($checkedon,$checkedoff);
 7482:         if (ref($quotas{'webdav'}) eq 'HASH') {
 7483:             if ($quotas{'webdav'}{'_LC_adv'} =~ /^0|1$/) {
 7484:                 if ($quotas{'webdav'}{'_LC_adv'}) {
 7485:                     $checkedon = $checkedno;
 7486:                 } else {
 7487:                     $checkedoff = $checkedno;
 7488:                 }
 7489:                 undef($checkedno);
 7490:             }
 7491:         }
 7492:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7493:         $datatable .= '<tr'.$css_class.'>'.
 7494:                       '<td>'.$titles{'webdav_LC_adv'}.'<br />'.
 7495:                              $titles{'webdav_LC_adv_over'}.
 7496:                       '</td>'.
 7497:                       '<td class="LC_left_item">';
 7498:         foreach my $option ('none','off','on') {
 7499:             my ($text,$val,$checked);
 7500:             if ($option eq 'none') {
 7501:                 $text = $titles{'none'};
 7502:                 $val = '';
 7503:                 $checked = $checkedno;
 7504:             } elsif ($option eq 'off') {
 7505:                 $text = $titles{'overoff'};
 7506:                 $val = 0;
 7507:                 $checked = $checkedoff;
 7508:             } elsif ($option eq 'on') {
 7509:                 $text = $titles{'overon'};
 7510:                 $val = 1;
 7511:                 $checked = $checkedon;
 7512:             }
 7513:             $datatable .= '<span class="LC_nobreak"><label>'.
 7514:                           '<input type="radio" name="webdav_LC_adv"'.
 7515:                           ' value="'.$val.'"'.$checked.' />'.
 7516:                           $text.'</label></span>&nbsp; ';
 7517:         }
 7518:         $datatable .= '</td></tr>';
 7519:         $itemcount ++;
 7520:         my %defchecked = (
 7521:                                 'archive' => 'off',
 7522:                              );
 7523:         my @toggles = ('archive');
 7524:         (my $archive,$itemcount) = &radiobutton_prefs($settings,['archive'],
 7525:                                                       {'archive' => 'off'},
 7526:                                                       \%titles,$itemcount);
 7527:         $datatable .= $archive."\n";
 7528:         $itemcount ++;
 7529:     }
 7530:     $$rowtotal += $itemcount;
 7531:     return $datatable;
 7532: }
 7533: 
 7534: sub authordefaults_titles {
 7535:     return &Apache::lonlocal::texthash(
 7536:                copyright => 'Copyright/Distribution',
 7537:                sourceavail => ' Source Available',
 7538:                editors => 'Available Editors',
 7539:                webdav => 'WebDAV',
 7540:                authorquota => 'Authoring Space quotas (MB)',
 7541:                nocodemirror => 'Deactivate CodeMirror for EditXML editor',
 7542:                daxecollapse => 'Daxe editor: LON-CAPA standard menus start collapsed',
 7543:                domcoordacc => 'Dom. Coords. can enter Authoring Spaces in domain',
 7544:                edit  => 'Standard editor (Edit)',
 7545:                xml   => 'Text editor (EditXML)',
 7546:                daxe  => 'Daxe editor (Daxe)',
 7547:                webdav_LC_adv => 'WebDAV access for LON-CAPA "advanced" users',
 7548:                webdav_LC_adv_over => '(overrides access based on affiliation, if set)',
 7549:                none => 'No override set',
 7550:                overon => 'Override -- webDAV on',
 7551:                overoff => 'Override -- webDAV off',
 7552:                archive => 'Authors can download tar.gz file of Authoring Space',
 7553:     );
 7554: }
 7555: 
 7556: sub selectbox {
 7557:     my ($name,$value,$readonly,$functionref,@idlist)=@_;
 7558:     my $selout = '<select name="'.$name.'">';
 7559:     foreach my $id (@idlist) {
 7560:         $selout.='<option value="'.$id.'"';
 7561:         if ($id eq $value) {
 7562:             $selout.=' selected="selected"';
 7563:         }
 7564:         if ($readonly) {
 7565:             $selout .= ' disabled="disabled"';
 7566:         }
 7567:         $selout.='>'.&{$functionref}($id).'</option>';
 7568:     }
 7569:     $selout.='</select>';
 7570:     return $selout;
 7571: }
 7572: 
 7573: sub print_selfenrollment {
 7574:     my ($position,$dom,$settings,$rowtotal) = @_;
 7575:     my ($css_class,$datatable);
 7576:     my $itemcount = 1;
 7577:     my @types = ('official','unofficial','community','textbook','placement');
 7578:     if (($position eq 'top') || ($position eq 'middle')) {
 7579:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7580:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7581:         my @rows;
 7582:         my $key;
 7583:         if ($position eq 'top') {
 7584:             $key = 'admin'; 
 7585:             if (ref($rowsref) eq 'ARRAY') {
 7586:                 @rows = @{$rowsref};
 7587:             }
 7588:         } elsif ($position eq 'middle') {
 7589:             $key = 'default';
 7590:             @rows = ('types','registered','approval','limit');
 7591:         }
 7592:         foreach my $row (@rows) {
 7593:             if (defined($titlesref->{$row})) {
 7594:                 $itemcount ++;
 7595:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7596:                 $datatable .= '<tr'.$css_class.'>'.
 7597:                               '<td>'.$titlesref->{$row}.'</td>'.
 7598:                               '<td class="LC_left_item">'.
 7599:                               '<table><tr>';
 7600:                 my (%current,%currentcap);
 7601:                 if (ref($settings) eq 'HASH') {
 7602:                     if (ref($settings->{$key}) eq 'HASH') {
 7603:                         foreach my $type (@types) {
 7604:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7605:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7606:                             }
 7607:                             if (($row eq 'limit') && ($key eq 'default')) {
 7608:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7609:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7610:                                 }
 7611:                             }
 7612:                         }
 7613:                     }
 7614:                 }
 7615:                 my %roles = (
 7616:                              '0' => &Apache::lonnet::plaintext('dc'),
 7617:                             ); 
 7618:             
 7619:                 foreach my $type (@types) {
 7620:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7621:                         $datatable .= '<th>'.&mt($type).'</th>';
 7622:                     }
 7623:                 }
 7624:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7625:                     $datatable .= '</tr><tr>';
 7626:                 }
 7627:                 foreach my $type (@types) {
 7628:                     if ($type eq 'community') {
 7629:                         $roles{'1'} = &mt('Community personnel');
 7630:                     } else {
 7631:                         $roles{'1'} = &mt('Course personnel');
 7632:                     }
 7633:                     $datatable .= '<td style="vertical-align: top">';
 7634:                     if ($position eq 'top') {
 7635:                         my %checked;
 7636:                         if ($current{$type} eq '0') {
 7637:                             $checked{'0'} = ' checked="checked"';
 7638:                         } else {
 7639:                             $checked{'1'} = ' checked="checked"';
 7640:                         }
 7641:                         foreach my $role ('1','0') {
 7642:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7643:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7644:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7645:                                           $roles{$role}.'</label></span> ';
 7646:                         }
 7647:                     } else {
 7648:                         if ($row eq 'types') {
 7649:                             my %checked;
 7650:                             if ($current{$type} =~ /^(all|dom)$/) {
 7651:                                 $checked{$1} = ' checked="checked"';
 7652:                             } else {
 7653:                                 $checked{''} = ' checked="checked"';
 7654:                             }
 7655:                             foreach my $val ('','dom','all') {
 7656:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7657:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7658:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7659:                             }
 7660:                         } elsif ($row eq 'registered') {
 7661:                             my %checked;
 7662:                             if ($current{$type} eq '1') {
 7663:                                 $checked{'1'} = ' checked="checked"';
 7664:                             } else {
 7665:                                 $checked{'0'} = ' checked="checked"';
 7666:                             }
 7667:                             foreach my $val ('0','1') {
 7668:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7669:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7670:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7671:                             }
 7672:                         } elsif ($row eq 'approval') {
 7673:                             my %checked;
 7674:                             if ($current{$type} =~ /^([12])$/) {
 7675:                                 $checked{$1} = ' checked="checked"';
 7676:                             } else {
 7677:                                 $checked{'0'} = ' checked="checked"';
 7678:                             }
 7679:                             for my $val (0..2) {
 7680:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7681:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7682:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7683:                             }
 7684:                         } elsif ($row eq 'limit') {
 7685:                             my %checked;
 7686:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7687:                                 $checked{$1} = ' checked="checked"';
 7688:                             } else {
 7689:                                 $checked{'none'} = ' checked="checked"';
 7690:                             }
 7691:                             my $cap;
 7692:                             if ($currentcap{$type} =~ /^\d+$/) {
 7693:                                 $cap = $currentcap{$type};
 7694:                             }
 7695:                             foreach my $val ('none','allstudents','selfenrolled') {
 7696:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7697:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7698:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7699:                             }
 7700:                             $datatable .= '<br />'.
 7701:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7702:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7703:                                           '</span>'; 
 7704:                         }
 7705:                     }
 7706:                     $datatable .= '</td>';
 7707:                 }
 7708:                 $datatable .= '</tr>';
 7709:             }
 7710:             $datatable .= '</table></td></tr>';
 7711:         }
 7712:     } elsif ($position eq 'bottom') {
 7713:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7714:     }
 7715:     $$rowtotal += $itemcount;
 7716:     return $datatable;
 7717: }
 7718: 
 7719: sub print_validation_rows {
 7720:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7721:     my ($itemsref,$namesref,$fieldsref);
 7722:     if ($caller eq 'selfenroll') { 
 7723:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7724:     } elsif ($caller eq 'requestcourses') {
 7725:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7726:     }
 7727:     my %currvalidation;
 7728:     if (ref($settings) eq 'HASH') {
 7729:         if (ref($settings->{'validation'}) eq 'HASH') {
 7730:             %currvalidation = %{$settings->{'validation'}};
 7731:         }
 7732:     }
 7733:     my $datatable;
 7734:     my $itemcount = 0;
 7735:     foreach my $item (@{$itemsref}) {
 7736:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7737:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7738:                       $namesref->{$item}.
 7739:                       '</span></td>'.
 7740:                       '<td class="LC_left_item">';
 7741:         if (($item eq 'url') || ($item eq 'button')) {
 7742:             $datatable .= '<span class="LC_nobreak">'.
 7743:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7744:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7745:         } elsif ($item eq 'fields') {
 7746:             my @currfields;
 7747:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7748:                 @currfields = @{$currvalidation{$item}};
 7749:             }
 7750:             foreach my $field (@{$fieldsref}) {
 7751:                 my $check = '';
 7752:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7753:                     $check = ' checked="checked"';
 7754:                 }
 7755:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7756:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7757:                               ' value="'.$field.'"'.$check.' />'.$field.
 7758:                               '</label></span> ';
 7759:             }
 7760:         } elsif ($item eq 'markup') {
 7761:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7762:                            $currvalidation{$item}.
 7763:                               '</textarea>';
 7764:         }
 7765:         $datatable .= '</td></tr>'."\n";
 7766:         if (ref($rowtotal)) {
 7767:             $itemcount ++;
 7768:         }
 7769:     }
 7770:     if ($caller eq 'requestcourses') {
 7771:         my %currhash;
 7772:         if (ref($settings) eq 'HASH') {
 7773:             if (ref($settings->{'validation'}) eq 'HASH') {
 7774:                 if ($settings->{'validation'}{'dc'} ne '') {
 7775:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7776:                 }
 7777:             }
 7778:         }
 7779:         my $numinrow = 2;
 7780:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7781:                                                        'validationdc',%currhash);
 7782:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7783:         $datatable .= '<tr'.$css_class.'><td>';
 7784:         if ($numdc > 1) {
 7785:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7786:         } else {
 7787:             $datatable .=  &mt('Course creation processed as: ');
 7788:         }
 7789:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7790:         $itemcount ++;
 7791:     }
 7792:     if (ref($rowtotal)) {
 7793:         $$rowtotal += $itemcount;
 7794:     }
 7795:     return $datatable;
 7796: }
 7797: 
 7798: sub print_privacy {
 7799:     my ($position,$dom,$settings,$rowtotal) = @_;
 7800:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7801:     my $itemcount = 0;
 7802:     if ($position eq 'top') {
 7803:         $numinrow = 2;
 7804:     } else {
 7805:         @items = ('domain','author','course','community');
 7806:         %names = &Apache::lonlocal::texthash (
 7807:                      domain => 'Assigned domain role(s)',
 7808:                      author => 'Assigned co-author role(s)',
 7809:                      course => 'Assigned course role(s)',
 7810:                      community => 'Assigned community role(s)',
 7811:                  );
 7812:         $numinrow = 4;
 7813:         ($othertitle,$usertypes,$types) =
 7814:             &Apache::loncommon::sorted_inst_types($dom);
 7815:     }
 7816:     if (($position eq 'top') || ($position eq 'middle')) {
 7817:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7818:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7819:         if ($position eq 'top') {
 7820:             my %curr;
 7821:             my @options = ('none','user','domain','auto');
 7822:             my %titles = &Apache::lonlocal::texthash (
 7823:                 none   => 'Not allowed',
 7824:                 user   => 'User authorizes',
 7825:                 domain => 'DC authorizes',
 7826:                 auto   => 'Unrestricted',
 7827:                 instdom => 'Other domain shares institution/provider',
 7828:                 extdom => 'Other domain has different institution/provider',
 7829:                 notify => 'Notify when role needs authorization',
 7830:             );
 7831:             my %names = &Apache::lonlocal::texthash (
 7832:                 domain => 'Domain role',
 7833:                 author => 'Co-author role',
 7834:                 course => 'Course role',
 7835:                 community => 'Community role',
 7836:             );
 7837:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7838:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7839:             foreach my $domtype ('instdom','extdom') {
 7840:                 my (%checked,$skip);
 7841:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7842:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7843:                               '<td class="LC_left_item">';
 7844:                 if ($domtype eq 'instdom') {
 7845:                     unless (@instdoms > 1) {
 7846:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7847:                         $skip = 1;
 7848:                     }
 7849:                 } elsif ($domtype eq 'extdom') {
 7850:                     if (keys(%by_location) == 0) {
 7851:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7852:                         $skip = 1;
 7853:                     }
 7854:                 }
 7855:                 unless ($skip) {
 7856:                     foreach my $roletype ('domain','author','course','community') {
 7857:                         $checked{'auto'} = ' checked="checked"';
 7858:                         if (ref($settings) eq 'HASH') {
 7859:                             if (ref($settings->{approval}) eq 'HASH') {
 7860:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7861:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7862:                                         $checked{$1} = ' checked="checked"';
 7863:                                         $checked{'auto'} = '';
 7864:                                     }
 7865:                                 }
 7866:                             }
 7867:                         }
 7868:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7869:                         foreach my $option (@options) {
 7870:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7871:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7872:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7873:                                           '</label></span>&nbsp; ';
 7874:                         }
 7875:                         $datatable .= '</fieldset>';
 7876:                     }
 7877:                 }
 7878:                 $datatable .= '</td></tr>';
 7879:                 $itemcount ++;
 7880:             }
 7881:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7882:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7883:                           '<td class="LC_left_item">';
 7884:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7885:                 my %curr;
 7886:                 if (ref($settings) eq 'HASH') {
 7887:                     if ($settings->{'notify'} ne '') {
 7888:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7889:                     }
 7890:                 }
 7891:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7892:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7893:                                                              'privacy_notify',%curr);
 7894:                 if ($numdc > 0) {
 7895:                     $datatable .= $table;
 7896:                 } else {
 7897:                     $datatable .= &mt('There are no active Domain Coordinators');
 7898:                 }
 7899:             } else {
 7900:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7901:             }
 7902:             $datatable .='</td></tr>';
 7903:         } elsif ($position eq 'middle') {
 7904:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7905:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7906:                     foreach my $item (@{$types}) {
 7907:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7908:                                                                $numinrow,$itemcount,'','','','','',
 7909:                                                                '',$usertypes->{$item});
 7910:                         $itemcount ++;
 7911:                     }
 7912:                 }
 7913:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7914:                                                        $numinrow,$itemcount,'','','','','',
 7915:                                                        '',$othertitle);
 7916:                 $itemcount ++;
 7917:             } else {
 7918:                 my (@insttypes,%insttitles);
 7919:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7920:                     @insttypes = @{$types};
 7921:                     %insttitles = %{$usertypes};
 7922:                 }
 7923:                 foreach my $item (@insttypes,'default') {
 7924:                     my $title;
 7925:                     if ($item eq 'default') {
 7926:                         $title = $othertitle;
 7927:                     } else {
 7928:                         $title = $insttitles{$item};
 7929:                     }
 7930:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7931:                     $datatable .= '<tr'.$css_class.'>'.
 7932:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7933:                                   '<td class="LC_left_item">'.
 7934:                                   &mt('Nothing to set here, as there are no other domains').
 7935:                                   '</td></tr>';
 7936:                     $itemcount ++;
 7937:                 }
 7938:             }
 7939:         }
 7940:     } else {
 7941:         my $prefix;
 7942:         if ($position eq 'lower') {
 7943:             $prefix = 'priv';
 7944:         } else {
 7945:             $prefix = 'unpriv';
 7946:         }
 7947:         foreach my $item (@items) {
 7948:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7949:                                                    $numinrow,$itemcount,'','','','','',
 7950:                                                    '',$names{$item});
 7951:             $itemcount ++;
 7952:         }
 7953:     }
 7954:     if (ref($rowtotal)) {
 7955:         $$rowtotal += $itemcount;
 7956:     }
 7957:     return $datatable;
 7958: }
 7959: 
 7960: sub print_passwords {
 7961:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7962:     my ($datatable,$css_class);
 7963:     my $itemcount = 0;
 7964:     my %titles = &Apache::lonlocal::texthash (
 7965:         captcha        => '"Forgot Password" CAPTCHA validation',
 7966:         link           => 'Reset link expiration (hours)',
 7967:         case           => 'Case-sensitive usernames/e-mail',
 7968:         prelink        => 'Information required (form 1)',
 7969:         postlink       => 'Information required (form 2)',
 7970:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7971:         customtext     => 'Domain specific text (HTML)',
 7972:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7973:         intauth_check  => 'Check bcrypt cost if authenticated',
 7974:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7975:         permanent      => 'Permanent e-mail address',
 7976:         critical       => 'Critical notification address',
 7977:         notify         => 'Notification address',
 7978:         min            => 'Minimum password length',
 7979:         max            => 'Maximum password length',
 7980:         chars          => 'Required characters',
 7981:         expire         => 'Password expiration (days)',
 7982:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7983:     );
 7984:     if ($position eq 'top') {
 7985:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7986:         my $shownlinklife = 2;
 7987:         my $prelink = 'both';
 7988:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7989:         if (ref($settings) eq 'HASH') {
 7990:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7991:                 $shownlinklife = $settings->{resetlink};
 7992:             }
 7993:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7994:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7995:             }
 7996:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7997:                 $prelink = $settings->{resetprelink};
 7998:             }
 7999:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 8000:                 %postlink = %{$settings->{resetpostlink}};
 8001:             }
 8002:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 8003:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 8004:             }
 8005:             if ($settings->{resetremove}) {
 8006:                 $nostdtext = 1;
 8007:             }
 8008:             if ($settings->{resetcustom}) {
 8009:                 $customurl = $settings->{resetcustom};
 8010:             }
 8011:         } else {
 8012:             if (ref($types) eq 'ARRAY') {
 8013:                 foreach my $item (@{$types}) {
 8014:                     $casesens{$item} = 1;
 8015:                     $postlink{$item} = ['username','email'];
 8016:                 }
 8017:             }
 8018:             $casesens{'default'} = 1;
 8019:             $postlink{'default'} = ['username','email'];
 8020:             $prelink = 'both';
 8021:             %emailsrc = (
 8022:                           permanent => 1,
 8023:                           critical  => 1,
 8024:                           notify    => 1,
 8025:             );
 8026:         }
 8027:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 8028:         $itemcount ++;
 8029:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8030:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 8031:                       '<td class="LC_left_item">'.
 8032:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 8033:                       'name="passwords_link" size="3" /></td></tr>';
 8034:         $itemcount ++;
 8035:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8036:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 8037:                       '<td class="LC_left_item">';
 8038:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8039:             foreach my $item (@{$types}) {
 8040:                 my $checkedcase;
 8041:                 if ($casesens{$item}) {
 8042:                     $checkedcase = ' checked="checked"';
 8043:                 }
 8044:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8045:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 8046:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 8047:                               '</span>&nbsp;&nbsp; ';
 8048:             }
 8049:         }
 8050:         my $checkedcase;
 8051:         if ($casesens{'default'}) {
 8052:             $checkedcase = ' checked="checked"';
 8053:         }
 8054:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8055:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 8056:                       $othertitle.'</label></span></td>';
 8057:         $itemcount ++;
 8058:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8059:         my %checkedpre = (
 8060:                              both => ' checked="checked"',
 8061:                              either => '',
 8062:                          );
 8063:         if ($prelink eq 'either') {
 8064:             $checkedpre{either} = ' checked="checked"';
 8065:             $checkedpre{both} = '';
 8066:         }
 8067:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 8068:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8069:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 8070:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 8071:                       '<span class="LC_nobreak"><label>'.
 8072:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 8073:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 8074:         $itemcount ++;
 8075:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8076:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 8077:                       '<td class="LC_left_item">';
 8078:         my %postlinked;
 8079:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8080:             foreach my $item (@{$types}) {
 8081:                 undef(%postlinked);
 8082:                 $datatable .= '<fieldset style="display: inline-block;">'.
 8083:                               '<legend>'.$usertypes->{$item}.'</legend>';
 8084:                 if (ref($postlink{$item}) eq 'ARRAY') {
 8085:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 8086:                 }
 8087:                 foreach my $field ('email','username') {
 8088:                     my $checked;
 8089:                     if ($postlinked{$field}) {
 8090:                         $checked = ' checked="checked"';
 8091:                     }
 8092:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8093:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 8094:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 8095:                                   '<span>&nbsp;&nbsp; ';
 8096:                 }
 8097:                 $datatable .= '</fieldset>';
 8098:             }
 8099:         }
 8100:         if (ref($postlink{'default'}) eq 'ARRAY') {
 8101:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 8102:         }
 8103:         $datatable .= '<fieldset style="display: inline-block;">'.
 8104:                       '<legend>'.$othertitle.'</legend>';
 8105:         foreach my $field ('email','username') {
 8106:             my $checked;
 8107:             if ($postlinked{$field}) {
 8108:                 $checked = ' checked="checked"';
 8109:             }
 8110:             $datatable .= '<span class="LC_nobreak"><label>'.
 8111:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 8112:                           $field.'"'.$checked.' />'.$field.'</label>'.
 8113:                           '<span>&nbsp;&nbsp; ';
 8114:         }
 8115:         $datatable .= '</fieldset></td></tr>';
 8116:         $itemcount ++;
 8117:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8118:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 8119:                       '<td class="LC_left_item">';
 8120:         foreach my $type ('permanent','critical','notify') {
 8121:             my $checkedemail;
 8122:             if ($emailsrc{$type}) {
 8123:                 $checkedemail = ' checked="checked"';
 8124:             }
 8125:             $datatable .= '<span class="LC_nobreak"><label>'.
 8126:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 8127:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 8128:                           '<span>&nbsp;&nbsp; ';
 8129:         }
 8130:         $datatable .= '</td></tr>';
 8131:         $itemcount ++;
 8132:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8133:         my $switchserver = &check_switchserver($dom,$confname);
 8134:         my ($showstd,$noshowstd);
 8135:         if ($nostdtext) {
 8136:             $noshowstd = ' checked="checked"';
 8137:         } else {
 8138:             $showstd = ' checked="checked"';
 8139:         }
 8140:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 8141:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8142:                       &mt('Retain standard text:').
 8143:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 8144:                       &mt('Yes').'</label>'.'&nbsp;'.
 8145:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 8146:                       &mt('No').'</label></span><br />'.
 8147:                       '<span class="LC_fontsize_small">'.
 8148:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 8149:                       &mt('Include custom text:');
 8150:         if ($customurl) {
 8151:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 8152:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 8153:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 8154:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 8155:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 8156:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 8157:         }
 8158:         if ($switchserver) {
 8159:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 8160:         } else {
 8161:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 8162:                          '<input type="file" name="passwords_customfile" /></span>';
 8163:         }
 8164:         $datatable .= '</td></tr>';
 8165:     } elsif ($position eq 'middle') {
 8166:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 8167:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 8168:         my %defaults;
 8169:         if (ref($domconf{'defaults'}) eq 'HASH') {
 8170:             %defaults = %{$domconf{'defaults'}};
 8171:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 8172:                 $defaults{'intauth_cost'} = 10;
 8173:             }
 8174:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 8175:                 $defaults{'intauth_check'} = 0;
 8176:             }
 8177:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 8178:                 $defaults{'intauth_switch'} = 0;
 8179:             }
 8180:         } else {
 8181:             %defaults = (
 8182:                           'intauth_cost'   => 10,
 8183:                           'intauth_check'  => 0,
 8184:                           'intauth_switch' => 0,
 8185:                         );
 8186:         }
 8187:         foreach my $item (@items) {
 8188:             if ($itemcount%2) {
 8189:                 $css_class = '';
 8190:             } else {
 8191:                 $css_class = ' class="LC_odd_row" ';
 8192:             }
 8193:             $datatable .= '<tr'.$css_class.'>'.
 8194:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 8195:                           '</span></td><td class="LC_left_item" colspan="3">';
 8196:             if ($item eq 'intauth_switch') {
 8197:                 my @options = (0,1,2);
 8198:                 my %optiondesc = &Apache::lonlocal::texthash (
 8199:                                    0 => 'No',
 8200:                                    1 => 'Yes',
 8201:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 8202:                                  );
 8203:                 $datatable .= '<table width="100%">';
 8204:                 foreach my $option (@options) {
 8205:                     my $checked = ' ';
 8206:                     if ($defaults{$item} eq $option) {
 8207:                         $checked = ' checked="checked"';
 8208:                     }
 8209:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8210:                                   '<label><input type="radio" name="'.$item.
 8211:                                   '" value="'.$option.'"'.$checked.' />'.
 8212:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8213:                 }
 8214:                 $datatable .= '</table>';
 8215:             } elsif ($item eq 'intauth_check') {
 8216:                 my @options = (0,1,2);
 8217:                 my %optiondesc = &Apache::lonlocal::texthash (
 8218:                                    0 => 'No',
 8219:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 8220:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 8221:                                  );
 8222:                 $datatable .= '<table width="100%">';
 8223:                 foreach my $option (@options) {
 8224:                     my $checked = ' ';
 8225:                     my $onclick;
 8226:                     if ($defaults{$item} eq $option) {
 8227:                         $checked = ' checked="checked"';
 8228:                     }
 8229:                     if ($option == 2) {
 8230:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 8231:                     }
 8232:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 8233:                                   '<label><input type="radio" name="'.$item.
 8234:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 8235:                                   $optiondesc{$option}.'</label></span></td></tr>';
 8236:                 }
 8237:                 $datatable .= '</table>';
 8238:             } else {
 8239:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 8240:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 8241:             }
 8242:             $datatable .= '</td></tr>';
 8243:             $itemcount ++;
 8244:         }
 8245:     } elsif ($position eq 'lower') {
 8246:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 8247:     } else {
 8248:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8249:         my %ownerchg = (
 8250:                           by  => {},
 8251:                           for => {},
 8252:                        );
 8253:         my %ownertitles = &Apache::lonlocal::texthash (
 8254:                             by  => 'Course owner status(es) allowed',
 8255:                             for => 'Student status(es) allowed',
 8256:                           );
 8257:         if (ref($settings) eq 'HASH') {
 8258:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 8259:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 8260:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 8261:                 }
 8262:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 8263:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 8264:                 }
 8265:             }
 8266:         }
 8267:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8268:         $datatable .= '<tr '.$css_class.'>'.
 8269:                       '<td>'.
 8270:                       &mt('Requirements').'<ul>'.
 8271:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 8272:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 8273:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 8274:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 8275:                       '</ul>'.
 8276:                       '</td>'.
 8277:                       '<td class="LC_left_item">';
 8278:         foreach my $item ('by','for') {
 8279:             $datatable .= '<fieldset style="display: inline-block;">'.
 8280:                           '<legend>'.$ownertitles{$item}.'</legend>';
 8281:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 8282:                 foreach my $type (@{$types}) {
 8283:                     my $checked;
 8284:                     if ($ownerchg{$item}{$type}) {
 8285:                         $checked = ' checked="checked"';
 8286:                     }
 8287:                     $datatable .= '<span class="LC_nobreak"><label>'.
 8288:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 8289:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 8290:                                   '</span>&nbsp;&nbsp; ';
 8291:                 }
 8292:             }
 8293:             my $checked;
 8294:             if ($ownerchg{$item}{'default'}) {
 8295:                 $checked = ' checked="checked"';
 8296:             }
 8297:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 8298:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 8299:                           $othertitle.'</label></span></fieldset>';
 8300:         }
 8301:         $datatable .= '</td></tr>';
 8302:     }
 8303:     return $datatable;
 8304: }
 8305: 
 8306: sub password_rules {
 8307:     my ($prefix,$itemcountref,$settings) = @_;
 8308:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 8309:     my %titles;
 8310:     if ($prefix eq 'passwords') {
 8311:         %titles = &Apache::lonlocal::texthash (
 8312:             min            => 'Minimum password length',
 8313:             max            => 'Maximum password length',
 8314:             chars          => 'Required characters',
 8315:         );
 8316:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 8317:         %titles = &Apache::lonlocal::texthash (
 8318:             min            => 'Minimum secret length',
 8319:             max            => 'Maximum secret length',
 8320:             chars          => 'Required characters',
 8321:         );
 8322:     }
 8323:     $min = $Apache::lonnet::passwdmin;
 8324:     my $datatable;
 8325:     my $itemcount;
 8326:     if (ref($itemcountref)) {
 8327:         $itemcount = $$itemcountref;
 8328:     }
 8329:     if (ref($settings) eq 'HASH') {
 8330:         if ($settings->{min}) {
 8331:             $min = $settings->{min};
 8332:         }
 8333:         if ($settings->{max}) {
 8334:             $max = $settings->{max};
 8335:         }
 8336:         if (ref($settings->{chars}) eq 'ARRAY') {
 8337:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 8338:         }
 8339:         if ($prefix eq 'passwords') {
 8340:             if ($settings->{expire}) {
 8341:                 $expire = $settings->{expire};
 8342:             }
 8343:             if ($settings->{numsaved}) {
 8344:                 $numsaved = $settings->{numsaved};
 8345:             }
 8346:         }
 8347:     }
 8348:     my %rulenames = &Apache::lonlocal::texthash(
 8349:                                                  uc => 'At least one upper case letter',
 8350:                                                  lc => 'At least one lower case letter',
 8351:                                                  num => 'At least one number',
 8352:                                                  spec => 'At least one non-alphanumeric',
 8353:                                                );
 8354:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8355:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 8356:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8357:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 8358:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8359:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 8360:                   '</span></td></tr>';
 8361:     $itemcount ++;
 8362:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8363:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 8364:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 8365:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 8366:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8367:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 8368:                   '</span></td></tr>';
 8369:     $itemcount ++;
 8370:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8371:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 8372:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 8373:                   '</span></td>';
 8374:     my $numinrow = 2;
 8375:     my @possrules = ('uc','lc','num','spec');
 8376:     $datatable .= '<td class="LC_left_item"><table>';
 8377:     for (my $i=0; $i<@possrules; $i++) {
 8378:         my ($rem,$checked);
 8379:         if ($chars{$possrules[$i]}) {
 8380:             $checked = ' checked="checked"';
 8381:         }
 8382:         $rem = $i%($numinrow);
 8383:         if ($rem == 0) {
 8384:             if ($i > 0) {
 8385:                 $datatable .= '</tr>';
 8386:             }
 8387:             $datatable .= '<tr>';
 8388:         }
 8389:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 8390:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 8391:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 8392:     }
 8393:     my $rem = @possrules%($numinrow);
 8394:     my $colsleft = $numinrow - $rem;
 8395:     if ($colsleft > 1 ) {
 8396:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8397:                       '&nbsp;</td>';
 8398:     } elsif ($colsleft == 1) {
 8399:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8400:     }
 8401:     $datatable .='</table></td></tr>';
 8402:     $itemcount ++;
 8403:     if ($prefix eq 'passwords') {
 8404:         $titles{'expire'} = &mt('Password expiration (days)');
 8405:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 8406:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8407:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 8408:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8409:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 8410:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8411:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 8412:                       '</span></td></tr>';
 8413:         $itemcount ++;
 8414:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8415:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 8416:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 8417:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 8418:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 8419:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 8420:                       '</span></td></tr>';
 8421:         $itemcount ++;
 8422:     }
 8423:     if (ref($itemcountref)) {
 8424:         $$itemcountref += $itemcount;
 8425:     }
 8426:     return $datatable;
 8427: }
 8428: 
 8429: sub print_wafproxy {
 8430:     my ($position,$dom,$settings,$rowtotal) = @_;
 8431:     my $css_class;
 8432:     my $itemcount = 0;
 8433:     my $datatable;
 8434:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8435:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 8436:     my %lt = &wafproxy_titles();
 8437:     foreach my $server (sort(keys(%servers))) {
 8438:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 8439:         next if ($serverhome eq '');
 8440:         my $serverdom;
 8441:         if ($serverhome ne $server) {
 8442:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 8443:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 8444:                 $othercontrol{$server} = $serverdom;
 8445:             }
 8446:         } else {
 8447:             $serverdom = &Apache::lonnet::host_domain($server);
 8448:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 8449:             if ($serverdom ne $dom) {
 8450:                 $othercontrol{$server} = $serverdom;
 8451:             } else {
 8452:                 $setdom = 1;
 8453:                 if (ref($settings) eq 'HASH') {
 8454:                     if (ref($settings->{'alias'}) eq 'HASH') {
 8455:                         $aliases{$dom} = $settings->{'alias'};
 8456:                         if ($aliases{$dom} ne '') {
 8457:                             $showdom = 1;
 8458:                         }
 8459:                     }
 8460:                     if (ref($settings->{'saml'}) eq 'HASH') {
 8461:                         $saml{$dom} = $settings->{'saml'};
 8462:                     }
 8463:                 }
 8464:             }
 8465:         }
 8466:     }
 8467:     if ($setdom) {
 8468:         %{$values{$dom}} = ();
 8469:         if (ref($settings) eq 'HASH') {
 8470:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8471:                 $values{$dom}{$item} = $settings->{$item};
 8472:             }
 8473:         }
 8474:     }
 8475:     if (keys(%othercontrol)) {
 8476:         %otherdoms = reverse(%othercontrol);
 8477:         foreach my $domain (keys(%otherdoms)) {
 8478:             %{$values{$domain}} = ();
 8479:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8480:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8481:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8482:                 if (exists($config{'wafproxy'}{'saml'})) {
 8483:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8484:                 }
 8485:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8486:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8487:                 }
 8488:             }
 8489:         }
 8490:     }
 8491:     if ($position eq 'top') {
 8492:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8493:         my %aliasinfo;
 8494:         foreach my $server (sort(keys(%servers))) {
 8495:             $itemcount ++;
 8496:             my $dom_in_effect;
 8497:             my $aliasrows = '<tr>'.
 8498:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8499:                             &mt('Hostname').': '.
 8500:                             '<span class="LC_nobreak LC_cusr_emph">'.
 8501:                             &Apache::lonnet::hostname($server).
 8502:                             '</span></td><td>&nbsp;</td>';
 8503:             if ($othercontrol{$server}) {
 8504:                 $dom_in_effect = $othercontrol{$server};
 8505:                 my ($current,$forsaml);
 8506:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8507:                     $current = $aliases{$dom_in_effect}{$server};
 8508:                 }
 8509:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8510:                     if ($saml{$dom_in_effect}{$server}) {
 8511:                         $forsaml = 1;
 8512:                     }
 8513:                 }
 8514:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8515:                               &mt('Alias').':&nbsp';
 8516:                 if ($current) {
 8517:                     $aliasrows .= $current;
 8518:                     if ($forsaml) {
 8519:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8520:                     }
 8521:                 } else {
 8522:                     $aliasrows .= &mt('None');
 8523:                 }
 8524:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8525:                               &mt('controlled by domain: [_1]',
 8526:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8527:             } else {
 8528:                 $dom_in_effect = $dom;
 8529:                 my ($current,$samlon,$samloff);
 8530:                 $samloff = ' checked="checked"';
 8531:                 if (ref($aliases{$dom}) eq 'HASH') {
 8532:                     if ($aliases{$dom}{$server}) {
 8533:                         $current = $aliases{$dom}{$server};
 8534:                     }
 8535:                 }
 8536:                 if (ref($saml{$dom}) eq 'HASH') {
 8537:                     if ($saml{$dom}{$server}) {
 8538:                         $samlon = $samloff;
 8539:                         undef($samloff);
 8540:                     }
 8541:                 }
 8542:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8543:                               &mt('Alias').':&nbsp;'.
 8544:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8545:                               'value="'.$current.'" size="30" />'.
 8546:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8547:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8548:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8549:                               &mt('No').'</label>&nbsp;<label>'.
 8550:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8551:                               &mt('Yes').'</label></span>'.
 8552:                               '</td>';
 8553:             }
 8554:             $aliasrows .= '</tr>';
 8555:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8556:         }
 8557:         if ($aliasinfo{$dom}) {
 8558:             my ($onclick,$wafon,$wafoff,$showtable);
 8559:             $onclick = ' onclick="javascript:toggleWAF();"';
 8560:             $wafoff = ' checked="checked"';
 8561:             $showtable = ' style="display:none";';
 8562:             if ($showdom) {
 8563:                 $wafon = $wafoff;
 8564:                 $wafoff = '';
 8565:                 $showtable = ' style="display:inline;"';
 8566:             }
 8567:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8568:             $datatable = '<tr'.$css_class.'>'.
 8569:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8570:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8571:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8572:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8573:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8574:                          &mt('No').'</label></span></td>'.
 8575:                          '<td class="LC_left_item">'.
 8576:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8577:                          '</table></td></tr>';
 8578:             $itemcount++;
 8579:         }
 8580:         if (keys(%otherdoms)) {
 8581:             foreach my $key (sort(keys(%otherdoms))) {
 8582:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8583:                 $datatable .= '<tr'.$css_class.'>'.
 8584:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8585:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8586:                               '</table></td></tr>';
 8587:                 $itemcount++;
 8588:             }
 8589:         }
 8590:     } else {
 8591:         my %ip_methods = &remoteip_methods();
 8592:         if ($setdom) {
 8593:             $itemcount ++;
 8594:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8595:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8596:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8597:             $wafstyle = ' style="display:none;"';
 8598:             $nowafstyle = ' style="display:table-row;"';
 8599:             $currwafdisplay = ' style="display: none"';
 8600:             $wafrangestyle = ' style="display: none"';
 8601:             $curr_remotip = 'n';
 8602:             $ssltossl = ' checked="checked"';
 8603:             if ($showdom) {
 8604:                 $wafstyle = ' style="display:table-row;"';
 8605:                 $nowafstyle =  ' style="display:none;"';
 8606:                 if (keys(%{$values{$dom}})) {
 8607:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8608:                         $curr_remotip = $values{$dom}{remoteip};
 8609:                     }
 8610:                     if ($curr_remotip eq 'h') {
 8611:                         $currwafdisplay = ' style="display:table-row"';
 8612:                         $wafrangestyle = ' style="display:inline-block;"';
 8613:                     }
 8614:                     if ($values{$dom}{'sslopt'}) {
 8615:                         $alltossl = ' checked="checked"';
 8616:                         $ssltossl = '';
 8617:                     }
 8618:                 }
 8619:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8620:                     $vpndircheck = ' checked="checked"';
 8621:                     $currwafvpn = ' style="display:table-row;"';
 8622:                     $wafrangestyle = ' style="display:inline-block;"';
 8623:                 } else {
 8624:                     $vpnaliascheck = ' checked="checked"';
 8625:                     $currwafvpn = ' style="display:none;"';
 8626:                 }
 8627:             }
 8628:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8629:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8630:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8631:                           '</tr>'.
 8632:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8633:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8634:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8635:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8636:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8637:                           '<td class="LC_left_item"><table>'.
 8638:                           '<tr>'.
 8639:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8640:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8641:             foreach my $option ('m','h','n') {
 8642:                 my $sel;
 8643:                 if ($option eq $curr_remotip) {
 8644:                    $sel = ' selected="selected"';
 8645:                 }
 8646:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8647:                               $ip_methods{$option}.'</option>';
 8648:             }
 8649:             $datatable .= '</select></td></tr>'."\n".
 8650:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8651:                           $lt{'ipheader'}.':&nbsp;'.
 8652:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8653:                           'name="wafproxy_ipheader" />'.
 8654:                           '</td></tr>'."\n".
 8655:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8656:                           $lt{'trusted'}.':<br />'.
 8657:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8658:                           $values{$dom}{'trusted'}.'</textarea>'.
 8659:                           '</td></tr>'."\n".
 8660:                           '<tr><td><hr /></td></tr>'."\n".
 8661:                           '<tr>'.
 8662:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8663:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8664:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8665:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8666:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8667:             foreach my $item ('vpnint','vpnext') {
 8668:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8669:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8670:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8671:                               $values{$dom}{$item}.'</textarea>'.
 8672:                               '</td></tr>'."\n";
 8673:             }
 8674:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8675:                           '<tr>'.
 8676:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8677:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8678:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8679:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8680:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8681:                           '</table></td></tr>';
 8682:         }
 8683:         if (keys(%otherdoms)) {
 8684:             foreach my $domain (sort(keys(%otherdoms))) {
 8685:                 $itemcount ++;
 8686:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8687:                 $datatable .= '<tr'.$css_class.'>'.
 8688:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8689:                               '<td class="LC_left_item"><table>';
 8690:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8691:                     my $showval = &mt('None');
 8692:                     if ($item eq 'ssl') {
 8693:                         $showval = $lt{'ssltossl'};
 8694:                     }
 8695:                     if ($values{$domain}{$item}) {
 8696:                         $showval = $values{$domain}{$item};
 8697:                         if ($item eq 'ssl') {
 8698:                             $showval = $lt{'alltossl'};
 8699:                         } elsif ($item eq 'remoteip') {
 8700:                             $showval = $ip_methods{$values{$domain}{$item}};
 8701:                         }
 8702:                     }
 8703:                     $datatable .= '<tr>'.
 8704:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8705:                 }
 8706:                 $datatable .= '</table></td></tr>';
 8707:             }
 8708:         }
 8709:     }
 8710:     $$rowtotal += $itemcount;
 8711:     return $datatable;
 8712: }
 8713: 
 8714: sub wafproxy_titles {
 8715:     return &Apache::lonlocal::texthash(
 8716:                remoteip   => "Method for determining user's IP",
 8717:                ipheader   => 'Request header containing remote IP',
 8718:                trusted    => 'Trusted IP range(s)',
 8719:                vpnaccess  => 'Access from institutional VPN',
 8720:                vpndirect  => 'via regular hostname (no WAF)',
 8721:                vpnaliased => 'via aliased hostname (WAF)',
 8722:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8723:                vpnext     => 'IP Range(s) for backend WAF connections',
 8724:                sslopt     => 'Forwarding http/https',
 8725:                alltossl   => 'WAF forwards both http and https requests to https',
 8726:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8727:            );
 8728: }
 8729: 
 8730: sub remoteip_methods {
 8731:     return &Apache::lonlocal::texthash(
 8732:               m => 'Use Apache mod_remoteip',
 8733:               h => 'Use headers parsed by LON-CAPA',
 8734:               n => 'Not in use',
 8735:            );
 8736: }
 8737: 
 8738: sub print_usersessions {
 8739:     my ($position,$dom,$settings,$rowtotal) = @_;
 8740:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8741:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8742:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8743: 
 8744:     my @alldoms = &Apache::lonnet::all_domains();
 8745:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8746:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8747:     my %altids = &id_for_thisdom(%servers);
 8748:     if ($position eq 'top') {
 8749:         if (keys(%serverhomes) > 1) {
 8750:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8751:             my ($curroffloadnow,$curroffloadoth);
 8752:             if (ref($settings) eq 'HASH') {
 8753:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8754:                     $curroffloadnow = $settings->{'offloadnow'};
 8755:                 }
 8756:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8757:                     $curroffloadoth = $settings->{'offloadoth'};
 8758:                 }
 8759:             }
 8760:             my $other_insts = scalar(keys(%by_location));
 8761:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8762:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8763:         } else {
 8764:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8765:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8766:                           '</td></tr>';
 8767:         }
 8768:     } else {
 8769:         my %titles = &usersession_titles();
 8770:         my ($prefix,@types);
 8771:         if ($position eq 'bottom') {
 8772:             $prefix = 'remote';
 8773:             @types = ('version','excludedomain','includedomain');
 8774:         } else {
 8775:             $prefix = 'hosted';
 8776:             @types = ('excludedomain','includedomain');
 8777:         }
 8778:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8779:     }
 8780:     $$rowtotal += $itemcount;
 8781:     return $datatable;
 8782: }
 8783: 
 8784: sub rules_by_location {
 8785:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8786:     my ($datatable,$itemcount,$css_class);
 8787:     if (keys(%{$by_location}) == 0) {
 8788:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8789:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8790:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8791:                      '</td></tr>';
 8792:         $itemcount = 1;
 8793:     } else {
 8794:         $itemcount = 0;
 8795:         my $numinrow = 5;
 8796:         my (%current,%checkedon,%checkedoff);
 8797:         my @locations = sort(keys(%{$by_location}));
 8798:         foreach my $type (@{$types}) {
 8799:             $checkedon{$type} = '';
 8800:             $checkedoff{$type} = ' checked="checked"';
 8801:         }
 8802:         if (ref($settings) eq 'HASH') {
 8803:             if (ref($settings->{$prefix}) eq 'HASH') {
 8804:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8805:                     $current{$key} = $settings->{$prefix}{$key};
 8806:                     if ($key eq 'version') {
 8807:                         if ($current{$key} ne '') {
 8808:                             $checkedon{$key} = ' checked="checked"';
 8809:                             $checkedoff{$key} = '';
 8810:                         }
 8811:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8812:                         $checkedon{$key} = ' checked="checked"';
 8813:                         $checkedoff{$key} = '';
 8814:                     }
 8815:                 }
 8816:             }
 8817:         }
 8818:         foreach my $type (@{$types}) {
 8819:             next if ($type ne 'version' && !@locations);
 8820:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8821:             $datatable .= '<tr'.$css_class.'>
 8822:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8823:                            <span class="LC_nobreak">&nbsp;
 8824:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8825:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8826:             if ($type eq 'version') {
 8827:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8828:                 my $selector = '<select name="'.$prefix.'_version">';
 8829:                 foreach my $version (@lcversions) {
 8830:                     my $selected = '';
 8831:                     if ($current{'version'} eq $version) {
 8832:                         $selected = ' selected="selected"';
 8833:                     }
 8834:                     $selector .= ' <option value="'.$version.'"'.
 8835:                                  $selected.'>'.$version.'</option>';
 8836:                 }
 8837:                 $selector .= '</select> ';
 8838:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8839:             } else {
 8840:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8841:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8842:                              ' />'.('&nbsp;'x2).
 8843:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8844:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8845:                              "\n".
 8846:                              '</div><div><table>';
 8847:                 my $rem;
 8848:                 for (my $i=0; $i<@locations; $i++) {
 8849:                     my ($showloc,$value,$checkedtype);
 8850:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8851:                         my $ip = $by_location->{$locations[$i]}->[0];
 8852:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8853:                             $value = join(':',@{$by_ip->{$ip}});
 8854:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8855:                             if (ref($current{$type}) eq 'ARRAY') {
 8856:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8857:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8858:                                         $checkedtype = ' checked="checked"';
 8859:                                         last;
 8860:                                     }
 8861:                                 }
 8862:                             }
 8863:                         }
 8864:                     }
 8865:                     $rem = $i%($numinrow);
 8866:                     if ($rem == 0) {
 8867:                         if ($i > 0) {
 8868:                             $datatable .= '</tr>';
 8869:                         }
 8870:                         $datatable .= '<tr>';
 8871:                     }
 8872:                     $datatable .= '<td class="LC_left_item">'.
 8873:                                   '<span class="LC_nobreak"><label>'.
 8874:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8875:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8876:                                   '</label></span></td>';
 8877:                 }
 8878:                 $rem = @locations%($numinrow);
 8879:                 my $colsleft = $numinrow - $rem;
 8880:                 if ($colsleft > 1 ) {
 8881:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8882:                                   '&nbsp;</td>';
 8883:                 } elsif ($colsleft == 1) {
 8884:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8885:                 }
 8886:                 $datatable .= '</tr></table>';
 8887:             }
 8888:             $datatable .= '</td></tr>';
 8889:             $itemcount ++;
 8890:         }
 8891:     }
 8892:     return ($datatable,$itemcount);
 8893: }
 8894: 
 8895: sub print_ssl {
 8896:     my ($position,$dom,$settings,$rowtotal) = @_;
 8897:     my ($css_class,$datatable);
 8898:     my $itemcount = 1;
 8899:     if ($position eq 'top') {
 8900:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8901:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8902:         my $same_institution;
 8903:         if ($intdom ne '') {
 8904:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8905:             if (ref($internet_names) eq 'ARRAY') {
 8906:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8907:                     $same_institution = 1;
 8908:                 }
 8909:             }
 8910:         }
 8911:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8912:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8913:         if ($same_institution) {
 8914:             my %domservers = &Apache::lonnet::get_servers($dom);
 8915:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8916:         } else {
 8917:             $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.");
 8918:         }
 8919:         $datatable .= '</td></tr>';
 8920:         $itemcount ++;
 8921:     } else {
 8922:         my %titles = &ssl_titles();
 8923:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8924:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8925:         my @alldoms = &Apache::lonnet::all_domains();
 8926:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8927:         my @domservers = &Apache::lonnet::get_servers($dom);
 8928:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8929:         my %altids = &id_for_thisdom(%servers);
 8930:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8931:             my $legacy;
 8932:             unless (ref($settings) eq 'HASH') {
 8933:                 my $name;
 8934:                 if ($position eq 'connto') {
 8935:                     $name = 'loncAllowInsecure';
 8936:                 } else {
 8937:                     $name = 'londAllowInsecure';
 8938:                 }
 8939:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8940:                 my @ids=&Apache::lonnet::current_machine_ids();
 8941:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8942:                     my %what = (
 8943:                                    $name => 1,
 8944:                                );
 8945:                     my ($result,$returnhash) =
 8946:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8947:                     if ($result eq 'ok') {
 8948:                         if (ref($returnhash) eq 'HASH') {
 8949:                             $legacy = $returnhash->{$name};
 8950:                         }
 8951:                     }
 8952:                 } else {
 8953:                     $legacy = $Apache::lonnet::perlvar{$name};
 8954:                 }
 8955:             }
 8956:             foreach my $type ('dom','intdom','other') {
 8957:                 my %checked;
 8958:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8959:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8960:                               '<td class="LC_right_item">';
 8961:                 my $skip; 
 8962:                 if ($type eq 'dom') {
 8963:                     unless (keys(%servers) > 1) {
 8964:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8965:                         $skip = 1;
 8966:                     }
 8967:                 }
 8968:                 if ($type eq 'intdom') {
 8969:                     unless (@instdoms > 1) {
 8970:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8971:                         $skip = 1;
 8972:                     } 
 8973:                 } elsif ($type eq 'other') {
 8974:                     if (keys(%by_location) == 0) {
 8975:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8976:                         $skip = 1;
 8977:                     }
 8978:                 }
 8979:                 unless ($skip) {
 8980:                     $checked{'yes'} = ' checked="checked"'; 
 8981:                     if (ref($settings) eq 'HASH') {
 8982:                         if (ref($settings->{$position}) eq 'HASH') {
 8983:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8984:                                 $checked{$1} = $checked{'yes'};
 8985:                                 delete($checked{'yes'}); 
 8986:                             }
 8987:                         }
 8988:                     } else {
 8989:                         if ($legacy == 0) {
 8990:                             $checked{'req'} = $checked{'yes'};
 8991:                             delete($checked{'yes'});    
 8992:                         }
 8993:                     }
 8994:                     foreach my $option ('no','yes','req') {
 8995:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8996:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8997:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8998:                                       '</label></span>'.('&nbsp;'x2);
 8999:                     }
 9000:                 }
 9001:                 $datatable .= '</td></tr>';
 9002:                 $itemcount ++; 
 9003:             }
 9004:         } else {
 9005:             my $prefix = 'replication';
 9006:             my @types = ('certreq','nocertreq');
 9007:             if (keys(%by_location) == 0) {
 9008:                 $datatable .= '<tr'.$css_class.'><td>'.
 9009:                               &mt('Nothing to set here, as there are no other institutions').
 9010:                               '</td></tr>';
 9011:                 $itemcount ++;
 9012:             } else {
 9013:                 ($datatable,$itemcount) = 
 9014:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9015:             }
 9016:         }
 9017:     }
 9018:     $$rowtotal += $itemcount;
 9019:     return $datatable;
 9020: }
 9021: 
 9022: sub ssl_titles {
 9023:     return &Apache::lonlocal::texthash (
 9024:                dom           => 'LON-CAPA servers/VMs from same domain',
 9025:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 9026:                other         => 'External LON-CAPA servers/VMs',
 9027:                connto        => 'Connections to other servers',
 9028:                connfrom      => 'Connections from other servers',
 9029:                replication   => 'Replicating content to other institutions',
 9030:                certreq       => 'Client certificate required, but specific domains exempt',
 9031:                nocertreq     => 'No client certificate required, except for specific domains',
 9032:                no            => 'SSL not used',
 9033:                yes           => 'SSL Optional (used if available)',
 9034:                req           => 'SSL Required',
 9035:     );
 9036: }
 9037: 
 9038: sub print_trust {
 9039:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 9040:     my ($css_class,$datatable,%checked,%choices);
 9041:     my (%by_ip,%by_location,@intdoms,@instdoms);
 9042:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 9043:     my $itemcount = 1;
 9044:     my %titles = &trust_titles();
 9045:     my @types = ('exc','inc');
 9046:     if ($prefix eq 'top') {
 9047:         $prefix = 'content';
 9048:     } elsif ($prefix eq 'bottom') {
 9049:         $prefix = 'msg';
 9050:     }
 9051:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 9052:     $$rowtotal += $itemcount;
 9053:     return $datatable;
 9054: }
 9055: 
 9056: sub trust_titles {
 9057:     return &Apache::lonlocal::texthash(
 9058:                content  => "Access to this domain's content by others",
 9059:                shared   => "Access to other domain's content by this domain",
 9060:                enroll   => "Enrollment in this domain's courses by others", 
 9061:                othcoau  => "Co-author roles in this domain for others",
 9062:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 9063:                domroles => "Domain roles in this domain assignable to others",
 9064:                catalog  => "Course Catalog for this domain displayed elsewhere",
 9065:                reqcrs   => "Requests for creation of courses in this domain by others",
 9066:                msg      => "Users in other domains can send messages to this domain",
 9067:                exc      => "Allow all, but exclude specific domains",
 9068:                inc      => "Deny all, but include specific domains",
 9069:            );
 9070: } 
 9071: 
 9072: sub build_location_hashes {
 9073:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 9074:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 9075:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 9076:     my %iphost = &Apache::lonnet::get_iphost();
 9077:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 9078:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 9079:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 9080:         foreach my $id (@{$iphost{$primary_ip}}) {
 9081:             my $intdom = &Apache::lonnet::internet_dom($id);
 9082:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 9083:                 push(@{$intdoms},$intdom);
 9084:             }
 9085:         }
 9086:     }
 9087:     foreach my $ip (keys(%iphost)) {
 9088:         if (ref($iphost{$ip}) eq 'ARRAY') {
 9089:             foreach my $id (@{$iphost{$ip}}) {
 9090:                 my $location = &Apache::lonnet::internet_dom($id);
 9091:                 if ($location) {
 9092:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 9093:                         my $dom = &Apache::lonnet::host_domain($id);
 9094:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 9095:                             push(@{$instdoms},$dom);
 9096:                         }
 9097:                         next;
 9098:                     }
 9099:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9100:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 9101:                             push(@{$by_ip->{$ip}},$location);
 9102:                         }
 9103:                     } else {
 9104:                         $by_ip->{$ip} = [$location];
 9105:                     }
 9106:                 }
 9107:             }
 9108:         }
 9109:     }
 9110:     foreach my $ip (sort(keys(%{$by_ip}))) {
 9111:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 9112:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 9113:             my $first = $by_ip->{$ip}->[0];
 9114:             if (ref($by_location->{$first}) eq 'ARRAY') {
 9115:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 9116:                     push(@{$by_location->{$first}},$ip);
 9117:                 }
 9118:             } else {
 9119:                 $by_location->{$first} = [$ip];
 9120:             }
 9121:         }
 9122:     }
 9123:     return;
 9124: }
 9125: 
 9126: sub current_offloads_to {
 9127:     my ($dom,$settings,$servers) = @_;
 9128:     my (%spareid,%otherdomconfigs);
 9129:     if (ref($servers) eq 'HASH') {
 9130:         foreach my $lonhost (sort(keys(%{$servers}))) {
 9131:             my $gotspares;
 9132:             if (ref($settings) eq 'HASH') {
 9133:                 if (ref($settings->{'spares'}) eq 'HASH') {
 9134:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 9135:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 9136:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 9137:                         $gotspares = 1;
 9138:                     }
 9139:                 }
 9140:             }
 9141:             unless ($gotspares) {
 9142:                 my $gotspares;
 9143:                 my $serverhomeID =
 9144:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 9145:                 my $serverhomedom =
 9146:                     &Apache::lonnet::host_domain($serverhomeID);
 9147:                 if ($serverhomedom ne $dom) {
 9148:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 9149:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9150:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9151:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9152:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9153:                                 $gotspares = 1;
 9154:                             }
 9155:                         }
 9156:                     } else {
 9157:                         $otherdomconfigs{$serverhomedom} =
 9158:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 9159:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 9160:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 9161:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 9162:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 9163:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 9164:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 9165:                                         $gotspares = 1;
 9166:                                     }
 9167:                                 }
 9168:                             }
 9169:                         }
 9170:                     }
 9171:                 }
 9172:             }
 9173:             unless ($gotspares) {
 9174:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9175:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9176:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9177:                } else {
 9178:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 9179:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 9180:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 9181:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 9182:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 9183:                     } else {
 9184:                         my %what = (
 9185:                              spareid => 1,
 9186:                         );
 9187:                         my ($result,$returnhash) = 
 9188:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 9189:                         if ($result eq 'ok') { 
 9190:                             if (ref($returnhash) eq 'HASH') {
 9191:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 9192:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 9193:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 9194:                                 }
 9195:                             }
 9196:                         }
 9197:                     }
 9198:                 }
 9199:             }
 9200:         }
 9201:     }
 9202:     return %spareid;
 9203: }
 9204: 
 9205: sub spares_row {
 9206:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 9207:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 9208:     my $css_class;
 9209:     my $numinrow = 4;
 9210:     my $itemcount = 1;
 9211:     my $datatable;
 9212:     my %typetitles = &sparestype_titles();
 9213:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9214:         foreach my $server (sort(keys(%{$servers}))) {
 9215:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 9216:             my ($othercontrol,$serverdom);
 9217:             if ($serverhome ne $server) {
 9218:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 9219:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9220:             } else {
 9221:                 $serverdom = &Apache::lonnet::host_domain($server);
 9222:                 if ($serverdom ne $dom) {
 9223:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 9224:                 }
 9225:             }
 9226:             next unless (ref($spareid->{$server}) eq 'HASH');
 9227:             my ($checkednow,$checkedoth);
 9228:             if (ref($curroffloadnow) eq 'HASH') {
 9229:                 if ($curroffloadnow->{$server}) {
 9230:                     $checkednow = ' checked="checked"';
 9231:                 }
 9232:             }
 9233:             if (ref($curroffloadoth) eq 'HASH') {
 9234:                 if ($curroffloadoth->{$server}) {
 9235:                     $checkedoth = ' checked="checked"';
 9236:                 }
 9237:             }
 9238:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 9239:             $datatable .= '<tr'.$css_class.'>
 9240:                            <td rowspan="2">
 9241:                             <span class="LC_nobreak">'.
 9242:                           &mt('[_1] when busy, offloads to:'
 9243:                               ,'<b>'.$server.'</b>').'</span><br />'.
 9244:                           '<span class="LC_nobreak">'."\n".
 9245:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 9246:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 9247:                           "\n";
 9248:             if ($other_insts) {
 9249:                 $datatable .= '<br />'.
 9250:                               '<span class="LC_nobreak">'."\n".
 9251:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 9252:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 9253:                           "\n";
 9254:             }
 9255:             my (%current,%canselect);
 9256:             my @choices = 
 9257:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 9258:             foreach my $type ('primary','default') {
 9259:                 if (ref($spareid->{$server}) eq 'HASH') {
 9260:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 9261:                         my @spares = @{$spareid->{$server}{$type}};
 9262:                         if (@spares > 0) {
 9263:                             if ($othercontrol) {
 9264:                                 $current{$type} = join(', ',@spares);
 9265:                             } else {
 9266:                                 $current{$type} .= '<table>';
 9267:                                 my $numspares = scalar(@spares);
 9268:                                 for (my $i=0;  $i<@spares; $i++) {
 9269:                                     my $rem = $i%($numinrow);
 9270:                                     if ($rem == 0) {
 9271:                                         if ($i > 0) {
 9272:                                             $current{$type} .= '</tr>';
 9273:                                         }
 9274:                                         $current{$type} .= '<tr>';
 9275:                                     }
 9276:                                     $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;'.
 9277:                                                        $spareid->{$server}{$type}[$i].
 9278:                                                        '</label></td>'."\n";
 9279:                                 }
 9280:                                 my $rem = @spares%($numinrow);
 9281:                                 my $colsleft = $numinrow - $rem;
 9282:                                 if ($colsleft > 1 ) {
 9283:                                     $current{$type} .= '<td colspan="'.$colsleft.
 9284:                                                        '" class="LC_left_item">'.
 9285:                                                        '&nbsp;</td>';
 9286:                                 } elsif ($colsleft == 1) {
 9287:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 9288:                                 }
 9289:                                 $current{$type} .= '</tr></table>';
 9290:                             }
 9291:                         }
 9292:                     }
 9293:                     if ($current{$type} eq '') {
 9294:                         $current{$type} = &mt('None specified');
 9295:                     }
 9296:                     if ($othercontrol) {
 9297:                         if ($type eq 'primary') {
 9298:                             $canselect{$type} = $othercontrol;
 9299:                         }
 9300:                     } else {
 9301:                         $canselect{$type} = 
 9302:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 9303:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 9304:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 9305:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 9306:                         if (@choices > 0) {
 9307:                             foreach my $lonhost (@choices) {
 9308:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 9309:                             }
 9310:                         }
 9311:                         $canselect{$type} .= '</select>'."\n";
 9312:                     }
 9313:                 } else {
 9314:                     $current{$type} = &mt('Could not be determined');
 9315:                     if ($type eq 'primary') {
 9316:                         $canselect{$type} =  $othercontrol;
 9317:                     }
 9318:                 }
 9319:                 if ($type eq 'default') {
 9320:                     $datatable .= '<tr'.$css_class.'>';
 9321:                 }
 9322:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 9323:                               '<td>'.$current{$type}.'</td>'."\n".
 9324:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 9325:             }
 9326:             $itemcount ++;
 9327:         }
 9328:     }
 9329:     $$rowtotal += $itemcount;
 9330:     return $datatable;
 9331: }
 9332: 
 9333: sub possible_newspares {
 9334:     my ($server,$currspares,$serverhomes,$altids) = @_;
 9335:     my $serverhostname = &Apache::lonnet::hostname($server);
 9336:     my %excluded;
 9337:     if ($serverhostname ne '') {
 9338:         %excluded = (
 9339:                        $serverhostname => 1,
 9340:                     );
 9341:     }
 9342:     if (ref($currspares) eq 'HASH') {
 9343:         foreach my $type (keys(%{$currspares})) {
 9344:             if (ref($currspares->{$type}) eq 'ARRAY') {
 9345:                 if (@{$currspares->{$type}} > 0) {
 9346:                     foreach my $curr (@{$currspares->{$type}}) {
 9347:                         my $hostname = &Apache::lonnet::hostname($curr);
 9348:                         $excluded{$hostname} = 1;
 9349:                     }
 9350:                 }
 9351:             }
 9352:         }
 9353:     }
 9354:     my @choices;
 9355:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 9356:         if (keys(%{$serverhomes}) > 1) {
 9357:             foreach my $name (sort(keys(%{$serverhomes}))) {
 9358:                 unless ($excluded{$name}) {
 9359:                     if (exists($altids->{$serverhomes->{$name}})) {
 9360:                         push(@choices,$altids->{$serverhomes->{$name}});
 9361:                     } else {
 9362:                         push(@choices,$serverhomes->{$name});
 9363:                     }
 9364:                 }
 9365:             }
 9366:         }
 9367:     }
 9368:     return sort(@choices);
 9369: }
 9370: 
 9371: sub print_loadbalancing {
 9372:     my ($dom,$settings,$rowtotal) = @_;
 9373:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9374:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9375:     my $numinrow = 1;
 9376:     my $datatable;
 9377:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 9378:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 9379:     if (ref($settings) eq 'HASH') {
 9380:         %existing = %{$settings};
 9381:     }
 9382:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 9383:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 9384:                                   \%currtargets,\%currrules,\%currcookies);
 9385:     } else {
 9386:         return;
 9387:     }
 9388:     my ($othertitle,$usertypes,$types) =
 9389:         &Apache::loncommon::sorted_inst_types($dom);
 9390:     my $rownum = 8;
 9391:     if (ref($types) eq 'ARRAY') {
 9392:         $rownum += scalar(@{$types});
 9393:     }
 9394:     my @css_class = ('LC_odd_row','LC_even_row');
 9395:     my $balnum = 0;
 9396:     my $islast;
 9397:     my (@toshow,$disabledtext);
 9398:     if (keys(%currbalancer) > 0) {
 9399:         @toshow = sort(keys(%currbalancer));
 9400:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 9401:             push(@toshow,'');
 9402:         }
 9403:     } else {
 9404:         @toshow = ('');
 9405:         $disabledtext = &mt('No existing load balancer');
 9406:     }
 9407:     foreach my $lonhost (@toshow) {
 9408:         if ($balnum == scalar(@toshow)-1) {
 9409:             $islast = 1;
 9410:         } else {
 9411:             $islast = 0;
 9412:         }
 9413:         my $cssidx = $balnum%2;
 9414:         my $targets_div_style = 'display: none';
 9415:         my $disabled_div_style = 'display: block';
 9416:         my $homedom_div_style = 'display: none';
 9417:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 9418:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9419:                       '<p>';
 9420:         if ($lonhost eq '') {
 9421:             $datatable .= '<span class="LC_nobreak">';
 9422:             if (keys(%currbalancer) > 0) {
 9423:                 $datatable .= &mt('Add balancer:');
 9424:             } else {
 9425:                 $datatable .= &mt('Enable balancer:');
 9426:             }
 9427:             $datatable .= '&nbsp;'.
 9428:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 9429:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 9430:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 9431:                           '<option value="" selected="selected">'.&mt('None').
 9432:                           '</option>'."\n";
 9433:             foreach my $server (sort(keys(%servers))) {
 9434:                 next if ($currbalancer{$server});
 9435:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 9436:             }
 9437:             $datatable .=
 9438:                 '</select>'."\n".
 9439:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 9440:         } else {
 9441:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 9442:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 9443:                            &mt('Stop balancing').'</label>'.
 9444:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 9445:             $targets_div_style = 'display: block';
 9446:             $disabled_div_style = 'display: none';
 9447:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 9448:                 $homedom_div_style = 'display: block';
 9449:             }
 9450:         }
 9451:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 9452:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 9453:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 9454:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 9455:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 9456:         my @sparestypes = ('primary','default');
 9457:         my %typetitles = &sparestype_titles();
 9458:         my %hostherechecked = (
 9459:                                   no => ' checked="checked"',
 9460:                               );
 9461:         my %balcookiechecked = (
 9462:                                   no => ' checked="checked"',
 9463:                                );
 9464:         foreach my $sparetype (@sparestypes) {
 9465:             my $targettable;
 9466:             for (my $i=0; $i<$numspares; $i++) {
 9467:                 my $checked;
 9468:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 9469:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9470:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9471:                             $checked = ' checked="checked"';
 9472:                         }
 9473:                     }
 9474:                 }
 9475:                 my ($chkboxval,$disabled);
 9476:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 9477:                     $chkboxval = $spares[$i];
 9478:                 }
 9479:                 if (exists($currbalancer{$spares[$i]})) {
 9480:                     $disabled = ' disabled="disabled"';
 9481:                 }
 9482:                 $targettable .=
 9483:                     '<td><span class="LC_nobreak"><label>'.
 9484:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9485:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9486:                     '</span></label></span></td>';
 9487:                 my $rem = $i%($numinrow);
 9488:                 if ($rem == 0) {
 9489:                     if (($i > 0) && ($i < $numspares-1)) {
 9490:                         $targettable .= '</tr>';
 9491:                     }
 9492:                     if ($i < $numspares-1) {
 9493:                         $targettable .= '<tr>';
 9494:                     }
 9495:                 }
 9496:             }
 9497:             if ($targettable ne '') {
 9498:                 my $rem = $numspares%($numinrow);
 9499:                 my $colsleft = $numinrow - $rem;
 9500:                 if ($colsleft > 1 ) {
 9501:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9502:                                     '&nbsp;</td>';
 9503:                 } elsif ($colsleft == 1) {
 9504:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9505:                 }
 9506:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9507:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9508:             }
 9509:             $hostherechecked{$sparetype} = '';
 9510:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9511:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9512:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9513:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9514:                         $hostherechecked{'no'} = '';
 9515:                     }
 9516:                 }
 9517:             }
 9518:         }
 9519:         if ($currcookies{$lonhost}) {
 9520:             %balcookiechecked = (
 9521:                                     yes => ' checked="checked"',
 9522:                                 );
 9523:         }
 9524:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9525:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9526:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9527:         foreach my $sparetype (@sparestypes) {
 9528:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9529:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9530:                           '</i></label><br />';
 9531:         }
 9532:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9533:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9534:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9535:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9536:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9537:                       '</div></td></tr>'.
 9538:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9539:                                            $othertitle,$usertypes,$types,\%servers,
 9540:                                            \%currbalancer,$lonhost,
 9541:                                            $targets_div_style,$homedom_div_style,
 9542:                                            $css_class[$cssidx],$balnum,$islast);
 9543:         $$rowtotal += $rownum;
 9544:         $balnum ++;
 9545:     }
 9546:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9547:     return $datatable;
 9548: }
 9549: 
 9550: sub get_loadbalancers_config {
 9551:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9552:     return unless ((ref($servers) eq 'HASH') &&
 9553:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9554:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9555:                    (ref($currcookies) eq 'HASH'));
 9556:     if (keys(%{$existing}) > 0) {
 9557:         my $oldlonhost;
 9558:         foreach my $key (sort(keys(%{$existing}))) {
 9559:             if ($key eq 'lonhost') {
 9560:                 $oldlonhost = $existing->{'lonhost'};
 9561:                 $currbalancer->{$oldlonhost} = 1;
 9562:             } elsif ($key eq 'targets') {
 9563:                 if ($oldlonhost) {
 9564:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9565:                 }
 9566:             } elsif ($key eq 'rules') {
 9567:                 if ($oldlonhost) {
 9568:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9569:                 }
 9570:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9571:                 $currbalancer->{$key} = 1;
 9572:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9573:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9574:                 if ($existing->{$key}{'cookie'}) {
 9575:                     $currcookies->{$key} = 1;
 9576:                 }
 9577:             }
 9578:         }
 9579:     } else {
 9580:         my ($balancerref,$targetsref) =
 9581:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9582:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9583:             foreach my $server (sort(keys(%{$balancerref}))) {
 9584:                 $currbalancer->{$server} = 1;
 9585:                 $currtargets->{$server} = $targetsref->{$server};
 9586:             }
 9587:         }
 9588:     }
 9589:     return;
 9590: }
 9591: 
 9592: sub loadbalancing_rules {
 9593:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9594:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9595:         $css_class,$balnum,$islast) = @_;
 9596:     my $output;
 9597:     my $num = 0;
 9598:     my ($alltypes,$othertypes,$titles) =
 9599:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9600:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9601:         foreach my $type (@{$alltypes}) {
 9602:             $num ++;
 9603:             my $current;
 9604:             if (ref($currrules) eq 'HASH') {
 9605:                 $current = $currrules->{$type};
 9606:             }
 9607:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9608:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9609:                     $current = '';
 9610:                 }
 9611:             }
 9612:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9613:                                              $servers,$currbalancer,$lonhost,$dom,
 9614:                                              $targets_div_style,$homedom_div_style,
 9615:                                              $css_class,$balnum,$num,$islast);
 9616:         }
 9617:     }
 9618:     return $output;
 9619: }
 9620: 
 9621: sub loadbalancing_titles {
 9622:     my ($dom,$intdom,$usertypes,$types) = @_;
 9623:     my %othertypes = (
 9624:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9625:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9626:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9627:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9628:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9629:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9630:                      );
 9631:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9632:     my @available;
 9633:     if (ref($types) eq 'ARRAY') {
 9634:         @available = @{$types};
 9635:     }
 9636:     unless (grep(/^default$/,@available)) {
 9637:         push(@available,'default');
 9638:     }
 9639:     unshift(@alltypes,@available);
 9640:     my %titles;
 9641:     foreach my $type (@alltypes) {
 9642:         if ($type =~ /^_LC_/) {
 9643:             $titles{$type} = $othertypes{$type};
 9644:         } elsif ($type eq 'default') {
 9645:             $titles{$type} = &mt('All users from [_1]',$dom);
 9646:             if (ref($types) eq 'ARRAY') {
 9647:                 if (@{$types} > 0) {
 9648:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9649:                 }
 9650:             }
 9651:         } elsif (ref($usertypes) eq 'HASH') {
 9652:             $titles{$type} = $usertypes->{$type};
 9653:         }
 9654:     }
 9655:     return (\@alltypes,\%othertypes,\%titles);
 9656: }
 9657: 
 9658: sub loadbalance_rule_row {
 9659:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9660:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9661:     my @rulenames;
 9662:     my %ruletitles = &offloadtype_text();
 9663:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9664:         @rulenames = ('balancer','offloadedto','specific');
 9665:     } else {
 9666:         @rulenames = ('default','homeserver');
 9667:         if ($type eq '_LC_external') {
 9668:             push(@rulenames,'externalbalancer');
 9669:         } else {
 9670:             push(@rulenames,'specific');
 9671:         }
 9672:         push(@rulenames,'none');
 9673:     }
 9674:     my $style = $targets_div_style;
 9675:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9676:         $style = $homedom_div_style;
 9677:     }
 9678:     my $space;
 9679:     if ($islast && $num == 1) {
 9680:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9681:     }
 9682:     my $output =
 9683:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9684:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9685:         '<td valaign="top">'.$space.
 9686:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9687:     for (my $i=0; $i<@rulenames; $i++) {
 9688:         my $rule = $rulenames[$i];
 9689:         my ($checked,$extra);
 9690:         if ($rulenames[$i] eq 'default') {
 9691:             $rule = '';
 9692:         }
 9693:         if ($rulenames[$i] eq 'specific') {
 9694:             if (ref($servers) eq 'HASH') {
 9695:                 my $default;
 9696:                 if (($current ne '') && (exists($servers->{$current}))) {
 9697:                     $checked = ' checked="checked"';
 9698:                 }
 9699:                 unless ($checked) {
 9700:                     $default = ' selected="selected"';
 9701:                 }
 9702:                 $extra =
 9703:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9704:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9705:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9706:                     '<option value=""'.$default.'></option>'."\n";
 9707:                 foreach my $server (sort(keys(%{$servers}))) {
 9708:                     if (ref($currbalancer) eq 'HASH') {
 9709:                         next if (exists($currbalancer->{$server}));
 9710:                     }
 9711:                     my $selected;
 9712:                     if ($server eq $current) {
 9713:                         $selected = ' selected="selected"';
 9714:                     }
 9715:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9716:                 }
 9717:                 $extra .= '</select>';
 9718:             }
 9719:         } elsif ($rule eq $current) {
 9720:             $checked = ' checked="checked"';
 9721:         }
 9722:         $output .= '<span class="LC_nobreak"><label>'.
 9723:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9724:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9725:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9726:                    ')"'.$checked.' />&nbsp;';
 9727:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9728:             $output .= $ruletitles{'particular'};
 9729:         } else {
 9730:             $output .= $ruletitles{$rulenames[$i]};
 9731:         }
 9732:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9733:     }
 9734:     $output .= '</div></td></tr>'."\n";
 9735:     return $output;
 9736: }
 9737: 
 9738: sub offloadtype_text {
 9739:     my %ruletitles = &Apache::lonlocal::texthash (
 9740:            'default'          => 'Offloads to default destinations',
 9741:            'homeserver'       => "Offloads to user's home server",
 9742:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9743:            'specific'         => 'Offloads to specific server',
 9744:            'none'             => 'No offload',
 9745:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9746:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9747:            'particular'       => 'Session hosted (after re-auth) on server:',
 9748:     );
 9749:     return %ruletitles;
 9750: }
 9751: 
 9752: sub sparestype_titles {
 9753:     my %typestitles = &Apache::lonlocal::texthash (
 9754:                           'primary' => 'primary',
 9755:                           'default' => 'default',
 9756:                       );
 9757:     return %typestitles;
 9758: }
 9759: 
 9760: sub contact_titles {
 9761:     my %titles = &Apache::lonlocal::texthash (
 9762:                    'supportemail'    => 'Support E-mail address',
 9763:                    'adminemail'      => 'Default Server Admin E-mail address',
 9764:                    'errormail'       => 'Error reports to be e-mailed to',
 9765:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9766:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9767:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9768:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9769:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9770:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9771:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9772:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9773:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9774:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9775:                    'errorweights'    => 'Weights used to compute error count',
 9776:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9777:                  );
 9778:     my %short_titles = &Apache::lonlocal::texthash (
 9779:                            adminemail   => 'Admin E-mail address',
 9780:                            supportemail => 'Support E-mail',
 9781:                        );   
 9782:     return (\%titles,\%short_titles);
 9783: }
 9784: 
 9785: sub helpform_fields {
 9786:     my %titles =  &Apache::lonlocal::texthash (
 9787:                        'username'   => 'Name',
 9788:                        'user'       => 'Username/domain',
 9789:                        'phone'      => 'Phone',
 9790:                        'cc'         => 'Cc e-mail',
 9791:                        'course'     => 'Course Details',
 9792:                        'section'    => 'Sections',
 9793:                        'screenshot' => 'File upload',
 9794:     );
 9795:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9796:     my %possoptions = (
 9797:                         username     => ['yes','no','req'],
 9798:                         phone        => ['yes','no','req'],
 9799:                         user         => ['yes','no'],
 9800:                         cc           => ['yes','no'],
 9801:                         course       => ['yes','no'],
 9802:                         section      => ['yes','no'],
 9803:                         screenshot   => ['yes','no'],
 9804:                       );
 9805:     my %fieldoptions = &Apache::lonlocal::texthash (
 9806:                          'yes'  => 'Optional',
 9807:                          'req'  => 'Required',
 9808:                          'no'   => "Not shown",
 9809:     );
 9810:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9811: }
 9812: 
 9813: sub tool_titles {
 9814:     my %titles = &Apache::lonlocal::texthash (
 9815:                      aboutme    => 'Personal web page',
 9816:                      blog       => 'Blog',
 9817:                      portfolio  => 'Portfolio',
 9818:                      portaccess => 'Share portfolio files',
 9819:                      timezone   => 'Can set time zone',
 9820:                      official   => 'Official courses (with institutional codes)',
 9821:                      unofficial => 'Unofficial courses',
 9822:                      community  => 'Communities',
 9823:                      textbook   => 'Textbook courses',
 9824:                      placement  => 'Placement tests',
 9825:                  );
 9826:     return %titles;
 9827: }
 9828: 
 9829: sub courserequest_titles {
 9830:     my %titles = &Apache::lonlocal::texthash (
 9831:                                    official   => 'Official',
 9832:                                    unofficial => 'Unofficial',
 9833:                                    community  => 'Communities',
 9834:                                    textbook   => 'Textbook',
 9835:                                    placement  => 'Placement tests',
 9836:                                    lti        => 'LTI Provider',
 9837:                                    norequest  => 'Not allowed',
 9838:                                    approval   => 'Approval by DC',
 9839:                                    validate   => 'With validation',
 9840:                                    autolimit  => 'Numerical limit',
 9841:                                    unlimited  => '(blank for unlimited)',
 9842:                  );
 9843:     return %titles;
 9844: }
 9845: 
 9846: sub authorrequest_titles {
 9847:     my %titles = &Apache::lonlocal::texthash (
 9848:                                    norequest  => 'Not allowed',
 9849:                                    approval   => 'Approval by Dom. Coord.',
 9850:                                    automatic  => 'Automatic approval',
 9851:                  );
 9852:     return %titles;
 9853: }
 9854: 
 9855: sub courserequest_conditions {
 9856:     my %conditions = &Apache::lonlocal::texthash (
 9857:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9858:        validate   => '(Processing of request subject to institutional validation).',
 9859:                  );
 9860:     return %conditions;
 9861: }
 9862: 
 9863: 
 9864: sub print_usercreation {
 9865:     my ($position,$dom,$settings,$rowtotal) = @_;
 9866:     my $numinrow = 4;
 9867:     my $datatable;
 9868:     if ($position eq 'top') {
 9869:         $$rowtotal ++;
 9870:         my $rowcount = 0;
 9871:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9872:         if (ref($rules) eq 'HASH') {
 9873:             if (keys(%{$rules}) > 0) {
 9874:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9875:                                                 $ruleorder,$numinrow,$rowcount);
 9876:                 $$rowtotal ++;
 9877:                 $rowcount ++;
 9878:             }
 9879:         }
 9880:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9881:         if (ref($idrules) eq 'HASH') {
 9882:             if (keys(%{$idrules}) > 0) {
 9883:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9884:                                                 $idruleorder,$numinrow,$rowcount);
 9885:                 $$rowtotal ++;
 9886:                 $rowcount ++;
 9887:             }
 9888:         }
 9889:         if ($rowcount == 0) {
 9890:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9891:             $$rowtotal ++;
 9892:             $rowcount ++;
 9893:         }
 9894:     } elsif ($position eq 'middle') {
 9895:         my @creators = ('author','course','requestcrs');
 9896:         my ($rules,$ruleorder) =
 9897:             &Apache::lonnet::inst_userrules($dom,'username');
 9898:         my %lt = &usercreation_types();
 9899:         my %checked;
 9900:         if (ref($settings) eq 'HASH') {
 9901:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9902:                 foreach my $item (@creators) {
 9903:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9904:                 }
 9905:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9906:                 foreach my $item (@creators) {
 9907:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9908:                         $checked{$item} = 'none';
 9909:                     }
 9910:                 }
 9911:             }
 9912:         }
 9913:         my $rownum = 0;
 9914:         foreach my $item (@creators) {
 9915:             $rownum ++;
 9916:             if ($checked{$item} eq '') {
 9917:                 $checked{$item} = 'any';
 9918:             }
 9919:             my $css_class;
 9920:             if ($rownum%2) {
 9921:                 $css_class = '';
 9922:             } else {
 9923:                 $css_class = ' class="LC_odd_row" ';
 9924:             }
 9925:             $datatable .= '<tr'.$css_class.'>'.
 9926:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9927:                          '</span></td><td style="text-align: right">';
 9928:             my @options = ('any');
 9929:             if (ref($rules) eq 'HASH') {
 9930:                 if (keys(%{$rules}) > 0) {
 9931:                     push(@options,('official','unofficial'));
 9932:                 }
 9933:             }
 9934:             push(@options,'none');
 9935:             foreach my $option (@options) {
 9936:                 my $type = 'radio';
 9937:                 my $check = ' ';
 9938:                 if ($checked{$item} eq $option) {
 9939:                     $check = ' checked="checked" ';
 9940:                 } 
 9941:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9942:                               '<input type="'.$type.'" name="can_createuser_'.
 9943:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9944:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9945:             }
 9946:             $datatable .= '</td></tr>';
 9947:         }
 9948:     } else {
 9949:         my @contexts = ('author','course','domain');
 9950:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9951:         my %checked;
 9952:         if (ref($settings) eq 'HASH') {
 9953:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9954:                 foreach my $item (@contexts) {
 9955:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9956:                         foreach my $auth (@authtypes) {
 9957:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9958:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9959:                             }
 9960:                         }
 9961:                     }
 9962:                 }
 9963:             }
 9964:         } else {
 9965:             foreach my $item (@contexts) {
 9966:                 foreach my $auth (@authtypes) {
 9967:                     $checked{$item}{$auth} = ' checked="checked" ';
 9968:                 }
 9969:             }
 9970:         }
 9971:         my %title = &context_names();
 9972:         my %authname = &authtype_names();
 9973:         my $rownum = 0;
 9974:         my $css_class; 
 9975:         foreach my $item (@contexts) {
 9976:             if ($rownum%2) {
 9977:                 $css_class = '';
 9978:             } else {
 9979:                 $css_class = ' class="LC_odd_row" ';
 9980:             }
 9981:             $datatable .=   '<tr'.$css_class.'>'.
 9982:                             '<td>'.$title{$item}.
 9983:                             '</td><td class="LC_left_item">'.
 9984:                             '<span class="LC_nobreak">';
 9985:             foreach my $auth (@authtypes) {
 9986:                 $datatable .= '<label>'. 
 9987:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9988:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9989:                               $authname{$auth}.'</label>&nbsp;';
 9990:             }
 9991:             $datatable .= '</span></td></tr>';
 9992:             $rownum ++;
 9993:         }
 9994:         $$rowtotal += $rownum;
 9995:     }
 9996:     return $datatable;
 9997: }
 9998: 
 9999: sub print_selfcreation {
10000:     my ($position,$dom,$settings,$rowtotal) = @_;
10001:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
10002:         $emaildomain,$datatable);
10003:     if (ref($settings) eq 'HASH') {
10004:         if (ref($settings->{'cancreate'}) eq 'HASH') {
10005:             $createsettings = $settings->{'cancreate'};
10006:             if (ref($createsettings) eq 'HASH') {
10007:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
10008:                     @selfcreate = @{$createsettings->{'selfcreate'}};
10009:                 } elsif ($createsettings->{'selfcreate'} ne '') {
10010:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
10011:                         @selfcreate = ('email','login','sso');
10012:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
10013:                         @selfcreate = ($createsettings->{'selfcreate'});
10014:                     }
10015:                 }
10016:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
10017:                     $processing = $createsettings->{'selfcreateprocessing'};
10018:                 }
10019:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
10020:                     $emailoptions = $createsettings->{'emailoptions'};
10021:                 }
10022:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
10023:                     $emailverified = $createsettings->{'emailverified'};
10024:                 }
10025:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
10026:                     $emaildomain = $createsettings->{'emaildomain'};
10027:                 }
10028:             }
10029:         }
10030:     }
10031:     my %radiohash;
10032:     my $numinrow = 4;
10033:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
10034:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10035:     if ($position eq 'top') {
10036:         my %choices = &Apache::lonlocal::texthash (
10037:                                                       cancreate_login      => 'Institutional Login',
10038:                                                       cancreate_sso        => 'Institutional Single Sign On',
10039:                                                   );
10040:         my @toggles = sort(keys(%choices));
10041:         my %defaultchecked = (
10042:                                'cancreate_login' => 'off',
10043:                                'cancreate_sso'   => 'off',
10044:                              );
10045:         my ($onclick,$itemcount);
10046:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10047:                                                      \%choices,$itemcount,$onclick);
10048:         $$rowtotal += $itemcount;
10049: 
10050:         if (ref($usertypes) eq 'HASH') {
10051:             if (keys(%{$usertypes}) > 0) {
10052:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
10053:                                              $dom,$numinrow,$othertitle,
10054:                                              'statustocreate',$rowtotal);
10055:                 $$rowtotal ++;
10056:             }
10057:         }
10058:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
10059:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10060:         $fieldtitles{'inststatus'} = &mt('Institutional status');
10061:         my $rem;
10062:         my $numperrow = 2;
10063:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
10064:         $datatable .= '<tr'.$css_class.'>'.
10065:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
10066:                      '<td class="LC_left_item">'."\n".
10067:                      '<table>'."\n";
10068:         for (my $i=0; $i<@fields; $i++) {
10069:             $rem = $i%($numperrow);
10070:             if ($rem == 0) {
10071:                 if ($i > 0) {
10072:                     $datatable .= '</tr>';
10073:                 }
10074:                 $datatable .= '<tr>';
10075:             }
10076:             my $currval;
10077:             if (ref($createsettings) eq 'HASH') {
10078:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
10079:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
10080:                 }
10081:             }
10082:             $datatable .= '<td class="LC_left_item">'.
10083:                           '<span class="LC_nobreak">'.
10084:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
10085:                           'value="'.$currval.'" size="10" />&nbsp;'.
10086:                           $fieldtitles{$fields[$i]}.'</span></td>';
10087:         }
10088:         my $colsleft = $numperrow - $rem;
10089:         if ($colsleft > 1 ) {
10090:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10091:                          '&nbsp;</td>';
10092:         } elsif ($colsleft == 1) {
10093:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
10094:         }
10095:         $datatable .= '</tr></table></td></tr>';
10096:         $$rowtotal ++;
10097:     } elsif ($position eq 'middle') {
10098:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
10099:         my @posstypes;
10100:         if (ref($types) eq 'ARRAY') {
10101:             @posstypes = @{$types};
10102:         }
10103:         unless (grep(/^default$/,@posstypes)) {
10104:             push(@posstypes,'default');
10105:         }
10106:         my %usertypeshash;
10107:         if (ref($usertypes) eq 'HASH') {
10108:             %usertypeshash = %{$usertypes};
10109:         }
10110:         $usertypeshash{'default'} = $othertitle;
10111:         foreach my $status (@posstypes) {
10112:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
10113:                                                    $numinrow,$$rowtotal,\%usertypeshash);
10114:             $$rowtotal ++;
10115:         }
10116:     } else {
10117:         my %choices = &Apache::lonlocal::texthash (
10118:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
10119:                                                   );
10120:         my @toggles = sort(keys(%choices));
10121:         my %defaultchecked = (
10122:                                'cancreate_email' => 'off',
10123:                              );
10124:         my $customclass = 'LC_selfcreate_email';
10125:         my $classprefix = 'LC_canmodify_emailusername_';
10126:         my $optionsprefix = 'LC_options_emailusername_';
10127:         my $display = 'none';
10128:         my $rowstyle = 'display:none';
10129:         if (grep(/^\Qemail\E$/,@selfcreate)) {
10130:             $display = 'block';
10131:             $rowstyle = 'display:table-row';
10132:         }
10133:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
10134:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
10135:                                                      \%choices,$$rowtotal,$onclick);
10136:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
10137:                                          $rowstyle);
10138:         $$rowtotal ++;
10139:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
10140:                                       $rowstyle);
10141:         $$rowtotal ++;
10142:         my (@ordered,@posstypes,%usertypeshash);
10143:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
10144:         my ($emailrules,$emailruleorder) =
10145:             &Apache::lonnet::inst_userrules($dom,'email');
10146:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
10147:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
10148:         if (ref($types) eq 'ARRAY') {
10149:             @posstypes = @{$types};
10150:         }
10151:         if (@posstypes) {
10152:             unless (grep(/^default$/,@posstypes)) {
10153:                 push(@posstypes,'default');
10154:             }
10155:             if (ref($usertypes) eq 'HASH') {
10156:                 %usertypeshash = %{$usertypes};
10157:             }
10158:             my $currassign;
10159:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
10160:                 $currassign = {
10161:                                   selfassign => $domdefaults{'inststatusguest'},
10162:                               };
10163:                 @ordered = @{$domdefaults{'inststatusguest'}};
10164:             } else {
10165:                 $currassign = { selfassign => [] };
10166:             }
10167:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
10168:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
10169:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
10170:                                          $numinrow,$othertitle,'selfassign',
10171:                                          $rowtotal,$onclicktypes,$customclass,
10172:                                          $rowstyle);
10173:             $$rowtotal ++;
10174:             $usertypeshash{'default'} = $othertitle;
10175:             foreach my $status (@posstypes) {
10176:                 my $css_class;
10177:                 if ($$rowtotal%2) {
10178:                     $css_class = 'LC_odd_row ';
10179:                 }
10180:                 $css_class .= $customclass;
10181:                 my $rowid = $optionsprefix.$status;
10182:                 my $hidden = 1;
10183:                 my $currstyle = 'display:none';
10184:                 if (grep(/^\Q$status\E$/,@ordered)) {
10185:                     $currstyle = $rowstyle;
10186:                     $hidden = 0;
10187:                 }
10188:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10189:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
10190:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
10191:                 unless ($hidden) {
10192:                     $$rowtotal ++;
10193:                 }
10194:             }
10195:         } else {
10196:             my $css_class;
10197:             if ($$rowtotal%2) {
10198:                 $css_class = 'LC_odd_row ';
10199:             }
10200:             $css_class .= $customclass;
10201:             $usertypeshash{'default'} = $othertitle;
10202:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
10203:                                          $emailrules,$emailruleorder,$settings,'default','',
10204:                                          $othertitle,$css_class,$rowstyle,$intdom);
10205:             $$rowtotal ++;
10206:         }
10207:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
10208:         $numinrow = 1;
10209:         if (@posstypes) {
10210:             foreach my $status (@posstypes) {
10211:                 my $rowid = $classprefix.$status;
10212:                 my $datarowstyle = 'display:none';
10213:                 if (grep(/^\Q$status\E$/,@ordered)) {
10214:                     $datarowstyle = $rowstyle;
10215:                 }
10216:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
10217:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10218:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
10219:                 unless ($datarowstyle eq 'display:none') {
10220:                     $$rowtotal ++;
10221:                 }
10222:             }
10223:         } else {
10224:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
10225:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
10226:                                                    $infotitles,'',$customclass,$rowstyle);
10227:         }
10228:     }
10229:     return $datatable;
10230: }
10231: 
10232: sub selfcreate_javascript {
10233:     return <<"ENDSCRIPT";
10234: 
10235: <script type="text/javascript">
10236: // <![CDATA[
10237: 
10238: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
10239:     var x = document.getElementsByClassName(target);
10240:     var insttypes = 0;
10241:     var insttypeRegExp = new RegExp(prefix);
10242:     if ((x.length != undefined) && (x.length > 0)) {
10243:         if (form.elements[radio].length != undefined) {
10244:             for (var i=0; i<form.elements[radio].length; i++) {
10245:                 if (form.elements[radio][i].checked) {
10246:                     if (form.elements[radio][i].value == 1) {
10247:                         for (var j=0; j<x.length; j++) {
10248:                             if (x[j].id == 'undefined') {
10249:                                 x[j].style.display = 'table-row';
10250:                             } else if (insttypeRegExp.test(x[j].id)) {
10251:                                 insttypes ++;
10252:                             } else {
10253:                                 x[j].style.display = 'table-row';
10254:                             }
10255:                         }
10256:                     } else {
10257:                         for (var j=0; j<x.length; j++) {
10258:                             x[j].style.display = 'none';
10259:                         }
10260:                     }
10261:                     break;
10262:                 }
10263:             }
10264:             if (insttypes > 0) {
10265:                 toggleDataRow(form,checkbox,target,altprefix);
10266:                 toggleDataRow(form,checkbox,target,prefix,1);
10267:             }
10268:         }
10269:     }
10270:     return;
10271: }
10272: 
10273: function toggleDataRow(form,checkbox,target,prefix,docount) {
10274:     if (form.elements[checkbox].length != undefined) {
10275:         var count = 0;
10276:         if (docount) {
10277:             for (var i=0; i<form.elements[checkbox].length; i++) {
10278:                 if (form.elements[checkbox][i].checked) {
10279:                     count ++;
10280:                 }
10281:             }
10282:         }
10283:         for (var i=0; i<form.elements[checkbox].length; i++) {
10284:             var type = form.elements[checkbox][i].value;
10285:             if (document.getElementById(prefix+type)) {
10286:                 if (form.elements[checkbox][i].checked) {
10287:                     document.getElementById(prefix+type).style.display = 'table-row';
10288:                     if (count % 2 == 1) {
10289:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
10290:                     } else {
10291:                         document.getElementById(prefix+type).className = target;
10292:                     }
10293:                     count ++;
10294:                 } else {
10295:                     document.getElementById(prefix+type).style.display = 'none';
10296:                 }
10297:             }
10298:         }
10299:     }
10300:     return;
10301: }
10302: 
10303: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
10304:     var caller = radio+'_'+status;
10305:     if (form.elements[caller].length != undefined) {
10306:         for (var i=0; i<form.elements[caller].length; i++) {
10307:             if (form.elements[caller][i].checked) {
10308:                 if (document.getElementById(altprefix+'_inst_'+status)) {
10309:                     var curr = form.elements[caller][i].value;
10310:                     if (prefix) {
10311:                         document.getElementById(prefix+'_'+status).style.display = 'none';
10312:                     }
10313:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
10314:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
10315:                     if (curr == 'custom') {
10316:                         if (prefix) {
10317:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
10318:                         }
10319:                     } else if (curr == 'inst') {
10320:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
10321:                     } else if (curr == 'noninst') {
10322:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
10323:                     }
10324:                     break;
10325:                 }
10326:             }
10327:         }
10328:     }
10329: }
10330: 
10331: // ]]>
10332: </script>
10333: 
10334: ENDSCRIPT
10335: }
10336: 
10337: sub noninst_users {
10338:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
10339:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_;
10340:     my $class = 'LC_left_item';
10341:     if ($css_class) {
10342:         $css_class = ' class="'.$css_class.'"';
10343:     }
10344:     if ($rowid) {
10345:         $rowid = ' id="'.$rowid.'"';
10346:     }
10347:     if ($rowstyle) {
10348:         $rowstyle = ' style="'.$rowstyle.'"';
10349:     }
10350:     my ($output,$description);
10351:     if ($type eq 'default') {
10352:         $description = &mt('Requests for: [_1]',$typetitle);
10353:     } else {
10354:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
10355:     }
10356:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
10357:               "<td>$description</td>\n".
10358:               '<td class="'.$class.'" colspan="2">'.
10359:               '<table><tr>';
10360:     my %headers = &Apache::lonlocal::texthash(
10361:               approve  => 'Processing',
10362:               email    => 'E-mail',
10363:               username => 'Username',
10364:     );
10365:     foreach my $item ('approve','email','username') {
10366:         $output .= '<th>'.$headers{$item}.'</th>';
10367:     }
10368:     $output .= '</tr><tr>';
10369:     foreach my $item ('approve','email','username') {
10370:         $output .= '<td style="vertical-align: top">';
10371:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
10372:         if ($item eq 'approve') {
10373:             %choices = &Apache::lonlocal::texthash (
10374:                                                      automatic => 'Automatically approved',
10375:                                                      approval  => 'Queued for approval',
10376:                                                    );
10377:             @options = ('automatic','approval');
10378:             $hashref = $processing;
10379:             $defoption = 'automatic';
10380:             $name = 'cancreate_emailprocess_'.$type;
10381:         } elsif ($item eq 'email') {
10382:             %choices = &Apache::lonlocal::texthash (
10383:                                                      any     => 'Any e-mail',
10384:                                                      inst    => 'Institutional only',
10385:                                                      noninst => 'Non-institutional only',
10386:                                                      custom  => 'Custom restrictions',
10387:                                                    );
10388:             @options = ('any','inst','noninst');
10389:             my $showcustom;
10390:             if (ref($emailrules) eq 'HASH') {
10391:                 if (keys(%{$emailrules}) > 0) {
10392:                     push(@options,'custom');
10393:                     $showcustom = 'cancreate_emailrule';
10394:                     if (ref($settings) eq 'HASH') {
10395:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
10396:                             foreach my $rule (@{$settings->{'email_rule'}}) {
10397:                                 if (exists($emailrules->{$rule})) {
10398:                                     $hascustom ++;
10399:                                 }
10400:                             }
10401:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
10402:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
10403:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
10404:                                     if (exists($emailrules->{$rule})) {
10405:                                         $hascustom ++;
10406:                                     }
10407:                                 }
10408:                             }
10409:                         }
10410:                     }
10411:                 }
10412:             }
10413:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
10414:                                                      "'cancreate_emaildomain','$type'".');"';
10415:             $hashref = $emailoptions;
10416:             $defoption = 'any';
10417:             $name = 'cancreate_emailoptions_'.$type;
10418:         } elsif ($item eq 'username') {
10419:             %choices = &Apache::lonlocal::texthash (
10420:                                                      all    => 'Same as e-mail',
10421:                                                      first  => 'Omit @domain',
10422:                                                      free   => 'Free to choose',
10423:                                                    );
10424:             @options = ('all','first','free');
10425:             $hashref = $emailverified;
10426:             $defoption = 'all';
10427:             $name = 'cancreate_usernameoptions_'.$type;
10428:         }
10429:         foreach my $option (@options) {
10430:             my $checked;
10431:             if (ref($hashref) eq 'HASH') {
10432:                 if ($type eq '') {
10433:                     if (!exists($hashref->{'default'})) {
10434:                         if ($option eq $defoption) {
10435:                             $checked = ' checked="checked"';
10436:                         }
10437:                     } else {
10438:                         if ($hashref->{'default'} eq $option) {
10439:                             $checked = ' checked="checked"';
10440:                         }
10441:                     }
10442:                 } else {
10443:                     if (!exists($hashref->{$type})) {
10444:                         if ($option eq $defoption) {
10445:                             $checked = ' checked="checked"';
10446:                         }
10447:                     } else {
10448:                         if ($hashref->{$type} eq $option) {
10449:                             $checked = ' checked="checked"';
10450:                         }
10451:                     }
10452:                 }
10453:             } elsif (($item eq 'email') && ($hascustom)) {
10454:                 if ($option eq 'custom') {
10455:                     $checked = ' checked="checked"';
10456:                 }
10457:             } elsif ($option eq $defoption) {
10458:                 $checked = ' checked="checked"';
10459:             }
10460:             $output .= '<span class="LC_nobreak"><label>'.
10461:                        '<input type="radio" name="'.$name.'"'.
10462:                        $checked.' value="'.$option.'"'.$onclick.' />'.
10463:                        $choices{$option}.'</label></span><br />';
10464:             if ($item eq 'email') {
10465:                 if ($option eq 'custom') {
10466:                     my $id = 'cancreate_emailrule_'.$type;
10467:                     my $display = 'none';
10468:                     if ($checked) {
10469:                         $display = 'inline';
10470:                     }
10471:                     my $numinrow = 2;
10472:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
10473:                                '<legend>'.&mt('Disallow').'</legend><table>'.
10474:                                &user_formats_row('email',$settings,$emailrules,
10475:                                                  $emailruleorder,$numinrow,'',$type);
10476:                               '</table></fieldset>';
10477:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
10478:                     my %text = &Apache::lonlocal::texthash (
10479:                                                              inst    => 'must end:',
10480:                                                              noninst => 'cannot end:',
10481:                                                            );
10482:                     my $value;
10483:                     if (ref($emaildomain) eq 'HASH') {
10484:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10485:                             $value = $emaildomain->{$type}->{$option};
10486:                         }
10487:                     }
10488:                     if ($value eq '') {
10489:                         $value = '@'.$intdom;
10490:                     }
10491:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10492:                     my $display = 'none';
10493:                     if ($checked) {
10494:                         $display = 'inline';
10495:                     }
10496:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10497:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10498:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10499:                                '</div>';
10500:                 }
10501:             }
10502:         }
10503:         $output .= '</td>'."\n";
10504:     }
10505:     $output .= "</tr></table></td></tr>\n";
10506:     return $output;
10507: }
10508: 
10509: sub captcha_choice {
10510:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10511:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10512:         $vertext,$currver);
10513:     my %lt = &captcha_phrases();
10514:     $keyentry = 'hidden';
10515:     my $colspan=2;
10516:     if ($context eq 'cancreate') {
10517:         $rowname = &mt('CAPTCHA validation');
10518:     } elsif ($context eq 'login') {
10519:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10520:     } elsif ($context eq 'passwords') {
10521:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10522:         $colspan=1;
10523:     }
10524:     if (ref($settings) eq 'HASH') {
10525:         if ($settings->{'captcha'}) {
10526:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10527:         } else {
10528:             $checked{'original'} = ' checked="checked"';
10529:         }
10530:         if ($settings->{'captcha'} eq 'recaptcha') {
10531:             $pubtext = $lt{'pub'};
10532:             $privtext = $lt{'priv'};
10533:             $keyentry = 'text';
10534:             $vertext = $lt{'ver'};
10535:             $currver = $settings->{'recaptchaversion'};
10536:             if ($currver ne '2') {
10537:                 $currver = 1;
10538:             }
10539:         }
10540:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10541:             $currpub = $settings->{'recaptchakeys'}{'public'};
10542:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10543:         }
10544:     } else {
10545:         $checked{'original'} = ' checked="checked"';
10546:     }
10547:     my $css_class;
10548:     if ($itemcount%2) {
10549:         $css_class = 'LC_odd_row';
10550:     }
10551:     if ($customcss) {
10552:         $css_class .= " $customcss";
10553:     }
10554:     $css_class =~ s/^\s+//;
10555:     if ($css_class) {
10556:         $css_class = ' class="'.$css_class.'"';
10557:     }
10558:     if ($rowstyle) {
10559:         $css_class .= ' style="'.$rowstyle.'"';
10560:     }
10561:     my $output = '<tr'.$css_class.'>'.
10562:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10563:                  '<table><tr><td>'."\n";
10564:     foreach my $option ('original','recaptcha','notused') {
10565:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10566:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10567:                    $lt{$option}.'</label></span>';
10568:         unless ($option eq 'notused') {
10569:             $output .= ('&nbsp;'x2)."\n";
10570:         }
10571:     }
10572: #
10573: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10574: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10575: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10576: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10577: #
10578:     $output .= '</td></tr>'."\n".
10579:                '<tr><td class="LC_zero_height">'."\n".
10580:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10581:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10582:                $currpub.'" size="40" /></span><br />'."\n".
10583:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10584:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10585:                $currpriv.'" size="40" /></span><br />'.
10586:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10587:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10588:                $currver.'" size="3" /></span><br />'.
10589:                '</td></tr></table>'."\n".
10590:                '</td></tr>';
10591:     return $output;
10592: }
10593: 
10594: sub user_formats_row {
10595:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10596:     my $output;
10597:     my %text = (
10598:                    'username' => 'new usernames',
10599:                    'id'       => 'IDs',
10600:                );
10601:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10602:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10603:         $output = '<tr '.$css_class.'>'.
10604:                   '<td><span class="LC_nobreak">'.
10605:                   &mt("Format rules to check for $text{$type}: ").
10606:                   '</td><td class="LC_left_item" colspan="2"><table>';
10607:     }
10608:     my $rem;
10609:     if (ref($ruleorder) eq 'ARRAY') {
10610:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10611:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10612:                 my $rem = $i%($numinrow);
10613:                 if ($rem == 0) {
10614:                     if ($i > 0) {
10615:                         $output .= '</tr>';
10616:                     }
10617:                     $output .= '<tr>';
10618:                 }
10619:                 my $check = ' ';
10620:                 if (ref($settings) eq 'HASH') {
10621:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10622:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10623:                             $check = ' checked="checked" ';
10624:                         }
10625:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10626:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10627:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10628:                                 $check = ' checked="checked" ';
10629:                             }
10630:                         }
10631:                     }
10632:                 }
10633:                 my $name = $type.'_rule';
10634:                 if ($type eq 'email') {
10635:                     $name .= '_'.$status;
10636:                 }
10637:                 $output .= '<td class="LC_left_item">'.
10638:                            '<span class="LC_nobreak"><label>'.
10639:                            '<input type="checkbox" name="'.$name.'" '.
10640:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10641:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10642:             }
10643:         }
10644:         $rem = @{$ruleorder}%($numinrow);
10645:     }
10646:     my $colsleft;
10647:     if ($rem) {
10648:         $colsleft = $numinrow - $rem;
10649:     }
10650:     if ($colsleft > 1 ) {
10651:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10652:                    '&nbsp;</td>';
10653:     } elsif ($colsleft == 1) {
10654:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10655:     }
10656:     $output .= '</tr>';
10657:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10658:         $output .= '</table></td></tr>';
10659:     }
10660:     return $output;
10661: }
10662: 
10663: sub usercreation_types {
10664:     my %lt = &Apache::lonlocal::texthash (
10665:                     author     => 'When adding a co-author',
10666:                     course     => 'When adding a user to a course',
10667:                     requestcrs => 'When requesting a course',
10668:                     any        => 'Any',
10669:                     official   => 'Institutional only ',
10670:                     unofficial => 'Non-institutional only',
10671:                     none       => 'None',
10672:     );
10673:     return %lt;
10674: }
10675: 
10676: sub selfcreation_types {
10677:     my %lt = &Apache::lonlocal::texthash (
10678:                     selfcreate => 'User creates own account',
10679:                     any        => 'Any',
10680:                     official   => 'Institutional only ',
10681:                     unofficial => 'Non-institutional only',
10682:                     email      => 'E-mail address',
10683:                     login      => 'Institutional Login',
10684:                     sso        => 'SSO',
10685:              );
10686: }
10687: 
10688: sub authtype_names {
10689:     my %lt = &Apache::lonlocal::texthash(
10690:                       int    => 'Internal',
10691:                       krb4   => 'Kerberos 4',
10692:                       krb5   => 'Kerberos 5',
10693:                       loc    => 'Local',
10694:                       lti    => 'LTI',
10695:                   );
10696:     return %lt;
10697: }
10698: 
10699: sub context_names {
10700:     my %context_title = &Apache::lonlocal::texthash(
10701:        author => 'Creating users when an Author',
10702:        course => 'Creating users when in a course',
10703:        domain => 'Creating users when a Domain Coordinator',
10704:     );
10705:     return %context_title;
10706: }
10707: 
10708: sub print_usermodification {
10709:     my ($position,$dom,$settings,$rowtotal) = @_;
10710:     my $numinrow = 4;
10711:     my ($context,$datatable,$rowcount);
10712:     if ($position eq 'top') {
10713:         $rowcount = 0;
10714:         $context = 'author'; 
10715:         foreach my $role ('ca','aa') {
10716:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10717:                                                    $numinrow,$rowcount);
10718:             $$rowtotal ++;
10719:             $rowcount ++;
10720:         }
10721:     } elsif ($position eq 'middle') {
10722:         $rowcount = 0;
10723:         $context = 'coauthor';
10724:         foreach my $role ('ca','aa') {
10725:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10726:                                                    $numinrow,$rowcount);
10727:             $$rowtotal ++;
10728:             $rowcount ++;
10729:         }
10730:     } elsif ($position eq 'bottom') {
10731:         $context = 'course';
10732:         $rowcount = 0;
10733:         foreach my $role ('st','ep','ta','in','cr') {
10734:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10735:                                                    $numinrow,$rowcount);
10736:             $$rowtotal ++;
10737:             $rowcount ++;
10738:         }
10739:     }
10740:     return $datatable;
10741: }
10742: 
10743: sub print_defaults {
10744:     my ($position,$dom,$settings,$rowtotal) = @_;
10745:     my $rownum = 0;
10746:     my ($datatable,$css_class,$titles);
10747:     unless ($position eq 'bottom') {
10748:         $titles = &defaults_titles($dom);
10749:     }
10750:     if ($position eq 'top') {
10751:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10752:                      'datelocale_def','portal_def');
10753:         my %defaults;
10754:         if (ref($settings) eq 'HASH') {
10755:             %defaults = %{$settings};
10756:         } else {
10757:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10758:             foreach my $item (@items) {
10759:                 $defaults{$item} = $domdefaults{$item};
10760:             }
10761:         }
10762:         foreach my $item (@items) {
10763:             if ($rownum%2) {
10764:                 $css_class = '';
10765:             } else {
10766:                 $css_class = ' class="LC_odd_row" ';
10767:             }
10768:             $datatable .= '<tr'.$css_class.'>'.
10769:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10770:                           '</span></td><td class="LC_right_item" colspan="3">';
10771:             if ($item eq 'auth_def') {
10772:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10773:                 my %shortauth = (
10774:                                  internal => 'int',
10775:                                  krb4 => 'krb4',
10776:                                  krb5 => 'krb5',
10777:                                  localauth  => 'loc',
10778:                                  lti => 'lti',
10779:                                 );
10780:                 my %authnames = &authtype_names();
10781:                 foreach my $auth (@authtypes) {
10782:                     my $checked = ' ';
10783:                     if ($defaults{$item} eq $auth) {
10784:                         $checked = ' checked="checked" ';
10785:                     }
10786:                     $datatable .= '<label><input type="radio" name="'.$item.
10787:                                   '" value="'.$auth.'"'.$checked.'/>'.
10788:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10789:                 }
10790:             } elsif ($item eq 'timezone_def') {
10791:                 my $includeempty = 1;
10792:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10793:             } elsif ($item eq 'datelocale_def') {
10794:                 my $includeempty = 1;
10795:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10796:             } elsif ($item eq 'lang_def') {
10797:                 my $includeempty = 1;
10798:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10799:             } elsif ($item eq 'portal_def') {
10800:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10801:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10802:                 my $portalsty = 'none';
10803:                 if ($defaults{$item}) {
10804:                     $portalsty = 'block';
10805:                 }
10806:                 foreach my $field ('email','web') {
10807:                     my $checkedoff = ' checked="checked"';
10808:                     my $checkedon;
10809:                     if ($defaults{$item.'_'.$field}) {
10810:                         $checkedon = $checkedoff;
10811:                         $checkedoff = '';
10812:                     }
10813:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10814:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10815:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10816:                               ('&nbsp;'x2).
10817:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10818:                               '</div>';
10819:                 }
10820:             } else {
10821:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10822:             }
10823:             $datatable .= '</td></tr>';
10824:             $rownum ++;
10825:         }
10826:     } elsif ($position eq 'middle') {
10827:         my %defaults;
10828:         if (ref($settings) eq 'HASH') {
10829:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10830:                 my $maxnum = @{$settings->{'inststatusorder'}};
10831:                 for (my $i=0; $i<$maxnum; $i++) {
10832:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10833:                     my $item = $settings->{'inststatusorder'}->[$i];
10834:                     my $title = $settings->{'inststatustypes'}->{$item};
10835:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10836:                     $datatable .= '<tr'.$css_class.'>'.
10837:                                   '<td><span class="LC_nobreak">'.
10838:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10839:                     for (my $k=0; $k<=$maxnum; $k++) {
10840:                         my $vpos = $k+1;
10841:                         my $selstr;
10842:                         if ($k == $i) {
10843:                             $selstr = ' selected="selected" ';
10844:                         }
10845:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10846:                     }
10847:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10848:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10849:                                   &mt('delete').'</span></td>'.
10850:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10851:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10852:                                   '</span></td></tr>';
10853:                 }
10854:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10855:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10856:                 $datatable .= '<tr '.$css_class.'>'.
10857:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10858:                 for (my $k=0; $k<=$maxnum; $k++) {
10859:                     my $vpos = $k+1;
10860:                     my $selstr;
10861:                     if ($k == $maxnum) {
10862:                         $selstr = ' selected="selected" ';
10863:                     }
10864:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10865:                 }
10866:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10867:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10868:                               '&nbsp;'.&mt('(new)').
10869:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10870:                               &mt('Name displayed').':'.
10871:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10872:                               '</tr>'."\n";
10873:                 $rownum ++;
10874:             }
10875:         }
10876:     } else {
10877:         my ($unamemaprules,$ruleorder) =
10878:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10879:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10880:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10881:             my $numinrow = 2;
10882:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10883:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10884:                                             $ruleorder,$numinrow).
10885:                           '</table></td></tr>';
10886:         }
10887:         if ($datatable eq '') {
10888:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10889:                           &mt('No rules set for domain in customized localenroll.pm').
10890:                           '</td></tr>';
10891:         }
10892:     }
10893:     $$rowtotal += $rownum;
10894:     return $datatable;
10895: }
10896: 
10897: sub get_languages_hash {
10898:     my %langchoices;
10899:     foreach my $id (&Apache::loncommon::languageids()) {
10900:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10901:         if ($code ne '') {
10902:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10903:         }
10904:     }
10905:     return %langchoices;
10906: }
10907: 
10908: sub defaults_titles {
10909:     my ($dom) = @_;
10910:     my %titles = &Apache::lonlocal::texthash (
10911:                    'auth_def'      => 'Default authentication type',
10912:                    'auth_arg_def'  => 'Default authentication argument',
10913:                    'lang_def'      => 'Default language',
10914:                    'timezone_def'  => 'Default timezone',
10915:                    'datelocale_def' => 'Default locale for dates',
10916:                    'portal_def'     => 'Portal/Default URL',
10917:                    'email'          => 'Email links use portal URL',
10918:                    'web'            => 'Public web links use portal URL',
10919:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10920:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10921:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10922:                  );
10923:     if ($dom) {
10924:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10925:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10926:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10927:         $protocol = 'http' if ($protocol ne 'https');
10928:         if ($uint_dom) {
10929:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10930:                                          $uint_dom);
10931:         }
10932:     }
10933:     return (\%titles);
10934: }
10935: 
10936: sub print_scantron {
10937:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10938:     if ($position eq 'top') {
10939:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10940:     } else {
10941:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10942:     }
10943: }
10944: 
10945: sub scantron_javascript {
10946:     return <<"ENDSCRIPT";
10947: 
10948: <script type="text/javascript">
10949: // <![CDATA[
10950: 
10951: function toggleScantron(form) {
10952:     var csvfieldset = new Array();
10953:     if (document.getElementById('scantroncsv_cols')) {
10954:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10955:     }
10956:     if (document.getElementById('scantroncsv_options')) {
10957:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10958:     }
10959:     if (csvfieldset.length) {
10960:         if (document.getElementById('scantronconfcsv')) {
10961:             var scantroncsv = document.getElementById('scantronconfcsv');
10962:             if (scantroncsv.checked) {
10963:                 for (var i=0; i<csvfieldset.length; i++) {
10964:                     csvfieldset[i].style.display = 'block';
10965:                 }
10966:             } else {
10967:                 for (var i=0; i<csvfieldset.length; i++) {
10968:                     csvfieldset[i].style.display = 'none';
10969:                 }
10970:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10971:                 if (csvselects.length) {
10972:                     for (var j=0; j<csvselects.length; j++) {
10973:                         csvselects[j].selectedIndex = 0;
10974:                     }
10975:                 }
10976:             }
10977:         }
10978:     }
10979:     return;
10980: }
10981: // ]]>
10982: </script>
10983: 
10984: ENDSCRIPT
10985: 
10986: }
10987: 
10988: sub print_scantronformat {
10989:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10990:     my $itemcount = 1;
10991:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10992:         %confhash);
10993:     my $switchserver = &check_switchserver($dom,$confname);
10994:     my %lt = &Apache::lonlocal::texthash (
10995:                 default => 'Default bubblesheet format file error',
10996:                 custom  => 'Custom bubblesheet format file error',
10997:              );
10998:     my %scantronfiles = (
10999:         default => 'default.tab',
11000:         custom => 'custom.tab',
11001:     );
11002:     foreach my $key (keys(%scantronfiles)) {
11003:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
11004:                               .$scantronfiles{$key};
11005:     }
11006:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
11007:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
11008:         if (!$switchserver) {
11009:             my $servadm = $r->dir_config('lonAdmEMail');
11010:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
11011:             if ($configuserok eq 'ok') {
11012:                 if ($author_ok eq 'ok') {
11013:                     my %legacyfile = (
11014:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
11015:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
11016:                     );
11017:                     my %md5chk;
11018:                     foreach my $type (keys(%legacyfile)) {
11019:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
11020:                         chomp($md5chk{$type});
11021:                     }
11022:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
11023:                         foreach my $type (keys(%legacyfile)) {
11024:                             ($scantronurls{$type},my $error) =
11025:                                 &legacy_scantronformat($r,$dom,$confname,
11026:                                                  $type,$legacyfile{$type},
11027:                                                  $scantronurls{$type},
11028:                                                  $scantronfiles{$type});
11029:                             if ($error ne '') {
11030:                                 $error{$type} = $error;
11031:                             }
11032:                         }
11033:                         if (keys(%error) == 0) {
11034:                             $is_custom = 1;
11035:                             $confhash{'scantron'}{'scantronformat'} =
11036:                                 $scantronurls{'custom'};
11037:                             my $putresult =
11038:                                 &Apache::lonnet::put_dom('configuration',
11039:                                                          \%confhash,$dom);
11040:                             if ($putresult ne 'ok') {
11041:                                 $error{'custom'} =
11042:                                     '<span class="LC_error">'.
11043:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11044:                             }
11045:                         }
11046:                     } else {
11047:                         ($scantronurls{'default'},my $error) =
11048:                             &legacy_scantronformat($r,$dom,$confname,
11049:                                           'default',$legacyfile{'default'},
11050:                                           $scantronurls{'default'},
11051:                                           $scantronfiles{'default'});
11052:                         if ($error eq '') {
11053:                             $confhash{'scantron'}{'scantronformat'} = ''; 
11054:                             my $putresult =
11055:                                 &Apache::lonnet::put_dom('configuration',
11056:                                                          \%confhash,$dom);
11057:                             if ($putresult ne 'ok') {
11058:                                 $error{'default'} =
11059:                                     '<span class="LC_error">'.
11060:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
11061:                             }
11062:                         } else {
11063:                             $error{'default'} = $error;
11064:                         }
11065:                     }
11066:                 }
11067:             }
11068:         } else {
11069:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
11070:         }
11071:     }
11072:     if (ref($settings) eq 'HASH') {
11073:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
11074:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
11075:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
11076:                 $scantronurl = '';
11077:             } else {
11078:                 $scantronurl = $settings->{'scantronformat'};
11079:             }
11080:             $is_custom = 1;
11081:         } else {
11082:             $scantronurl = $scantronurls{'default'};
11083:         }
11084:     } else {
11085:         if ($is_custom) {
11086:             $scantronurl = $scantronurls{'custom'};
11087:         } else {
11088:             $scantronurl = $scantronurls{'default'};
11089:         }
11090:     }
11091:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11092:     $datatable .= '<tr'.$css_class.'>';
11093:     if (!$is_custom) {
11094:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
11095:                       '<span class="LC_nobreak">';
11096:         if ($scantronurl) {
11097:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
11098:                                                          undef,undef,undef,undef,'background-color:#ffffff');
11099:         } else {
11100:             $datatable = &mt('File unavailable for display');
11101:         }
11102:         $datatable .= '</span></td>';
11103:         if (keys(%error) == 0) { 
11104:             $datatable .= '<td style="vertical-align: bottom">';
11105:             if (!$switchserver) {
11106:                 $datatable .= &mt('Upload:').'<br />';
11107:             }
11108:         } else {
11109:             my $errorstr;
11110:             foreach my $key (sort(keys(%error))) {
11111:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11112:             }
11113:             $datatable .= '<td>'.$errorstr;
11114:         }
11115:     } else {
11116:         if (keys(%error) > 0) {
11117:             my $errorstr;
11118:             foreach my $key (sort(keys(%error))) {
11119:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
11120:             } 
11121:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
11122:         } elsif ($scantronurl) {
11123:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
11124:                                                        undef,undef,undef,undef,'background-color:#ffffff');
11125:             $datatable .= '<td><span class="LC_nobreak">'.
11126:                           $link.
11127:                           '<label><input type="checkbox" name="scantronformat_del"'.
11128:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
11129:                           '<td><span class="LC_nobreak">&nbsp;'.
11130:                           &mt('Replace:').'</span><br />';
11131:         }
11132:     }
11133:     if (keys(%error) == 0) {
11134:         if ($switchserver) {
11135:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
11136:         } else {
11137:             $datatable .='<span class="LC_nobreak">&nbsp;'.
11138:                          '<input type="file" name="scantronformat" /></span>';
11139:         }
11140:     }
11141:     $datatable .= '</td></tr>';
11142:     $$rowtotal ++;
11143:     return $datatable;
11144: }
11145: 
11146: sub legacy_scantronformat {
11147:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
11148:     my ($url,$error);
11149:     my @statinfo = &Apache::lonnet::stat_file($newurl);
11150:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
11151:         my $modified = [];
11152:         (my $result,$url) =
11153:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
11154:                                                     'scantron','','',$newfile,$modified);
11155:         if ($result eq 'ok') {
11156:             &update_modify_urls($r,$modified);
11157:         } else {
11158:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
11159:         }
11160:     }
11161:     return ($url,$error);
11162: }
11163: 
11164: sub print_scantronconfig {
11165:     my ($dom,$settings,$rowtotal) = @_;
11166:     my $itemcount = 2;
11167:     my $is_checked = ' checked="checked"';
11168:     my %optionson = (
11169:                      hdr => ' checked="checked"',
11170:                      pad => ' checked="checked"',
11171:                      rem => ' checked="checked"',
11172:                     );
11173:     my %optionsoff = (
11174:                       hdr => '',
11175:                       pad => '',
11176:                       rem => '',
11177:                      );
11178:     my $currcsvsty = 'none';
11179:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
11180:     my @fields = &scantroncsv_fields();
11181:     my %titles = &scantronconfig_titles();
11182:     if (ref($settings) eq 'HASH') {
11183:         if (ref($settings->{config}) eq 'HASH') {
11184:             if ($settings->{config}->{dat}) {
11185:                 $checked{'dat'} = $is_checked;
11186:             }
11187:             if (ref($settings->{config}->{csv}) eq 'HASH') {
11188:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
11189:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
11190:                     if (keys(%csvfields) > 0) {
11191:                         $checked{'csv'} = $is_checked;
11192:                         $currcsvsty = 'block';
11193:                     }
11194:                 }
11195:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
11196:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
11197:                     foreach my $option (keys(%optionson)) {
11198:                         unless ($csvoptions{$option}) {
11199:                             $optionsoff{$option} = $optionson{$option};
11200:                             $optionson{$option} = '';
11201:                         }
11202:                     }
11203:                 }
11204:             }
11205:         } else {
11206:             $checked{'dat'} = $is_checked;
11207:         }
11208:     } else {
11209:         $checked{'dat'} = $is_checked;
11210:     }
11211:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
11212:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11213:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
11214:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
11215:     foreach my $item ('dat','csv') {
11216:         my $id;
11217:         if ($item eq 'csv') {
11218:             $id = 'id="scantronconfcsv" ';
11219:         }
11220:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
11221:                       $titles{$item}.'</label>'.('&nbsp;'x3);
11222:         if ($item eq 'csv') {
11223:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
11224:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
11225:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
11226:             foreach my $col (@fields) {
11227:                 my $selnone;
11228:                 if ($csvfields{$col} eq '') {
11229:                     $selnone = ' selected="selected"';
11230:                 }
11231:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
11232:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
11233:                               '<option value=""'.$selnone.'></option>';
11234:                 for (my $i=0; $i<20; $i++) {
11235:                     my $shown = $i+1;
11236:                     my $sel;
11237:                     unless ($selnone) {
11238:                         if (exists($csvfields{$col})) {
11239:                             if ($csvfields{$col} == $i) {
11240:                                 $sel = ' selected="selected"';
11241:                             }
11242:                         }
11243:                     }
11244:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
11245:                 }
11246:                 $datatable .= '</select></td></tr>';
11247:            }
11248:            $datatable .= '</table></fieldset>'.
11249:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
11250:                          '<legend>'.&mt('CSV Options').'</legend>';
11251:            foreach my $option ('hdr','pad','rem') {
11252:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
11253:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
11254:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
11255:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
11256:            }
11257:            $datatable .= '</fieldset>';
11258:            $itemcount ++;
11259:         }
11260:     }
11261:     $datatable .= '</td></tr>';
11262:     $$rowtotal ++;
11263:     return $datatable;
11264: }
11265: 
11266: sub scantronconfig_titles {
11267:     return &Apache::lonlocal::texthash(
11268:                                           dat => 'Standard format (.dat)',
11269:                                           csv => 'Comma separated values (.csv)',
11270:                                           hdr => 'Remove first line in file (contains column titles)',
11271:                                           pad => 'Prepend 0s to PaperID',
11272:                                           rem => 'Remove leading spaces (except Question Response columns)',
11273:                                           CODE => 'CODE',
11274:                                           ID   => 'Student ID',
11275:                                           PaperID => 'Paper ID',
11276:                                           FirstName => 'First Name',
11277:                                           LastName => 'Last Name',
11278:                                           FirstQuestion => 'First Question Response',
11279:                                           Section => 'Section',
11280:     );
11281: }
11282: 
11283: sub scantroncsv_fields {
11284:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
11285: }
11286: 
11287: sub print_coursecategories {
11288:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
11289:     my $datatable;
11290:     if ($position eq 'top') {
11291:         my (%checked);
11292:         my @catitems = ('unauth','auth');
11293:         my @cattypes = ('std','domonly','codesrch','none');
11294:         $checked{'unauth'} = 'std';
11295:         $checked{'auth'} = 'std';
11296:         if (ref($settings) eq 'HASH') {
11297:             foreach my $type (@cattypes) {
11298:                 if ($type eq $settings->{'unauth'}) {
11299:                     $checked{'unauth'} = $type;
11300:                 }
11301:                 if ($type eq $settings->{'auth'}) {
11302:                     $checked{'auth'} = $type;
11303:                 }
11304:             }
11305:         }
11306:         my %lt = &Apache::lonlocal::texthash (
11307:                                                unauth   => 'Catalog type for unauthenticated users',
11308:                                                auth     => 'Catalog type for authenticated users',
11309:                                                none     => 'No catalog',
11310:                                                std      => 'Standard catalog',
11311:                                                domonly  => 'Domain-only catalog',
11312:                                                codesrch => "Code search form",
11313:                                              );
11314:        my $itemcount = 0;
11315:        foreach my $item (@catitems) {
11316:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
11317:            $datatable .= '<tr '.$css_class.'>'.
11318:                          '<td>'.$lt{$item}.'</td>'.
11319:                          '<td class="LC_right_item"><span class="LC_nobreak">';
11320:            foreach my $type (@cattypes) {
11321:                my $ischecked;
11322:                if ($checked{$item} eq $type) {
11323:                    $ischecked=' checked="checked"';
11324:                }
11325:                $datatable .= '<label>'.
11326:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
11327:                              ' />'.$lt{$type}.'</label>&nbsp;';
11328:            }
11329:            $datatable .= '</span></td></tr>';
11330:            $itemcount ++;
11331:         }
11332:         $$rowtotal += $itemcount;
11333:     } elsif ($position eq 'middle') {
11334:         my $toggle_cats_crs = ' ';
11335:         my $toggle_cats_dom = ' checked="checked" ';
11336:         my $can_cat_crs = ' ';
11337:         my $can_cat_dom = ' checked="checked" ';
11338:         my $toggle_catscomm_comm = ' ';
11339:         my $toggle_catscomm_dom = ' checked="checked" ';
11340:         my $can_catcomm_comm = ' ';
11341:         my $can_catcomm_dom = ' checked="checked" ';
11342:         my $toggle_catsplace_place = ' ';
11343:         my $toggle_catsplace_dom = ' checked="checked" ';
11344:         my $can_catplace_place = ' ';
11345:         my $can_catplace_dom = ' checked="checked" ';
11346: 
11347:         if (ref($settings) eq 'HASH') {
11348:             if ($settings->{'togglecats'} eq 'crs') {
11349:                 $toggle_cats_crs = $toggle_cats_dom;
11350:                 $toggle_cats_dom = ' ';
11351:             }
11352:             if ($settings->{'categorize'} eq 'crs') {
11353:                 $can_cat_crs = $can_cat_dom;
11354:                 $can_cat_dom = ' ';
11355:             }
11356:             if ($settings->{'togglecatscomm'} eq 'comm') {
11357:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
11358:                 $toggle_catscomm_dom = ' ';
11359:             }
11360:             if ($settings->{'categorizecomm'} eq 'comm') {
11361:                 $can_catcomm_comm = $can_catcomm_dom;
11362:                 $can_catcomm_dom = ' ';
11363:             }
11364:             if ($settings->{'togglecatsplace'} eq 'place') {
11365:                 $toggle_catsplace_place = $toggle_catsplace_dom;
11366:                 $toggle_catsplace_dom = ' ';
11367:             }
11368:             if ($settings->{'categorizeplace'} eq 'place') {
11369:                 $can_catplace_place = $can_catplace_dom;
11370:                 $can_catplace_dom = ' ';
11371:             }
11372:         }
11373:         my %title = &Apache::lonlocal::texthash (
11374:                      togglecats      => 'Show/Hide a course in catalog',
11375:                      togglecatscomm  => 'Show/Hide a community in catalog',
11376:                      togglecatsplace => 'Show/Hide a placement test in catalog',
11377:                      categorize      => 'Assign a category to a course',
11378:                      categorizecomm  => 'Assign a category to a community',
11379:                      categorizeplace => 'Assign a category to a placement test',
11380:                     );
11381:         my %level = &Apache::lonlocal::texthash (
11382:                      dom   => 'Set in Domain',
11383:                      crs   => 'Set in Course',
11384:                      comm  => 'Set in Community',
11385:                      place => 'Set in Placement Test',
11386:                     );
11387:         $datatable = '<tr class="LC_odd_row">'.
11388:                   '<td>'.$title{'togglecats'}.'</td>'.
11389:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11390:                   '<input type="radio" name="togglecats"'.
11391:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11392:                   '<label><input type="radio" name="togglecats"'.
11393:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
11394:                   '</tr><tr>'.
11395:                   '<td>'.$title{'categorize'}.'</td>'.
11396:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11397:                   '<label><input type="radio" name="categorize"'.
11398:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11399:                   '<label><input type="radio" name="categorize"'.
11400:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
11401:                   '</tr><tr class="LC_odd_row">'.
11402:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
11403:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11404:                   '<input type="radio" name="togglecatscomm"'.
11405:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11406:                   '<label><input type="radio" name="togglecatscomm"'.
11407:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
11408:                   '</tr><tr>'.
11409:                   '<td>'.$title{'categorizecomm'}.'</td>'.
11410:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11411:                   '<label><input type="radio" name="categorizecomm"'.
11412:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11413:                   '<label><input type="radio" name="categorizecomm"'.
11414:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
11415:                   '</tr><tr class="LC_odd_row">'.
11416:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
11417:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
11418:                   '<input type="radio" name="togglecatsplace"'.
11419:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11420:                   '<label><input type="radio" name="togglecatscomm"'.
11421:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
11422:                   '</tr><tr>'.
11423:                   '<td>'.$title{'categorizeplace'}.'</td>'.
11424:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
11425:                   '<label><input type="radio" name="categorizeplace"'.
11426:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
11427:                   '<label><input type="radio" name="categorizeplace"'.
11428:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
11429:                   '</tr>';
11430:         $$rowtotal += 6;
11431:     } else {
11432:         my $css_class;
11433:         my $itemcount = 1;
11434:         my $cathash; 
11435:         if (ref($settings) eq 'HASH') {
11436:             $cathash = $settings->{'cats'};
11437:         }
11438:         if (ref($cathash) eq 'HASH') {
11439:             my (@cats,@trails,%allitems,%idx,@jsarray);
11440:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
11441:                                                    \%allitems,\%idx,\@jsarray);
11442:             my $maxdepth = scalar(@cats);
11443:             my $colattrib = '';
11444:             if ($maxdepth > 2) {
11445:                 $colattrib = ' colspan="2" ';
11446:             }
11447:             my @path;
11448:             if (@cats > 0) {
11449:                 if (ref($cats[0]) eq 'ARRAY') {
11450:                     my $numtop = @{$cats[0]};
11451:                     my $maxnum = $numtop;
11452:                     my %default_names = (
11453:                           instcode    => &mt('Official courses'),
11454:                           communities => &mt('Communities'),
11455:                           placement   => &mt('Placement Tests'),
11456:                     );
11457: 
11458:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
11459:                         ($cathash->{'instcode::0'} eq '') ||
11460:                         (!grep(/^communities$/,@{$cats[0]})) || 
11461:                         ($cathash->{'communities::0'} eq '') ||
11462:                         (!grep(/^placement$/,@{$cats[0]})) ||
11463:                         ($cathash->{'placement::0'} eq '')) {
11464:                         $maxnum ++;
11465:                     }
11466:                     my $lastidx;
11467:                     for (my $i=0; $i<$numtop; $i++) {
11468:                         my $parent = $cats[0][$i];
11469:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11470:                         my $item = &escape($parent).'::0';
11471:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
11472:                         $lastidx = $idx{$item};
11473:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11474:                                       .'<select name="'.$item.'"'.$chgstr.'>';
11475:                         for (my $k=0; $k<=$maxnum; $k++) {
11476:                             my $vpos = $k+1;
11477:                             my $selstr;
11478:                             if ($k == $i) {
11479:                                 $selstr = ' selected="selected" ';
11480:                             }
11481:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11482:                         }
11483:                         $datatable .= '</select></span></td><td>';
11484:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
11485:                             $datatable .=  '<span class="LC_nobreak">'
11486:                                            .$default_names{$parent}.'</span>';
11487:                             if ($parent eq 'instcode') {
11488:                                 $datatable .= '<br /><span class="LC_nobreak">('
11489:                                               .&mt('with institutional codes')
11490:                                               .')</span></td><td'.$colattrib.'>';
11491:                             } else {
11492:                                 $datatable .= '<table><tr><td>';
11493:                             }
11494:                             $datatable .= '<span class="LC_nobreak">'
11495:                                           .'<label><input type="radio" name="'
11496:                                           .$parent.'" value="1" checked="checked" />'
11497:                                           .&mt('Display').'</label>';
11498:                             if ($parent eq 'instcode') {
11499:                                 $datatable .= '&nbsp;';
11500:                             } else {
11501:                                 $datatable .= '</span></td></tr><tr><td>'
11502:                                               .'<span class="LC_nobreak">';
11503:                             }
11504:                             $datatable .= '<label><input type="radio" name="'
11505:                                           .$parent.'" value="0" />'
11506:                                           .&mt('Do not display').'</label></span>';
11507:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11508:                                 $datatable .= '</td></tr></table>';
11509:                             }
11510:                             $datatable .= '</td>';
11511:                         } else {
11512:                             $datatable .= $parent
11513:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11514:                                           .'<input type="checkbox" name="deletecategory" '
11515:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11516:                         }
11517:                         my $depth = 1;
11518:                         push(@path,$parent);
11519:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11520:                         pop(@path);
11521:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11522:                         $itemcount ++;
11523:                     }
11524:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11525:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11526:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11527:                     for (my $k=0; $k<=$maxnum; $k++) {
11528:                         my $vpos = $k+1;
11529:                         my $selstr;
11530:                         if ($k == $numtop) {
11531:                             $selstr = ' selected="selected" ';
11532:                         }
11533:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11534:                     }
11535:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11536:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11537:                                   .'</tr>'."\n";
11538:                     $itemcount ++;
11539:                     foreach my $default ('instcode','communities','placement') {
11540:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11541:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11542:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11543:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11544:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11545:                             for (my $k=0; $k<=$maxnum; $k++) {
11546:                                 my $vpos = $k+1;
11547:                                 my $selstr;
11548:                                 if ($k == $maxnum) {
11549:                                     $selstr = ' selected="selected" ';
11550:                                 }
11551:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11552:                             }
11553:                             $datatable .= '</select></span></td>'.
11554:                                           '<td><span class="LC_nobreak">'.
11555:                                           $default_names{$default}.'</span>';
11556:                             if ($default eq 'instcode') {
11557:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11558:                                               .&mt('with institutional codes').')</span>';
11559:                             }
11560:                             $datatable .= '</td>'
11561:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11562:                                           .&mt('Display').'</label>&nbsp;'
11563:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11564:                                           .&mt('Do not display').'</label></span></td></tr>';
11565:                         }
11566:                     }
11567:                 }
11568:             } else {
11569:                 $datatable .= &initialize_categories($itemcount);
11570:             }
11571:         } else {
11572:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11573:                           .&initialize_categories($itemcount);
11574:         }
11575:         $$rowtotal += $itemcount;
11576:     }
11577:     return $datatable;
11578: }
11579: 
11580: sub print_serverstatuses {
11581:     my ($dom,$settings,$rowtotal) = @_;
11582:     my $datatable;
11583:     my @pages = &serverstatus_pages();
11584:     my (%namedaccess,%machineaccess);
11585:     foreach my $type (@pages) {
11586:         $namedaccess{$type} = '';
11587:         $machineaccess{$type}= '';
11588:     }
11589:     if (ref($settings) eq 'HASH') {
11590:         foreach my $type (@pages) {
11591:             if (exists($settings->{$type})) {
11592:                 if (ref($settings->{$type}) eq 'HASH') {
11593:                     foreach my $key (keys(%{$settings->{$type}})) {
11594:                         if ($key eq 'namedusers') {
11595:                             $namedaccess{$type} = $settings->{$type}->{$key};
11596:                         } elsif ($key eq 'machines') {
11597:                             $machineaccess{$type} = $settings->{$type}->{$key};
11598:                         }
11599:                     }
11600:                 }
11601:             }
11602:         }
11603:     }
11604:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11605:     my $rownum = 0;
11606:     my $css_class;
11607:     foreach my $type (@pages) {
11608:         $rownum ++;
11609:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11610:         $datatable .= '<tr'.$css_class.'>'.
11611:                       '<td><span class="LC_nobreak">'.
11612:                       $titles->{$type}.'</span></td>'.
11613:                       '<td class="LC_left_item">'.
11614:                       '<input type="text" name="'.$type.'_namedusers" '.
11615:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11616:                       '<td class="LC_right_item">'.
11617:                       '<span class="LC_nobreak">'.
11618:                       '<input type="text" name="'.$type.'_machines" '.
11619:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11620:                       '</span></td></tr>'."\n";
11621:     }
11622:     $$rowtotal += $rownum;
11623:     return $datatable;
11624: }
11625: 
11626: sub serverstatus_pages {
11627:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11628:             'checksums','clusterstatus','certstatus','metadata_keywords',
11629:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11630:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11631: }
11632: 
11633: sub defaults_javascript {
11634:     my ($settings) = @_;
11635:     return unless (ref($settings) eq 'HASH');
11636:     my $portal_js = <<"ENDPORTAL";
11637: 
11638: function portalExtras(caller) {
11639:     var x = caller.value;
11640:     var y = new Array('email','web');
11641:     for (var i=0; i<y.length; i++) {
11642:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11643:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11644:             if (x.length > 0) {
11645:                 z.style.display = 'block';
11646:             } else {
11647:                 z.style.display = 'none';
11648:             }
11649:         }
11650:     }
11651: }
11652: ENDPORTAL
11653:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11654:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11655:         if ($maxnum eq '') {
11656:             $maxnum = 0;
11657:         }
11658:         $maxnum ++;
11659:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11660:         return <<"ENDSCRIPT";
11661: <script type="text/javascript">
11662: // <![CDATA[
11663: function reorderTypes(form,caller) {
11664:     var changedVal;
11665: $jstext 
11666:     var newpos = 'addinststatus_pos';
11667:     var current = new Array;
11668:     var maxh = $maxnum;
11669:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11670:     var oldVal;
11671:     if (caller == newpos) {
11672:         changedVal = newitemVal;
11673:     } else {
11674:         var curritem = 'inststatus_pos_'+caller;
11675:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11676:         current[newitemVal] = newpos;
11677:     }
11678:     for (var i=0; i<inststatuses.length; i++) {
11679:         if (inststatuses[i] != caller) {
11680:             var elementName = 'inststatus_pos_'+inststatuses[i];
11681:             if (form.elements[elementName]) {
11682:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11683:                 current[currVal] = elementName;
11684:             }
11685:         }
11686:     }
11687:     for (var j=0; j<maxh; j++) {
11688:         if (current[j] == undefined) {
11689:             oldVal = j;
11690:         }
11691:     }
11692:     if (oldVal < changedVal) {
11693:         for (var k=oldVal+1; k<=changedVal ; k++) {
11694:            var elementName = current[k];
11695:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11696:         }
11697:     } else {
11698:         for (var k=changedVal; k<oldVal; k++) {
11699:             var elementName = current[k];
11700:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11701:         }
11702:     }
11703:     return;
11704: }
11705: 
11706: $portal_js
11707: 
11708: // ]]>
11709: </script>
11710: 
11711: ENDSCRIPT
11712:     } else {
11713: return <<"ENDSCRIPT";
11714: <script type="text/javascript">
11715: // <![CDATA[
11716: $portal_js
11717: // ]]>
11718: </script>
11719: 
11720: ENDSCRIPT
11721:     }
11722:     return;
11723: }
11724: 
11725: sub passwords_javascript {
11726:     my ($prefix) = @_;
11727:     my %intalert;
11728:     if ($prefix eq 'passwords') {
11729:         %intalert = &Apache::lonlocal::texthash (
11730:             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.',
11731:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11732:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11733:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11734:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11735:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11736:         );
11737:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11738:         %intalert = &Apache::lonlocal::texthash (
11739:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11740:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11741:         );
11742:     }
11743:     &js_escape(\%intalert);
11744:     my $defmin = $Apache::lonnet::passwdmin;
11745:     my $intauthjs;
11746:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11747: 
11748: function warnIntAuth(field) {
11749:     if (field.name == 'intauth_check') {
11750:         if (field.value == '2') {
11751:             alert('$intalert{authcheck}');
11752:         }
11753:     }
11754:     if (field.name == 'intauth_cost') {
11755:         field.value.replace(/\s/g,'');
11756:         if (field.value != '') {
11757:             var regexdigit=/^\\d+\$/;
11758:             if (!regexdigit.test(field.value)) {
11759:                 alert('$intalert{authcost}');
11760:             }
11761:         }
11762:     }
11763:     return;
11764: }
11765: 
11766: ENDSCRIPT
11767: 
11768:      }
11769: 
11770:      $intauthjs .= <<"ENDSCRIPT";
11771: 
11772: function warnInt$prefix(field) {
11773:     field.value.replace(/^\s+/,'');
11774:     field.value.replace(/\s+\$/,'');
11775:     var regexdigit=/^\\d+\$/;
11776:     if (field.name == '${prefix}_min') {
11777:         if (field.value == '') {
11778:             alert('$intalert{passmin}');
11779:             field.value = '$defmin';
11780:         } else {
11781:             if (!regexdigit.test(field.value)) {
11782:                 alert('$intalert{passmin}');
11783:                 field.value = '$defmin';
11784:             }
11785:             var minval = parseInt(field.value,10);
11786:             if (minval < $defmin) {
11787:                 alert('$intalert{passmin}');
11788:                 field.value = '$defmin';
11789:             }
11790:         }
11791:     } else {
11792:         if (field.value == '0') {
11793:             field.value = '';
11794:         }
11795:         if (field.value != '') {
11796:             if (field.name == '${prefix}_expire') {
11797:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11798:                 if (!regexpposnum.test(field.value)) {
11799:                     alert('$intalert{passexp}');
11800:                     field.value = '';
11801:                 } else {
11802:                     var expval = parseFloat(field.value);
11803:                     if (expval == 0) {
11804:                         alert('$intalert{passexp}');
11805:                         field.value = '';
11806:                     }
11807:                 }
11808:             } else {
11809:                 if (!regexdigit.test(field.value)) {
11810:                     if (field.name == '${prefix}_max') {
11811:                         alert('$intalert{passmax}');
11812:                     } else {
11813:                         if (field.name == '${prefix}_numsaved') {
11814:                             alert('$intalert{passnum}');
11815:                         }
11816:                     }
11817:                     field.value = '';
11818:                 }
11819:             }
11820:         }
11821:     }
11822:     return;
11823: }
11824: 
11825: ENDSCRIPT
11826:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11827: }
11828: 
11829: sub coursecategories_javascript {
11830:     my ($settings) = @_;
11831:     my ($output,$jstext,$cathash);
11832:     if (ref($settings) eq 'HASH') {
11833:         $cathash = $settings->{'cats'};
11834:     }
11835:     if (ref($cathash) eq 'HASH') {
11836:         my (@cats,@jsarray,%idx);
11837:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11838:         if (@jsarray > 0) {
11839:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11840:             for (my $i=0; $i<@jsarray; $i++) {
11841:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11842:                     my $catstr = join('","',@{$jsarray[$i]});
11843:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11844:                 }
11845:             }
11846:         }
11847:     } else {
11848:         $jstext  = '    var categories = Array(1);'."\n".
11849:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11850:     }
11851:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11852:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11853:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11854:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11855:     &js_escape(\$instcode_reserved);
11856:     &js_escape(\$communities_reserved);
11857:     &js_escape(\$placement_reserved);
11858:     &js_escape(\$choose_again);
11859:     $output = <<"ENDSCRIPT";
11860: <script type="text/javascript">
11861: // <![CDATA[
11862: function reorderCats(form,parent,item,idx) {
11863:     var changedVal;
11864: $jstext
11865:     var newpos = 'addcategory_pos';
11866:     if (parent == '') {
11867:         var has_instcode = 0;
11868:         var maxtop = categories[idx].length;
11869:         for (var j=0; j<maxtop; j++) {
11870:             if (categories[idx][j] == 'instcode::0') {
11871:                 has_instcode == 1;
11872:             }
11873:         }
11874:         if (has_instcode == 0) {
11875:             categories[idx][maxtop] = 'instcode_pos';
11876:         }
11877:     } else {
11878:         newpos += '_'+parent;
11879:     }
11880:     var maxh = 1 + categories[idx].length;
11881:     var current = new Array;
11882:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11883:     if (item == newpos) {
11884:         changedVal = newitemVal;
11885:     } else {
11886:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11887:         current[newitemVal] = newpos;
11888:     }
11889:     for (var i=0; i<categories[idx].length; i++) {
11890:         var elementName = categories[idx][i];
11891:         if (elementName != item) {
11892:             if (form.elements[elementName]) {
11893:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11894:                 current[currVal] = elementName;
11895:             }
11896:         }
11897:     }
11898:     var oldVal;
11899:     for (var j=0; j<maxh; j++) {
11900:         if (current[j] == undefined) {
11901:             oldVal = j;
11902:         }
11903:     }
11904:     if (oldVal < changedVal) {
11905:         for (var k=oldVal+1; k<=changedVal ; k++) {
11906:            var elementName = current[k];
11907:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11908:         }
11909:     } else {
11910:         for (var k=changedVal; k<oldVal; k++) {
11911:             var elementName = current[k];
11912:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11913:         }
11914:     }
11915:     return;
11916: }
11917: 
11918: function categoryCheck(form) {
11919:     if (form.elements['addcategory_name'].value == 'instcode') {
11920:         alert('$instcode_reserved\\n$choose_again');
11921:         return false;
11922:     }
11923:     if (form.elements['addcategory_name'].value == 'communities') {
11924:         alert('$communities_reserved\\n$choose_again');
11925:         return false;
11926:     }
11927:     if (form.elements['addcategory_name'].value == 'placement') {
11928:         alert('$placement_reserved\\n$choose_again');
11929:         return false;
11930:     }
11931:     return true;
11932: }
11933: 
11934: // ]]>
11935: </script>
11936: 
11937: ENDSCRIPT
11938:     return $output;
11939: }
11940: 
11941: sub initialize_categories {
11942:     my ($itemcount) = @_;
11943:     my ($datatable,$css_class,$chgstr);
11944:     my %default_names = &Apache::lonlocal::texthash (
11945:                       instcode    => 'Official courses (with institutional codes)',
11946:                       communities => 'Communities',
11947:                       placement   => 'Placement Tests',
11948:                         );
11949:     my %selnum = (
11950:                    instcode    => '0',
11951:                    communities => '1',
11952:                    placement   => '2',
11953:                  );
11954:     my %selected;
11955:     foreach my $default ('instcode','communities','placement') {
11956:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11957:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11958:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11959:         $selected{$selnum{$default}} = ' selected="selected"';
11960:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11961:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11962:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11963:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11964:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11965:                      .'<option value="3">4</option></select>&nbsp;'
11966:                      .$default_names{$default}
11967:                      .'</span></td><td><span class="LC_nobreak">'
11968:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11969:                      .&mt('Display').'</label>&nbsp;<label>'
11970:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11971:                  .'</label></span></td></tr>';
11972:         $itemcount ++;
11973:     }
11974:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11975:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11976:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11977:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11978:                   .'<option value="0">1</option>'
11979:                   .'<option value="1">2</option>'
11980:                   .'<option value="2">3</option>'
11981:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11982:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11983:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11984:                   .'</td></tr>';
11985:     return $datatable;
11986: }
11987: 
11988: sub build_category_rows {
11989:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11990:     my ($text,$name,$item,$chgstr);
11991:     if (ref($cats) eq 'ARRAY') {
11992:         my $maxdepth = scalar(@{$cats});
11993:         if (ref($cats->[$depth]) eq 'HASH') {
11994:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11995:                 my $numchildren = @{$cats->[$depth]{$parent}};
11996:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11997:                 $text .= '<td><table class="LC_data_table">';
11998:                 my ($idxnum,$parent_name,$parent_item);
11999:                 my $higher = $depth - 1;
12000:                 if ($higher == 0) {
12001:                     $parent_name = &escape($parent).'::'.$higher;
12002:                 } else {
12003:                     if (ref($path) eq 'ARRAY') {
12004:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12005:                     }
12006:                 }
12007:                 $parent_item = 'addcategory_pos_'.$parent_name;
12008:                 for (my $j=0; $j<=$numchildren; $j++) {
12009:                     if ($j < $numchildren) {
12010:                         $name = $cats->[$depth]{$parent}[$j];
12011:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
12012:                         $idxnum = $idx->{$item};
12013:                     } else {
12014:                         $name = $parent_name;
12015:                         $item = $parent_item;
12016:                     }
12017:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
12018:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
12019:                     for (my $i=0; $i<=$numchildren; $i++) {
12020:                         my $vpos = $i+1;
12021:                         my $selstr;
12022:                         if ($j == $i) {
12023:                             $selstr = ' selected="selected" ';
12024:                         }
12025:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
12026:                     }
12027:                     $text .= '</select>&nbsp;';
12028:                     if ($j < $numchildren) {
12029:                         my $deeper = $depth+1;
12030:                         $text .= $name.'&nbsp;'
12031:                                  .'<label><input type="checkbox" name="deletecategory" value="'
12032:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
12033:                         if(ref($path) eq 'ARRAY') {
12034:                             push(@{$path},$name);
12035:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
12036:                             pop(@{$path});
12037:                         }
12038:                     } else {
12039:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
12040:                         if ($j == $numchildren) {
12041:                             $text .= $name;
12042:                         } else {
12043:                             $text .= $item;
12044:                         }
12045:                         $text .= '" value="" />';
12046:                     }
12047:                     $text .= '</td></tr>';
12048:                 }
12049:                 $text .= '</table></td>';
12050:             } else {
12051:                 my $higher = $depth-1;
12052:                 if ($higher == 0) {
12053:                     $name = &escape($parent).'::'.$higher;
12054:                 } else {
12055:                     if (ref($path) eq 'ARRAY') {
12056:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
12057:                     }
12058:                 }
12059:                 my $colspan;
12060:                 if ($parent ne 'instcode') {
12061:                     $colspan = $maxdepth - $depth - 1;
12062:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
12063:                 }
12064:             }
12065:         }
12066:     }
12067:     return $text;
12068: }
12069: 
12070: sub modifiable_userdata_row {
12071:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
12072:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
12073:     my ($role,$rolename,$statustype);
12074:     $role = $item;
12075:     if ($context eq 'cancreate') {
12076:         if ($item =~ /^(emailusername)_(.+)$/) {
12077:             $role = $1;
12078:             $statustype = $2;
12079:             if (ref($usertypes) eq 'HASH') {
12080:                 if ($usertypes->{$statustype}) {
12081:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
12082:                 } else {
12083:                     $rolename = &mt('Data provided by user');
12084:                 }
12085:             }
12086:         }
12087:     } elsif ($context eq 'selfcreate') {
12088:         if (ref($usertypes) eq 'HASH') {
12089:             $rolename = $usertypes->{$role};
12090:         } else {
12091:             $rolename = $role;
12092:         }
12093:     } elsif ($context eq 'lti') {
12094:         $rolename = &mt('Institutional data used (if available)');
12095:     } elsif ($context eq 'privacy') {
12096:         $rolename = $itemdesc;
12097:     } else {
12098:         if ($role eq 'cr') {
12099:             $rolename = &mt('Custom role');
12100:         } else {
12101:             $rolename = &Apache::lonnet::plaintext($role);
12102:         }
12103:     }
12104:     my (@fields,%fieldtitles);
12105:     if (ref($fieldsref) eq 'ARRAY') {
12106:         @fields = @{$fieldsref};
12107:     } else {
12108:         @fields = ('lastname','firstname','middlename','generation',
12109:                    'permanentemail','id');
12110:     }
12111:     if ((ref($titlesref) eq 'HASH')) {
12112:         %fieldtitles = %{$titlesref};
12113:     } else {
12114:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12115:     }
12116:     my $output;
12117:     my $css_class;
12118:     if ($rowcount%2) {
12119:         $css_class = 'LC_odd_row';
12120:     }
12121:     if ($customcss) {
12122:         $css_class .= " $customcss";
12123:     }
12124:     $css_class =~ s/^\s+//;
12125:     if ($css_class) {
12126:         $css_class = ' class="'.$css_class.'"';
12127:     }
12128:     if ($rowstyle) {
12129:         $css_class .= ' style="'.$rowstyle.'"';
12130:     }
12131:     if ($rowid) {
12132:         $rowid = ' id="'.$rowid.'"';
12133:     }
12134:     $output = '<tr '.$css_class.$rowid.'>'.
12135:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
12136:               '<td class="LC_left_item" colspan="2"><table>';
12137:     my $rem;
12138:     my %checks;
12139:     if (ref($settings) eq 'HASH') {
12140:         my $hashref;
12141:         if ($context eq 'lti') {
12142:             if (ref($settings) eq 'HASH') {
12143:                 my %instdata;
12144:                 if (ref($settings->{'instdata'}) eq 'ARRAY') {
12145:                     map { $instdata{$_} = 1; } @{$settings->{'instdata'}};
12146:                 }
12147:                 $hashref = \%instdata;
12148:             }
12149:         } elsif ($context eq 'privacy') {
12150:             my ($key,$inner) = split(/_/,$role);
12151:             if (ref($settings) eq 'HASH') {
12152:                 if (ref($settings->{$key}) eq 'HASH') {
12153:                     $hashref = $settings->{$key}->{$inner};
12154:                 }
12155:             }
12156:         } elsif (ref($settings->{$context}) eq 'HASH') {
12157:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
12158:                 $hashref = $settings->{$context}->{$role};
12159:             }
12160:             if ($role eq 'emailusername') {
12161:                 if ($statustype) {
12162:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
12163:                         $hashref = $settings->{$context}->{$role}->{$statustype};
12164:                     }
12165:                 }
12166:             }
12167:         }
12168:         if (ref($hashref) eq 'HASH') {
12169:             foreach my $field (@fields) {
12170:                 if ($hashref->{$field}) {
12171:                     if ($role eq 'emailusername') {
12172:                         $checks{$field} = $hashref->{$field};
12173:                     } else {
12174:                         $checks{$field} = ' checked="checked" ';
12175:                     }
12176:                 }
12177:             }
12178:         }
12179:     }
12180:     my $total = scalar(@fields);
12181:     for (my $i=0; $i<$total; $i++) {
12182:         $rem = $i%($numinrow);
12183:         if ($rem == 0) {
12184:             if ($i > 0) {
12185:                 $output .= '</tr>';
12186:             }
12187:             $output .= '<tr>';
12188:         }
12189:         my $check = ' ';
12190:         unless ($role eq 'emailusername') {
12191:             if (exists($checks{$fields[$i]})) {
12192:                 $check = $checks{$fields[$i]};
12193:             } elsif ($context eq 'privacy') {
12194:                 if ($role =~ /^priv_(domain|course)$/) {
12195:                     if (ref($settings) ne 'HASH') {
12196:                         $check = ' checked="checked" ';
12197:                     }
12198:                 } elsif ($role =~ /^priv_(author|community)$/) {
12199:                     if (ref($settings) ne 'HASH') {
12200:                         unless ($fields[$i] eq 'id') {
12201:                             $check = ' checked="checked" ';
12202:                         }
12203:                     }
12204:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
12205:                     if (ref($settings) ne 'HASH') {
12206:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
12207:                             $check = ' checked="checked" ';
12208:                         }
12209:                     }
12210:                 }
12211:             } elsif ($context ne 'lti') {
12212:                 if ($role eq 'st') {
12213:                     if (ref($settings) ne 'HASH') {
12214:                         $check = ' checked="checked" '; 
12215:                     }
12216:                 }
12217:             }
12218:         }
12219:         $output .= '<td class="LC_left_item">'.
12220:                    '<span class="LC_nobreak">';
12221:         my $prefix = 'canmodify';
12222:         if ($role eq 'emailusername') {
12223:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
12224:                 $checks{$fields[$i]} = 'omit';
12225:             }
12226:             foreach my $option ('required','optional','omit') {
12227:                 my $checked='';
12228:                 if ($checks{$fields[$i]} eq $option) {
12229:                     $checked='checked="checked" ';
12230:                 }
12231:                 $output .= '<label>'.
12232:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
12233:                            &mt($option).'</label>'.('&nbsp;' x2);
12234:             }
12235:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
12236:         } else {
12237:             if ($context eq 'lti') {
12238:                 $prefix = 'lti';
12239:             } elsif ($context eq 'coauthor') {
12240:                 $prefix = 'cacanmodify';
12241:             } elsif ($context eq 'privacy') {
12242:                 $prefix = 'privacy';
12243:             }
12244:             $output .= '<label>'.
12245:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
12246:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
12247:                        '</label>';
12248:         }
12249:         $output .= '</span></td>';
12250:     }
12251:     $rem = $total%$numinrow;
12252:     my $colsleft;
12253:     if ($rem) {
12254:         $colsleft = $numinrow - $rem;
12255:     }
12256:     if ($colsleft > 1) {
12257:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
12258:                    '&nbsp;</td>';
12259:     } elsif ($colsleft == 1) {
12260:         $output .= '<td class="LC_left_item">&nbsp;</td>';
12261:     }
12262:     $output .= '</tr></table></td></tr>';
12263:     return $output;
12264: }
12265: 
12266: sub insttypes_row {
12267:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
12268:         $customcss,$rowstyle) = @_;
12269:     my %lt = &Apache::lonlocal::texthash (
12270:                       cansearch      => 'Users allowed to search',
12271:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
12272:                       lockablenames  => 'User preference to lock name',
12273:                       selfassign     => 'Self-reportable affiliations',
12274:                       overrides      => "Override domain's helpdesk settings based on requester's affiliation",
12275:                       webdav         => 'WebDAV access available',
12276:                       authorquota    => 'Authoring Space quota (MB)',
12277:              );
12278:     my ($showdom,$defaultquota);
12279:     if ($context eq 'cansearch') {
12280:         $showdom = ' ('.$dom.')';
12281:     } elsif ($context eq 'authorquota') {
12282:         $defaultquota = 500;
12283:     }
12284:     my $class = 'LC_left_item';
12285:     if ($context eq 'statustocreate') {
12286:         $class = 'LC_right_item';
12287:     }
12288:     my $css_class;
12289:     if ($$rowtotal%2) {
12290:         $css_class = 'LC_odd_row';
12291:     }
12292:     if ($customcss) {
12293:         $css_class .= ' '.$customcss;
12294:     }
12295:     $css_class =~ s/^\s+//;
12296:     if ($css_class) {
12297:         $css_class = ' class="'.$css_class.'"';
12298:     }
12299:     if ($rowstyle) {
12300:         $css_class .= ' style="'.$rowstyle.'"';
12301:     }
12302:     if ($onclick) {
12303:         $onclick = 'onclick="'.$onclick.'" ';
12304:     }
12305:     my $output = '<tr'.$css_class.'>'.
12306:                  '<td>'.$lt{$context}.$showdom.
12307:                  '</td><td class="'.$class.'" colspan="2"><table>';
12308:     my $rem;
12309:     if (ref($types) eq 'ARRAY') {
12310:         for (my $i=0; $i<@{$types}; $i++) {
12311:             if (defined($usertypes->{$types->[$i]})) {
12312:                 my $rem = $i%($numinrow);
12313:                 if ($rem == 0) {
12314:                     if ($i > 0) {
12315:                         $output .= '</tr>';
12316:                     }
12317:                     $output .= '<tr>';
12318:                 }
12319:                 if ($context eq 'authorquota') {
12320:                     my $currquota;
12321:                     if ($settings->{$context}->{$types->[$i]} =~ /^\d+$/) {
12322:                         $currquota = $settings->{$context}->{$types->[$i]};
12323:                     } else {
12324:                         $currquota = $defaultquota;
12325:                     }
12326:                     $output .= '<td class="LC_left_item">'."\n".
12327:                                '<label><span class="LC_nobreak">'."\n".
12328:                                $usertypes->{$types->[$i]}.'</span><br />'."\n".
12329:                                '<input type="text" name="'.$context.'_'.$types->[$i].'" '.
12330:                                'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12331:                                '</label></td>';
12332:                 } else {
12333:                     my $check = ' ';
12334:                     if (ref($settings) eq 'HASH') {
12335:                         if (ref($settings->{$context}) eq 'ARRAY') {
12336:                             if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
12337:                                 $check = ' checked="checked" ';
12338:                             }
12339:                         } elsif (ref($settings->{$context}) eq 'HASH') {
12340:                             if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
12341:                                 $check = ' checked="checked" ';
12342:                             } elsif ($context eq 'webdav') {
12343:                                 if ($settings->{$context}->{$types->[$i]}) {
12344:                                     $check = ' checked="checked" ';
12345:                                 }
12346:                             }
12347:                         } elsif ($context eq 'statustocreate') {
12348:                             $check = ' checked="checked" ';
12349:                         }
12350:                     }
12351:                     $output .= '<td class="LC_left_item">'.
12352:                                '<span class="LC_nobreak"><label>'.
12353:                                '<input type="checkbox" name="'.$context.'" '.
12354:                                'value="'.$types->[$i].'"'.$check.$onclick.'/>'.
12355:                                $usertypes->{$types->[$i]}.'</label></span></td>';
12356:                 }
12357:             }
12358:         }
12359:         $rem = @{$types}%($numinrow);
12360:     }
12361:     my $colsleft = $numinrow - $rem;
12362:     if ($context eq 'overrides') {
12363:         if ($colsleft > 1) {
12364:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12365:         } else {
12366:             $output .= '<td class="LC_left_item">';
12367:         }
12368:         $output .= '&nbsp;';
12369:     } else {
12370:         if ($rem == 0) {
12371:             $output .= '<tr>';
12372:         }
12373:         if ($colsleft > 1) {
12374:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
12375:         } else {
12376:             $output .= '<td class="LC_left_item">';
12377:         }
12378:         if ($context eq 'authorquota') {
12379:             my $currquota = 500;
12380:             if ((ref($settings) eq 'HASH') && (ref($settings->{$context}) eq 'HASH')) {
12381:                 if ($settings->{$context}{'default'} =~ /^\d+$/) {
12382:                     $currquota = $settings->{$context}{'default'};
12383:                 }
12384:             }
12385:             $output .= '<label><span class="LC_nobreak">'.$othertitle.'</span><br />'."\n".
12386:                        '<input type="text" name="'.$context.'_default" '.
12387:                        'value="'.$currquota.'" size="5"'.$onclick.'/>'."\n".
12388:                        '</label>';
12389:         } else {
12390:             my $defcheck = ' ';
12391:             if (ref($settings) eq 'HASH') {
12392:                 if (ref($settings->{$context}) eq 'ARRAY') {
12393:                     if (grep(/^default$/,@{$settings->{$context}})) {
12394:                         $defcheck = ' checked="checked" ';
12395:                     }
12396:                 } elsif (ref($settings->{$context}) eq 'HASH') {
12397:                     if (ref($settings->{$context}->{'default'}) eq 'HASH') {
12398:                         $defcheck = ' checked="checked" ';
12399:                     } elsif ($context eq 'webdav') {
12400:                         if ($settings->{$context}->{'default'}) {
12401:                             $defcheck = ' checked="checked" ';
12402:                         }
12403:                     }
12404:                 } elsif ($context eq 'statustocreate') {
12405:                     $defcheck = ' checked="checked" ';
12406:                 }
12407:             }
12408:             $output .= '<span class="LC_nobreak"><label>'.
12409:                        '<input type="checkbox" name="'.$context.'" '.
12410:                        'value="default"'.$defcheck.$onclick.'/>'.
12411:                        $othertitle.'</label></span>';
12412:         }
12413:     }
12414:     $output .= '</td></tr></table></td></tr>';
12415:     return $output;
12416: }
12417: 
12418: sub sorted_searchtitles {
12419:     my %searchtitles = &Apache::lonlocal::texthash(
12420:                          'uname' => 'username',
12421:                          'lastname' => 'last name',
12422:                          'lastfirst' => 'last name, first name',
12423:                      );
12424:     my @titleorder = ('uname','lastname','lastfirst');
12425:     return (\%searchtitles,\@titleorder);
12426: }
12427: 
12428: sub sorted_searchtypes {
12429:     my %srchtypes_desc = (
12430:                            exact    => 'is exact match',
12431:                            contains => 'contains ..',
12432:                            begins   => 'begins with ..',
12433:                          );
12434:     my @srchtypeorder = ('exact','begins','contains');
12435:     return (\%srchtypes_desc,\@srchtypeorder);
12436: }
12437: 
12438: sub usertype_update_row {
12439:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
12440:     my $datatable;
12441:     my $numinrow = 4;
12442:     foreach my $type (@{$types}) {
12443:         if (defined($usertypes->{$type})) {
12444:             $$rownums ++;
12445:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
12446:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
12447:                           '</td><td class="LC_left_item"><table>';
12448:             for (my $i=0; $i<@{$fields}; $i++) {
12449:                 my $rem = $i%($numinrow);
12450:                 if ($rem == 0) {
12451:                     if ($i > 0) {
12452:                         $datatable .= '</tr>';
12453:                     }
12454:                     $datatable .= '<tr>';
12455:                 }
12456:                 my $check = ' ';
12457:                 if (ref($settings) eq 'HASH') {
12458:                     if (ref($settings->{'fields'}) eq 'HASH') {
12459:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
12460:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
12461:                                 $check = ' checked="checked" ';
12462:                             }
12463:                         }
12464:                     }
12465:                 }
12466: 
12467:                 if ($i == @{$fields}-1) {
12468:                     my $colsleft = $numinrow - $rem;
12469:                     if ($colsleft > 1) {
12470:                         $datatable .= '<td colspan="'.$colsleft.'">';
12471:                     } else {
12472:                         $datatable .= '<td>';
12473:                     }
12474:                 } else {
12475:                     $datatable .= '<td>';
12476:                 }
12477:                 $datatable .= '<span class="LC_nobreak"><label>'.
12478:                               '<input type="checkbox" name="updateable_'.$type.
12479:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
12480:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
12481:             }
12482:             $datatable .= '</tr></table></td></tr>';
12483:         }
12484:     }
12485:     return $datatable;
12486: }
12487: 
12488: sub modify_login {
12489:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12490:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
12491:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
12492:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
12493:     %title = ( coursecatalog => 'Display course catalog',
12494:                adminmail => 'Display administrator E-mail address',
12495:                helpdesk  => 'Display "Contact Helpdesk" link',
12496:                newuser => 'Link for visitors to create a user account',
12497:                loginheader => 'Log-in box header',
12498:                saml => 'Dual SSO and non-SSO login');
12499:     @offon = ('off','on');
12500:     if (ref($domconfig{login}) eq 'HASH') {
12501:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
12502:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
12503:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
12504:             }
12505:         }
12506:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
12507:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
12508:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
12509:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
12510:                     $saml{$lonhost} = 1;
12511:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
12512:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
12513:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
12514:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
12515:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
12516:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
12517:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
12518:                 }
12519:             }
12520:         }
12521:     }
12522:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
12523:                                            \%domconfig,\%loginhash);
12524:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12525:     foreach my $item (@toggles) {
12526:         $loginhash{login}{$item} = $env{'form.'.$item};
12527:     }
12528:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
12529:     if (ref($colchanges{'login'}) eq 'HASH') {  
12530:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
12531:                                          \%loginhash);
12532:     }
12533: 
12534:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
12535:     my %domservers = &Apache::lonnet::get_servers($dom);
12536:     my @loginvia_attribs = ('serverpath','custompath','exempt');
12537:     if (keys(%servers) > 1) {
12538:         foreach my $lonhost (keys(%servers)) {
12539:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12540:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12541:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12542:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12543:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12544:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12545:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12546:                         $changes{'loginvia'}{$lonhost} = 1;
12547:                     } else {
12548:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12549:                         $changes{'loginvia'}{$lonhost} = 1;
12550:                     }
12551:                 } else {
12552:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12553:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12554:                         $changes{'loginvia'}{$lonhost} = 1;
12555:                     }
12556:                 }
12557:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12558:                     foreach my $item (@loginvia_attribs) {
12559:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12560:                     }
12561:                 } else {
12562:                     foreach my $item (@loginvia_attribs) {
12563:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12564:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12565:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12566:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12567:                                 $new = '/';
12568:                             }
12569:                         }
12570:                         if (($item eq 'custompath') && 
12571:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12572:                             $new = '';
12573:                         }
12574:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12575:                             $changes{'loginvia'}{$lonhost} = 1;
12576:                         }
12577:                         if ($item eq 'exempt') {
12578:                             $new = &check_exempt_addresses($new);
12579:                         }
12580:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12581:                     }
12582:                 }
12583:             } else {
12584:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12585:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12586:                     $changes{'loginvia'}{$lonhost} = 1;
12587:                     foreach my $item (@loginvia_attribs) {
12588:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12589:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12590:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12591:                                 $new = '/';
12592:                             }
12593:                         }
12594:                         if (($item eq 'custompath') && 
12595:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12596:                             $new = '';
12597:                         }
12598:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12599:                     }
12600:                 }
12601:             }
12602:         }
12603:     }
12604: 
12605:     my $servadm = $r->dir_config('lonAdmEMail');
12606:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12607:     if (ref($domconfig{'login'}) eq 'HASH') {
12608:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12609:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12610:                 if ($lang eq 'nolang') {
12611:                     push(@currlangs,$lang);
12612:                 } elsif (defined($langchoices{$lang})) {
12613:                     push(@currlangs,$lang);
12614:                 } else {
12615:                     next;
12616:                 }
12617:             }
12618:         }
12619:     }
12620:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12621:     if (@currlangs > 0) {
12622:         foreach my $lang (@currlangs) {
12623:             if (grep(/^\Q$lang\E$/,@delurls)) {
12624:                 $changes{'helpurl'}{$lang} = 1;
12625:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12626:                 $changes{'helpurl'}{$lang} = 1;
12627:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12628:                 push(@newlangs,$lang);
12629:             } else {
12630:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12631:             }
12632:         }
12633:     }
12634:     unless (grep(/^nolang$/,@currlangs)) {
12635:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12636:             $changes{'helpurl'}{'nolang'} = 1;
12637:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12638:             push(@newlangs,'nolang');
12639:         }
12640:     }
12641:     if ($env{'form.loginhelpurl_add_lang'}) {
12642:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12643:             ($env{'form.loginhelpurl_add_file.filename'})) {
12644:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12645:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12646:         }
12647:     }
12648:     if ((@newlangs > 0) || ($addedfile)) {
12649:         my $error;
12650:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12651:         if ($configuserok eq 'ok') {
12652:             if ($switchserver) {
12653:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12654:             } elsif ($author_ok eq 'ok') {
12655:                 my @allnew = @newlangs;
12656:                 if ($addedfile ne '') {
12657:                     push(@allnew,$addedfile);
12658:                 }
12659:                 my $modified = [];
12660:                 foreach my $lang (@allnew) {
12661:                     my $formelem = 'loginhelpurl_'.$lang;
12662:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12663:                         $formelem = 'loginhelpurl_add_file';
12664:                     }
12665:                     (my $result,$newurl{$lang}) =
12666:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12667:                                                                 "help/$lang",'','',$newfile{$lang},
12668:                                                                 $modified);
12669:                     if ($result eq 'ok') {
12670:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12671:                         $changes{'helpurl'}{$lang} = 1;
12672:                     } else {
12673:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12674:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12675:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12676:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12677:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12678:                         }
12679:                     }
12680:                 }
12681:                 &update_modify_urls($r,$modified);
12682:             } else {
12683:                 $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);
12684:             }
12685:         } else {
12686:             $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);
12687:         }
12688:         if ($error) {
12689:             &Apache::lonnet::logthis($error);
12690:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12691:         }
12692:     }
12693: 
12694:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12695:     if (ref($domconfig{'login'}) eq 'HASH') {
12696:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12697:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12698:                 if ($domservers{$lonhost}) {
12699:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12700:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12701:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12702:                     }
12703:                 }
12704:             }
12705:         }
12706:     }
12707:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12708:     foreach my $lonhost (sort(keys(%domservers))) {
12709:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12710:             $changes{'headtag'}{$lonhost} = 1;
12711:         } else {
12712:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12713:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12714:             }
12715:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12716:                 push(@newhosts,$lonhost);
12717:             } elsif ($currheadtagurls{$lonhost}) {
12718:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12719:                 if ($currexempt{$lonhost}) {
12720:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12721:                         $changes{'headtag'}{$lonhost} = 1;
12722:                     }
12723:                 } elsif ($possexempt{$lonhost}) {
12724:                     $changes{'headtag'}{$lonhost} = 1;
12725:                 }
12726:                 if ($possexempt{$lonhost}) {
12727:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12728:                 }
12729:             }
12730:         }
12731:     }
12732:     if (@newhosts) {
12733:         my $error;
12734:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12735:         if ($configuserok eq 'ok') {
12736:             if ($switchserver) {
12737:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12738:             } elsif ($author_ok eq 'ok') {
12739:                 my $modified = [];
12740:                 foreach my $lonhost (@newhosts) {
12741:                     my $formelem = 'loginheadtag_'.$lonhost;
12742:                     (my $result,$newheadtagurls{$lonhost}) =
12743:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12744:                                                                 "login/headtag/$lonhost",'','',
12745:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12746:                                                                 $modified);
12747:                     if ($result eq 'ok') {
12748:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12749:                         $changes{'headtag'}{$lonhost} = 1;
12750:                         if ($possexempt{$lonhost}) {
12751:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12752:                         }
12753:                     } else {
12754:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12755:                                            $newheadtagurls{$lonhost},$result);
12756:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12757:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12758:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12759:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12760:                         }
12761:                     }
12762:                 }
12763:                 &update_modify_urls($r,$modified);
12764:             } else {
12765:                 $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);
12766:             }
12767:         } else {
12768:             $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);
12769:         }
12770:         if ($error) {
12771:             &Apache::lonnet::logthis($error);
12772:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12773:         }
12774:     }
12775:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12776:     my @newsamlimgs;
12777:     foreach my $lonhost (keys(%domservers)) {
12778:         if ($env{'form.saml_'.$lonhost}) {
12779:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12780:                 push(@newsamlimgs,$lonhost);
12781:             }
12782:             foreach my $item ('text','alt','url','title','window','notsso') {
12783:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12784:             }
12785:             if ($saml{$lonhost}) {
12786:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12787:                     $env{'form.saml_window_'.$lonhost} = '';
12788:                 }
12789:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12790: #FIXME Need to obsolete published image
12791:                     delete($currsaml{$lonhost}{'img'});
12792:                     $changes{'saml'}{$lonhost} = 1;
12793:                 }
12794:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12795:                     $changes{'saml'}{$lonhost} = 1;
12796:                 }
12797:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12798:                     $changes{'saml'}{$lonhost} = 1;
12799:                 }
12800:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12801:                     $changes{'saml'}{$lonhost} = 1;
12802:                 }
12803:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12804:                     $changes{'saml'}{$lonhost} = 1;
12805:                 }
12806:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12807:                     $changes{'saml'}{$lonhost} = 1;
12808:                 }
12809:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12810:                     $changes{'saml'}{$lonhost} = 1;
12811:                 }
12812:             } else {
12813:                 $changes{'saml'}{$lonhost} = 1;
12814:             }
12815:             foreach my $item ('text','alt','url','title','window','notsso') {
12816:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12817:             }
12818:         } else {
12819:             if ($saml{$lonhost}) {
12820:                 $changes{'saml'}{$lonhost} = 1;
12821:                 delete($currsaml{$lonhost});
12822:             }
12823:         }
12824:     }
12825:     foreach my $posshost (keys(%currsaml)) {
12826:         unless (exists($domservers{$posshost})) {
12827:             delete($currsaml{$posshost});
12828:         }
12829:     }
12830:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12831:     if (@newsamlimgs) {
12832:         my $error;
12833:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12834:         if ($configuserok eq 'ok') {
12835:             if ($switchserver) {
12836:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12837:             } elsif ($author_ok eq 'ok') {
12838:                 my $modified = [];
12839:                 foreach my $lonhost (@newsamlimgs) {
12840:                     my $formelem = 'saml_img_'.$lonhost;
12841:                     my ($result,$imgurl) =
12842:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12843:                                                                 "login/saml/$lonhost",'','',
12844:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12845:                                                                 $modified);
12846:                     if ($result eq 'ok') {
12847:                         $currsaml{$lonhost}{'img'} = $imgurl;
12848:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12849:                         $changes{'saml'}{$lonhost} = 1;
12850:                     } else {
12851:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12852:                                            $lonhost,$result);
12853:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12854:                     }
12855:                 }
12856:                 &update_modify_urls($r,$modified);
12857:             } else {
12858:                 $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);
12859:             }
12860:         } else {
12861:             $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);
12862:         }
12863:         if ($error) {
12864:             &Apache::lonnet::logthis($error);
12865:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12866:         }
12867:     }
12868:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12869: 
12870:     my $defaulthelpfile = '/adm/loginproblems.html';
12871:     my $defaulttext = &mt('Default in use');
12872: 
12873:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12874:                                              $dom);
12875:     if ($putresult eq 'ok') {
12876:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12877:         my %defaultchecked = (
12878:                     'coursecatalog' => 'on',
12879:                     'helpdesk'      => 'on',
12880:                     'adminmail'     => 'off',
12881:                     'newuser'       => 'off',
12882:         );
12883:         if (ref($domconfig{'login'}) eq 'HASH') {
12884:             foreach my $item (@toggles) {
12885:                 if ($defaultchecked{$item} eq 'on') { 
12886:                     if (($domconfig{'login'}{$item} eq '0') &&
12887:                         ($env{'form.'.$item} eq '1')) {
12888:                         $changes{$item} = 1;
12889:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12890:                               $domconfig{'login'}{$item} eq '1') &&
12891:                              ($env{'form.'.$item} eq '0')) {
12892:                         $changes{$item} = 1;
12893:                     }
12894:                 } elsif ($defaultchecked{$item} eq 'off') {
12895:                     if (($domconfig{'login'}{$item} eq '1') &&
12896:                         ($env{'form.'.$item} eq '0')) {
12897:                         $changes{$item} = 1;
12898:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12899:                               $domconfig{'login'}{$item} eq '0') &&
12900:                              ($env{'form.'.$item} eq '1')) {
12901:                         $changes{$item} = 1;
12902:                     }
12903:                 }
12904:             }
12905:         }
12906:         if (keys(%changes) > 0 || $colchgtext) {
12907:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12908:             if (exists($changes{'saml'})) {
12909:                 my $hostid_in_use;
12910:                 my @hosts = &Apache::lonnet::current_machine_ids();
12911:                 if (@hosts > 1) {
12912:                     foreach my $hostid (@hosts) {
12913:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12914:                             $hostid_in_use = $hostid;
12915:                             last;
12916:                         }
12917:                     }
12918:                 } else {
12919:                     $hostid_in_use = $r->dir_config('lonHostID');
12920:                 }
12921:                 if (($hostid_in_use) &&
12922:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12923:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12924:                 }
12925:                 if (ref($lastactref) eq 'HASH') {
12926:                     if (ref($changes{'saml'}) eq 'HASH') {
12927:                         my %updates;
12928:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12929:                         $lastactref->{'samllanding'} = \%updates;
12930:                     }
12931:                 }
12932:             }
12933:             if (ref($lastactref) eq 'HASH') {
12934:                 $lastactref->{'domainconfig'} = 1;
12935:             }
12936:             $resulttext = &mt('Changes made:').'<ul>';
12937:             foreach my $item (sort(keys(%changes))) {
12938:                 if ($item eq 'loginvia') {
12939:                     if (ref($changes{$item}) eq 'HASH') {
12940:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12941:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12942:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12943:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12944:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12945:                                     $protocol = 'http' if ($protocol ne 'https');
12946:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12947: 
12948:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12949:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12950:                                     } else {
12951:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12952:                                     }
12953:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12954:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12955:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12956:                                     }
12957:                                     $resulttext .= '</li>';
12958:                                 } else {
12959:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12960:                                 }
12961:                             } else {
12962:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12963:                             }
12964:                         }
12965:                         $resulttext .= '</ul></li>';
12966:                     }
12967:                 } elsif ($item eq 'helpurl') {
12968:                     if (ref($changes{$item}) eq 'HASH') {
12969:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12970:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12971:                                 my ($chg,$link);
12972:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12973:                                 if ($lang eq 'nolang') {
12974:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12975:                                 } else {
12976:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12977:                                 }
12978:                                 $resulttext .= '<li>'.$chg.'</li>';
12979:                             } else {
12980:                                 my $chg;
12981:                                 if ($lang eq 'nolang') {
12982:                                     $chg = &mt('custom log-in help file for no preferred language');
12983:                                 } else {
12984:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12985:                                 }
12986:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12987:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12988:                                                       '?inhibitmenu=yes',$chg,600,500).
12989:                                                '</li>';
12990:                             }
12991:                         }
12992:                     }
12993:                 } elsif ($item eq 'headtag') {
12994:                     if (ref($changes{$item}) eq 'HASH') {
12995:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12996:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12997:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12998:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12999:                                 $resulttext .= '<li><a href="'.
13000:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
13001:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
13002:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
13003:                                 if ($possexempt{$lonhost}) {
13004:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
13005:                                 } else {
13006:                                     $resulttext .= &mt('included for any client IP');
13007:                                 }
13008:                                 $resulttext .= '</li>';
13009:                             }
13010:                         }
13011:                     }
13012:                 } elsif ($item eq 'saml') {
13013:                     if (ref($changes{$item}) eq 'HASH') {
13014:                         my %notlt = (
13015:                                        text   => 'Text for log-in by SSO',
13016:                                        img    => 'SSO button image',
13017:                                        alt    => 'Alt text for button image',
13018:                                        url    => 'SSO URL',
13019:                                        title  => 'Tooltip for SSO link',
13020:                                        window => 'Pop-up window if iframe',
13021:                                        notsso => 'Text for non-SSO log-in',
13022:                                     );
13023:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
13024:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
13025:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
13026:                                                '<ul>';
13027:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
13028:                                     if ($currsaml{$lonhost}{$key} eq '') {
13029:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
13030:                                     } else {
13031:                                         my $value = "'$currsaml{$lonhost}{$key}'";
13032:                                         if ($key eq 'img') {
13033:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
13034:                                         } elsif ($key eq 'window') {
13035:                                             $value = 'On';
13036:                                         }
13037:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
13038:                                                                   $value).'</li>';
13039:                                     }
13040:                                 }
13041:                                 $resulttext .= '</ul></li>';
13042:                             } else {
13043:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
13044:                             }
13045:                         }
13046:                     }
13047:                 } elsif ($item eq 'captcha') {
13048:                     if (ref($loginhash{'login'}) eq 'HASH') {
13049:                         my $chgtxt;
13050:                         if ($loginhash{'login'}{$item} eq 'notused') {
13051:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
13052:                         } else {
13053:                             my %captchas = &captcha_phrases();
13054:                             if ($captchas{$loginhash{'login'}{$item}}) {
13055:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
13056:                             } else {
13057:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
13058:                             }
13059:                         }
13060:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13061:                     }
13062:                 } elsif ($item eq 'recaptchakeys') {
13063:                     if (ref($loginhash{'login'}) eq 'HASH') {
13064:                         my ($privkey,$pubkey);
13065:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
13066:                             $pubkey = $loginhash{'login'}{$item}{'public'};
13067:                             $privkey = $loginhash{'login'}{$item}{'private'};
13068:                         }
13069:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
13070:                         if (!$pubkey) {
13071:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
13072:                         } else {
13073:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
13074:                         }
13075:                         if (!$privkey) {
13076:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
13077:                         } else {
13078:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
13079:                         }
13080:                         $chgtxt .= '</ul>';
13081:                         $resulttext .= '<li>'.$chgtxt.'</li>';
13082:                     }
13083:                 } elsif ($item eq 'recaptchaversion') {
13084:                     if (ref($loginhash{'login'}) eq 'HASH') {
13085:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
13086:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
13087:                                            '</li>';
13088:                         }
13089:                     }
13090:                 } else {
13091:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
13092:                 }
13093:             }
13094:             $resulttext .= $colchgtext.'</ul>';
13095:         } else {
13096:             $resulttext = &mt('No changes made to log-in page settings');
13097:         }
13098:     } else {
13099:         $resulttext = '<span class="LC_error">'.
13100: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13101:     }
13102:     if ($errors) {
13103:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13104:                        $errors.'</ul>';
13105:     }
13106:     return $resulttext;
13107: }
13108: 
13109: sub check_exempt_addresses {
13110:     my ($iplist) = @_;
13111:     $iplist =~ s/^\s+//;
13112:     $iplist =~ s/\s+$//;
13113:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
13114:     my (@okips,$new);
13115:     foreach my $ip (@poss_ips) {
13116:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
13117:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
13118:                 push(@okips,$ip);
13119:             }
13120:         }
13121:     }
13122:     if (@okips > 0) {
13123:         $new = join(',',@okips);
13124:     } else {
13125:         $new = '';
13126:     }
13127:     return $new;
13128: }
13129: 
13130: sub color_font_choices {
13131:     my %choices =
13132:         &Apache::lonlocal::texthash (
13133:             bgs => "Background colors",
13134:             links => "Link colors",
13135:             images => "Images",
13136:             font => "Font color",
13137:             fontmenu => "Font menu",
13138:             pgbg => "Page",
13139:             tabbg => "Header",
13140:             sidebg => "Border",
13141:             link => "Link",
13142:             alink => "Active link",
13143:             vlink => "Visited link",
13144:         );
13145:     return %choices;
13146: }
13147: 
13148: sub modify_ipaccess {
13149:     my ($dom,$lastactref,%domconfig) = @_;
13150:     my (@allpos,%changes,%confhash,$errors,$resulttext);
13151:     my (@items,%deletions,%itemids,@warnings);
13152:     my ($typeorder,$types) = &commblocktype_text();
13153:     if ($env{'form.ipaccess_add'}) {
13154:         my $name = $env{'form.ipaccess_name_add'};
13155:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
13156:         if ($newid) {
13157:             $itemids{'add'} = $newid;
13158:             push(@items,'add');
13159:             $changes{$newid} = 1;
13160:         } else {
13161:             $error = &mt('Failed to acquire unique ID for new IP access control item');
13162:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13163:         }
13164:     }
13165:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13166:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
13167:         if (@todelete) {
13168:             map { $deletions{$_} = 1; } @todelete;
13169:         }
13170:         my $maxnum = $env{'form.ipaccess_maxnum'};
13171:         for (my $i=0; $i<$maxnum; $i++) {
13172:             my $itemid = $env{'form.ipaccess_id_'.$i};
13173:             $itemid =~ s/\D+//g;
13174:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13175:                 if ($deletions{$itemid}) {
13176:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
13177:                 } else {
13178:                     push(@items,$i);
13179:                     $itemids{$i} = $itemid;
13180:                 }
13181:             }
13182:         }
13183:     }
13184:     foreach my $idx (@items) {
13185:         my $itemid = $itemids{$idx};
13186:         next unless ($itemid);
13187:         my ($position,%current);
13188:         if ($idx eq 'add') {
13189:             $position = $env{'form.ipaccess_pos_add'};
13190:         } else {
13191:             $position = $env{'form.ipaccess_pos_'.$itemid};
13192:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13193:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
13194:             }
13195:         }
13196:         $position =~ s/\D+//g;
13197:         if ($position ne '') {
13198:             $allpos[$position] = $itemid;
13199:         }
13200:         my $name = $env{'form.ipaccess_name_'.$idx};
13201:         $name =~ s/^\s+|\s+$//g;
13202:         $confhash{$itemid}{'name'} = $name;
13203:         my $possrange = $env{'form.ipaccess_range_'.$idx};
13204:         $possrange =~ s/^\s+|\s+$//g;
13205:         unless ($possrange eq '') {
13206:             $possrange =~ s/[\r\n]+/\s/g;
13207:             $possrange =~ s/\s*-\s*/-/g;
13208:             $possrange =~ s/\s+/,/g;
13209:             $possrange =~ s/,+/,/g;
13210:             if ($possrange ne '') {
13211:                 my (@ok,$count);
13212:                 $count = 0;
13213:                 foreach my $poss (split(/\,/,$possrange)) {
13214:                     $count ++;
13215:                     $poss = &validate_ip_pattern($poss);
13216:                     if ($poss ne '') {
13217:                         push(@ok,$poss);
13218:                     }
13219:                 }
13220:                 my $diff = $count - scalar(@ok);
13221:                 if ($diff) {
13222:                     $errors .= '<li><span class="LC_error">'.
13223:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
13224:                                    $diff,$name).
13225:                                '</span></li>';
13226:                 }
13227:                 if (@ok) {
13228:                     my @cidr_list;
13229:                     foreach my $item (@ok) {
13230:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
13231:                     }
13232:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
13233:                 }
13234:             }
13235:         }
13236:         foreach my $field ('name','ip') {
13237:             unless (($idx eq 'add') || ($changes{$itemid})) {
13238:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
13239:                     $changes{$itemid} = 1;
13240:                     last;
13241:                 }
13242:             }
13243:         }
13244:         $confhash{$itemid}{'commblocks'} = {};
13245: 
13246:         my %commblocks;
13247:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);
13248:         foreach my $type (@{$typeorder}) {
13249:             if ($commblocks{$type}) {
13250:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
13251:             }
13252:             unless (($idx eq 'add') || ($changes{$itemid})) {
13253:                 if (ref($current{'commblocks'}) eq 'HASH') {
13254:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
13255:                         $changes{$itemid} = 1;
13256:                     }
13257:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
13258:                     $changes{$itemid} = 1;
13259:                 }
13260:             }
13261:         }
13262:         $confhash{$itemid}{'courses'} = {};
13263:         my %crsdeletions;
13264:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
13265:         if (@delcrs) {
13266:             map { $crsdeletions{$_} = 1; } @delcrs;
13267:         }
13268:         if (ref($current{'courses'}) eq 'HASH') {
13269:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
13270:                 if ($crsdeletions{$cid}) {
13271:                     $changes{$itemid} = 1;
13272:                 } else {
13273:                     $confhash{$itemid}{'courses'}{$cid} = 1;
13274:                 }
13275:             }
13276:         }
13277:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
13278:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
13279:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) &&
13280:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
13281:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
13282:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
13283:                 $errors .= '<li><span class="LC_error">'.
13284:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
13285:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
13286:                            '</span></li>';
13287:             } else {
13288:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
13289:                 $changes{$itemid} = 1;
13290:             }
13291:         }
13292:     }
13293:     if (@allpos > 0) {
13294:         my $idx = 0;
13295:         foreach my $itemid (@allpos) {
13296:             if ($itemid ne '') {
13297:                 $confhash{$itemid}{'order'} = $idx;
13298:                 unless ($changes{$itemid}) {
13299:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
13300:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
13301:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
13302:                                 $changes{$itemid} = 1;
13303:                             }
13304:                         }
13305:                     }
13306:                 }
13307:                 $idx ++;
13308:             }
13309:         }
13310:     }
13311:     if (keys(%changes)) {
13312:         my %defaultshash = (
13313:                               ipaccess => \%confhash,
13314:                            );
13315:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13316:                                                  $dom);
13317:         if ($putresult eq 'ok') {
13318:             my $cachetime = 1800;
13319:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
13320:             if (ref($lastactref) eq 'HASH') {
13321:                 $lastactref->{'ipaccess'} = 1;
13322:             }
13323:             $resulttext = &mt('Changes made:').'<ul>';
13324:             my %bynum;
13325:             foreach my $itemid (sort(keys(%changes))) {
13326:                 if (ref($confhash{$itemid}) eq 'HASH') {
13327:                     my $position = $confhash{$itemid}{'order'};
13328:                     if ($position =~ /^\d+$/) {
13329:                         $bynum{$position} = $itemid;
13330:                     }
13331:                 }
13332:             }
13333:             if (keys(%deletions)) {
13334:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
13335:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13336:                 }
13337:             }
13338:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13339:                 my $itemid = $bynum{$pos};
13340:                 if (ref($confhash{$itemid}) eq 'HASH') {
13341:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
13342:                     my $position = $pos + 1;
13343:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13344:                     if ($confhash{$itemid}{'ip'} eq '') {
13345:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
13346:                     } else {
13347:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
13348:                     }
13349:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
13350:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
13351:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
13352:                                        '</li>';
13353:                     } else {
13354:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
13355:                     }
13356:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
13357:                         my @courses;
13358:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
13359:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
13360:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
13361:                         }
13362:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
13363:                                              join('</li><li>',@courses).'</li></ul>';
13364:                     } else {
13365:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
13366:                     }
13367:                     $resulttext .= '</ul></li>';
13368:                 }
13369:             }
13370:             $resulttext .= '</ul>';
13371:         } else {
13372:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13373:         }
13374:     } else {
13375:         $resulttext = &mt('No changes made');
13376:     }
13377:     if ($errors) {
13378:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
13379:                        $errors.'</ul></p>';
13380:     }
13381:     return $resulttext;
13382: }
13383: 
13384: sub get_ipaccess_id {
13385:     my ($domain,$location) = @_;
13386:     # get lock on ipaccess db
13387:     my $lockhash = {
13388:                       lock => $env{'user.name'}.
13389:                               ':'.$env{'user.domain'},
13390:                    };
13391:     my $tries = 0;
13392:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13393:     my ($id,$error);
13394: 
13395:     while (($gotlock ne 'ok') && ($tries<10)) {
13396:         $tries ++;
13397:         sleep (0.1);
13398:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
13399:     }
13400:     if ($gotlock eq 'ok') {
13401:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
13402:         if ($currids{'lock'}) {
13403:             delete($currids{'lock'});
13404:             if (keys(%currids)) {
13405:                 my @curr = sort { $a <=> $b } keys(%currids);
13406:                 if ($curr[-1] =~ /^\d+$/) {
13407:                     $id = 1 + $curr[-1];
13408:                 }
13409:             } else {
13410:                 $id = 1;
13411:             }
13412:             if ($id) {
13413:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
13414:                     $error = 'nostore';
13415:                 }
13416:             } else {
13417:                 $error = 'nonumber';
13418:             }
13419:         }
13420:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
13421:     } else {
13422:         $error = 'nolock';
13423:     }
13424:     return ($id,$error);
13425: }
13426: 
13427: sub modify_authordefaults {
13428:     my ($dom,$lastactref,%domconfig) = @_;
13429: #
13430: # Retrieve current domain configuration for webDAV and Authoring Space quotas from $domconfig{'quotas'}.
13431: #
13432:     my (%curr_quotas,%save_quotas,%confhash,%changes,%newvalues);
13433:     if (ref($domconfig{'quotas'}) eq 'HASH') {
13434:         foreach my $key (keys(%{$domconfig{'quotas'}})) {
13435:             if ($key =~ /^webdav|authorquota$/) {
13436:                 $curr_quotas{$key} = $domconfig{'quotas'}{$key};
13437:             } else {
13438:                 $save_quotas{$key} = $domconfig{'quotas'}{$key};
13439:             }
13440:         }
13441:     }
13442:     my %staticdefaults = (
13443:                            'copyright'    => 'default',
13444:                            'sourceavail'  => 'closed',
13445:                            'nocodemirror' => 'off',
13446:                            'daxecollapse' => 'off',
13447:                            'domcoordacc'  => 'on',
13448:                            'editors'      => ['edit','xml'],
13449:                            'authorquota'  => 500,
13450:                            'webdav'       => 0,
13451:                            'archive'      => 'off',
13452:                          );
13453:     my %titles = &authordefaults_titles();
13454:     foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13455:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
13456:             $confhash{$item} = $env{'form.'.$item};
13457:         }
13458:     }
13459:     if ($env{'form.copyright'} =~ /^(default|domain|public)$/) {
13460:         $confhash{'copyright'} = $1;
13461:     }
13462:     if ($env{'form.sourceavail'} =~ /^(closed|open)$/) {
13463:         $confhash{'sourceavail'} = $1;
13464:     }
13465:     my @posseditors =  &Apache::loncommon::get_env_multiple('form.author_editors');
13466:     my @okeditors = ('edit','xml','daxe');
13467:     my @editors;
13468:     foreach my $item (@posseditors) {
13469:         if (grep(/^\Q$item\E$/,@okeditors)) {
13470:             push(@editors,$item);
13471:         }
13472:     }
13473:     $confhash{'editors'} = \@editors;
13474: 
13475:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13476:     my @insttypes;
13477:     if (ref($types) eq 'ARRAY') {
13478:         @insttypes = @{$types};
13479:     }
13480:     my @webdavon = &Apache::loncommon::get_env_multiple('form.webdav');
13481:     my %webdav;
13482:     map { $webdav{$_} = 1; } @webdavon;
13483:     foreach my $type (@insttypes,'default') {
13484:         my $possquota = $env{'form.authorquota_'.$type};
13485:         if ($possquota =~ /^\d+$/) {
13486:             $save_quotas{'authorquota'}{$type} = $possquota;
13487:         }
13488:         if ($webdav{$type}) {
13489:             $save_quotas{'webdav'}{$type} = 1;
13490:         } else {
13491:             $save_quotas{'webdav'}{$type} = 0;
13492:         }
13493:     }
13494:     if ($env{'form.webdav_LC_adv'} =~ /^(0|1)$/) {
13495:         $save_quotas{'webdav'}{'_LC_adv'} = $env{'form.webdav_LC_adv'};
13496:     }
13497:     if (ref($domconfig{'authordefaults'}) eq 'HASH') {
13498:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail','archive') {
13499:             if ($domconfig{'authordefaults'}{$item} ne $confhash{$item}) {
13500:                 $changes{$item} = 1;
13501:              }
13502:         }
13503:         if (ref($domconfig{'authordefaults'}{'editors'}) eq 'ARRAY') {
13504:             my @diffs =
13505:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13506:                                                    $domconfig{'authordefaults'}{'editors'});
13507:             unless (@diffs == 0) {
13508:                 $changes{'editors'} = 1;
13509:             }
13510:         } else {
13511:             my @diffs =
13512:                 &Apache::loncommon::compare_arrays($confhash{'editors'},
13513:                                                    $staticdefaults{'editors'});
13514:             unless (@diffs == 0) {
13515:                 $changes{'editors'} = 1;
13516:             }
13517:         }
13518:     } else {
13519:         my @offon = ('off','on');
13520:         foreach my $item ('nocodemirror','daxecollapse','domcoordacc','archive') {
13521:             if ($offon[$confhash{$item}] ne $staticdefaults{$item}) {
13522:                 $changes{$item} = 1; 
13523:             }
13524:         }
13525:         foreach my $item ('copyright','sourceavail') {
13526:             if ($confhash{$item} ne $staticdefaults{$item}) {
13527:                 $changes{$item} = 1;
13528:             }
13529:         }
13530:         my @diffs =
13531:             &Apache::loncommon::compare_arrays($confhash{'editors'},
13532:                                                $staticdefaults{'editors'});
13533:         unless (@diffs == 0) {
13534:             $changes{'editors'} = 1;
13535:         }
13536:     }
13537:     foreach my $key ('authorquota','webdav') {
13538:         if (ref($curr_quotas{$key}) eq 'HASH') {
13539:             foreach my $type (@insttypes,'default') {
13540:                 if (exists($save_quotas{$key}{$type})) {
13541:                     if ($save_quotas{$key}{$type} ne $curr_quotas{$key}{$type}) {
13542:                         $changes{$key}{$type} = 1;
13543:                     }
13544:                 } elsif (exists($curr_quotas{$key}{$type})) {
13545:                     $save_quotas{$key}{$type} = $curr_quotas{$key}{$type};
13546:                 } else {
13547:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13548:                 }
13549:             }
13550:         } else {
13551:             foreach my $type (@insttypes,'default') {
13552:                 if (exists($save_quotas{$key}{$type})) {
13553:                     unless ($save_quotas{$key}{$type} eq $staticdefaults{$key}) {
13554:                         $changes{$key}{$type} = 1;
13555:                     }
13556:                 } else {
13557:                     $save_quotas{$key}{$type} = $staticdefaults{$key};
13558:                 }
13559:             }
13560:         }
13561:     }
13562:     if (ref($curr_quotas{'webdav'}) eq 'HASH') {
13563:         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13564:             if ($save_quotas{'webdav'}{'_LC_adv'} ne $curr_quotas{'webdav'}{'_LC_adv'}) {
13565:                 $changes{'webdav_LC_adv'} = 1;
13566:             }
13567:         } elsif (exists($curr_quotas{'webdav'}{'_LC_adv'})) {
13568:             $changes{'webdav_LC_adv'} = 1;
13569:         }
13570:     } elsif (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13571:         $changes{'webdav_LC_adv'} = 1;
13572:     }
13573:     my %confighash = (
13574:                         quotas  => \%save_quotas,
13575:                         authordefaults => \%confhash,
13576:                      );
13577:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,
13578:                                              $dom);
13579:     my $resulttext;
13580:     if ($putresult eq 'ok') {
13581:         if (keys(%changes)) {
13582:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13583:             if ((exists($changes{'authorquota'})) || (exists($changes{'webdav'})) ||
13584:                 ($changes{'webdav_LC_adv'})) {
13585:                 if ((exists($changes{'authorquota'})) && (ref($save_quotas{'authorquota'}) eq 'HASH')) {
13586:                     $domdefaults{'authorquota'} = $save_quotas{'authorquota'};
13587:                 }
13588:                 if (((exists($changes{'webdav'})) || ($changes{'webdav_LC_adv'})) &&
13589:                     (ref($save_quotas{'webdav'}) eq 'HASH')) {
13590:                     $domdefaults{'webdav'} = $save_quotas{'webdav'};
13591:                 }
13592:             }
13593:             $resulttext = &mt('Changes made:').'<ul>';
13594:             my $authoroverride;
13595:             foreach my $key ('nocodemirror','daxecollapse','domcoordacc','copyright','sourceavail') {
13596:                 if (exists($changes{$key})) {
13597:                     $domdefaults{$key} = $confhash{$key};
13598:                     my $shown;
13599:                     unless ($authoroverride) {
13600:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by Author').'<ul>';
13601:                         $authoroverride = 1;
13602:                     }
13603:                     if (($key eq 'nocodemirror') || ($key eq 'daxecollapse') || ($key eq 'domcoordacc')) {
13604:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13605:                     } elsif ($key eq 'copyright') {
13606:                         $shown = &Apache::loncommon::copyrightdescription($confhash{$key});
13607:                     } elsif ($key eq 'sourceavail') {
13608:                         $shown = &Apache::loncommon::source_copyrightdescription($confhash{$key});
13609:                     }
13610:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13611:                 }
13612:             }
13613:             if ($authoroverride) {
13614:                 $resulttext .= '</ul></li>';
13615:             }
13616:             my $domcoordoverride;
13617:             foreach my $key ('editors','authorquota','webdav','webdav_LC_adv','archive') {
13618:                 if (exists($changes{$key})) {
13619:                     my $shown;
13620:                     unless ($domcoordoverride) {
13621:                         $resulttext .= '<li>'.&mt('Defaults which can be overridden by a Domain Coodinator').'<ul>';
13622:                         $domcoordoverride = 1;
13623:                     }
13624:                     if ($key eq 'editors') {
13625:                         if (ref($confhash{'editors'}) eq 'ARRAY') {
13626:                             $domdefaults{'editors'} = join(',',@{$confhash{'editors'}});
13627:                             if (@{$confhash{'editors'}}) {
13628:                                 $shown = join(', ', map { $titles{$_} } @{$confhash{'editors'}});
13629:                             } else {
13630:                                 $shown = &mt('None');
13631:                             }
13632:                         }
13633:                     } elsif ($key eq 'authorquota') {
13634:                         foreach my $type (@insttypes) {
13635:                             $shown .= $usertypes->{$type}.' -- '.$save_quotas{$key}{$type}.', ';
13636:                         }
13637:                         $shown .= $othertitle.' -- '.$save_quotas{$key}{'default'};
13638:                     } elsif ($key eq 'webdav') {
13639:                         foreach my $type (@insttypes) {
13640:                             $shown .= $usertypes->{$type}.' -- '. ($save_quotas{$key}{$type} ? &mt('Yes') : &mt('No')).', ';
13641:                         }
13642:                         $shown .= $othertitle.' -- '. ($save_quotas{$key}{'default'} ? &mt('Yes') : &mt('No'));
13643:                     } elsif ($key eq 'webdav_LC_adv') {
13644:                         if (exists($save_quotas{'webdav'}{'_LC_adv'})) {
13645:                             $shown = ($save_quotas{'webdav'}{'_LC_adv'} ? $titles{'overon'} : $titles{'overoff'});
13646:                         } else {
13647:                             $shown = $titles{'none'};
13648:                         }
13649:                     } elsif ($key eq 'archive') {
13650:                         $domdefaults{$key} = $confhash{$key};
13651:                         $shown = ($confhash{$key} ? &mt('Yes') : &mt('No'));
13652:                     }
13653:                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{$key},$shown).'</li>';
13654:                 }
13655:             }
13656:             if ($domcoordoverride) {
13657:                 $resulttext .= '</ul></li>';
13658:             }
13659:             $resulttext .= '</ul>';
13660:             my $cachetime = 24*60*60;
13661:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13662:             if (ref($lastactref) eq 'HASH') {
13663:                 $lastactref->{'domdefaults'} = 1;
13664:             }
13665:         } else {
13666:             $resulttext = &mt('No changes made to Authoring Space defaults');
13667:         }
13668:     }
13669:     return $resulttext;
13670: }
13671: 
13672: sub modify_rolecolors {
13673:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
13674:     my ($resulttext,%rolehash);
13675:     $rolehash{'rolecolors'} = {};
13676:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
13677:         if ($domconfig{'rolecolors'} eq '') {
13678:             $domconfig{'rolecolors'} = {};
13679:         }
13680:     }
13681:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
13682:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
13683:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
13684:                                              $dom);
13685:     if ($putresult eq 'ok') {
13686:         if (keys(%changes) > 0) {
13687:             &Apache::loncommon::devalidate_domconfig_cache($dom);
13688:             if (ref($lastactref) eq 'HASH') {
13689:                 $lastactref->{'domainconfig'} = 1;
13690:             }
13691:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
13692:                                              $rolehash{'rolecolors'});
13693:         } else {
13694:             $resulttext = &mt('No changes made to default color schemes');
13695:         }
13696:     } else {
13697:         $resulttext = '<span class="LC_error">'.
13698: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
13699:     }
13700:     if ($errors) {
13701:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13702:                        $errors.'</ul>';
13703:     }
13704:     return $resulttext;
13705: }
13706: 
13707: sub modify_colors {
13708:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
13709:     my (%changes,%choices);
13710:     my @bgs;
13711:     my @links = ('link','alink','vlink');
13712:     my @logintext;
13713:     my @images;
13714:     my $servadm = $r->dir_config('lonAdmEMail');
13715:     my $errors;
13716:     my %defaults;
13717:     foreach my $role (@{$roles}) {
13718:         if ($role eq 'login') {
13719:             %choices = &login_choices();
13720:             @logintext = ('textcol','bgcol');
13721:         } else {
13722:             %choices = &color_font_choices();
13723:         }
13724:         if ($role eq 'login') {
13725:             @images = ('img','logo','domlogo','login');
13726:             @bgs = ('pgbg','mainbg','sidebg');
13727:         } else {
13728:             @images = ();
13729:             @bgs = ('pgbg','tabbg','sidebg');
13730:         }
13731:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
13732:         $env{'form.'.$role.'_font'} = lc($env{'form.'.$role.'_font'});
13733:         if ($env{'form.'.$role.'_font'} =~ /^\w+/) {
13734:             $env{'form.'.$role.'_font'} = '#'.$env{'form.'.$role.'_font'};
13735:         }
13736:         unless ($env{'form.'.$role.'_font'} eq lc($defaults{'font'})) {
13737:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
13738:         }
13739:         if ($role eq 'login') {
13740:             foreach my $item (@logintext) {
13741:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13742:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13743:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13744:                 }
13745:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
13746:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13747:                 }
13748:             }
13749:         } else {
13750:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
13751:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
13752:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
13753:             }
13754:             unless ($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
13755:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
13756:             }
13757:         }
13758:         foreach my $item (@bgs) {
13759:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13760:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13761:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13762:             }
13763:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
13764:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13765:             }
13766:         }
13767:         foreach my $item (@links) {
13768:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
13769:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
13770:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
13771:             }
13772:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
13773:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
13774:             }
13775:         }
13776:         my ($configuserok,$author_ok,$switchserver) = 
13777:             &config_check($dom,$confname,$servadm);
13778:         my ($width,$height) = &thumb_dimensions();
13779:         if (ref($domconfig->{$role}) ne 'HASH') {
13780:             $domconfig->{$role} = {};
13781:         }
13782:         foreach my $img (@images) {
13783:             if ($role eq 'login') {
13784:                 if (($img eq 'img') || ($img eq 'logo')) {  
13785:                     if (defined($env{'form.login_showlogo_'.$img})) {
13786:                         $confhash->{$role}{'showlogo'}{$img} = 1;
13787:                     } else { 
13788:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13789:                     }
13790:                 }
13791:                 if ($env{'form.login_alt_'.$img} ne '') {
13792:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13793:                 }
13794:             }
13795: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13796: 		 && !defined($domconfig->{$role}{$img})
13797: 		 && !$env{'form.'.$role.'_del_'.$img}
13798: 		 && $env{'form.'.$role.'_import_'.$img}) {
13799: 		# import the old configured image from the .tab setting
13800: 		# if they haven't provided a new one 
13801: 		$domconfig->{$role}{$img} = 
13802: 		    $env{'form.'.$role.'_import_'.$img};
13803: 	    }
13804:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13805:                 my $error;
13806:                 if ($configuserok eq 'ok') {
13807:                     if ($switchserver) {
13808:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13809:                     } else {
13810:                         if ($author_ok eq 'ok') {
13811:                             my $modified = [];
13812:                             my ($result,$logourl) = 
13813:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13814:                                                                         $dom,$confname,$img,$width,$height,
13815:                                                                         '',$modified);
13816:                             if ($result eq 'ok') {
13817:                                 $confhash->{$role}{$img} = $logourl;
13818:                                 $changes{$role}{'images'}{$img} = 1;
13819:                                 &update_modify_urls($r,$modified);
13820:                             } else {
13821:                                 $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);
13822:                             }
13823:                         } else {
13824:                             $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);
13825:                         }
13826:                     }
13827:                 } else {
13828:                     $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);
13829:                 }
13830:                 if ($error) {
13831:                     &Apache::lonnet::logthis($error);
13832:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13833:                 }
13834:             } elsif ($domconfig->{$role}{$img} ne '') {
13835:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13836:                     my $error;
13837:                     if ($configuserok eq 'ok') {
13838: # is confname an author?
13839:                         if ($switchserver eq '') {
13840:                             if ($author_ok eq 'ok') {
13841:                                 my $modified = [];
13842:                                 my ($result,$logourl) = 
13843:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13844:                                                                             $dom,$confname,$img,$width,$height,
13845:                                                                             '',$modified);
13846:                                 if ($result eq 'ok') {
13847:                                     $confhash->{$role}{$img} = $logourl;
13848: 				    $changes{$role}{'images'}{$img} = 1;
13849:                                     &update_modify_urls($r,$modified);
13850:                                 }
13851:                             }
13852:                         }
13853:                     }
13854:                 }
13855:             }
13856:         }
13857:         if (ref($domconfig) eq 'HASH') {
13858:             if (ref($domconfig->{$role}) eq 'HASH') {
13859:                 foreach my $img (@images) {
13860:                     if ($domconfig->{$role}{$img} ne '') {
13861:                         if ($env{'form.'.$role.'_del_'.$img}) {
13862:                             $confhash->{$role}{$img} = '';
13863:                             $changes{$role}{'images'}{$img} = 1;
13864:                         } else {
13865:                             if ($confhash->{$role}{$img} eq '') {
13866:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13867:                             }
13868:                         }
13869:                     } else {
13870:                         if ($env{'form.'.$role.'_del_'.$img}) {
13871:                             $confhash->{$role}{$img} = '';
13872:                             $changes{$role}{'images'}{$img} = 1;
13873:                         } 
13874:                     }
13875:                     if ($role eq 'login') {
13876:                         if (($img eq 'logo') || ($img eq 'img')) {
13877:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13878:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13879:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13880:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13881:                                 }
13882:                             } else {
13883:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13884:                                     $changes{$role}{'showlogo'}{$img} = 1;
13885:                                 }
13886:                             }
13887:                         }
13888:                         if ($img ne 'login') {
13889:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13890:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13891:                                     $domconfig->{$role}{'alttext'}{$img}) {
13892:                                     $changes{$role}{'alttext'}{$img} = 1;
13893:                                 }
13894:                             } else {
13895:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13896:                                     $changes{$role}{'alttext'}{$img} = 1;
13897:                                 }
13898:                             }
13899:                         }
13900:                     }
13901:                 }
13902:                 if ($domconfig->{$role}{'font'} ne '') {
13903:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13904:                         $changes{$role}{'font'} = 1;
13905:                     }
13906:                 } else {
13907:                     if ($confhash->{$role}{'font'}) {
13908:                         $changes{$role}{'font'} = 1;
13909:                     }
13910:                 }
13911:                 if ($role ne 'login') {
13912:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13913:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13914:                             $changes{$role}{'fontmenu'} = 1;
13915:                         }
13916:                     } else {
13917:                         if ($confhash->{$role}{'fontmenu'}) {
13918:                             $changes{$role}{'fontmenu'} = 1;
13919:                         }
13920:                     }
13921:                 }
13922:                 foreach my $item (@bgs) {
13923:                     if ($domconfig->{$role}{$item} ne '') {
13924:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13925:                             $changes{$role}{'bgs'}{$item} = 1;
13926:                         } 
13927:                     } else {
13928:                         if ($confhash->{$role}{$item}) {
13929:                             $changes{$role}{'bgs'}{$item} = 1;
13930:                         }
13931:                     }
13932:                 }
13933:                 foreach my $item (@links) {
13934:                     if ($domconfig->{$role}{$item} ne '') {
13935:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13936:                             $changes{$role}{'links'}{$item} = 1;
13937:                         }
13938:                     } else {
13939:                         if ($confhash->{$role}{$item}) {
13940:                             $changes{$role}{'links'}{$item} = 1;
13941:                         }
13942:                     }
13943:                 }
13944:                 foreach my $item (@logintext) {
13945:                     if ($domconfig->{$role}{$item} ne '') {
13946:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13947:                             $changes{$role}{'logintext'}{$item} = 1;
13948:                         }
13949:                     } else {
13950:                         if ($confhash->{$role}{$item}) {
13951:                             $changes{$role}{'logintext'}{$item} = 1;
13952:                         }
13953:                     }
13954:                 }
13955:             } else {
13956:                 &default_change_checker($role,\@images,\@links,\@bgs,
13957:                                         \@logintext,$confhash,\%changes); 
13958:             }
13959:         } else {
13960:             &default_change_checker($role,\@images,\@links,\@bgs,
13961:                                     \@logintext,$confhash,\%changes); 
13962:         }
13963:     }
13964:     return ($errors,%changes);
13965: }
13966: 
13967: sub config_check {
13968:     my ($dom,$confname,$servadm) = @_;
13969:     my ($configuserok,$author_ok,$switchserver,%currroles);
13970:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13971:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13972:                                                    $confname,$servadm);
13973:     if ($configuserok eq 'ok') {
13974:         $switchserver = &check_switchserver($dom,$confname);
13975:         if ($switchserver eq '') {
13976:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13977:         }
13978:     }
13979:     return ($configuserok,$author_ok,$switchserver);
13980: }
13981: 
13982: sub default_change_checker {
13983:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13984:     foreach my $item (@{$links}) {
13985:         if ($confhash->{$role}{$item}) {
13986:             $changes->{$role}{'links'}{$item} = 1;
13987:         }
13988:     }
13989:     foreach my $item (@{$bgs}) {
13990:         if ($confhash->{$role}{$item}) {
13991:             $changes->{$role}{'bgs'}{$item} = 1;
13992:         }
13993:     }
13994:     foreach my $item (@{$logintext}) {
13995:         if ($confhash->{$role}{$item}) {
13996:             $changes->{$role}{'logintext'}{$item} = 1;
13997:         }
13998:     }
13999:     foreach my $img (@{$images}) {
14000:         if ($env{'form.'.$role.'_del_'.$img}) {
14001:             $confhash->{$role}{$img} = '';
14002:             $changes->{$role}{'images'}{$img} = 1;
14003:         }
14004:         if ($role eq 'login') {
14005:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
14006:                 $changes->{$role}{'showlogo'}{$img} = 1;
14007:             }
14008:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
14009:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
14010:                     $changes->{$role}{'alttext'}{$img} = 1;
14011:                 }
14012:             }
14013:         }
14014:     }
14015:     if ($confhash->{$role}{'font'}) {
14016:         $changes->{$role}{'font'} = 1;
14017:     }
14018: }
14019: 
14020: sub display_colorchgs {
14021:     my ($dom,$changes,$roles,$confhash) = @_;
14022:     my (%choices,$resulttext);
14023:     if (!grep(/^login$/,@{$roles})) {
14024:         $resulttext = &mt('Changes made:').'<br />';
14025:     }
14026:     foreach my $role (@{$roles}) {
14027:         if ($role eq 'login') {
14028:             %choices = &login_choices();
14029:         } else {
14030:             %choices = &color_font_choices();
14031:         }
14032:         if (ref($changes->{$role}) eq 'HASH') {
14033:             if ($role ne 'login') {
14034:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
14035:             }
14036:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
14037:                 if ($role ne 'login') {
14038:                     $resulttext .= '<ul>';
14039:                 }
14040:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
14041:                     if ($role ne 'login') {
14042:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
14043:                     }
14044:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
14045:                         if (($role eq 'login') && ($key eq 'showlogo')) {
14046:                             if ($confhash->{$role}{$key}{$item}) {
14047:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
14048:                             } else {
14049:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
14050:                             }
14051:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
14052:                             if ($confhash->{$role}{$key}{$item} ne '') {
14053:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
14054:                                                $confhash->{$role}{$key}{$item}).'</li>';
14055:                             } else {
14056:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
14057:                             }
14058:                         } elsif ($confhash->{$role}{$item} eq '') {
14059:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
14060:                         } else {
14061:                             my $newitem = $confhash->{$role}{$item};
14062:                             if ($key eq 'images') {
14063:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
14064:                             }
14065:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
14066:                         }
14067:                     }
14068:                     if ($role ne 'login') {
14069:                         $resulttext .= '</ul></li>';
14070:                     }
14071:                 } else {
14072:                     if ($confhash->{$role}{$key} eq '') {
14073:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
14074:                     } else {
14075:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
14076:                     }
14077:                 }
14078:                 if ($role ne 'login') {
14079:                     $resulttext .= '</ul>';
14080:                 }
14081:             }
14082:         }
14083:     }
14084:     return $resulttext;
14085: }
14086: 
14087: sub thumb_dimensions {
14088:     return ('200','50');
14089: }
14090: 
14091: sub check_dimensions {
14092:     my ($inputfile) = @_;
14093:     my ($fullwidth,$fullheight);
14094:     if ($inputfile =~ m|^[/\w.\-]+$|) {
14095:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
14096:             my $imageinfo = <PIPE>;
14097:             if (!close(PIPE)) {
14098:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
14099:             }
14100:             chomp($imageinfo);
14101:             my ($fullsize) = 
14102:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
14103:             if ($fullsize) {
14104:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
14105:             }
14106:         }
14107:     }
14108:     return ($fullwidth,$fullheight);
14109: }
14110: 
14111: sub check_configuser {
14112:     my ($uhome,$dom,$confname,$servadm) = @_;
14113:     my ($configuserok,%currroles);
14114:     if ($uhome eq 'no_host') {
14115:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
14116:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
14117:         $configuserok = 
14118:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
14119:                              $configpass,'','','','','',undef,$servadm);
14120:     } else {
14121:         $configuserok = 'ok';
14122:         %currroles = 
14123:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
14124:     }
14125:     return ($configuserok,%currroles);
14126: }
14127: 
14128: sub check_authorstatus {
14129:     my ($dom,$confname,%currroles) = @_;
14130:     my $author_ok;
14131:     if (!$currroles{':'.$dom.':au'}) {
14132:         my $start = time;
14133:         my $end = 0;
14134:         $author_ok = 
14135:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
14136:                                         'au',$end,$start,'','','domconfig');
14137:     } else {
14138:         $author_ok = 'ok';
14139:     }
14140:     return $author_ok;
14141: }
14142: 
14143: sub update_modify_urls {
14144:     my ($r,$modified) = @_;
14145:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
14146:         push(@{$modified_urls},$modified);
14147:         unless ($registered_cleanup) {
14148:             my $handlers = $r->get_handlers('PerlCleanupHandler');
14149:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
14150:             $registered_cleanup=1;
14151:         }
14152:     }
14153: }
14154: 
14155: sub notifysubscribed {
14156:     foreach my $targetsource (@{$modified_urls}){
14157:         next unless (ref($targetsource) eq 'ARRAY');
14158:         my ($target,$source)=@{$targetsource};
14159:         if ($source ne '') {
14160:             if (open(my $logfh,">>",$source.'.log')) {
14161:                 print $logfh "\nCleanup phase: Notifications\n";
14162:                 my @subscribed=&subscribed_hosts($target);
14163:                 foreach my $subhost (@subscribed) {
14164:                     print $logfh "\nNotifying host ".$subhost.':';
14165:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
14166:                     print $logfh $reply;
14167:                 }
14168:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
14169:                 foreach my $subhost (@subscribedmeta) {
14170:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
14171:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
14172:                                                         $subhost);
14173:                     print $logfh $reply;
14174:                 }
14175:                 print $logfh "\n============ Done ============\n";
14176:                 close($logfh);
14177:             }
14178:         }
14179:     }
14180:     return OK;
14181: }
14182: 
14183: sub subscribed_hosts {
14184:     my ($target) = @_;
14185:     my @subscribed;
14186:     if (open(my $fh,"<","$target.subscription")) {
14187:         while (my $subline=<$fh>) {
14188:             if ($subline =~ /^($match_lonid):/) {
14189:                 my $host = $1;
14190:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
14191:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
14192:                         push(@subscribed,$host);
14193:                     }
14194:                 }
14195:             }
14196:         }
14197:     }
14198:     return @subscribed;
14199: }
14200: 
14201: sub check_switchserver {
14202:     my ($dom,$confname) = @_;
14203:     my ($allowed,$switchserver,$home);
14204:     if ($confname eq '') {
14205:         $home = &Apache::lonnet::domain($dom,'primary');
14206:     } else {
14207:         $home = &Apache::lonnet::homeserver($confname,$dom);
14208:         if ($home eq 'no_host') {
14209:             $home = &Apache::lonnet::domain($dom,'primary');
14210:         }
14211:     }
14212:     my @ids=&Apache::lonnet::current_machine_ids();
14213:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
14214:     if (!$allowed) {
14215: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
14216:                       &HTML::Entities::encode($env{'request.role'},'\'<>"&').
14217:                       '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
14218:     }
14219:     return $switchserver;
14220: }
14221: 
14222: sub modify_quotas {
14223:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14224:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
14225:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
14226:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
14227:         $validationfieldsref);
14228:     if ($action eq 'quotas') {
14229:         $context = 'tools';
14230:     } else {
14231:         $context = $action;
14232:     }
14233:     if ($context eq 'requestcourses') {
14234:         @usertools = ('official','unofficial','community','textbook','placement','lti');
14235:         @options =('norequest','approval','validate','autolimit');
14236:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
14237:         %titles = &courserequest_titles();
14238:         $toolregexp = join('|',@usertools);
14239:         %conditions = &courserequest_conditions();
14240:         $confname = $dom.'-domainconfig';
14241:         my $servadm = $r->dir_config('lonAdmEMail');
14242:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14243:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
14244:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
14245:     } elsif ($context eq 'requestauthor') {
14246:         @usertools = ('author');
14247:         %titles = &authorrequest_titles();
14248:     } else {
14249:         @usertools = ('aboutme','blog','portfolio','portaccess','timezone');
14250:         %titles = &tool_titles();
14251:     }
14252:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14253:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14254:     foreach my $key (keys(%env)) {
14255:         if ($context eq 'requestcourses') {
14256:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
14257:                 my $item = $1;
14258:                 my $type = $2;
14259:                 if ($type =~ /^limit_(.+)/) {
14260:                     $limithash{$item}{$1} = $env{$key};
14261:                 } else {
14262:                     $confhash{$item}{$type} = $env{$key};
14263:                 }
14264:             }
14265:         } elsif ($context eq 'requestauthor') {
14266:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
14267:                 $confhash{$1} = $env{$key};
14268:             }
14269:         } else {
14270:             if ($key =~ /^form\.quota_(.+)$/) {
14271:                 $confhash{'defaultquota'}{$1} = $env{$key};
14272:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
14273:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
14274:             }
14275:         }
14276:     }
14277:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14278:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
14279:         @approvalnotify = sort(@approvalnotify);
14280:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
14281:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
14282:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
14283:         foreach my $type (@hasuniquecode) {
14284:             if (grep(/^\Q$type\E$/,@crstypes)) {
14285:                 $confhash{'uniquecode'}{$type} = 1;
14286:             }
14287:         }
14288:         my (%newbook,%allpos);
14289:         if ($context eq 'requestcourses') {
14290:             foreach my $type ('textbooks','templates') {
14291:                 @{$allpos{$type}} = (); 
14292:                 my $invalid;
14293:                 if ($type eq 'textbooks') {
14294:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
14295:                 } else {
14296:                     $invalid = &mt('Invalid LON-CAPA course for template');
14297:                 }
14298:                 if ($env{'form.'.$type.'_addbook'}) {
14299:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
14300:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
14301:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
14302:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
14303:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14304:                         } else {
14305:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
14306:                             my $position = $env{'form.'.$type.'_addbook_pos'};
14307:                             $position =~ s/\D+//g;
14308:                             if ($position ne '') {
14309:                                 $allpos{$type}[$position] = $newbook{$type};
14310:                             }
14311:                         }
14312:                     } else {
14313:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
14314:                     }
14315:                 }
14316:             } 
14317:         }
14318:         if (ref($domconfig{$action}) eq 'HASH') {
14319:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
14320:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
14321:                     $changes{'notify'}{'approval'} = 1;
14322:                 }
14323:             } else {
14324:                 if ($confhash{'notify'}{'approval'}) {
14325:                     $changes{'notify'}{'approval'} = 1;
14326:                 }
14327:             }
14328:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
14329:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
14330:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
14331:                         unless ($confhash{'uniquecode'}{$crstype}) {
14332:                             $changes{'uniquecode'} = 1;
14333:                         }
14334:                     }
14335:                     unless ($changes{'uniquecode'}) {
14336:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
14337:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
14338:                                 $changes{'uniquecode'} = 1;
14339:                             }
14340:                         }
14341:                     }
14342:                } else {
14343:                    $changes{'uniquecode'} = 1;
14344:                }
14345:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
14346:                 $changes{'uniquecode'} = 1;
14347:             }
14348:             if ($context eq 'requestcourses') {
14349:                 foreach my $type ('textbooks','templates') {
14350:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
14351:                         my %deletions;
14352:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
14353:                         if (@todelete) {
14354:                             map { $deletions{$_} = 1; } @todelete;
14355:                         }
14356:                         my %imgdeletions;
14357:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
14358:                         if (@todeleteimages) {
14359:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
14360:                         }
14361:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
14362:                         for (my $i=0; $i<=$maxnum; $i++) {
14363:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
14364:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
14365:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
14366:                                 if ($deletions{$key}) {
14367:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
14368:                                         #FIXME need to obsolete item in RES space
14369:                                     }
14370:                                     next;
14371:                                 } else {
14372:                                     my $newpos = $env{'form.'.$itemid};
14373:                                     $newpos =~ s/\D+//g;
14374:                                     foreach my $item ('subject','title','publisher','author') {
14375:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14376:                                                  ($type eq 'templates'));
14377:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
14378:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
14379:                                             $changes{$type}{$key} = 1;
14380:                                         }
14381:                                     }
14382:                                     $allpos{$type}[$newpos] = $key;
14383:                                 }
14384:                                 if ($imgdeletions{$key}) {
14385:                                     $changes{$type}{$key} = 1;
14386:                                     #FIXME need to obsolete item in RES space
14387:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
14388:                                     my ($cdom,$cnum) = split(/_/,$key);
14389:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14390:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14391:                                     } else {
14392:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
14393:                                                                                       $cdom,$cnum,$type,$configuserok,
14394:                                                                                       $switchserver,$author_ok);
14395:                                         if ($imgurl) {
14396:                                             $confhash{$type}{$key}{'image'} = $imgurl;
14397:                                             $changes{$type}{$key} = 1; 
14398:                                         }
14399:                                         if ($error) {
14400:                                             &Apache::lonnet::logthis($error);
14401:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14402:                                         }
14403:                                     }
14404:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
14405:                                     $confhash{$type}{$key}{'image'} = 
14406:                                         $domconfig{$action}{$type}{$key}{'image'};
14407:                                 }
14408:                             }
14409:                         }
14410:                     }
14411:                 }
14412:             }
14413:         } else {
14414:             if ($confhash{'notify'}{'approval'}) {
14415:                 $changes{'notify'}{'approval'} = 1;
14416:             }
14417:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
14418:                 $changes{'uniquecode'} = 1;
14419:             }
14420:         }
14421:         if ($context eq 'requestcourses') {
14422:             foreach my $type ('textbooks','templates') {
14423:                 if ($newbook{$type}) {
14424:                     $changes{$type}{$newbook{$type}} = 1;
14425:                     foreach my $item ('subject','title','publisher','author') {
14426:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
14427:                                  ($type eq 'template'));
14428:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
14429:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
14430:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
14431:                         }
14432:                     }
14433:                     if ($type eq 'textbooks') {
14434:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
14435:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
14436:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
14437:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
14438:                             } else {
14439:                                 my ($imageurl,$error) =
14440:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
14441:                                                             $configuserok,$switchserver,$author_ok);
14442:                                 if ($imageurl) {
14443:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
14444:                                 }
14445:                                 if ($error) {
14446:                                     &Apache::lonnet::logthis($error);
14447:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14448:                                 }
14449:                             }
14450:                         }
14451:                     }
14452:                 }
14453:                 if (@{$allpos{$type}} > 0) {
14454:                     my $idx = 0;
14455:                     foreach my $item (@{$allpos{$type}}) {
14456:                         if ($item ne '') {
14457:                             $confhash{$type}{$item}{'order'} = $idx;
14458:                             if (ref($domconfig{$action}) eq 'HASH') {
14459:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
14460:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
14461:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
14462:                                             $changes{$type}{$item} = 1;
14463:                                         }
14464:                                     }
14465:                                 }
14466:                             }
14467:                             $idx ++;
14468:                         }
14469:                     }
14470:                 }
14471:             }
14472:             if (ref($validationitemsref) eq 'ARRAY') {
14473:                 foreach my $item (@{$validationitemsref}) {
14474:                     if ($item eq 'fields') {
14475:                         my @changed;
14476:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
14477:                         if (@{$confhash{'validation'}{$item}} > 0) {
14478:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
14479:                         }
14480:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14481:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14482:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
14483:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
14484:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
14485:                                 } else {
14486:                                     @changed = @{$confhash{'validation'}{$item}};
14487:                                 }
14488:                             } else {
14489:                                 @changed = @{$confhash{'validation'}{$item}};
14490:                             }
14491:                         } else {
14492:                             @changed = @{$confhash{'validation'}{$item}};
14493:                         }
14494:                         if (@changed) {
14495:                             if ($confhash{'validation'}{$item}) {
14496:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
14497:                             } else {
14498:                                 $changes{'validation'}{$item} = &mt('None');
14499:                             }
14500:                         }
14501:                     } else {
14502:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
14503:                         if ($item eq 'markup') {
14504:                             if ($env{'form.requestcourses_validation_'.$item}) {
14505:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14506:                             }
14507:                         }
14508:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14509:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14510:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
14511:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14512:                                 }
14513:                             } else {
14514:                                 if ($confhash{'validation'}{$item} ne '') {
14515:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14516:                                 }
14517:                             }
14518:                         } else {
14519:                             if ($confhash{'validation'}{$item} ne '') {
14520:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
14521:                             }
14522:                         }
14523:                     }
14524:                 }
14525:             }
14526:             if ($env{'form.validationdc'}) {
14527:                 my $newval = $env{'form.validationdc'};
14528:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14529:                 if (exists($domcoords{$newval})) {
14530:                     $confhash{'validation'}{'dc'} = $newval;
14531:                 }
14532:             }
14533:             if (ref($confhash{'validation'}) eq 'HASH') {
14534:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14535:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14536:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14537:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
14538:                                 if ($confhash{'validation'}{'dc'} eq '') {
14539:                                     $changes{'validation'}{'dc'} = &mt('None');
14540:                                 } else {
14541:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14542:                                 }
14543:                             }
14544:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
14545:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14546:                         }
14547:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
14548:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14549:                     }
14550:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
14551:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
14552:                 }
14553:             } else {
14554:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
14555:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
14556:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
14557:                             $changes{'validation'}{'dc'} = &mt('None');
14558:                         }
14559:                     }
14560:                 }
14561:             }
14562:         }
14563:     } else {
14564:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
14565:     }
14566:     foreach my $item (@usertools) {
14567:         foreach my $type (@{$types},'default','_LC_adv') {
14568:             my $unset; 
14569:             if ($context eq 'requestcourses') {
14570:                 $unset = '0';
14571:                 if ($type eq '_LC_adv') {
14572:                     $unset = '';
14573:                 }
14574:                 if ($confhash{$item}{$type} eq 'autolimit') {
14575:                     $confhash{$item}{$type} .= '=';
14576:                     unless ($limithash{$item}{$type} =~ /\D/) {
14577:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
14578:                     }
14579:                 }
14580:             } elsif ($context eq 'requestauthor') {
14581:                 $unset = '0';
14582:                 if ($type eq '_LC_adv') {
14583:                     $unset = '';
14584:                 }
14585:             } else {
14586:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
14587:                     $confhash{$item}{$type} = 1;
14588:                 } else {
14589:                     $confhash{$item}{$type} = 0;
14590:                 }
14591:             }
14592:             if (ref($domconfig{$action}) eq 'HASH') {
14593:                 if ($action eq 'requestauthor') {
14594:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
14595:                         $changes{$type} = 1;
14596:                     }
14597:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
14598:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
14599:                         $changes{$item}{$type} = 1;
14600:                     }
14601:                 } else {
14602:                     if ($context eq 'requestcourses') {
14603:                         if ($confhash{$item}{$type} ne $unset) {
14604:                             $changes{$item}{$type} = 1;
14605:                         }
14606:                     } else {
14607:                         if (!$confhash{$item}{$type}) {
14608:                             $changes{$item}{$type} = 1;
14609:                         }
14610:                     }
14611:                 }
14612:             } else {
14613:                 if ($context eq 'requestcourses') {
14614:                     if ($confhash{$item}{$type} ne $unset) {
14615:                         $changes{$item}{$type} = 1;
14616:                     }
14617:                 } elsif ($context eq 'requestauthor') {
14618:                     if ($confhash{$type} ne $unset) {
14619:                         $changes{$type} = 1;
14620:                     }
14621:                 } else {
14622:                     if (!$confhash{$item}{$type}) {
14623:                         $changes{$item}{$type} = 1;
14624:                     }
14625:                 }
14626:             }
14627:         }
14628:     }
14629:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
14630:         if (ref($domconfig{'quotas'}) eq 'HASH') {
14631:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14632:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
14633:                     if (exists($confhash{'defaultquota'}{$key})) {
14634:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
14635:                             $changes{'defaultquota'}{$key} = 1;
14636:                         }
14637:                     } else {
14638:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
14639:                     }
14640:                 }
14641:             } else {
14642:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
14643:                     if (exists($confhash{'defaultquota'}{$key})) {
14644:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
14645:                             $changes{'defaultquota'}{$key} = 1;
14646:                         }
14647:                     } else {
14648:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
14649:                     }
14650:                 }
14651:             }
14652:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
14653:                 $confhash{'authorquota'} = $domconfig{'quotas'}{'authorquota'};
14654:             }
14655:             if (ref($domconfig{'quotas'}{'webdav'}) eq 'HASH') {
14656:                 $confhash{'webdav'} = $domconfig{'quotas'}{'webdav'};
14657:             }
14658:         }
14659:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
14660:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
14661:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
14662:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
14663:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
14664:                             $changes{'defaultquota'}{$key} = 1;
14665:                         }
14666:                     } else {
14667:                         if (!exists($domconfig{'quotas'}{$key})) {
14668:                             $changes{'defaultquota'}{$key} = 1;
14669:                         }
14670:                     }
14671:                 } else {
14672:                     $changes{'defaultquota'}{$key} = 1;
14673:                 }
14674:             }
14675:         }
14676:     }
14677: 
14678:     if ($context eq 'requestauthor') {
14679:         $domdefaults{'requestauthor'} = \%confhash;
14680:     } else {
14681:         foreach my $key (keys(%confhash)) {
14682:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
14683:                 $domdefaults{$key} = $confhash{$key};
14684:             }
14685:         }
14686:     }
14687: 
14688:     my %quotahash = (
14689:                       $action => { %confhash }
14690:                     );
14691:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
14692:                                              $dom);
14693:     if ($putresult eq 'ok') {
14694:         if (keys(%changes) > 0) {
14695:             my $cachetime = 24*60*60;
14696:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14697:             if (ref($lastactref) eq 'HASH') {
14698:                 $lastactref->{'domdefaults'} = 1;
14699:             }
14700:             $resulttext = &mt('Changes made:').'<ul>';
14701:             unless (($context eq 'requestcourses') ||
14702:                     ($context eq 'requestauthor')) {
14703:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
14704:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
14705:                     foreach my $type (@{$types},'default') {
14706:                         if (defined($changes{'defaultquota'}{$type})) {
14707:                             my $typetitle = $usertypes->{$type};
14708:                             if ($type eq 'default') {
14709:                                 $typetitle = $othertitle;
14710:                             }
14711:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
14712:                         }
14713:                     }
14714:                     $resulttext .= '</ul></li>';
14715:                 }
14716:             }
14717:             my %newenv;
14718:             foreach my $item (@usertools) {
14719:                 my (%haschgs,%inconf);
14720:                 if ($context eq 'requestauthor') {
14721:                     %haschgs = %changes;
14722:                     %inconf = %confhash;
14723:                 } else {
14724:                     if (ref($changes{$item}) eq 'HASH') {
14725:                         %haschgs = %{$changes{$item}};
14726:                     }
14727:                     if (ref($confhash{$item}) eq 'HASH') {
14728:                         %inconf = %{$confhash{$item}};
14729:                     }
14730:                 }
14731:                 if (keys(%haschgs) > 0) {
14732:                     my $newacc = 
14733:                         &Apache::lonnet::usertools_access($env{'user.name'},
14734:                                                           $env{'user.domain'},
14735:                                                           $item,'reload',$context);
14736:                     if (($context eq 'requestcourses') ||
14737:                         ($context eq 'requestauthor')) {
14738:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
14739:                             $newenv{'environment.canrequest.'.$item} = $newacc;
14740:                         }
14741:                     } else {
14742:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
14743:                             $newenv{'environment.availabletools.'.$item} = $newacc;
14744:                         }
14745:                     }
14746:                     unless ($context eq 'requestauthor') {
14747:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
14748:                     }
14749:                     foreach my $type (@{$types},'default','_LC_adv') {
14750:                         if ($haschgs{$type}) {
14751:                             my $typetitle = $usertypes->{$type};
14752:                             if ($type eq 'default') {
14753:                                 $typetitle = $othertitle;
14754:                             } elsif ($type eq '_LC_adv') {
14755:                                 $typetitle = 'LON-CAPA Advanced Users'; 
14756:                             }
14757:                             if ($inconf{$type}) {
14758:                                 if ($context eq 'requestcourses') {
14759:                                     my $cond;
14760:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14761:                                         if ($1 eq '') {
14762:                                             $cond = &mt('(Automatic processing of any request).');
14763:                                         } else {
14764:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14765:                                         }
14766:                                     } else { 
14767:                                         $cond = $conditions{$inconf{$type}};
14768:                                     }
14769:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14770:                                 } elsif ($context eq 'requestauthor') {
14771:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14772:                                                              $titles{$inconf{$type}},$typetitle);
14773: 
14774:                                 } else {
14775:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14776:                                 }
14777:                             } else {
14778:                                 if ($type eq '_LC_adv') {
14779:                                     if ($inconf{$type} eq '0') {
14780:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14781:                                     } else { 
14782:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14783:                                     }
14784:                                 } else {
14785:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14786:                                 }
14787:                             }
14788:                         }
14789:                     }
14790:                     unless ($context eq 'requestauthor') {
14791:                         $resulttext .= '</ul></li>';
14792:                     }
14793:                 }
14794:             }
14795:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14796:                 if (ref($changes{'notify'}) eq 'HASH') {
14797:                     if ($changes{'notify'}{'approval'}) {
14798:                         if (ref($confhash{'notify'}) eq 'HASH') {
14799:                             if ($confhash{'notify'}{'approval'}) {
14800:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14801:                             } else {
14802:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14803:                             }
14804:                         }
14805:                     }
14806:                 }
14807:             }
14808:             if ($action eq 'requestcourses') {
14809:                 my @offon = ('off','on');
14810:                 if ($changes{'uniquecode'}) {
14811:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14812:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14813:                         $resulttext .= '<li>'.
14814:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14815:                                        '</li>';
14816:                     } else {
14817:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14818:                                        '</li>';
14819:                     }
14820:                 }
14821:                 foreach my $type ('textbooks','templates') {
14822:                     if (ref($changes{$type}) eq 'HASH') {
14823:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14824:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14825:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14826:                             my $coursetitle = $coursehash{'description'};
14827:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14828:                             $resulttext .= '<li>';
14829:                             foreach my $item ('subject','title','publisher','author') {
14830:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14831:                                          ($type eq 'templates'));
14832:                                 my $name = $item.':';
14833:                                 $name =~ s/^(\w)/\U$1/;
14834:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14835:                             }
14836:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14837:                             if ($type eq 'textbooks') {
14838:                                 if ($confhash{$type}{$key}{'image'}) {
14839:                                     $resulttext .= ' '.&mt('Image: [_1]',
14840:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14841:                                                    ' alt="Textbook cover" />').'<br />';
14842:                                 }
14843:                             }
14844:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14845:                         }
14846:                         $resulttext .= '</ul></li>';
14847:                     }
14848:                 }
14849:                 if (ref($changes{'validation'}) eq 'HASH') {
14850:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14851:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14852:                         foreach my $item (@{$validationitemsref}) {
14853:                             if (exists($changes{'validation'}{$item})) {
14854:                                 if ($item eq 'markup') {
14855:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14856:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14857:                                 } else {
14858:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14859:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14860:                                 }
14861:                             }
14862:                         }
14863:                         if (exists($changes{'validation'}{'dc'})) {
14864:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14865:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14866:                         }
14867:                         $resulttext .= '</ul></li>';
14868:                     }
14869:                 }
14870:             }
14871:             $resulttext .= '</ul>';
14872:             if (keys(%newenv)) {
14873:                 &Apache::lonnet::appenv(\%newenv);
14874:             }
14875:         } else {
14876:             if ($context eq 'requestcourses') {
14877:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14878:             } elsif ($context eq 'requestauthor') {
14879:                 $resulttext = &mt('No changes made to rights to request author space.');
14880:             } else {
14881:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14882:             }
14883:         }
14884:     } else {
14885:         $resulttext = '<span class="LC_error">'.
14886: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14887:     }
14888:     if ($errors) {
14889:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14890:                        '<ul>'.$errors.'</ul></p>';
14891:     }
14892:     return $resulttext;
14893: }
14894: 
14895: sub process_textbook_image {
14896:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14897:     my $filename = $env{'form.'.$caller.'.filename'};
14898:     my ($error,$url);
14899:     my ($width,$height) = (50,50);
14900:     if ($configuserok eq 'ok') {
14901:         if ($switchserver) {
14902:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14903:                          $switchserver);
14904:         } elsif ($author_ok eq 'ok') {
14905:             my $modified = [];
14906:             my ($result,$imageurl) =
14907:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14908:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14909:                                                         '',$modified);
14910:             if ($result eq 'ok') {
14911:                 $url = $imageurl;
14912:                 &update_modify_urls($r,$modified);
14913:             } else {
14914:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14915:             }
14916:         } else {
14917:             $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);
14918:         }
14919:     } else {
14920:         $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);
14921:     }
14922:     return ($url,$error);
14923: }
14924: 
14925: sub modify_ltitools {
14926:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14927:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14928:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14929: 
14930:     my $confname = $dom.'-domainconfig';
14931:     my $servadm = $r->dir_config('lonAdmEMail');
14932:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14933: 
14934:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14935:     my $toolserror =
14936:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14937:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14938: 
14939:     my $home = &Apache::lonnet::domain($dom,'primary');
14940:     unless (($home eq 'no_host') || ($home eq '')) {
14941:         my @ids=&Apache::lonnet::current_machine_ids();
14942:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14943:     }
14944: 
14945:     if (keys(%ltitoolschg)) {
14946:         foreach my $id (keys(%ltitoolschg)) {
14947:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14948:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14949:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14950:                         if ($is_home) {
14951:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14952:                         }
14953:                     }
14954:                 }
14955:             }
14956:         }
14957:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14958:         if (keys(%ltitoolschg)) {
14959:             %newltitools = %ltitoolschg;
14960:         }
14961:     }
14962:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14963:         foreach my $id (%{$domconfig{'ltitools'}}) {
14964:             next if ($id !~ /^\d+$/);
14965:             unless (exists($ltitoolschg{$id})) {
14966:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14967:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14968:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14969:                             if ($is_home) {
14970:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14971:                             }
14972:                         } else {
14973:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14974:                         }
14975:                     }
14976:                 } else {
14977:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14978:                 }
14979:             }
14980:         }
14981:     }
14982:     if ($toolserror) {
14983:         $errors = '<li>'.$toolserror.'</li>';
14984:     }
14985:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14986:         $resulttext = &mt('No changes made.');
14987:         if ($errors) {
14988:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14989:                                  $errors.'</ul>';
14990:         }
14991:         return $resulttext;
14992:     }
14993:     my %ltitoolshash = (
14994:                           $action => { %newltitools }
14995:                        );
14996:     if (keys(%secchanges)) {
14997:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14998:     }
14999:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
15000:     if ($putresult eq 'ok') {
15001:         my %keystore;
15002:         if ($is_home) {
15003:             my %toolsenchash = (
15004:                                    $action => { %newtoolsenc }
15005:                                );
15006:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
15007:             my $cachetime = 24*60*60;
15008:             &Apache::lonnet::do_cache_new('ltitoolsenc',$dom,\%newtoolsenc,$cachetime);
15009:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
15010:         }
15011:         $resulttext = &mt('Changes made:').'<ul>';
15012:         if (keys(%secchanges) > 0) {
15013:             $resulttext .= &lti_security_results($dom,'ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
15014:         }
15015:         if (keys(%ltitoolschg) > 0) {
15016:             $resulttext .= $ltitoolsoutput;
15017:         }
15018:         my $cachetime = 24*60*60;
15019:         &Apache::lonnet::do_cache_new('ltitools',$dom,\%newltitools,$cachetime);
15020:         if (ref($lastactref) eq 'HASH') {
15021:             $lastactref->{'ltitools'} = 1;
15022:         }
15023:     } else {
15024:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15025:     }
15026:     if ($errors) {
15027:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
15028:                        $errors.'</ul></p>';
15029:     }
15030:     return $resulttext;
15031: }
15032: 
15033: sub fetch_secrets {
15034:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
15035:     my %keyset;
15036:     %{$currsec} = ();
15037:     $newsec->{'private'}{'keys'} = [];
15038:     $newsec->{'encrypt'} = {};
15039:     $newsec->{'rules'} = {};
15040:     if ($context eq 'ltisec') {
15041:         $newsec->{'linkprot'} = {};
15042:     }
15043:     if (ref($domconfig->{$context}) eq 'HASH') {
15044:         %{$currsec} = %{$domconfig->{$context}};
15045:         if ($context eq 'ltisec') {
15046:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
15047:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
15048:                     unless ($id =~ /^\d+$/) {
15049:                         delete($currsec->{'linkprot'}{$id});
15050:                     }
15051:                 }
15052:             }
15053:         }
15054:         if (ref($currsec->{'private'}) eq 'HASH') {
15055:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
15056:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
15057:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
15058:             }
15059:         }
15060:     }
15061:     my @items= ('crs','dom');
15062:     if ($context eq 'ltisec') {
15063:         push(@items,'consumers');
15064:     }
15065:     foreach my $item (@items) {
15066:         my $formelement;
15067:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
15068:             $formelement = 'form.'.$context.'_'.$item;
15069:         } else {
15070:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
15071:         }
15072:         if ($env{$formelement}) {
15073:             $newsec->{'encrypt'}{$item} = 1;
15074:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
15075:                 unless ($currsec->{'encrypt'}{$item}) {
15076:                     $secchanges->{'encrypt'} = 1;
15077:                 }
15078:             } else {
15079:                 $secchanges->{'encrypt'} = 1;
15080:             }
15081:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
15082:             if ($currsec->{'encrypt'}{$item}) {
15083:                 $secchanges->{'encrypt'} = 1;
15084:             }
15085:         }
15086:     }
15087:     my $secrets;
15088:     if ($context eq 'ltisec') {
15089:         $secrets = 'ltisecrets';
15090:     } else {
15091:         $secrets = 'toolsecrets';
15092:     }
15093:     unless (exists($currsec->{'rules'})) {
15094:         $currsec->{'rules'} = {};
15095:     }
15096:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
15097: 
15098:     my @ids=&Apache::lonnet::current_machine_ids();
15099:     my %servers = &Apache::lonnet::get_servers($dom,'library');
15100: 
15101:     foreach my $hostid (keys(%servers)) {
15102:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
15103:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
15104:             if (exists($env{$keyitem})) {
15105:                 $env{$keyitem} =~ s/(`)/'/g;
15106:                 if ($keyset{$hostid}) {
15107:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
15108:                         if ($env{$keyitem} ne '') {
15109:                             $secchanges->{'private'} = 1;
15110:                             $newkeyset->{$hostid} = $env{$keyitem};
15111:                         }
15112:                     }
15113:                 } elsif ($env{$keyitem} ne '') {
15114:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
15115:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
15116:                     }
15117:                     $secchanges->{'private'} = 1;
15118:                     $newkeyset->{$hostid} = $env{$keyitem};
15119:                 }
15120:             }
15121:         }
15122:     }
15123: }
15124: 
15125: sub store_security {
15126:     my ($dom,$context,$secchanges,$newkeyset,$keystore) = @_;
15127:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
15128:                    (ref($keystore) eq 'HASH'));
15129:     if (keys(%{$secchanges})) {
15130:         if ($secchanges->{'private'}) {
15131:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
15132:             foreach my $hostid (keys(%{$newkeyset})) {
15133:                 my $storehash = {
15134:                                    key => $newkeyset->{$hostid},
15135:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
15136:                                 };
15137:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
15138:                                                                   $dom,$hostid);
15139:             }
15140:         }
15141:     }
15142: }
15143: 
15144: sub lti_security_results {
15145:     my ($dom,$context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
15146:     my $output;
15147:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
15148:     my $needs_update;
15149:     foreach my $item (keys(%{$secchanges})) {
15150:         if ($item eq 'encrypt') {
15151:             $needs_update = 1;
15152:             my %encrypted;
15153:             if ($context eq 'lti') {
15154:                 %encrypted = (
15155:                               crs  => {
15156:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
15157:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
15158:                                       },
15159:                               dom => {
15160:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
15161:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
15162:                                      },
15163:                               consumers => {
15164:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
15165:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
15166:                                            },
15167:                              );
15168:             } else {
15169:                 %encrypted = (
15170:                               crs  => {
15171:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
15172:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
15173:                                       },
15174:                               dom => {
15175:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
15176:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
15177:                                      },
15178:                              );
15179:             }
15180:             my @types= ('crs','dom');
15181:             if ($context eq 'lti') {
15182:                 foreach my $type (@types) {
15183:                     undef($domdefaults{'linkprotenc_'.$type});
15184:                 }
15185:                 push(@types,'consumers');
15186:                 undef($domdefaults{'ltienc_consumers'});
15187:             } elsif ($context eq 'ltitools') {
15188:                 foreach my $type (@types) {
15189:                     undef($domdefaults{'toolenc_'.$type});
15190:                 }
15191:             }
15192:             foreach my $type (@types) {
15193:                 my $shown = $encrypted{$type}{'off'};
15194:                 if (ref($newsec->{$item}) eq 'HASH') {
15195:                     if ($newsec->{$item}{$type}) {
15196:                         if ($context eq 'lti') {
15197:                             if ($type eq 'consumers') {
15198:                                 $domdefaults{'ltienc_consumers'} = 1;
15199:                             } else {
15200:                                 $domdefaults{'linkprotenc_'.$type} = 1;
15201:                             }
15202:                         } elsif ($context eq 'ltitools') {
15203:                             $domdefaults{'toolenc_'.$type} = 1;
15204:                         }
15205:                         $shown = $encrypted{$type}{'on'};
15206:                     }
15207:                 }
15208:                 $output .= '<li>'.$shown.'</li>';
15209:             }
15210:         } elsif ($item eq 'rules') {
15211:             my %titles = &Apache::lonlocal::texthash(
15212:                                       min   => 'Minimum password length',
15213:                                       max   => 'Maximum password length',
15214:                                       chars => 'Required characters',
15215:                          );
15216:             foreach my $rule ('min','max') {
15217:                 if ($newsec->{rules}{$rule} eq '') {
15218:                     if ($rule eq 'min') {
15219:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
15220:                                    ' '.&mt('Default of [_1] will be used',
15221:                                            $Apache::lonnet::passwdmin).'</li>';
15222:                     } else {
15223:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
15224:                     }
15225:                 } else {
15226:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
15227:                 }
15228:             }
15229:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
15230:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
15231:                     my %rulenames = &Apache::lonlocal::texthash(
15232:                                              uc => 'At least one upper case letter',
15233:                                              lc => 'At least one lower case letter',
15234:                                              num => 'At least one number',
15235:                                              spec => 'At least one non-alphanumeric',
15236:                                     );
15237:                     my $needed = '<ul><li>'.
15238:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
15239:                                  '</li></ul>';
15240:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
15241:                 } else {
15242:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15243:                 }
15244:             } else {
15245:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
15246:             }
15247:         } elsif ($item eq 'private') {
15248:             $needs_update = 1;
15249:             if ($context eq 'lti') {
15250:                 undef($domdefaults{'ltiprivhosts'});
15251:             } elsif ($context eq 'ltitools') {
15252:                 undef($domdefaults{'toolprivhosts'});
15253:             }
15254:             if (keys(%{$newkeyset})) {
15255:                 my @privhosts;
15256:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
15257:                     if ($keystore->{$hostid} eq 'ok') {
15258:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
15259:                         unless (grep(/^\Q$hostid\E$/,@privhosts)) {
15260:                             push(@privhosts,$hostid);
15261:                         }
15262:                     }
15263:                 }
15264:                 if (@privhosts) {
15265:                     if ($context eq 'lti') {
15266:                         $domdefaults{'ltiprivhosts'} = \@privhosts;
15267:                     } elsif ($context eq 'ltitools') {
15268:                         $domdefaults{'toolprivhosts'} = \@privhosts;
15269:                     }
15270:                 }
15271:             }
15272:         } elsif ($item eq 'linkprot') {
15273:             next;
15274:         } elsif ($item eq 'suggested') {
15275:             if ((ref($secchanges->{'suggested'}) eq 'HASH') &&
15276:                 (ref($newsec->{'suggested'}) eq 'HASH')) {
15277:                 my $suggestions;
15278:                 foreach my $id (sort { $a <=> $b } keys(%{$secchanges->{'suggested'}})) {
15279:                     if (ref($newsec->{'suggested'}->{$id}) eq 'HASH') {
15280:                         my $name = $newsec->{'suggested'}->{$id}->{'name'};
15281:                         my $info = $newsec->{'suggested'}->{$id}->{'info'};
15282:                         $suggestions .= '<li>'.&mt('Launcher: [_1]',$name).'<br />'.
15283:                                                &mt('Recommend: [_1]','<pre>'.$info.'</pre>').
15284:                                         '</li>';
15285:                     } else {
15286:                         $suggestions .= '<li>'.&mt('Recommendations deleted for Launcher: [_1]',
15287:                                                    $newsec->{'suggested'}->{$id}).'</li>';
15288:                     }
15289:                 }
15290:                 if ($suggestions) {
15291:                     $output .= '<li>'.&mt('Hints in Courses for Link Protector Configuration').
15292:                                '<ul>'.$suggestions.'</ul>'.
15293:                                '</li>';
15294:                 }
15295:             }
15296:         }
15297:     }
15298:     if ($needs_update) {
15299:         my $cachetime = 24*60*60;
15300:         &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15301:     }
15302:     return $output;
15303: }
15304: 
15305: sub modify_proctoring {
15306:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15307:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15308:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
15309:     my $confname = $dom.'-domainconfig';
15310:     my $servadm = $r->dir_config('lonAdmEMail');
15311:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
15312:     my %providernames = &proctoring_providernames();
15313:     my $maxnum = scalar(keys(%providernames));
15314: 
15315:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
15316:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
15317:     if (ref($requref) eq 'HASH') {
15318:         %requserfields = %{$requref};
15319:     }
15320:     if (ref($opturef) eq 'HASH') {
15321:         %optuserfields = %{$opturef};
15322:     }
15323:     if (ref($defref) eq 'HASH') {
15324:         %defaults = %{$defref};
15325:     }
15326:     if (ref($extref) eq 'HASH') {
15327:         %extended = %{$extref};
15328:     }
15329:     if (ref($crsref) eq 'HASH') {
15330:         %crsconf = %{$crsref};
15331:     }
15332:     if (ref($rolesref) eq 'ARRAY') {
15333:         @courseroles = @{$rolesref};
15334:     }
15335:     if (ref($ltiref) eq 'ARRAY') {
15336:         @ltiroles = @{$ltiref};
15337:     }
15338: 
15339:     if (ref($domconfig{$action}) eq 'HASH') {
15340:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
15341:         if (@todeleteimages) {
15342:             map { $imgdeletions{$_} = 1; } @todeleteimages;
15343:         }
15344:     }
15345:     my %customadds;
15346:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
15347:     if (@newcustom) {
15348:         map { $customadds{$_} = 1; } @newcustom;
15349:     }
15350:     foreach my $provider (sort(keys(%providernames))) {
15351:         $confhash{$provider} = {};
15352:         my $pos = $env{'form.proctoring_pos_'.$provider};
15353:         $pos =~ s/\D+//g;
15354:         $allpos[$pos] = $provider;
15355:         my (%current,%currentenc);
15356:         my $showroles = 0;
15357:         if (ref($domconfig{$action}) eq 'HASH') {
15358:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15359:                 %current = %{$domconfig{$action}{$provider}};
15360:                 foreach my $item ('key','secret') { 
15361:                     $currentenc{$item} = $current{$item};
15362:                     delete($current{$item});
15363:                 }
15364:             }
15365:         }
15366:         if ($env{'form.proctoring_available_'.$provider}) {
15367:             $confhash{$provider}{'available'} = 1;
15368:             unless ($current{'available'}) {
15369:                 $changes{$provider} = 1;
15370:             }
15371:         } else {
15372:             %{$confhash{$provider}} = %current;
15373:             %{$encconfhash{$provider}} = %currentenc;
15374:             $confhash{$provider}{'available'} = 0;
15375:             if ($current{'available'}) {
15376:                 $changes{$provider} = 1;
15377:             }
15378:         }
15379:         if ($confhash{$provider}{'available'}) {
15380:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
15381:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
15382:                 if ($field eq 'lifetime') {
15383:                     if ($possval =~ /^\d+$/) {
15384:                         $confhash{$provider}{$field} = $possval;
15385:                     }
15386:                 } elsif ($field eq 'version') {
15387:                     if ($possval =~ /^\d+\.\d+$/) {
15388:                         $confhash{$provider}{$field} = $possval;
15389:                     }
15390:                 } elsif ($field eq 'sigmethod') {
15391:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
15392:                         $confhash{$provider}{$field} = $possval;
15393:                     }
15394:                 } elsif ($field eq 'url') {
15395:                     $confhash{$provider}{$field} = $possval;
15396:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
15397:                     $encconfhash{$provider}{$field} = $possval;
15398:                     unless ($currentenc{$field} eq $possval) {
15399:                         $changes{$provider} = 1;
15400:                     }
15401:                 }
15402:                 unless (($field eq 'key') || ($field eq 'secret')) {
15403:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
15404:                         $changes{$provider} = 1;
15405:                     }
15406:                 }
15407:             }
15408:             if ($imgdeletions{$provider}) {
15409:                 $changes{$provider} = 1;
15410:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
15411:                 my ($imageurl,$error) =
15412:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
15413:                                               $configuserok,$switchserver,$author_ok);
15414:                 if ($imageurl) {
15415:                     $confhash{$provider}{'image'} = $imageurl;
15416:                     $changes{$provider} = 1; 
15417:                 }
15418:                 if ($error) {
15419:                     &Apache::lonnet::logthis($error);
15420:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15421:                 }
15422:             } elsif (exists($current{'image'})) {
15423:                 $confhash{$provider}{'image'} = $current{'image'};
15424:             }
15425:             if (ref($requserfields{$provider}) eq 'ARRAY') {
15426:                 if (@{$requserfields{$provider}} > 0) {
15427:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
15428:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
15429:                             $confhash{$provider}{'incdom'} = 1;
15430:                         }
15431:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
15432:                             $changes{$provider} = 1;
15433:                         }
15434:                     }
15435:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
15436:                         $showroles = 1;
15437:                     }
15438:                 }
15439:             }
15440:             $confhash{$provider}{'fields'} = [];
15441:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
15442:                 if (@{$optuserfields{$provider}} > 0) {
15443:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
15444:                     foreach my $field (@{$optuserfields{$provider}}) {
15445:                         if (grep(/^\Q$field\E$/,@optfields)) {
15446:                             push(@{$confhash{$provider}{'fields'}},$field);
15447:                         }
15448:                     }
15449:                 }
15450:                 if (ref($current{'fields'}) eq 'ARRAY') {
15451:                     unless ($changes{$provider}) {
15452:                         my @new = sort(@{$confhash{$provider}{'fields'}});
15453:                         my @old = sort(@{$current{'fields'}}); 
15454:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15455:                         if (@diffs) {
15456:                             $changes{$provider} = 1;
15457:                         }
15458:                     }
15459:                 } elsif (@{$confhash{$provider}{'fields'}}) {
15460:                     $changes{$provider} = 1;
15461:                 }
15462:             }
15463:             if (ref($defaults{$provider}) eq 'ARRAY') {  
15464:                 if (@{$defaults{$provider}} > 0) {
15465:                     my %options;
15466:                     if (ref($extended{$provider}) eq 'HASH') {
15467:                         %options = %{$extended{$provider}};
15468:                     }
15469:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
15470:                     foreach my $field (@{$defaults{$provider}}) {
15471:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
15472:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
15473:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
15474:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
15475:                             }
15476:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
15477:                             foreach my $inner (keys(%{$options{$field}})) {
15478:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
15479:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
15480:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
15481:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
15482:                                     }
15483:                                 } else {
15484:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
15485:                                 }
15486:                             }
15487:                         } else {
15488:                             if (grep(/^\Q$field\E$/,@checked)) {
15489:                                 push(@{$confhash{$provider}{'defaults'}},$field);
15490:                             }
15491:                         }
15492:                     }
15493:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15494:                         if (ref($current{'defaults'}) eq 'ARRAY') {
15495:                             unless ($changes{$provider}) {
15496:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
15497:                                 my @old = sort(@{$current{'defaults'}});
15498:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15499:                                 if (@diffs) {
15500:                                     $changes{$provider} = 1; 
15501:                                 }
15502:                             }
15503:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
15504:                             if (@{$current{'defaults'}}) {
15505:                                 $changes{$provider} = 1;
15506:                             }
15507:                         }
15508:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15509:                         if (ref($current{'defaults'}) eq 'HASH') {
15510:                             unless ($changes{$provider}) {
15511:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
15512:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
15513:                                         $changes{$provider} = 1;
15514:                                         last;
15515:                                     }
15516:                                 }
15517:                             }
15518:                             unless ($changes{$provider}) {
15519:                                 foreach my $key (keys(%{$current{'defaults'}})) {
15520:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
15521:                                         $changes{$provider} = 1;
15522:                                         last;
15523:                                     }
15524:                                 }
15525:                             }
15526:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
15527:                             $changes{$provider} = 1;
15528:                         }
15529:                     }
15530:                 }
15531:             }
15532:             if (ref($crsconf{$provider}) eq 'ARRAY') {
15533:                 if (@{$crsconf{$provider}} > 0) {
15534:                     $confhash{$provider}{'crsconf'} = [];
15535:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
15536:                     foreach my $crsfield (@{$crsconf{$provider}}) {
15537:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
15538:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
15539:                         }
15540:                     }
15541:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
15542:                         unless ($changes{$provider}) {  
15543:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
15544:                             my @old = sort(@{$current{'crsconf'}});
15545:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
15546:                             if (@diffs) {
15547:                                 $changes{$provider} = 1;
15548:                             }
15549:                         }
15550:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
15551:                         $changes{$provider} = 1;
15552:                     }
15553:                 }
15554:             }
15555:             if ($showroles) {
15556:                 $confhash{$provider}{'roles'} = {};
15557:                 foreach my $role (@courseroles) {
15558:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
15559:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
15560:                         $confhash{$provider}{'roles'}{$role} = $poss;
15561:                     }
15562:                 }
15563:                 unless ($changes{$provider}) {
15564:                     if (ref($current{'roles'}) eq 'HASH') {
15565:                         foreach my $role (keys(%{$current{'roles'}})) {
15566:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
15567:                                 $changes{$provider} = 1;
15568:                                 last
15569:                             }
15570:                         }
15571:                         unless ($changes{$provider}) {
15572:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
15573:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
15574:                                     $changes{$provider} = 1;
15575:                                     last;
15576:                                 }
15577:                             }
15578:                         }
15579:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
15580:                         $changes{$provider} = 1;
15581:                     }
15582:                 }
15583:             }
15584:             if (ref($current{'custom'}) eq 'HASH') {
15585:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
15586:                 foreach my $key (keys(%{$current{'custom'}})) {
15587:                     if (grep(/^\Q$key\E$/,@customdels)) {
15588:                         $changes{$provider} = 1;
15589:                     } else {
15590:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
15591:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
15592:                             $changes{$provider} = 1;
15593:                         }
15594:                     }
15595:                 }
15596:             }
15597:             if ($customadds{$provider}) {
15598:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
15599:                 $name =~ s/(`)/'/g;
15600:                 $name =~ s/^\s+//;
15601:                 $name =~ s/\s+$//;
15602:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
15603:                 $value =~ s/(`)/'/g;
15604:                 $value =~ s/^\s+//;
15605:                 $value =~ s/\s+$//;
15606:                 if ($name ne '') {
15607:                     $confhash{$provider}{'custom'}{$name} = $value;
15608:                     $changes{$provider} = 1;
15609:                 }
15610:             }
15611:         }
15612:     }
15613:     if (@allpos > 0) {
15614:         my $idx = 0;
15615:         foreach my $provider (@allpos) {
15616:             if ($provider ne '') {
15617:                 $confhash{$provider}{'order'} = $idx;
15618:                 unless ($changes{$provider}) {
15619:                     if (ref($domconfig{$action}) eq 'HASH') {
15620:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
15621:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
15622:                                 $changes{$provider} = 1;
15623:                             }
15624:                         }
15625:                     }
15626:                 }
15627:                 $idx ++;
15628:             }
15629:         }
15630:     }
15631:     my %proc_hash = (
15632:                           $action => { %confhash }
15633:                        );
15634:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
15635:                                              $dom);
15636:     if ($putresult eq 'ok') {
15637:         my %proc_enchash = (
15638:                              $action => { %encconfhash }
15639:                          );
15640:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
15641:         if (keys(%changes) > 0) {
15642:             my $cachetime = 24*60*60;
15643:             my %procall = %confhash;
15644:             foreach my $provider (keys(%procall)) {
15645:                 if (ref($encconfhash{$provider}) eq 'HASH') {
15646:                     foreach my $key ('key','secret') {
15647:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
15648:                     }
15649:                 }
15650:             }
15651:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
15652:             if (ref($lastactref) eq 'HASH') {
15653:                 $lastactref->{'proctoring'} = 1;
15654:             }
15655:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
15656:             my %bynum;
15657:             foreach my $provider (sort(keys(%changes))) {
15658:                 my $position = $confhash{$provider}{'order'};
15659:                 $bynum{$position} = $provider;
15660:             }
15661:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15662:                 my $provider = $bynum{$pos};
15663:                 my %lt = &proctoring_titles($provider);
15664:                 my %fieldtitles = &proctoring_fieldtitles($provider);
15665:                 if (!$confhash{$provider}{'available'}) {
15666:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
15667:                 } else {
15668:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
15669:                     if ($confhash{$provider}{'image'}) {
15670:                         $resulttext .= '&nbsp;'.
15671:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
15672:                                        ' alt="'.&mt('Proctoring icon').'" />';
15673:                     }
15674:                     $resulttext .= '<ul>';
15675:                     my $position = $pos + 1;
15676:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15677:                     foreach my $key ('version','sigmethod','url','lifetime') {
15678:                         if ($confhash{$provider}{$key} ne '') {
15679:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
15680:                         }
15681:                     }
15682:                     if ($encconfhash{$provider}{'key'} ne '') {
15683:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
15684:                     }
15685:                     if ($encconfhash{$provider}{'secret'} ne '') {
15686:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15687:                         my $num = length($encconfhash{$provider}{'secret'});
15688:                         $resulttext .= ('*'x$num).'</li>';
15689:                     }
15690:                     my (@fields,$showroles);
15691:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15692:                         push(@fields,@{$requserfields{$provider}});
15693:                     }
15694:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
15695:                         push(@fields,@{$confhash{$provider}{'fields'}});
15696:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
15697:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
15698:                     }
15699:                     if (@fields) {
15700:                         if (grep(/^roles$/,@fields)) {
15701:                             $showroles = 1;
15702:                         }
15703:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
15704:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
15705:                     }
15706:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
15707:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
15708:                             if ($confhash{$provider}{'incdom'}) {
15709:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
15710:                             } else { 
15711:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
15712:                             }
15713:                         }
15714:                     }
15715:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
15716:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
15717:                             $resulttext .= '<li>'.$lt{'defa'};
15718:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
15719:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
15720:                             }
15721:                             $resulttext =~ s/,$//;
15722:                             $resulttext .= '</li>';
15723:                         }
15724:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
15725:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
15726:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
15727:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
15728:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
15729:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
15730:                                 }
15731:                             }
15732:                             $resulttext .= '</ul></li>';
15733:                         }
15734:                     }
15735:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
15736:                         if (@{$crsconf{$provider}} > 0) {
15737:                             $resulttext .= '<li>'.&mt('Configurable in course:');
15738:                             my $numconfig = 0;
15739:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
15740:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
15741:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
15742:                                         $numconfig ++;
15743:                                         if ($provider eq 'examity') {
15744:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
15745:                                         } else {
15746:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
15747:                                         }
15748:                                     }
15749:                                     $resulttext =~ s/,$//;
15750:                                 }
15751:                             }
15752:                             if (!$numconfig) {
15753:                                 $resulttext .= '&nbsp;'.&mt('None');
15754:                             }
15755:                             $resulttext .= '</li>';
15756:                         }
15757:                     }
15758:                     if ($showroles) {
15759:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
15760:                             my $rolemaps;
15761:                             foreach my $role (@courseroles) {
15762:                                 if ($confhash{$provider}{'roles'}{$role}) {
15763:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
15764:                                                  $confhash{$provider}{'roles'}{$role}.',';
15765:                                 }
15766:                             }
15767:                             if ($rolemaps) {
15768:                                 $rolemaps =~ s/,$//;
15769:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15770:                             }
15771:                         }
15772:                     }
15773:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
15774:                         my $customlist;
15775:                         if (keys(%{$confhash{$provider}{'custom'}})) {
15776:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
15777:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
15778:                             }
15779:                             $customlist =~ s/,$//;
15780:                         }
15781:                         if ($customlist) {
15782:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
15783:                         }
15784:                     } 
15785:                     $resulttext .= '</ul></li>';
15786:                 }
15787:             }
15788:             $resulttext .= '</ul>';
15789:         } else {
15790:             $resulttext = &mt('No changes made.');
15791:         }
15792:     } else {
15793:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15794:     }
15795:     if ($errors) {
15796:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15797:                        $errors.'</ul>';
15798:     }
15799:     return $resulttext;
15800: }
15801: 
15802: sub process_proctoring_image {
15803:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
15804:     my $filename = $env{'form.'.$caller.'.filename'};
15805:     my ($error,$url);
15806:     my ($width,$height) = (21,21);
15807:     if ($configuserok eq 'ok') {
15808:         if ($switchserver) {
15809:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
15810:                          $switchserver);
15811:         } elsif ($author_ok eq 'ok') {
15812:             my $modified = [];
15813:             my ($result,$imageurl,$madethumb) =
15814:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
15815:                                                         "proctoring/$provider/icon",$width,$height,
15816:                                                         '',$modified);
15817:             if ($result eq 'ok') {
15818:                 if ($madethumb) {
15819:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
15820:                     my $imagethumb = "$path/tn-".$imagefile;
15821:                     $url = $imagethumb;
15822:                 } else {
15823:                     $url = $imageurl;
15824:                 }
15825:                 &update_modify_urls($r,$modified);
15826:             } else {
15827:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15828:             }
15829:         } else {
15830:             $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);
15831:         }
15832:     } else {
15833:         $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);
15834:     }
15835:     return ($url,$error);
15836: }
15837: 
15838: sub modify_lti {
15839:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15840:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15841:     my ($newid,@allpos,%changes,%confhash,%ltienc,$errors,$resulttext);
15842:     my (%posslti,%posslticrs,%posscrstype);
15843:     my @courseroles = ('cc','in','ta','ep','st');
15844:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15845:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15846:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15847:     my %coursetypetitles = &Apache::lonlocal::texthash (
15848:                                official   => 'Official',
15849:                                unofficial => 'Unofficial',
15850:                                community  => 'Community',
15851:                                textbook   => 'Textbook',
15852:                                placement  => 'Placement Test',
15853:                                lti        => 'LTI Provider',
15854:     );
15855:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15856:     my %lt = &lti_names();
15857:     map { $posslti{$_} = 1; } @ltiroles;
15858:     map { $posslticrs{$_} = 1; } @lticourseroles;
15859:     map { $posscrstype{$_} = 1; } @coursetypes;
15860: 
15861:     my %menutitles = &ltimenu_titles();
15862:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15863: 
15864:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15865: 
15866:     my (%linkprotchg,$linkprotoutput,$is_home);
15867:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15868:                                                            \%linkprotchg,'domain');
15869:     my $home = &Apache::lonnet::domain($dom,'primary');
15870:     unless (($home eq 'no_host') || ($home eq '')) {
15871:         my @ids=&Apache::lonnet::current_machine_ids();
15872:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15873:     }
15874: 
15875:     if (keys(%linkprotchg)) {
15876:         $secchanges{'linkprot'} = 1;
15877:         my %oldlinkprot;
15878:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15879:             %oldlinkprot = %{$currltisec{'linkprot'}};
15880:         }
15881:         foreach my $id (keys(%linkprotchg)) {
15882:             if (ref($linkprotchg{$id}) eq 'HASH') {
15883:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15884:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15885:                         if ($is_home) {
15886:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15887:                         }
15888:                     }
15889:                 }
15890:             } else {
15891:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15892:             }
15893:         }
15894:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15895:         if (keys(%linkprotchg)) {
15896:             %{$newltisec{'linkprot'}} = %linkprotchg;
15897:         }
15898:     }
15899:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15900:         foreach my $id (keys(%{$currltisec{'linkprot'}})) {
15901:             next if ($id !~ /^\d+$/);
15902:             unless (exists($linkprotchg{$id})) {
15903:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15904:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15905:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15906:                             if ($is_home) {
15907:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15908:                             }
15909:                         } else {
15910:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15911:                         }
15912:                     }
15913:                 } else {
15914:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15915:                 }
15916:             }
15917:         }
15918:     }
15919:     if ($proterror) {
15920:         $errors .= '<li>'.$proterror.'</li>';
15921:     }
15922: 
15923:     my (%delsuggested,%suggids,@suggested);;
15924:     if (ref($currltisec{'suggested'}) eq 'HASH') {
15925:         my $maxnum = $env{'form.linkprot_suggested_maxnum'};
15926:         my @todelete = &Apache::loncommon::get_env_multiple('form.linkprot_suggested_del');
15927:         for (my $i=0; $i<$maxnum; $i++) {
15928:             my $itemid = $env{'form.linkprot_suggested_id_'.$i};
15929:             $itemid =~ s/\D+//g;
15930:             if ($itemid) {
15931:                 if (ref($currltisec{'suggested'}->{$itemid}) eq 'HASH') {
15932:                     push(@suggested,$i);
15933:                     $suggids{$i} = $itemid;
15934:                     if ((@todelete > 0) && (grep(/^$i$/,@todelete))) {
15935:                         if (ref($currltisec{'suggested'}{$itemid}) eq 'HASH') {
15936:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15937:                         }
15938:                     } else {
15939:                         if ($env{'form.linkprot_suggested_name_'.$i} eq '') {
15940:                             $delsuggested{$itemid} = $currltisec{'suggested'}{$itemid}{'name'};
15941:                         } else {
15942:                             $env{'form.linkprot_suggested_name_'.$i} =~ s/(`)/'/g;
15943:                             $env{'form.linkprot_suggested_info_'.$i} =~ s/(`)/'/g;
15944:                             $newltisec{'suggested'}{$itemid}{'name'} = $env{'form.linkprot_suggested_name_'.$i};
15945:                             $newltisec{'suggested'}{$itemid}{'info'} = $env{'form.linkprot_suggested_info_'.$i};
15946:                             if (($currltisec{'suggested'}{$itemid}{'name'} ne $newltisec{'suggested'}{$itemid}{'name'}) ||
15947:                                 ($currltisec{'suggested'}{$itemid}{'info'} ne $newltisec{'suggested'}{$itemid}{'info'})) {
15948:                                 $secchanges{'suggested'}{$itemid} = 1;
15949:                             }
15950:                         }
15951:                     }
15952:                 }
15953:             }
15954:         }
15955:     }
15956:     foreach my $key (keys(%delsuggested)) {
15957:         $newltisec{'suggested'}{$key} = $delsuggested{$key};
15958:         $secchanges{'suggested'}{$key} = 1;
15959:     }
15960:     if (($env{'form.linkprot_suggested_add'}) &&
15961:         ($env{'form.linkprot_suggested_name_add'} ne '')) {
15962:         $env{'form.linkprot_suggested_name_add'} =~ s/(`)/'/g;
15963:         $env{'form.linkprot_suggested_info_add'} =~ s/(`)/'/g;
15964:         my ($newsuggid,$errormsg) = &get_lti_id($dom,$env{'form.linkprot_suggested_name_add'},'suggested');
15965:         if ($newsuggid) {
15966:             $newltisec{'suggested'}{$newsuggid}{'name'} = $env{'form.linkprot_suggested_name_add'};
15967:             $newltisec{'suggested'}{$newsuggid}{'info'} = $env{'form.linkprot_suggested_info_add'};
15968:             $secchanges{'suggested'}{$newsuggid} = 1;
15969:         } else {
15970:             my $error = &mt('Failed to acquire unique ID for new Link Protectors in Courses Suggestion');
15971:             if ($errormsg) {
15972:                 $error .= ' ('.$errormsg.')';
15973:             }
15974:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15975:         }
15976:     }
15977:     my (@items,%deletions,%itemids);
15978:     if ($env{'form.lti_add'}) {
15979:         my $consumer = $env{'form.lti_consumer_add'};
15980:         $consumer =~ s/(`)/'/g;
15981:         ($newid,my $errormsg) = &get_lti_id($dom,$consumer,'lti');
15982:         if ($newid) {
15983:             $itemids{'add'} = $newid;
15984:             push(@items,'add');
15985:             $changes{$newid} = 1;
15986:         } else {
15987:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15988:             if ($errormsg) {
15989:                 $error .= ' ('.$errormsg.')';
15990:             }
15991:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15992:         }
15993:     }
15994:     if (ref($domconfig{$action}) eq 'HASH') {
15995:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15996:         if (@todelete) {
15997:             map { $deletions{$_} = 1; } @todelete;
15998:         }
15999:         my $maxnum = $env{'form.lti_maxnum'};
16000:         for (my $i=0; $i<$maxnum; $i++) {
16001:             my $itemid = $env{'form.lti_id_'.$i};
16002:             $itemid =~ s/\D+//g;
16003:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16004:                 if ($deletions{$itemid}) {
16005:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
16006:                 } else {
16007:                     push(@items,$i);
16008:                     $itemids{$i} = $itemid;
16009:                 }
16010:             }
16011:         }
16012:     }
16013:     my (%keystore,$secstored);
16014:     if ($is_home) {
16015:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore);
16016:     }
16017: 
16018:     my ($cipher,$privnum);
16019:     if ((@items > 0) && ($is_home)) {
16020:         ($cipher,$privnum) = &get_priv_creds($dom,$home,$secchanges{'encrypt'},
16021:                                              $newltisec{'encrypt'},$keystore{$home});
16022:     }
16023:     foreach my $idx (@items) {
16024:         my $itemid = $itemids{$idx};
16025:         next unless ($itemid);
16026:         my %currlti;
16027:         unless ($idx eq 'add') {
16028:             if (ref($domconfig{$action}) eq 'HASH') {
16029:                 if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16030:                     %currlti = %{$domconfig{$action}{$itemid}};
16031:                 }
16032:             }
16033:         }
16034:         my $position = $env{'form.lti_pos_'.$itemid};
16035:         $position =~ s/\D+//g;
16036:         if ($position ne '') {
16037:             $allpos[$position] = $itemid;
16038:         }
16039:         foreach my $item ('consumer','lifetime','requser','crsinc') {
16040:             my $formitem = 'form.lti_'.$item.'_'.$idx;
16041:             $env{$formitem} =~ s/(`)/'/g;
16042:             if ($item eq 'lifetime') {
16043:                 $env{$formitem} =~ s/[^\d.]//g;
16044:             }
16045:             if ($env{$formitem} ne '') {
16046:                 $confhash{$itemid}{$item} = $env{$formitem};
16047:                 unless (($idx eq 'add') || ($changes{$itemid})) {
16048:                     if ($currlti{$item} ne $confhash{$itemid}{$item}) {
16049:                         $changes{$itemid} = 1;
16050:                     }
16051:                 }
16052:             }
16053:         }
16054:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
16055:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
16056:         }
16057:         if ($confhash{$itemid}{'requser'}) {
16058:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
16059:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
16060:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
16061:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
16062:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
16063:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
16064:                 $mapuser =~ s/(`)/'/g;
16065:                 $mapuser =~ s/^\s+|\s+$//g;
16066:                 $confhash{$itemid}{'mapuser'} = $mapuser;
16067:             }
16068:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
16069:             my @makeuser;
16070:             foreach my $ltirole (sort(@possmakeuser)) {
16071:                 if ($posslti{$ltirole}) {
16072:                     push(@makeuser,$ltirole);
16073:                 }
16074:             }
16075:             $confhash{$itemid}{'makeuser'} = \@makeuser;
16076:             if (@makeuser) {
16077:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
16078:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
16079:                     $confhash{$itemid}{'lcauth'} = $lcauth;
16080:                     if ($lcauth ne 'internal') {
16081:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
16082:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
16083:                         $lcauthparm =~ s/`//g;
16084:                         if ($lcauthparm ne '') {
16085:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
16086:                         }
16087:                     }
16088:                 } else {
16089:                     $confhash{$itemid}{'lcauth'} = 'lti';
16090:                 }
16091:             }
16092:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
16093:             if (@possinstdata) {
16094:                 foreach my $field (sort(@possinstdata)) {
16095:                     if (exists($fieldtitles{$field})) {
16096:                         push(@{$confhash{$itemid}{'instdata'}},$field);
16097:                     }
16098:                 }
16099:             }
16100:             if ($env{'form.lti_callback_'.$idx}) {
16101:                 if ($env{'form.lti_callbackparam_'.$idx}) {
16102:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
16103:                     $callback =~ s/^\s+|\s+$//g;
16104:                     $confhash{$itemid}{'callback'} = $callback;
16105:                 }
16106:             }
16107:             foreach my $field ('topmenu','inlinemenu') {
16108:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
16109:                     $confhash{$itemid}{$field} = 1;
16110:                 }
16111:             }
16112:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
16113:                 $confhash{$itemid}{lcmenu} = [];
16114:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
16115:                 foreach my $field (@possmenu) {
16116:                     if (exists($menutitles{$field})) {
16117:                         if ($field eq 'grades') {
16118:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
16119:                         }
16120:                         push(@{$confhash{$itemid}{lcmenu}},$field);
16121:                     }
16122:                 }
16123:             }
16124:             if ($confhash{$itemid}{'crsinc'}) {
16125:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
16126:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
16127:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
16128:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
16129:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
16130:                     $mapcrs =~ s/(`)/'/g;
16131:                     $mapcrs =~ s/^\s+|\s+$//g;
16132:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
16133:                 }
16134:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
16135:                 my @crstypes;
16136:                 foreach my $type (sort(@posstypes)) {
16137:                     if ($posscrstype{$type}) {
16138:                         push(@crstypes,$type);
16139:                     }
16140:                 }
16141:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
16142:                 if ($env{'form.lti_storecrs_'.$idx}) {
16143:                     $confhash{$itemid}{'storecrs'} = 1;
16144:                 }       
16145:                 if ($env{'form.lti_makecrs_'.$idx}) {
16146:                     $confhash{$itemid}{'makecrs'} = 1;
16147:                 }
16148:                 foreach my $ltirole (@lticourseroles) {
16149:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
16150:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
16151:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
16152:                     }
16153:                 }
16154:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
16155:                 my @selfenroll;
16156:                 foreach my $type (sort(@possenroll)) {
16157:                     if ($posslticrs{$type}) {
16158:                         push(@selfenroll,$type);
16159:                     }
16160:                 }
16161:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
16162:                 if ($env{'form.lti_crssec_'.$idx}) {
16163:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
16164:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
16165:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
16166:                         my $section = $env{'form.lti_customsection_'.$idx};
16167:                         $section =~ s/(`)/'/g;
16168:                         $section =~ s/^\s+|\s+$//g;
16169:                         if ($section ne '') {
16170:                             $confhash{$itemid}{'section'} = $section;
16171:                         }
16172:                     }
16173:                 }
16174:                 foreach my $field ('passback','roster') {
16175:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
16176:                         $confhash{$itemid}{$field} = 1;
16177:                     }
16178:                 }
16179:                 if ($env{'form.lti_passback_'.$idx}) {
16180:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
16181:                         $confhash{$itemid}{'passbackformat'} = '1.0';
16182:                     } else {
16183:                         $confhash{$itemid}{'passbackformat'} = '1.1';
16184:                     }
16185:                 }
16186:             }
16187:             unless (($idx eq 'add') || ($changes{$itemid})) {
16188:                 if ($confhash{$itemid}{'crsinc'}) {
16189:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
16190:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16191:                             $changes{$itemid} = 1;
16192:                         }
16193:                     }
16194:                     unless ($changes{$itemid}) {
16195:                         if ($currlti{'passback'} eq $confhash{$itemid}{'passback'}) {
16196:                             if ($currlti{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
16197:                                 $changes{$itemid} = 1;
16198:                             }
16199:                         }
16200:                     }
16201:                     foreach my $field ('mapcrstype','selfenroll') {
16202:                         unless ($changes{$itemid}) {
16203:                             if (ref($currlti{$field}) eq 'ARRAY') {
16204:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16205:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16206:                                                                                    $confhash{$itemid}{$field});
16207:                                     if (@diffs) {
16208:                                         $changes{$itemid} = 1;
16209:                                     }
16210:                                 } elsif (@{$currlti{$field}} > 0) {
16211:                                     $changes{$itemid} = 1;
16212:                                 }
16213:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16214:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16215:                                     $changes{$itemid} = 1;
16216:                                 }
16217:                             }
16218:                         }
16219:                     }
16220:                     unless ($changes{$itemid}) {
16221:                         if (ref($currlti{'maproles'}) eq 'HASH') {
16222:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16223:                                 foreach my $ltirole (keys(%{$currlti{'maproles'}})) {
16224:                                     if ($currlti{'maproles'}{$ltirole} ne 
16225:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
16226:                                         $changes{$itemid} = 1;
16227:                                         last;
16228:                                     }
16229:                                 }
16230:                                 unless ($changes{$itemid}) {
16231:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
16232:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
16233:                                             $currlti{'maproles'}{$ltirole}) {
16234:                                             $changes{$itemid} = 1;
16235:                                             last;
16236:                                         }
16237:                                     }
16238:                                 }
16239:                             } elsif (keys(%{$currlti{'maproles'}}) > 0) {
16240:                                 $changes{$itemid} = 1;
16241:                             }
16242:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16243:                             unless ($changes{$itemid}) {
16244:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
16245:                                     $changes{$itemid} = 1;
16246:                                 }
16247:                             }
16248:                         }
16249:                     }
16250:                 }
16251:                 unless ($changes{$itemid}) {
16252:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
16253:                         if ($currlti{$field} ne $confhash{$itemid}{$field}) {
16254:                             $changes{$itemid} = 1;
16255:                         }
16256:                     }
16257:                     unless ($changes{$itemid}) {
16258:                         foreach my $field ('makeuser','lcmenu','instdata') {
16259:                             if (ref($currlti{$field}) eq 'ARRAY') {
16260:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16261:                                     my @diffs = &Apache::loncommon::compare_arrays($currlti{$field},
16262:                                                                                    $confhash{$itemid}{$field});
16263:                                     if (@diffs) {
16264:                                         $changes{$itemid} = 1;
16265:                                     }
16266:                                 } elsif (@{$currlti{$field}} > 0) {
16267:                                     $changes{$itemid} = 1;
16268:                                 }
16269:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
16270:                                 if (@{$confhash{$itemid}{$field}} > 0) {
16271:                                     $changes{$itemid} = 1;
16272:                                 }
16273:                             }
16274:                         }
16275:                     }
16276:                 }
16277:             }
16278:         }
16279:         if ($is_home) {
16280:             my $keyitem = 'form.lti_key_'.$idx;
16281:             $env{$keyitem} =~ s/(`)/'/g;
16282:             if ($env{$keyitem} ne '') {
16283:                 $ltienc{$itemid}{'key'} = $env{$keyitem};
16284:                 unless ($changes{$itemid}) {
16285:                     if ($currlti{'key'} ne $env{$keyitem}) {
16286:                         $changes{$itemid} = 1;
16287:                     }
16288:                 }
16289:             }
16290:             my $secretitem = 'form.lti_secret_'.$idx;
16291:             $env{$secretitem} =~ s/(`)/'/g;
16292:             if ($currlti{'usable'}) {
16293:                 if ($env{'form.lti_changesecret_'.$idx}) {
16294:                     if ($env{$secretitem} ne '') {
16295:                         if ($privnum && $cipher) {
16296:                             $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16297:                             $confhash{$itemid}{'cipher'} = $privnum;
16298:                         } else {
16299:                             $ltienc{$itemid}{'secret'} = $env{$secretitem};
16300:                         }
16301:                         $changes{$itemid} = 1;
16302:                     }
16303:                 } else {
16304:                     $ltienc{$itemid}{'secret'} = $currlti{'secret'};
16305:                     $confhash{$itemid}{'cipher'} = $currlti{'cipher'};
16306:                 }
16307:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16308:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'secret'} ne '')) {
16309:                         $confhash{$itemid}{'usable'} = 1;
16310:                     }
16311:                 }
16312:             } elsif ($env{$secretitem} ne '') {
16313:                 if ($privnum && $cipher) {
16314:                     $ltienc{$itemid}{'secret'} = $cipher->encrypt_hex($env{$secretitem});
16315:                     $confhash{$itemid}{'cipher'} = $privnum;
16316:                 } else {
16317:                     $ltienc{$itemid}{'secret'} = $env{$secretitem};
16318:                 }
16319:                 if (ref($ltienc{$itemid}) eq 'HASH') {
16320:                     if (($ltienc{$itemid}{'key'} ne '') && ($ltienc{$itemid}{'key'} ne '')) {
16321:                         $confhash{$itemid}{'usable'} = 1;
16322:                     }
16323:                 }
16324:                 $changes{$itemid} = 1;
16325:             }
16326:         }
16327:         unless ($changes{$itemid}) {
16328:             foreach my $key (keys(%currlti)) {
16329:                 if (ref($currlti{$key}) eq 'HASH') {
16330:                     if (ref($confhash{$itemid}{$key}) eq 'HASH') {
16331:                         foreach my $innerkey (keys(%{$currlti{$key}})) {
16332:                             unless (exists($confhash{$itemid}{$key}{$innerkey})) {
16333:                                 $changes{$itemid} = 1;
16334:                                 last;
16335:                             }
16336:                         }
16337:                     } elsif (keys(%{$currlti{$key}}) > 0) {
16338:                         $changes{$itemid} = 1;
16339:                     }
16340:                 }
16341:                 last if ($changes{$itemid});
16342:             }
16343:         }
16344:     }
16345:     if (@allpos > 0) {
16346:         my $idx = 0;
16347:         foreach my $itemid (@allpos) {
16348:             if ($itemid ne '') {
16349:                 $confhash{$itemid}{'order'} = $idx;
16350:                 if (ref($domconfig{$action}) eq 'HASH') {
16351:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
16352:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
16353:                             $changes{$itemid} = 1;
16354:                         }
16355:                     }
16356:                 }
16357:                 $idx ++;
16358:             }
16359:         }
16360:     }
16361: 
16362:     if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
16363:         return &mt('No changes made.');
16364:     }
16365: 
16366:     my %ltihash = (
16367:                       $action => { %confhash }
16368:                   );
16369:     my %ltienchash;
16370: 
16371:     if ($is_home) {
16372:         %ltienchash = (
16373:                          $action => { %ltienc }
16374:                       );
16375:     }
16376:     if (keys(%secchanges)) {
16377:         $ltihash{'ltisec'} = \%newltisec;
16378:         if ($secchanges{'linkprot'}) {
16379:             if ($is_home) {
16380:                 $ltienchash{'linkprot'} = \%newltienc;
16381:             }
16382:         }
16383:     }
16384:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
16385:     if ($putresult eq 'ok') {
16386:         if (keys(%ltienchash)) {
16387:             &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
16388:         }
16389:         $resulttext = &mt('Changes made:').'<ul>';
16390:         if (keys(%secchanges) > 0) {
16391:             $resulttext .= &lti_security_results($dom,'lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
16392:             if (exists($secchanges{'linkprot'})) {
16393:                 $resulttext .= $linkprotoutput;
16394:             }
16395:         }
16396:         if (keys(%changes) > 0) {
16397:             my $cachetime = 24*60*60;
16398:             &Apache::lonnet::do_cache_new('lti',$dom,\%confhash,$cachetime);
16399:             if (ref($lastactref) eq 'HASH') {
16400:                 $lastactref->{'lti'} = 1;
16401:             }
16402:             my %bynum;
16403:             foreach my $itemid (sort(keys(%changes))) {
16404:                 if (ref($confhash{$itemid}) eq 'HASH') {
16405:                     my $position = $confhash{$itemid}{'order'};
16406:                     $bynum{$position} = $itemid;
16407:                 }
16408:             }
16409:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
16410:                 my $itemid = $bynum{$pos};
16411:                 if (ref($confhash{$itemid}) eq 'HASH') {
16412:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
16413:                     my $position = $pos + 1;
16414:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
16415:                     foreach my $item ('version','lifetime') {
16416:                         if ($confhash{$itemid}{$item} ne '') {
16417:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
16418:                         }
16419:                     }
16420:                     if ($ltienc{$itemid}{'key'} ne '') {
16421:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$ltienc{$itemid}{'key'}.'</li>';
16422:                     }
16423:                     if ($ltienc{$itemid}{'secret'} ne '') {
16424:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;['.&mt('not shown').']</li>';
16425:                     }
16426:                     if ($confhash{$itemid}{'requser'}) {
16427:                         if ($confhash{$itemid}{'callback'}) {
16428:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
16429:                         } else {
16430:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
16431:                         }
16432:                         if ($confhash{$itemid}{'mapuser'}) {
16433:                             my $shownmapuser;
16434:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
16435:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
16436:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
16437:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
16438:                             } else {
16439:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
16440:                             }
16441:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
16442:                         }
16443:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
16444:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
16445:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
16446:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
16447:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
16448:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
16449:                                 } else {
16450:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
16451:                                                        $confhash{$itemid}{'lcauth'});
16452:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
16453:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
16454:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
16455:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
16456:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
16457:                                         }
16458:                                     } else {
16459:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
16460:                                     }
16461:                                 }
16462:                                 $resulttext .= '</li>';
16463:                             } else {
16464:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
16465:                             }
16466:                         }
16467:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
16468:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
16469:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
16470:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
16471:                             } else {
16472:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user').'</li>';
16473:                             }
16474:                         } else {
16475:                             $resulttext .= '<li>'.&mt('No institutional data used when creating a new user').'</li>';
16476:                         }
16477:                         foreach my $item ('topmenu','inlinemenu') {
16478:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16479:                             if ($confhash{$itemid}{$item}) {
16480:                                 $resulttext .= &mt('Yes');
16481:                             } else {
16482:                                 $resulttext .= &mt('No');
16483:                             }
16484:                             $resulttext .= '</li>';
16485:                         }
16486:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16487:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16488:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
16489:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
16490:                             } else {
16491:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
16492:                             }
16493:                         }
16494:                         if ($confhash{$itemid}{'crsinc'}) {
16495:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
16496:                                 my $rolemaps;
16497:                                 foreach my $role (@ltiroles) {
16498:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
16499:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
16500:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
16501:                                                                                 'Course').',';
16502:                                     }
16503:                                 }
16504:                                 if ($rolemaps) {
16505:                                     $rolemaps =~ s/,$//;
16506:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
16507:                                 }
16508:                             }
16509:                             if ($confhash{$itemid}{'mapcrs'}) {
16510:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
16511:                             }
16512:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
16513:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
16514:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
16515:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
16516:                                                    '</li>';
16517:                                 } else {
16518:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
16519:                                 }
16520:                             }
16521:                             if ($confhash{$itemid}{'storecrs'}) {
16522:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
16523:                             }
16524:                             if ($confhash{$itemid}{'makecrs'}) {
16525:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
16526:                             } else {
16527:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
16528:                             }
16529:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
16530:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
16531:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
16532:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
16533:                                                    '</li>';
16534:                                 } else {
16535:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
16536:                                 }
16537:                             }
16538:                             if ($confhash{$itemid}{'section'}) {
16539:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
16540:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
16541:                                                          ' (course_section_sourcedid)'.'</li>';  
16542:                                 } else {
16543:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
16544:                                                           $confhash{$itemid}{'section'}.'</li>';
16545:                                 }
16546:                             } else {
16547:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
16548:                             }
16549:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
16550:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
16551:                                 if ($confhash{$itemid}{$item}) {
16552:                                     $resulttext .= &mt('Yes');
16553:                                     if ($item eq 'passback') {
16554:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
16555:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
16556:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
16557:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
16558:                                         }
16559:                                     }
16560:                                 } else {
16561:                                     $resulttext .= &mt('No');
16562:                                 }
16563:                                 $resulttext .= '</li>';
16564:                             }
16565:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
16566:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
16567:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
16568:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
16569:                                 } else {
16570:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
16571:                                 }
16572:                             }
16573:                         }
16574:                     }
16575:                     $resulttext .= '</ul></li>';
16576:                 }
16577:             }
16578:             if (keys(%deletions)) {
16579:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
16580:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
16581:                 }
16582:             }
16583:         }
16584:         $resulttext .= '</ul>';
16585:         if (ref($lastactref) eq 'HASH') {
16586:             if (($secchanges{'encrypt'}) || ($secchanges{'private'}) || (exists($secchanges{'suggested'}))) {
16587:                 &Apache::lonnet::get_domain_defaults($dom,1);
16588:                 $lastactref->{'domdefaults'} = 1;
16589:             }
16590:         }
16591:     } else {
16592:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
16593:     }
16594:     if ($errors) {
16595:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
16596:                        $errors.'</ul>';
16597:     }
16598:     return $resulttext;
16599: }
16600: 
16601: sub get_priv_creds {
16602:     my ($dom,$home,$encchg,$encrypt,$storedsec) = @_;
16603:     my ($needenc,$cipher,$privnum);
16604:     my %domdefs = &Apache::lonnet::get_domain_defaults($dom);
16605:     if (($encchg) && (ref($encrypt) eq 'HASH')) {
16606:         $needenc = $encrypt->{'consumers'} 
16607:     } else {
16608:         $needenc = $domdefs{'ltienc_consumers'};
16609:     }
16610:     if ($needenc) {
16611:         if (($storedsec eq 'ok') || ((ref($domdefs{'ltiprivhosts'}) eq 'ARRAY') &&
16612:                                      (grep(/^\Q$home\E$/,@{$domdefs{'ltiprivhosts'}})))) {
16613:                         my %privhash  = &Apache::lonnet::restore_dom('lti','private',$dom,$home,1);
16614:             my $privkey = $privhash{'key'};
16615:             $privnum = $privhash{'version'};
16616:             if (($privnum) && ($privkey ne '')) {
16617:                 $cipher = Crypt::CBC->new({'key'     => $privkey,
16618:                                           'cipher'  => 'DES'});
16619:             }
16620:         }
16621:     }
16622:     return ($cipher,$privnum);
16623: }
16624: 
16625: sub get_lti_id {
16626:     my ($domain,$consumer,$dbname) = @_;
16627:     unless (($dbname eq 'lti') || ($dbname eq 'suggested')) {
16628:         return ('','invalid db');
16629:     }
16630:     # get lock on db
16631:     my $lockhash = {
16632:                       lock => $env{'user.name'}.
16633:                               ':'.$env{'user.domain'},
16634:                    };
16635:     my $tries = 0;
16636:     my $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16637:     my ($id,$error);
16638: 
16639:     while (($gotlock ne 'ok') && ($tries<10)) {
16640:         $tries ++;
16641:         sleep (0.1);
16642:         $gotlock = &Apache::lonnet::newput_dom($dbname,$lockhash,$domain);
16643:     }
16644:     if ($gotlock eq 'ok') {
16645:         my %currids = &Apache::lonnet::dump_dom($dbname,$domain);
16646:         if ($currids{'lock'}) {
16647:             delete($currids{'lock'});
16648:             if (keys(%currids)) {
16649:                 my @curr = sort { $a <=> $b } keys(%currids);
16650:                 if ($curr[-1] =~ /^\d+$/) {
16651:                     $id = 1 + $curr[-1];
16652:                 }
16653:             } else {
16654:                 $id = 1;
16655:             }
16656:             if ($id) {
16657:                 unless (&Apache::lonnet::newput_dom($dbname,{ $id => $consumer },$domain) eq 'ok') {
16658:                     $error = 'nostore';
16659:                 }
16660:             } else {
16661:                 $error = 'nonumber';
16662:             }
16663:         }
16664:         my $dellockoutcome = &Apache::lonnet::del_dom($dbname,['lock'],$domain);
16665:     } else {
16666:         $error = 'nolock';
16667:     }
16668:     return ($id,$error);
16669: }
16670: 
16671: sub modify_autoenroll {
16672:     my ($dom,$lastactref,%domconfig) = @_;
16673:     my ($resulttext,%changes);
16674:     my %currautoenroll;
16675:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
16676:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
16677:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
16678:         }
16679:     }
16680:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
16681:     my %title = ( run => 'Auto-enrollment active',
16682:                   sender => 'Sender for notification messages',
16683:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
16684:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
16685:     my @offon = ('off','on');
16686:     my $sender_uname = $env{'form.sender_uname'};
16687:     my $sender_domain = $env{'form.sender_domain'};
16688:     if ($sender_domain eq '') {
16689:         $sender_uname = '';
16690:     } elsif ($sender_uname eq '') {
16691:         $sender_domain = '';
16692:     }
16693:     my $coowners = $env{'form.autoassign_coowners'};
16694:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
16695:     $autofailsafe =~ s{^\s+|\s+$}{}g;
16696:     if ($autofailsafe =~ /\D/) {
16697:         undef($autofailsafe);
16698:     }
16699:     my $failsafe = $env{'form.autoenroll_failsafe'};
16700:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
16701:         $failsafe = 'off';
16702:         undef($autofailsafe);
16703:     }
16704:     my %autoenrollhash =  (
16705:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
16706:                                        'sender_uname' => $sender_uname,
16707:                                        'sender_domain' => $sender_domain,
16708:                                        'co-owners' => $coowners,
16709:                                        'autofailsafe' => $autofailsafe,
16710:                                        'failsafe' => $failsafe,
16711:                                 }
16712:                      );
16713:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
16714:                                              $dom);
16715:     if ($putresult eq 'ok') {
16716:         if (exists($currautoenroll{'run'})) {
16717:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
16718:                  $changes{'run'} = 1;
16719:              }
16720:         } elsif ($autorun) {
16721:             if ($env{'form.autoenroll_run'} ne '1') {
16722:                  $changes{'run'} = 1;
16723:             }
16724:         }
16725:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
16726:             $changes{'sender'} = 1;
16727:         }
16728:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
16729:             $changes{'sender'} = 1;
16730:         }
16731:         if ($currautoenroll{'co-owners'} ne '') {
16732:             if ($currautoenroll{'co-owners'} ne $coowners) {
16733:                 $changes{'coowners'} = 1;
16734:             }
16735:         } elsif ($coowners) {
16736:             $changes{'coowners'} = 1;
16737:         }
16738:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
16739:             $changes{'autofailsafe'} = 1;
16740:         }
16741:         if ($currautoenroll{'failsafe'} ne $failsafe) {
16742:             $changes{'failsafe'} = 1;
16743:         }
16744:         if (keys(%changes) > 0) {
16745:             $resulttext = &mt('Changes made:').'<ul>';
16746:             if ($changes{'run'}) {
16747:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
16748:             }
16749:             if ($changes{'sender'}) {
16750:                 if ($sender_uname eq '' || $sender_domain eq '') {
16751:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
16752:                 } else {
16753:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
16754:                 }
16755:             }
16756:             if ($changes{'coowners'}) {
16757:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
16758:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
16759:                 if (ref($lastactref) eq 'HASH') {
16760:                     $lastactref->{'domainconfig'} = 1;
16761:                 }
16762:             }
16763:             if ($changes{'autofailsafe'}) {
16764:                 if ($autofailsafe ne '') {
16765:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
16766:                 } else {
16767:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16768:                 }
16769:             }
16770:             if ($changes{'failsafe'}) {
16771:                 if ($failsafe eq 'off') {
16772:                     unless ($changes{'autofailsafe'}) {
16773:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
16774:                     }
16775:                 } elsif ($failsafe eq 'zero') {
16776:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
16777:                 } else {
16778:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
16779:                 }
16780:             }
16781:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
16782:                 &Apache::lonnet::get_domain_defaults($dom,1);
16783:                 if (ref($lastactref) eq 'HASH') {
16784:                     $lastactref->{'domdefaults'} = 1;
16785:                 }
16786:             }
16787:             $resulttext .= '</ul>';
16788:         } else {
16789:             $resulttext = &mt('No changes made to auto-enrollment settings');
16790:         }
16791:     } else {
16792:         $resulttext = '<span class="LC_error">'.
16793: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16794:     }
16795:     return $resulttext;
16796: }
16797: 
16798: sub modify_autoupdate {
16799:     my ($dom,%domconfig) = @_;
16800:     my ($resulttext,%currautoupdate,%fields,%changes);
16801:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
16802:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
16803:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
16804:         }
16805:     }
16806:     my @offon = ('off','on');
16807:     my %title = &Apache::lonlocal::texthash (
16808:                     run        => 'Auto-update:',
16809:                     classlists => 'Updates to user information in classlists?',
16810:                     unexpired  => 'Skip updates for users without active or future roles?',
16811:                     lastactive => 'Skip updates for inactive users?',
16812:                 );
16813:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16814:     my %fieldtitles = &Apache::lonlocal::texthash (
16815:                         id => 'Student/Employee ID',
16816:                         permanentemail => 'E-mail address',
16817:                         lastname => 'Last Name',
16818:                         firstname => 'First Name',
16819:                         middlename => 'Middle Name',
16820:                         generation => 'Generation',
16821:                       );
16822:     $othertitle = &mt('All users');
16823:     if (keys(%{$usertypes}) >  0) {
16824:         $othertitle = &mt('Other users');
16825:     }
16826:     foreach my $key (keys(%env)) {
16827:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
16828:             my ($usertype,$item) = ($1,$2);
16829:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
16830:                 if ($usertype eq 'default') {   
16831:                     push(@{$fields{$1}},$2);
16832:                 } elsif (ref($types) eq 'ARRAY') {
16833:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
16834:                         push(@{$fields{$1}},$2);
16835:                     }
16836:                 }
16837:             }
16838:         }
16839:     }
16840:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
16841:     @lockablenames = sort(@lockablenames);
16842:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
16843:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16844:         if (@changed) {
16845:             $changes{'lockablenames'} = 1;
16846:         }
16847:     } else {
16848:         if (@lockablenames) {
16849:             $changes{'lockablenames'} = 1;
16850:         }
16851:     }
16852:     my %updatehash = (
16853:                       autoupdate => { run => $env{'form.autoupdate_run'},
16854:                                       classlists => $env{'form.classlists'},
16855:                                       unexpired  => $env{'form.unexpired'},
16856:                                       fields => {%fields},
16857:                                       lockablenames => \@lockablenames,
16858:                                     }
16859:                      );
16860:     my $lastactivedays;
16861:     if ($env{'form.lastactive'}) {
16862:         $lastactivedays = $env{'form.lastactivedays'};
16863:         $lastactivedays =~ s/^\s+|\s+$//g;
16864:         unless ($lastactivedays =~ /^\d+$/) {
16865:             undef($lastactivedays);
16866:             $env{'form.lastactive'} = 0;
16867:         }
16868:     }
16869:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
16870:     foreach my $key (keys(%currautoupdate)) {
16871:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
16872:             if (exists($updatehash{autoupdate}{$key})) {
16873:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
16874:                     $changes{$key} = 1;
16875:                 }
16876:             }
16877:         } elsif ($key eq 'fields') {
16878:             if (ref($currautoupdate{$key}) eq 'HASH') {
16879:                 foreach my $item (@{$types},'default') {
16880:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
16881:                         my $change = 0;
16882:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
16883:                             if (!exists($fields{$item})) {
16884:                                 $change = 1;
16885:                                 last;
16886:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
16887:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
16888:                                     $change = 1;
16889:                                     last;
16890:                                 }
16891:                             }
16892:                         }
16893:                         if ($change) {
16894:                             push(@{$changes{$key}},$item);
16895:                         }
16896:                     } 
16897:                 }
16898:             }
16899:         } elsif ($key eq 'lockablenames') {
16900:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
16901:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
16902:                 if (@changed) {
16903:                     $changes{'lockablenames'} = 1;
16904:                 }
16905:             } else {
16906:                 if (@lockablenames) {
16907:                     $changes{'lockablenames'} = 1;
16908:                 }
16909:             }
16910:         }
16911:     }
16912:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
16913:         if (@lockablenames) {
16914:             $changes{'lockablenames'} = 1;
16915:         }
16916:     }
16917:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
16918:         if ($updatehash{'autoupdate'}{'unexpired'}) {
16919:             $changes{'unexpired'} = 1;
16920:         }
16921:     }
16922:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
16923:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
16924:             $changes{'lastactive'} = 1;
16925:         }
16926:     }
16927:     foreach my $item (@{$types},'default') {
16928:         if (defined($fields{$item})) {
16929:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
16930:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
16931:                     my $change = 0;
16932:                     if (ref($fields{$item}) eq 'ARRAY') {
16933:                         foreach my $type (@{$fields{$item}}) {
16934:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
16935:                                 $change = 1;
16936:                                 last;
16937:                             }
16938:                         }
16939:                     }
16940:                     if ($change) {
16941:                         push(@{$changes{'fields'}},$item);
16942:                     }
16943:                 } else {
16944:                     push(@{$changes{'fields'}},$item);
16945:                 }
16946:             } else {
16947:                 push(@{$changes{'fields'}},$item);
16948:             }
16949:         }
16950:     }
16951:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
16952:                                              $dom);
16953:     if ($putresult eq 'ok') {
16954:         if (keys(%changes) > 0) {
16955:             $resulttext = &mt('Changes made:').'<ul>';
16956:             foreach my $key (sort(keys(%changes))) {
16957:                 if ($key eq 'lockablenames') {
16958:                     $resulttext .= '<li>';
16959:                     if (@lockablenames) {
16960:                         $usertypes->{'default'} = $othertitle;
16961:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
16962:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
16963:                     } else {
16964:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
16965:                     }
16966:                     $resulttext .= '</li>';
16967:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
16968:                     foreach my $item (@{$changes{$key}}) {
16969:                         my @newvalues;
16970:                         foreach my $type (@{$fields{$item}}) {
16971:                             push(@newvalues,$fieldtitles{$type});
16972:                         }
16973:                         my $newvaluestr;
16974:                         if (@newvalues > 0) {
16975:                             $newvaluestr = join(', ',@newvalues);
16976:                         } else {
16977:                             $newvaluestr = &mt('none');
16978:                         }
16979:                         if ($item eq 'default') {
16980:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
16981:                         } else {
16982:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
16983:                         }
16984:                     }
16985:                 } else {
16986:                     my $newvalue;
16987:                     if ($key eq 'run') {
16988:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
16989:                     } elsif ($key eq 'lastactive') {
16990:                         $newvalue = $offon[$env{'form.lastactive'}];
16991:                         unless ($lastactivedays eq '') {
16992:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
16993:                         }
16994:                     } else {
16995:                         $newvalue = $offon[$env{'form.'.$key}];
16996:                     }
16997:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16998:                 }
16999:             }
17000:             $resulttext .= '</ul>';
17001:         } else {
17002:             $resulttext = &mt('No changes made to autoupdates');
17003:         }
17004:     } else {
17005:         $resulttext = '<span class="LC_error">'.
17006: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
17007:     }
17008:     return $resulttext;
17009: }
17010: 
17011: sub modify_autocreate {
17012:     my ($dom,%domconfig) = @_;
17013:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
17014:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
17015:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
17016:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
17017:         }
17018:     }
17019:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
17020:                  req => 'Auto-creation of validated requests for official courses',
17021:                  xmldc => 'Identity of course creator of courses from XML files',
17022:                );
17023:     my @types = ('xml','req');
17024:     foreach my $item (@types) {
17025:         $newvals{$item} = $env{'form.autocreate_'.$item};
17026:         $newvals{$item} =~ s/\D//g;
17027:         $newvals{$item} = 0 if ($newvals{$item} eq '');
17028:     }
17029:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
17030:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17031:     unless (exists($domcoords{$newvals{'xmldc'}})) {
17032:         $newvals{'xmldc'} = '';
17033:     } 
17034:     %autocreatehash =  (
17035:                         autocreate => { xml => $newvals{'xml'},
17036:                                         req => $newvals{'req'},
17037:                                       }
17038:                        );
17039:     if ($newvals{'xmldc'} ne '') {
17040:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
17041:     }
17042:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
17043:                                              $dom);
17044:     if ($putresult eq 'ok') {
17045:         my @items = @types;
17046:         if ($newvals{'xml'}) {
17047:             push(@items,'xmldc');
17048:         }
17049:         foreach my $item (@items) {
17050:             if (exists($currautocreate{$item})) {
17051:                 if ($currautocreate{$item} ne $newvals{$item}) {
17052:                     $changes{$item} = 1;
17053:                 }
17054:             } elsif ($newvals{$item}) {
17055:                 $changes{$item} = 1;
17056:             }
17057:         }
17058:         if (keys(%changes) > 0) {
17059:             my @offon = ('off','on'); 
17060:             $resulttext = &mt('Changes made:').'<ul>';
17061:             foreach my $item (@types) {
17062:                 if ($changes{$item}) {
17063:                     my $newtxt = $offon[$newvals{$item}];
17064:                     $resulttext .= '<li>'.
17065:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
17066:                                        '<b>','</b>').
17067:                                    '</li>';
17068:                 }
17069:             }
17070:             if ($changes{'xmldc'}) {
17071:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
17072:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
17073:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
17074:             }
17075:             $resulttext .= '</ul>';
17076:         } else {
17077:             $resulttext = &mt('No changes made to auto-creation settings');
17078:         }
17079:     } else {
17080:         $resulttext = '<span class="LC_error">'.
17081:             &mt('An error occurred: [_1]',$putresult).'</span>';
17082:     }
17083:     return $resulttext;
17084: }
17085: 
17086: sub modify_directorysrch {
17087:     my ($dom,$lastactref,%domconfig) = @_;
17088:     my ($resulttext,%changes);
17089:     my %currdirsrch;
17090:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
17091:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
17092:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
17093:         }
17094:     }
17095:     my %title = ( available => 'Institutional directory search available',
17096:                   localonly => 'Other domains can search institution',
17097:                   lcavailable => 'LON-CAPA directory search available',
17098:                   lclocalonly => 'Other domains can search LON-CAPA domain',
17099:                   searchby => 'Search types',
17100:                   searchtypes => 'Search latitude');
17101:     my @offon = ('off','on');
17102:     my @otherdoms = ('Yes','No');
17103: 
17104:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
17105:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
17106:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
17107: 
17108:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17109:     if (keys(%{$usertypes}) == 0) {
17110:         @cansearch = ('default');
17111:     } else {
17112:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
17113:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
17114:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
17115:                     push(@{$changes{'cansearch'}},$type);
17116:                 }
17117:             }
17118:             foreach my $type (@cansearch) {
17119:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
17120:                     push(@{$changes{'cansearch'}},$type);
17121:                 }
17122:             }
17123:         } else {
17124:             push(@{$changes{'cansearch'}},@cansearch);
17125:         }
17126:     }
17127: 
17128:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
17129:         foreach my $by (@{$currdirsrch{'searchby'}}) {
17130:             if (!grep(/^\Q$by\E$/,@searchby)) {
17131:                 push(@{$changes{'searchby'}},$by);
17132:             }
17133:         }
17134:         foreach my $by (@searchby) {
17135:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
17136:                 push(@{$changes{'searchby'}},$by);
17137:             }
17138:         }
17139:     } else {
17140:         push(@{$changes{'searchby'}},@searchby);
17141:     }
17142: 
17143:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
17144:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
17145:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
17146:                 push(@{$changes{'searchtypes'}},$type);
17147:             }
17148:         }
17149:         foreach my $type (@searchtypes) {
17150:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
17151:                 push(@{$changes{'searchtypes'}},$type);
17152:             }
17153:         }
17154:     } else {
17155:         if (exists($currdirsrch{'searchtypes'})) {
17156:             foreach my $type (@searchtypes) {  
17157:                 if ($type ne $currdirsrch{'searchtypes'}) { 
17158:                     push(@{$changes{'searchtypes'}},$type);
17159:                 }
17160:             }
17161:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
17162:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
17163:             }   
17164:         } else {
17165:             push(@{$changes{'searchtypes'}},@searchtypes); 
17166:         }
17167:     }
17168: 
17169:     my %dirsrch_hash =  (
17170:             directorysrch => { available => $env{'form.dirsrch_available'},
17171:                                cansearch => \@cansearch,
17172:                                localonly => $env{'form.dirsrch_instlocalonly'},
17173:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
17174:                                lcavailable => $env{'form.dirsrch_domavailable'},
17175:                                searchby => \@searchby,
17176:                                searchtypes => \@searchtypes,
17177:                              }
17178:             );
17179:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
17180:                                              $dom);
17181:     if ($putresult eq 'ok') {
17182:         if (exists($currdirsrch{'available'})) {
17183:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
17184:                  $changes{'available'} = 1;
17185:              }
17186:         } else {
17187:             if ($env{'form.dirsrch_available'} eq '1') {
17188:                 $changes{'available'} = 1;
17189:             }
17190:         }
17191:         if (exists($currdirsrch{'lcavailable'})) {
17192:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
17193:                 $changes{'lcavailable'} = 1;
17194:             }
17195:         } else {
17196:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
17197:                 $changes{'lcavailable'} = 1;
17198:             }
17199:         }
17200:         if (exists($currdirsrch{'localonly'})) {
17201:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
17202:                 $changes{'localonly'} = 1;
17203:             }
17204:         } else {
17205:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
17206:                 $changes{'localonly'} = 1;
17207:             }
17208:         }
17209:         if (exists($currdirsrch{'lclocalonly'})) {
17210:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
17211:                 $changes{'lclocalonly'} = 1;
17212:             }
17213:         } else {
17214:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
17215:                 $changes{'lclocalonly'} = 1;
17216:             }
17217:         }
17218:         if (keys(%changes) > 0) {
17219:             $resulttext = &mt('Changes made:').'<ul>';
17220:             if ($changes{'available'}) {
17221:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
17222:             }
17223:             if ($changes{'lcavailable'}) {
17224:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
17225:             }
17226:             if ($changes{'localonly'}) {
17227:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
17228:             }
17229:             if ($changes{'lclocalonly'}) {
17230:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
17231:             }
17232:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
17233:                 my $chgtext;
17234:                 if (ref($usertypes) eq 'HASH') {
17235:                     if (keys(%{$usertypes}) > 0) {
17236:                         foreach my $type (@{$types}) {
17237:                             if (grep(/^\Q$type\E$/,@cansearch)) {
17238:                                 $chgtext .= $usertypes->{$type}.'; ';
17239:                             }
17240:                         }
17241:                         if (grep(/^default$/,@cansearch)) {
17242:                             $chgtext .= $othertitle;
17243:                         } else {
17244:                             $chgtext =~ s/\; $//;
17245:                         }
17246:                         $resulttext .=
17247:                             '<li>'.
17248:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
17249:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
17250:                             '</li>';
17251:                     }
17252:                 }
17253:             }
17254:             if (ref($changes{'searchby'}) eq 'ARRAY') {
17255:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
17256:                 my $chgtext;
17257:                 foreach my $type (@{$titleorder}) {
17258:                     if (grep(/^\Q$type\E$/,@searchby)) {
17259:                         if (defined($searchtitles->{$type})) {
17260:                             $chgtext .= $searchtitles->{$type}.'; ';
17261:                         }
17262:                     }
17263:                 }
17264:                 $chgtext =~ s/\; $//;
17265:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
17266:             }
17267:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
17268:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
17269:                 my $chgtext;
17270:                 foreach my $type (@{$srchtypeorder}) {
17271:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
17272:                         if (defined($srchtypes_desc->{$type})) {
17273:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
17274:                         }
17275:                     }
17276:                 }
17277:                 $chgtext =~ s/\; $//;
17278:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
17279:             }
17280:             $resulttext .= '</ul>';
17281:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
17282:             if (ref($lastactref) eq 'HASH') {
17283:                 $lastactref->{'directorysrch'} = 1;
17284:             }
17285:         } else {
17286:             $resulttext = &mt('No changes made to directory search settings');
17287:         }
17288:     } else {
17289:         $resulttext = '<span class="LC_error">'.
17290:                       &mt('An error occurred: [_1]',$putresult).'</span>';
17291:     }
17292:     return $resulttext;
17293: }
17294: 
17295: sub modify_contacts {
17296:     my ($dom,$lastactref,%domconfig) = @_;
17297:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
17298:     if (ref($domconfig{'contacts'}) eq 'HASH') {
17299:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
17300:             $currsetting{$key} = $domconfig{'contacts'}{$key};
17301:         }
17302:     }
17303:     my (%others,%to,%bcc,%includestr,%includeloc);
17304:     my @contacts = ('supportemail','adminemail');
17305:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
17306:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
17307:     my @toggles = ('reporterrors','reportupdates','reportstatus');
17308:     my @lonstatus = ('threshold','sysmail','weights','excluded');
17309:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
17310:     foreach my $type (@mailings) {
17311:         @{$newsetting{$type}} = 
17312:             &Apache::loncommon::get_env_multiple('form.'.$type);
17313:         foreach my $item (@contacts) {
17314:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
17315:                 $contacts_hash{contacts}{$type}{$item} = 1;
17316:             } else {
17317:                 $contacts_hash{contacts}{$type}{$item} = 0;
17318:             }
17319:         }
17320:         $others{$type} = $env{'form.'.$type.'_others'};
17321:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
17322:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17323:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
17324:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
17325:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
17326:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
17327:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
17328:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17329:             }
17330:         }
17331:     }
17332:     foreach my $item (@contacts) {
17333:         $to{$item} = $env{'form.'.$item};
17334:         $contacts_hash{'contacts'}{$item} = $to{$item};
17335:     }
17336:     foreach my $item (@toggles) {
17337:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
17338:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
17339:         }
17340:     }
17341:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
17342:     foreach my $item (@lonstatus) {
17343:         if ($item eq 'excluded') {
17344:             my (%serverhomes,@excluded);
17345:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
17346:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
17347:             if (@possexcluded) {
17348:                 foreach my $id (sort(@possexcluded)) {
17349:                     if ($serverhomes{$id}) {
17350:                         push(@excluded,$id);
17351:                     }
17352:                 }
17353:             }
17354:             if (@excluded) {
17355:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
17356:             }
17357:         } elsif ($item eq 'weights') {
17358:             foreach my $type ('E','W','N','U') {
17359:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
17360:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
17361:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
17362:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
17363:                             $env{'form.error'.$item.'_'.$type};
17364:                     }
17365:                 }
17366:             }
17367:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
17368:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
17369:             if ($env{'form.error'.$item} =~ /^\d+$/) {
17370:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
17371:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
17372:                 }
17373:             }
17374:         }
17375:     }
17376:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
17377:         foreach my $field (@{$fields}) {
17378:             if (ref($possoptions->{$field}) eq 'ARRAY') {
17379:                 my $value = $env{'form.helpform_'.$field};
17380:                 $value =~ s/^\s+|\s+$//g;
17381:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
17382:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
17383:                     if ($field eq 'screenshot') {
17384:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
17385:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
17386:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
17387:                         }
17388:                     }
17389:                 }
17390:             }
17391:         }
17392:     }
17393:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17394:     my (@statuses,%usertypeshash,@overrides);
17395:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
17396:         @statuses = @{$types};
17397:         if (ref($usertypes) eq 'HASH') {
17398:             %usertypeshash = %{$usertypes};
17399:         }
17400:     }
17401:     if (@statuses) {
17402:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
17403:         foreach my $type (@possoverrides) {
17404:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
17405:                 push(@overrides,$type);
17406:             }
17407:         }
17408:         if (@overrides) {
17409:             foreach my $type (@overrides) {
17410:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
17411:                 foreach my $item (@contacts) {
17412:                     if (grep(/^\Q$item\E$/,@standard)) {
17413:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
17414:                         $newsetting{'override_'.$type}{$item} = 1;
17415:                     } else {
17416:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
17417:                         $newsetting{'override_'.$type}{$item} = 0;
17418:                     }
17419:                 }
17420:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
17421:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17422:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
17423:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
17424:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
17425:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
17426:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
17427:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
17428:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
17429:                 }
17430:             }
17431:         }
17432:     }
17433:     if (keys(%currsetting) > 0) {
17434:         foreach my $item (@contacts) {
17435:             if ($to{$item} ne $currsetting{$item}) {
17436:                 $changes{$item} = 1;
17437:             }
17438:         }
17439:         foreach my $type (@mailings) {
17440:             foreach my $item (@contacts) {
17441:                 if (ref($currsetting{$type}) eq 'HASH') {
17442:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
17443:                         push(@{$changes{$type}},$item);
17444:                     }
17445:                 } else {
17446:                     push(@{$changes{$type}},@{$newsetting{$type}});
17447:                 }
17448:             }
17449:             if ($others{$type} ne $currsetting{$type}{'others'}) {
17450:                 push(@{$changes{$type}},'others');
17451:             }
17452:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17453:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
17454:                     push(@{$changes{$type}},'bcc'); 
17455:                 }
17456:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
17457:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
17458:                     push(@{$changes{$type}},'include');
17459:                 }
17460:             }
17461:         }
17462:         if (ref($fields) eq 'ARRAY') {
17463:             if (ref($currsetting{'helpform'}) eq 'HASH') {
17464:                 foreach my $field (@{$fields}) {
17465:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
17466:                         push(@{$changes{'helpform'}},$field);
17467:                     }
17468:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17469:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
17470:                             push(@{$changes{'helpform'}},'maxsize');
17471:                         }
17472:                     }
17473:                 }
17474:             } else {
17475:                 foreach my $field (@{$fields}) {
17476:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17477:                         push(@{$changes{'helpform'}},$field);
17478:                     }
17479:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17480:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17481:                             push(@{$changes{'helpform'}},'maxsize');
17482:                         }
17483:                     }
17484:                 }
17485:             }
17486:         }
17487:         if (@statuses) {
17488:             if (ref($currsetting{'overrides'}) eq 'HASH') {
17489:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
17490:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
17491:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
17492:                             foreach my $item (@contacts,'bcc','others','include') {
17493:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) {
17494:                                     push(@{$changes{'overrides'}},$key);
17495:                                     last;
17496:                                 }
17497:                             }
17498:                         } else {
17499:                             push(@{$changes{'overrides'}},$key);
17500:                         }
17501:                     }
17502:                 }
17503:                 foreach my $key (@overrides) {
17504:                     unless (exists($currsetting{'overrides'}{$key})) {
17505:                         push(@{$changes{'overrides'}},$key);
17506:                     }
17507:                 }
17508:             } else {
17509:                 foreach my $key (@overrides) {
17510:                     push(@{$changes{'overrides'}},$key);
17511:                 }
17512:             }
17513:         }
17514:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
17515:             foreach my $key ('excluded','weights','threshold','sysmail') {
17516:                 if ($key eq 'excluded') {
17517:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17518:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
17519:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17520:                             (@{$currsetting{'lonstatus'}{$key}})) {
17521:                             my @diffs =
17522:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
17523:                                                                    $currsetting{'lonstatus'}{$key});
17524:                             if (@diffs) {
17525:                                 push(@{$changes{'lonstatus'}},$key);
17526:                             }
17527:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
17528:                             push(@{$changes{'lonstatus'}},$key);
17529:                         }
17530:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
17531:                              (@{$currsetting{'lonstatus'}{$key}})) {
17532:                         push(@{$changes{'lonstatus'}},$key);
17533:                     }
17534:                 } elsif ($key eq 'weights') {
17535:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
17536:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
17537:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17538:                             foreach my $type ('E','W','N','U') {
17539:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
17540:                                         $currsetting{'lonstatus'}{$key}{$type}) {
17541:                                     push(@{$changes{'lonstatus'}},$key);
17542:                                     last;
17543:                                 }
17544:                             }
17545:                         } else {
17546:                             foreach my $type ('E','W','N','U') {
17547:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
17548:                                     push(@{$changes{'lonstatus'}},$key);
17549:                                     last;
17550:                                 }
17551:                             }
17552:                         }
17553:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
17554:                         foreach my $type ('E','W','N','U') {
17555:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
17556:                                 push(@{$changes{'lonstatus'}},$key);
17557:                                 last;
17558:                             }
17559:                         }
17560:                     }
17561:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
17562:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17563:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17564:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
17565:                                 push(@{$changes{'lonstatus'}},$key);
17566:                             }
17567:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
17568:                             push(@{$changes{'lonstatus'}},$key);
17569:                         }
17570:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
17571:                         push(@{$changes{'lonstatus'}},$key);
17572:                     }
17573:                 }
17574:             }
17575:         } else {
17576:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17577:                 foreach my $key ('excluded','weights','threshold','sysmail') {
17578:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17579:                         push(@{$changes{'lonstatus'}},$key);
17580:                     }
17581:                 }
17582:             }
17583:         }
17584:     } else {
17585:         my %default;
17586:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
17587:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
17588:         $default{'errormail'} = 'adminemail';
17589:         $default{'packagesmail'} = 'adminemail';
17590:         $default{'helpdeskmail'} = 'supportemail';
17591:         $default{'otherdomsmail'} = 'supportemail';
17592:         $default{'lonstatusmail'} = 'adminemail';
17593:         $default{'requestsmail'} = 'adminemail';
17594:         $default{'updatesmail'} = 'adminemail';
17595:         $default{'hostipmail'} = 'adminemail';
17596:         foreach my $item (@contacts) {
17597:            if ($to{$item} ne $default{$item}) {
17598:                $changes{$item} = 1;
17599:            }
17600:         }
17601:         foreach my $type (@mailings) {
17602:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
17603:                 push(@{$changes{$type}},@{$newsetting{$type}});
17604:             }
17605:             if ($others{$type} ne '') {
17606:                 push(@{$changes{$type}},'others');
17607:             }
17608:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17609:                 if ($bcc{$type} ne '') {
17610:                     push(@{$changes{$type}},'bcc');
17611:                 }
17612:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
17613:                     push(@{$changes{$type}},'include');
17614:                 }
17615:             }
17616:         }
17617:         if (ref($fields) eq 'ARRAY') {
17618:             foreach my $field (@{$fields}) {
17619:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
17620:                     push(@{$changes{'helpform'}},$field);
17621:                 }
17622:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
17623:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
17624:                         push(@{$changes{'helpform'}},'maxsize');
17625:                     }
17626:                 }
17627:             }
17628:         }
17629:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
17630:             foreach my $key ('excluded','weights','threshold','sysmail') {
17631:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
17632:                     push(@{$changes{'lonstatus'}},$key);
17633:                 }
17634:             }
17635:         }
17636:     }
17637:     foreach my $item (@toggles) {
17638:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
17639:             $changes{$item} = 1;
17640:         } elsif ((!$env{'form.'.$item}) &&
17641:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
17642:             $changes{$item} = 1;
17643:         }
17644:     }
17645:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
17646:                                              $dom);
17647:     if ($putresult eq 'ok') {
17648:         if (keys(%changes) > 0) {
17649:             &Apache::loncommon::devalidate_domconfig_cache($dom);
17650:             if (ref($lastactref) eq 'HASH') {
17651:                 $lastactref->{'domainconfig'} = 1;
17652:             }
17653:             my ($titles,$short_titles)  = &contact_titles();
17654:             $resulttext = &mt('Changes made:').'<ul>';
17655:             foreach my $item (@contacts) {
17656:                 if ($changes{$item}) {
17657:                     $resulttext .= '<li>'.$titles->{$item}.
17658:                                     &mt(' set to: ').
17659:                                     '<span class="LC_cusr_emph">'.
17660:                                     $to{$item}.'</span></li>';
17661:                 }
17662:             }
17663:             foreach my $type (@mailings) {
17664:                 if (ref($changes{$type}) eq 'ARRAY') {
17665:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17666:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
17667:                     } else {
17668:                         $resulttext .= '<li>'.$titles->{$type}.': ';
17669:                     }
17670:                     my @text;
17671:                     foreach my $item (@{$newsetting{$type}}) {
17672:                         push(@text,$short_titles->{$item});
17673:                     }
17674:                     if ($others{$type} ne '') {
17675:                         push(@text,$others{$type});
17676:                     }
17677:                     if (@text) {
17678:                         $resulttext .= '<span class="LC_cusr_emph">'.
17679:                                        join(', ',@text).'</span>';
17680:                     }
17681:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
17682:                         if ($bcc{$type} ne '') {
17683:                             my $bcctext;
17684:                             if (@text) {
17685:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
17686:                             } else {
17687:                                 $bcctext = '(Bcc)';
17688:                             }
17689:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
17690:                         } elsif (!@text) {
17691:                             $resulttext .= &mt('No one');
17692:                         }
17693:                         if ($includestr{$type} ne '') {
17694:                             if ($includeloc{$type} eq 'b') {
17695:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
17696:                             } elsif ($includeloc{$type} eq 's') {
17697:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
17698:                             }
17699:                         }
17700:                     } elsif (!@text) {
17701:                         $resulttext .= &mt('No recipients');
17702:                     }
17703:                     $resulttext .= '</li>';
17704:                 }
17705:             }
17706:             if (ref($changes{'overrides'}) eq 'ARRAY') {
17707:                 my @deletions;
17708:                 foreach my $type (@{$changes{'overrides'}}) {
17709:                     if ($usertypeshash{$type}) {
17710:                         if (grep(/^\Q$type\E/,@overrides)) {
17711:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
17712:                                                       $usertypeshash{$type}).'<ul><li>';
17713:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
17714:                                 my @text;
17715:                                 foreach my $item (@contacts) {
17716:                                     if ($newsetting{'override_'.$type}{$item}) {
17717:                                         push(@text,$short_titles->{$item});
17718:                                     }
17719:                                 }
17720:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
17721:                                     push(@text,$newsetting{'override_'.$type}{'others'});
17722:                                 }
17723: 
17724:                                 if (@text) {
17725:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
17726:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
17727:                                 }
17728:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
17729:                                     my $bcctext;
17730:                                     if (@text) {
17731:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
17732:                                     } else {
17733:                                         $bcctext = '(Bcc)';
17734:                                     }
17735:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
17736:                                 } elsif (!@text) {
17737:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
17738:                                 }
17739:                                 $resulttext .= '</li>';
17740:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
17741:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
17742:                                     if ($loc eq 'b') {
17743:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
17744:                                     } elsif ($loc eq 's') {
17745:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
17746:                                     }
17747:                                 }
17748:                             }
17749:                             $resulttext .= '</li></ul></li>';
17750:                         } else {
17751:                             push(@deletions,$usertypeshash{$type});
17752:                         }
17753:                     }
17754:                 }
17755:                 if (@deletions) {
17756:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
17757:                                               join(', ',@deletions)).'</li>';
17758:                 }
17759:             }
17760:             my @offon = ('off','on');
17761:             my $corelink = &core_link_msu();
17762:             if ($changes{'reporterrors'}) {
17763:                 $resulttext .= '<li>'.
17764:                                &mt('E-mail error reports to [_1] set to "'.
17765:                                    $offon[$env{'form.reporterrors'}].'".',
17766:                                    $corelink).
17767:                                '</li>';
17768:             }
17769:             if ($changes{'reportupdates'}) {
17770:                 $resulttext .= '<li>'.
17771:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
17772:                                     $offon[$env{'form.reportupdates'}].'".',
17773:                                     $corelink).
17774:                                 '</li>';
17775:             }
17776:             if ($changes{'reportstatus'}) {
17777:                 $resulttext .= '<li>'.
17778:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
17779:                                     $offon[$env{'form.reportstatus'}].'".',
17780:                                     $corelink).
17781:                                 '</li>';
17782:             }
17783:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
17784:                 $resulttext .= '<li>'.
17785:                                &mt('Nightly status check e-mail settings').':<ul>';
17786:                 my (%defval,%use_def,%shown);
17787:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
17788:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
17789:                 $defval{'weights'} =
17790:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
17791:                 $defval{'excluded'} = &mt('None');
17792:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
17793:                     foreach my $item ('threshold','sysmail','weights','excluded') {
17794:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
17795:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
17796:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
17797:                             } elsif ($item eq 'weights') {
17798:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
17799:                                     foreach my $type ('E','W','N','U') {
17800:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
17801:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
17802:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
17803:                                         } else {
17804:                                             $shown{$item} .= $lonstatus_defs->{$type};
17805:                                         }
17806:                                         $shown{$item} .= ', ';
17807:                                     }
17808:                                     $shown{$item} =~ s/, $//;
17809:                                 } else {
17810:                                     $shown{$item} = $defval{$item};
17811:                                 }
17812:                             } elsif ($item eq 'excluded') {
17813:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
17814:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
17815:                                 } else {
17816:                                     $shown{$item} = $defval{$item};
17817:                                 }
17818:                             }
17819:                         } else {
17820:                             $shown{$item} = $defval{$item};
17821:                         }
17822:                     }
17823:                 } else {
17824:                     foreach my $item ('threshold','weights','excluded','sysmail') {
17825:                         $shown{$item} = $defval{$item};
17826:                     }
17827:                 }
17828:                 foreach my $item ('threshold','weights','excluded','sysmail') {
17829:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
17830:                                           $shown{$item}).'</li>';
17831:                 }
17832:                 $resulttext .= '</ul></li>';
17833:             }
17834:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
17835:                 my (@optional,@required,@unused,$maxsizechg);
17836:                 foreach my $field (@{$changes{'helpform'}}) {
17837:                     if ($field eq 'maxsize') {
17838:                         $maxsizechg = 1;
17839:                         next;
17840:                     }
17841:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
17842:                         push(@optional,$field);
17843:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
17844:                         push(@unused,$field);
17845:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
17846:                         push(@required,$field);
17847:                     }
17848:                 }
17849:                 if (@optional) {
17850:                     $resulttext .= '<li>'.
17851:                                    &mt('Help form fields changed to "Optional": [_1].',
17852:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
17853:                                    '</li>';
17854:                 }
17855:                 if (@required) {
17856:                     $resulttext .= '<li>'.
17857:                                    &mt('Help form fields changed to "Required": [_1].',
17858:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
17859:                                    '</li>';
17860:                 }
17861:                 if (@unused) {
17862:                     $resulttext .= '<li>'.
17863:                                    &mt('Help form fields changed to "Not shown": [_1].',
17864:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
17865:                                    '</li>';
17866:                 }
17867:                 if ($maxsizechg) {
17868:                     $resulttext .= '<li>'.
17869:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
17870:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
17871:                                    '</li>';
17872:                 }
17873:             }
17874:             $resulttext .= '</ul>';
17875:         } else {
17876:             $resulttext = &mt('No changes made to contacts and form settings');
17877:         }
17878:     } else {
17879:         $resulttext = '<span class="LC_error">'.
17880:             &mt('An error occurred: [_1].',$putresult).'</span>';
17881:     }
17882:     return $resulttext;
17883: }
17884: 
17885: sub modify_privacy {
17886:     my ($dom,$lastactref,%domconfig) = @_;
17887:     my ($resulttext,%current,%changes);
17888:     if (ref($domconfig{'privacy'}) eq 'HASH') {
17889:         %current = %{$domconfig{'privacy'}};
17890:     }
17891:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
17892:     my @items = ('domain','author','course','community');
17893:     my %names = &Apache::lonlocal::texthash (
17894:                    domain => 'Assigned domain role(s)',
17895:                    author => 'Assigned co-author role(s)',
17896:                    course => 'Assigned course role(s)',
17897:                    community => 'Assigned community role(s)',
17898:                 );
17899:     my %roles = &Apache::lonlocal::texthash (
17900:                    domain => 'Domain role',
17901:                    author => 'Co-author role',
17902:                    course => 'Course role',
17903:                    community => 'Community role',
17904:                 );
17905:     my %titles = &Apache::lonlocal::texthash (
17906:                   approval => 'Approval for role in different domain',
17907:                   othdom   => 'User information available in other domain',
17908:                   priv     => 'Information viewable by privileged user in same domain',
17909:                   unpriv   => 'Information viewable by unprivileged user in same domain',
17910:                   instdom  => 'Other domain shares institution/provider',
17911:                   extdom   => 'Other domain has different institution/provider',
17912:                   none     => 'Not allowed',
17913:                   user     => 'User authorizes',
17914:                   domain   => 'Domain Coordinator authorizes',
17915:                   auto     => 'Unrestricted',
17916:                   notify   => 'Notify when role needs authorization',
17917:     );
17918:     my %fieldnames = &Apache::lonlocal::texthash (
17919:                         id => 'Student/Employee ID',
17920:                         permanentemail => 'E-mail address',
17921:                         lastname => 'Last Name',
17922:                         firstname => 'First Name',
17923:                         middlename => 'Middle Name',
17924:                         generation => 'Generation',
17925:     );
17926:     my ($othertitle,$usertypes,$types) =
17927:         &Apache::loncommon::sorted_inst_types($dom);
17928:     my (%by_ip,%by_location,@intdoms,@instdoms);
17929:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
17930: 
17931:     my %privacyhash = (
17932:                        'approval' => {
17933:                                        instdom => {},
17934:                                        extdom  => {},
17935:                                      },
17936:                        'othdom'   => {},
17937:                        'priv'     => {},
17938:                        'unpriv'   => {},
17939:                       );
17940:     foreach my $item (@items) {
17941:         if (@instdoms > 1) {
17942:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17943:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
17944:             }
17945:             if (ref($current{'approval'}) eq 'HASH') {
17946:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
17947:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
17948:                         $changes{'approval'} = 1;
17949:                     }
17950:                 }
17951:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
17952:                 $changes{'approval'} = 1;
17953:             }
17954:         }
17955:         if (keys(%by_location) > 0) {
17956:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
17957:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
17958:             }
17959:             if (ref($current{'approval'}) eq 'HASH') {
17960:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
17961:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
17962:                         $changes{'approval'} = 1;
17963:                     }
17964:                 }
17965:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
17966:                 $changes{'approval'} = 1;
17967:             }
17968:         }
17969:         foreach my $status ('priv','unpriv') {
17970:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
17971:             my @newvalues;
17972:             foreach my $field (@possibles) {
17973:                 if (grep(/^\Q$field\E$/,@fields)) {
17974:                     $privacyhash{$status}{$item}{$field} = 1;
17975:                     push(@newvalues,$field);
17976:                 }
17977:             }
17978:             @newvalues = sort(@newvalues);
17979:             if (ref($current{$status}) eq 'HASH') {
17980:                 if (ref($current{$status}{$item}) eq 'HASH') {
17981:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
17982:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17983:                     if (@diffs > 0) {
17984:                         $changes{$status} = 1;
17985:                     }
17986:                 } elsif (@newvalues > 0) {
17987:                     $changes{$status} = 1;
17988:                 }
17989:             } else {
17990:                 my @stdfields;
17991:                 foreach my $field (@fields) {
17992:                     if ($field eq 'id') {
17993:                         next if ($status eq 'unpriv');
17994:                         next if (($status eq 'priv') && ($item eq 'community'));
17995:                     }
17996:                     push(@stdfields,$field);
17997:                 }
17998:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17999:                 if (@diffs > 0) {
18000:                     $changes{$status} = 1;
18001:                 }
18002:             }
18003:         }
18004:     }
18005:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
18006:         my @statuses;
18007:         if (ref($types) eq 'ARRAY') {
18008:             @statuses = @{$types};
18009:         }
18010:         foreach my $type (@statuses,'default') {
18011:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
18012:             my @newvalues;
18013:             foreach my $field (sort(@possfields)) {
18014:                 if (grep(/^\Q$field\E$/,@fields)) {
18015:                     $privacyhash{'othdom'}{$type}{$field} = 1;
18016:                     push(@newvalues,$field);
18017:                 }
18018:             }
18019:             @newvalues = sort(@newvalues);
18020:             if (ref($current{'othdom'}) eq 'HASH') {
18021:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
18022:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
18023:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
18024:                     if (@diffs > 0) {
18025:                         $changes{'othdom'} = 1;
18026:                     }
18027:                 } elsif (@newvalues > 0) {
18028:                     $changes{'othdom'} = 1;
18029:                 }
18030:             } else {
18031:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
18032:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
18033:                 if (@diffs > 0) {
18034:                     $changes{'othdom'} = 1;
18035:                 }
18036:             }
18037:         }
18038:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
18039:         my %notify;
18040:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
18041:             if (exists($domcoords{$possdc})) {
18042:                 $notify{$possdc} = 1;
18043:             }
18044:         }
18045:         my $notify = join(',',sort(keys(%notify)));
18046:         if ($current{'notify'} ne $notify) {
18047:             $changes{'notify'} = 1;
18048:         }
18049:         $privacyhash{'notify'} = $notify;
18050:     }
18051:     my %confighash = (
18052:                         privacy => \%privacyhash,
18053:                      );
18054:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18055:     if ($putresult eq 'ok') {
18056:         if (keys(%changes) > 0) {
18057:             $resulttext = &mt('Changes made: ').'<ul>';
18058:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
18059:                 if ($changes{$key}) {
18060:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18061:                     if ($key eq 'approval') {
18062:                         if (keys(%{$privacyhash{$key}{instdom}})) {
18063:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
18064:                             foreach my $item (@items) {
18065:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
18066:                             }
18067:                             $resulttext .= '</ul></li>';
18068:                         }
18069:                         if (keys(%{$privacyhash{$key}{extdom}})) {
18070:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
18071:                             foreach my $item (@items) {
18072:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
18073:                             }
18074:                             $resulttext .= '</ul></li>';
18075:                         }
18076:                     } elsif ($key eq 'notify') {
18077:                         if ($privacyhash{$key}) {
18078:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
18079:                                 my ($dcname,$dcdom) = split(/:/,$dc);
18080:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
18081:                             }
18082:                         } else {
18083:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
18084:                         }
18085:                     } elsif ($key eq 'othdom') {
18086:                         my @statuses;
18087:                         if (ref($types) eq 'ARRAY') {
18088:                             @statuses = @{$types};
18089:                         }
18090:                         if (ref($privacyhash{$key}) eq 'HASH') {
18091:                             foreach my $status (@statuses,'default') {
18092:                                 if ($status eq 'default') {
18093:                                     $resulttext .= '<li>'.$othertitle.': ';
18094:                                 } elsif (ref($usertypes) eq 'HASH') {
18095:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
18096:                                 } else {
18097:                                     next;
18098:                                 }
18099:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
18100:                                     if (keys(%{$privacyhash{$key}{$status}})) {
18101:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
18102:                                     } else {
18103:                                         $resulttext .= &mt('none');
18104:                                     }
18105:                                 }
18106:                                 $resulttext .= '</li>';
18107:                             }
18108:                         }
18109:                     } else {
18110:                         foreach my $item (@items) {
18111:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
18112:                                 $resulttext .= '<li>'.$names{$item}.': ';
18113:                                 if (keys(%{$privacyhash{$key}{$item}})) {
18114:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
18115:                                 } else {
18116:                                     $resulttext .= &mt('none');
18117:                                 }
18118:                                 $resulttext .= '</li>';
18119:                             }
18120:                         }
18121:                     }
18122:                     $resulttext .= '</ul></li>';
18123:                 }
18124:             }
18125:             $resulttext .= '</ul>';
18126:             if ($changes{'approval'}) {
18127:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18128:                 delete($domdefaults{'userapprovals'});
18129:                 if (ref($privacyhash{'approval'}) eq 'HASH') {
18130:                     foreach my $domtype ('instdom','extdom') {
18131:                         if (ref($privacyhash{'approval'}{$domtype}) eq 'HASH') {
18132:                             foreach my $roletype ('domain','author','course','community') {
18133:                                 if ($privacyhash{'approval'}{$domtype}{$roletype} eq 'user') {
18134:                                     $domdefaults{'userapprovals'} = 1;
18135:                                     last;
18136:                                 }
18137:                             }
18138:                         }
18139:                         last if ($domdefaults{'userapprovals'});               
18140:                     }
18141:                 }
18142:                 my $cachetime = 24*60*60;
18143:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18144:                 if (ref($lastactref) eq 'HASH') {
18145:                     $lastactref->{'domdefaults'} = 1;
18146:                 }
18147:             }
18148:         } else {
18149:             $resulttext = &mt('No changes made to user information settings');
18150:         }
18151:     } else {
18152:         $resulttext = '<span class="LC_error">'.
18153:             &mt('An error occurred: [_1]',$putresult).'</span>';
18154:     }
18155:     return $resulttext;
18156: }
18157: 
18158: sub modify_passwords {
18159:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18160:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
18161:         $updatedefaults,$updateconf);
18162:     my $customfn = 'resetpw.html';
18163:     if (ref($domconfig{'passwords'}) eq 'HASH') {
18164:         %current = %{$domconfig{'passwords'}};
18165:     }
18166:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18167:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18168:     if (ref($types) eq 'ARRAY') {
18169:         @oktypes = @{$types};
18170:     }
18171:     push(@oktypes,'default');
18172: 
18173:     my %titles = &Apache::lonlocal::texthash (
18174:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
18175:         intauth_check  => 'Check bcrypt cost if authenticated',
18176:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
18177:         permanent      => 'Permanent e-mail address',
18178:         critical       => 'Critical notification address',
18179:         notify         => 'Notification address',
18180:         min            => 'Minimum password length',
18181:         max            => 'Maximum password length',
18182:         chars          => 'Required characters',
18183:         expire         => 'Password expiration (days)',
18184:         numsaved       => 'Number of previous passwords to save',
18185:         reset          => 'Resetting Forgotten Password',
18186:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
18187:         rules          => 'Rules for LON-CAPA Passwords',
18188:         crsownerchg    => 'Course Owner Changing Student Passwords',
18189:         username       => 'Username',
18190:         email          => 'E-mail address',
18191:     );
18192: 
18193: #
18194: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
18195: #
18196:     my (%curr_defaults,%save_defaults);
18197:     if (ref($domconfig{'defaults'}) eq 'HASH') {
18198:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
18199:             if ($key =~ /^intauth_(cost|check|switch)$/) {
18200:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
18201:             } else {
18202:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
18203:             }
18204:         }
18205:     }
18206:     my %staticdefaults = (
18207:         'resetlink'      => 2,
18208:         'resetcase'      => \@oktypes,
18209:         'resetprelink'   => 'both',
18210:         'resetemail'     => ['critical','notify','permanent'],
18211:         'intauth_cost'   => 10,
18212:         'intauth_check'  => 0,
18213:         'intauth_switch' => 0,
18214:     );
18215:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18216:     foreach my $type (@oktypes) {
18217:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
18218:     }
18219:     my $linklife = $env{'form.passwords_link'};
18220:     $linklife =~ s/^\s+|\s+$//g;
18221:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
18222:         $newvalues{'resetlink'} = $linklife;
18223:         if ($current{'resetlink'}) {
18224:             if ($current{'resetlink'} ne $linklife) {
18225:                 $changes{'reset'} = 1;
18226:             }
18227:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18228:             if ($staticdefaults{'resetlink'} ne $linklife) {
18229:                 $changes{'reset'} = 1;
18230:             }
18231:         }
18232:     } elsif ($current{'resetlink'}) {
18233:         $changes{'reset'} = 1;
18234:     }
18235:     my @casesens;
18236:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
18237:     foreach my $case (sort(@posscase)) {
18238:         if (grep(/^\Q$case\E$/,@oktypes)) {
18239:             push(@casesens,$case);
18240:         }
18241:     }
18242:     $newvalues{'resetcase'} = \@casesens;
18243:     if (ref($current{'resetcase'}) eq 'ARRAY') {
18244:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
18245:         if (@diffs > 0) {
18246:             $changes{'reset'} = 1;
18247:         }
18248:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18249:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
18250:         if (@diffs > 0) {
18251:             $changes{'reset'} = 1;
18252:         }
18253:     }
18254:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
18255:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
18256:         if (exists($current{'resetprelink'})) {
18257:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
18258:                 $changes{'reset'} = 1;
18259:             }
18260:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18261:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
18262:                 $changes{'reset'} = 1;
18263:             }
18264:         }
18265:     } elsif ($current{'resetprelink'}) {
18266:         $changes{'reset'} = 1;
18267:     }
18268:     foreach my $type (@oktypes) {
18269:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
18270:         my @postlink;
18271:         foreach my $item (sort(@possplink)) {
18272:             if ($item =~ /^(email|username)$/) {
18273:                 push(@postlink,$item);
18274:             }
18275:         }
18276:         $newvalues{'resetpostlink'}{$type} = \@postlink;
18277:         unless ($changes{'reset'}) {
18278:             if (ref($current{'resetpostlink'}) eq 'HASH') {
18279:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
18280:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
18281:                     if (@diffs > 0) {
18282:                         $changes{'reset'} = 1;
18283:                     }
18284:                 } else {
18285:                     $changes{'reset'} = 1;
18286:                 }
18287:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18288:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
18289:                 if (@diffs > 0) {
18290:                     $changes{'reset'} = 1;
18291:                 }
18292:             }
18293:         }
18294:     }
18295:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
18296:     my @resetemail;
18297:     foreach my $item (sort(@possemailsrc)) {
18298:         if ($item =~ /^(permanent|critical|notify)$/) {
18299:             push(@resetemail,$item);
18300:         }
18301:     }
18302:     $newvalues{'resetemail'} = \@resetemail;
18303:     unless ($changes{'reset'}) {
18304:         if (ref($current{'resetemail'}) eq 'ARRAY') {
18305:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
18306:             if (@diffs > 0) {
18307:                 $changes{'reset'} = 1;
18308:             }
18309:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
18310:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
18311:             if (@diffs > 0) {
18312:                 $changes{'reset'} = 1;
18313:             }
18314:         }
18315:     }
18316:     if ($env{'form.passwords_stdtext'} == 0) {
18317:         $newvalues{'resetremove'} = 1;
18318:         unless ($current{'resetremove'}) {
18319:             $changes{'reset'} = 1;
18320:         }
18321:     } elsif ($current{'resetremove'}) {
18322:         $changes{'reset'} = 1;
18323:     }
18324:     if ($env{'form.passwords_customfile.filename'} ne '') {
18325:         my $servadm = $r->dir_config('lonAdmEMail');
18326:         my ($configuserok,$author_ok,$switchserver) =
18327:             &config_check($dom,$confname,$servadm);
18328:         my $error;
18329:         if ($configuserok eq 'ok') {
18330:             if ($switchserver) {
18331:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
18332:             } else {
18333:                 if ($author_ok eq 'ok') {
18334:                     my $modified = [];
18335:                     my ($result,$customurl) =
18336:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
18337:                                                                 $confname,'customtext/resetpw','','',$customfn,
18338:                                                                 $modified);
18339:                     if ($result eq 'ok') {
18340:                         $newvalues{'resetcustom'} = $customurl;
18341:                         $changes{'reset'} = 1;
18342:                         &update_modify_urls($r,$modified);
18343:                     } else {
18344:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
18345:                     }
18346:                 } else {
18347:                     $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);
18348:                 }
18349:             }
18350:         } else {
18351:             $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);
18352:         }
18353:         if ($error) {
18354:             &Apache::lonnet::logthis($error);
18355:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
18356:         }
18357:     } elsif ($current{'resetcustom'}) {
18358:         if ($env{'form.passwords_custom_del'}) {
18359:             $changes{'reset'} = 1;
18360:         } else {
18361:             $newvalues{'resetcustom'} = $current{'resetcustom'};
18362:         }
18363:     }
18364:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
18365:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
18366:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
18367:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
18368:             $changes{'intauth'} = 1;
18369:         }
18370:     } else {
18371:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
18372:     }
18373:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
18374:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
18375:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
18376:             $changes{'intauth'} = 1;
18377:         }
18378:     } else {
18379:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18380:     }
18381:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
18382:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
18383:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
18384:             $changes{'intauth'} = 1;
18385:         }
18386:     } else {
18387:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
18388:     }
18389:     foreach my $item ('cost','check','switch') {
18390:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
18391:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
18392:             $updatedefaults = 1;
18393:         }
18394:     }
18395:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
18396:     my %crsownerchg = (
18397:                         by => [],
18398:                         for => [],
18399:                       );
18400:     foreach my $item ('by','for') {
18401:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
18402:         foreach my $type (sort(@posstypes)) {
18403:             if (grep(/^\Q$type\E$/,@oktypes)) {
18404:                 push(@{$crsownerchg{$item}},$type);
18405:             }
18406:         }
18407:     }
18408:     $newvalues{'crsownerchg'} = \%crsownerchg;
18409:     if (ref($current{'crsownerchg'}) eq 'HASH') {
18410:         foreach my $item ('by','for') {
18411:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
18412:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
18413:                 if (@diffs > 0) {
18414:                     $changes{'crsownerchg'} = 1;
18415:                     last;
18416:                 }
18417:             }
18418:         }
18419:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
18420:         foreach my $item ('by','for') {
18421:             if (@{$crsownerchg{$item}} > 0) {
18422:                 $changes{'crsownerchg'} = 1;
18423:                 last;
18424:             }
18425:         }
18426:     }
18427: 
18428:     my %confighash = (
18429:                         defaults  => \%save_defaults,
18430:                         passwords => \%newvalues,
18431:                      );
18432:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
18433: 
18434:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
18435:     if ($putresult eq 'ok') {
18436:         if (keys(%changes) > 0) {
18437:             $resulttext = &mt('Changes made: ').'<ul>';
18438:             foreach my $key ('reset','intauth','rules','crsownerchg') {
18439:                 if ($changes{$key}) {
18440:                     unless ($key eq 'intauth') {
18441:                         $updateconf = 1;
18442:                     }
18443:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
18444:                     if ($key eq 'reset') {
18445:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
18446:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
18447:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
18448:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
18449:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
18450:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
18451:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
18452:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
18453:                             }
18454:                         } else {
18455:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
18456:                         }
18457:                         if ($confighash{'passwords'}{'resetlink'}) {
18458:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
18459:                         } else {
18460:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
18461:                                                   &mt('Will default to 2 hours').'</li>';
18462:                         }
18463:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
18464:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
18465:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
18466:                             } else {
18467:                                 my $casesens;
18468:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
18469:                                     if ($type eq 'default') {
18470:                                         $casesens .= $othertitle.', ';
18471:                                     } elsif ($usertypes->{$type} ne '') {
18472:                                         $casesens .= $usertypes->{$type}.', ';
18473:                                     }
18474:                                 }
18475:                                 $casesens =~ s/\Q, \E$//;
18476:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
18477:                             }
18478:                         } else {
18479:                             $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>';
18480:                         }
18481:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
18482:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
18483:                         } else {
18484:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
18485:                         }
18486:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
18487:                             my $output;
18488:                             if (ref($types) eq 'ARRAY') {
18489:                                 foreach my $type (@{$types}) {
18490:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
18491:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
18492:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
18493:                                         } else {
18494:                                             $output .= $usertypes->{$type}.' -- '.
18495:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
18496:                                         }
18497:                                     }
18498:                                 }
18499:                             }
18500:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
18501:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
18502:                                     $output .= $othertitle.' -- '.&mt('none');
18503:                                 } else {
18504:                                     $output .= $othertitle.' -- '.
18505:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
18506:                                 }
18507:                             }
18508:                             if ($output) {
18509:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
18510:                             } else {
18511:                                 $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>';
18512:                             }
18513:                         } else {
18514:                             $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>';
18515:                         }
18516:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
18517:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
18518:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
18519:                             } else {
18520:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18521:                             }
18522:                         } else {
18523:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
18524:                         }
18525:                         if ($confighash{'passwords'}{'resetremove'}) {
18526:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
18527:                         } else {
18528:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
18529:                         }
18530:                         if ($confighash{'passwords'}{'resetcustom'}) {
18531:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
18532:                                                                             &mt('custom text'),600,500,undef,undef,
18533:                                                                             undef,undef,'background-color:#ffffff');
18534:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
18535:                         } else {
18536:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
18537:                         }
18538:                     } elsif ($key eq 'intauth') {
18539:                         foreach my $item ('cost','switch','check') {
18540:                             my $value = $save_defaults{$key.'_'.$item};
18541:                             if ($item eq 'switch') {
18542:                                 my %optiondesc = &Apache::lonlocal::texthash (
18543:                                                      0 => 'No',
18544:                                                      1 => 'Yes',
18545:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
18546:                                                  );
18547:                                 if ($value =~ /^(0|1|2)$/) {
18548:                                     $value = $optiondesc{$value};
18549:                                 } else {
18550:                                     $value = &mt('none -- defaults to No');
18551:                                 }
18552:                             } elsif ($item eq 'check') {
18553:                                 my %optiondesc = &Apache::lonlocal::texthash (
18554:                                                      0 => 'No',
18555:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
18556:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
18557:                                                  );
18558:                                 if ($value =~ /^(0|1|2)$/) {
18559:                                     $value = $optiondesc{$value};
18560:                                 } else {
18561:                                     $value = &mt('none -- defaults to No');
18562:                                 }
18563:                             }
18564:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
18565:                         }
18566:                     } elsif ($key eq 'rules') {
18567:                         foreach my $rule ('min','max','expire','numsaved') {
18568:                             if ($confighash{'passwords'}{$rule} eq '') {
18569:                                 if ($rule eq 'min') {
18570:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
18571:                                                    ' '.&mt('Default of [_1] will be used',
18572:                                                            $Apache::lonnet::passwdmin).'</li>';
18573:                                 } else {
18574:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
18575:                                 }
18576:                             } else {
18577:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
18578:                             }
18579:                         }
18580:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
18581:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
18582:                                 my %rulenames = &Apache::lonlocal::texthash(
18583:                                                      uc => 'At least one upper case letter',
18584:                                                      lc => 'At least one lower case letter',
18585:                                                      num => 'At least one number',
18586:                                                      spec => 'At least one non-alphanumeric',
18587:                                                    );
18588:                                 my $needed = '<ul><li>'.
18589:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
18590:                                              '</li></ul>';
18591:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
18592:                             } else {
18593:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18594:                             }
18595:                         } else {
18596:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
18597:                         }
18598:                     } elsif ($key eq 'crsownerchg') {
18599:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
18600:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
18601:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
18602:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18603:                             } else {
18604:                                 my %crsownerstr;
18605:                                 foreach my $item ('by','for') {
18606:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
18607:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
18608:                                             if ($type eq 'default') {
18609:                                                 $crsownerstr{$item} .= $othertitle.', ';
18610:                                             } elsif ($usertypes->{$type} ne '') {
18611:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
18612:                                             }
18613:                                         }
18614:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
18615:                                     }
18616:                                 }
18617:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
18618:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
18619:                             }
18620:                         } else {
18621:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
18622:                         }
18623:                     }
18624:                     $resulttext .= '</ul></li>';
18625:                 }
18626:             }
18627:             $resulttext .= '</ul>';
18628:         } else {
18629:             $resulttext = &mt('No changes made to password settings');
18630:         }
18631:         my $cachetime = 24*60*60;
18632:         if ($updatedefaults) {
18633:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18634:             if (ref($lastactref) eq 'HASH') {
18635:                 $lastactref->{'domdefaults'} = 1;
18636:             }
18637:         }
18638:         if ($updateconf) {
18639:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
18640:             if (ref($lastactref) eq 'HASH') {
18641:                 $lastactref->{'passwdconf'} = 1;
18642:             }
18643:         }
18644:     } else {
18645:         $resulttext = '<span class="LC_error">'.
18646:             &mt('An error occurred: [_1]',$putresult).'</span>';
18647:     }
18648:     if ($errors) {
18649:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18650:                        $errors.'</ul></p>';
18651:     }
18652:     return $resulttext;
18653: }
18654: 
18655: sub password_rule_changes {
18656:     my ($prefix,$newvalues,$current,$changes) = @_;
18657:     return unless ((ref($newvalues) eq 'HASH') &&
18658:                    (ref($current) eq 'HASH') &&
18659:                    (ref($changes) eq 'HASH'));
18660:     my (@rules,%staticdefaults);
18661:     if ($prefix eq 'passwords') {
18662:         @rules = ('min','max','expire','numsaved');
18663:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
18664:         @rules = ('min','max');
18665:     }
18666:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
18667:     foreach my $rule (@rules) {
18668:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
18669:         my $ruleok;
18670:         if ($rule eq 'expire') {
18671:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
18672:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18673:                 $ruleok = 1;
18674:             }
18675:         } elsif ($rule eq 'min') {
18676:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
18677:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
18678:                     $ruleok = 1;
18679:                 }
18680:             }
18681:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
18682:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
18683:             $ruleok = 1;
18684:         }
18685:         if ($ruleok) {
18686:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
18687:             if (exists($current->{$rule})) {
18688:                 if ($newvalues->{$rule} ne $current->{$rule}) {
18689:                     $changes->{'rules'} = 1;
18690:                 }
18691:             } elsif ($rule eq 'min') {
18692:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
18693:                     $changes->{'rules'} = 1;
18694:                 }
18695:             } else {
18696:                 $changes->{'rules'} = 1;
18697:             }
18698:         } elsif (exists($current->{$rule})) {
18699:             $changes->{'rules'} = 1;
18700:         }
18701:     }
18702:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
18703:     my @chars;
18704:     foreach my $item (sort(@posschars)) {
18705:         if ($item =~ /^(uc|lc|num|spec)$/) {
18706:             push(@chars,$item);
18707:         }
18708:     }
18709:     $newvalues->{'chars'} = \@chars;
18710:     unless ($changes->{'rules'}) {
18711:         if (ref($current->{'chars'}) eq 'ARRAY') {
18712:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
18713:             if (@diffs > 0) {
18714:                 $changes->{'rules'} = 1;
18715:             }
18716:         } else {
18717:             if (@chars > 0) {
18718:                 $changes->{'rules'} = 1;
18719:             }
18720:         }
18721:     }
18722:     return;
18723: }
18724: 
18725: sub modify_usercreation {
18726:     my ($dom,%domconfig) = @_;
18727:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
18728:     my $warningmsg;
18729:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18730:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18731:             if ($key eq 'cancreate') {
18732:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18733:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18734:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
18735:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18736:                         } else {
18737:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18738:                         }
18739:                     }
18740:                 }
18741:             } elsif ($key eq 'email_rule') {
18742:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18743:             } else {
18744:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18745:             }
18746:         }
18747:     }
18748:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
18749:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
18750:     my @contexts = ('author','course','requestcrs');
18751:     foreach my $item(@contexts) {
18752:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
18753:     }
18754:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18755:         foreach my $item (@contexts) {
18756:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
18757:                 push(@{$changes{'cancreate'}},$item);
18758:             }
18759:         }
18760:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
18761:         foreach my $item (@contexts) {
18762:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
18763:                 if ($cancreate{$item} ne 'any') {
18764:                     push(@{$changes{'cancreate'}},$item);
18765:                 }
18766:             } else {
18767:                 if ($cancreate{$item} ne 'none') {
18768:                     push(@{$changes{'cancreate'}},$item);
18769:                 }
18770:             }
18771:         }
18772:     } else {
18773:         foreach my $item (@contexts)  {
18774:             push(@{$changes{'cancreate'}},$item);
18775:         }
18776:     }
18777: 
18778:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
18779:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
18780:             if (!grep(/^\Q$type\E$/,@username_rule)) {
18781:                 push(@{$changes{'username_rule'}},$type);
18782:             }
18783:         }
18784:         foreach my $type (@username_rule) {
18785:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
18786:                 push(@{$changes{'username_rule'}},$type);
18787:             }
18788:         }
18789:     } else {
18790:         push(@{$changes{'username_rule'}},@username_rule);
18791:     }
18792: 
18793:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
18794:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
18795:             if (!grep(/^\Q$type\E$/,@id_rule)) {
18796:                 push(@{$changes{'id_rule'}},$type);
18797:             }
18798:         }
18799:         foreach my $type (@id_rule) {
18800:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
18801:                 push(@{$changes{'id_rule'}},$type);
18802:             }
18803:         }
18804:     } else {
18805:         push(@{$changes{'id_rule'}},@id_rule);
18806:     }
18807: 
18808:     my @authen_contexts = ('author','course','domain');
18809:     my @authtypes = ('int','krb4','krb5','loc','lti');
18810:     my %authhash;
18811:     foreach my $item (@authen_contexts) {
18812:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
18813:         foreach my $auth (@authtypes) {
18814:             if (grep(/^\Q$auth\E$/,@authallowed)) {
18815:                 $authhash{$item}{$auth} = 1;
18816:             } else {
18817:                 $authhash{$item}{$auth} = 0;
18818:             }
18819:         }
18820:     }
18821:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
18822:         foreach my $item (@authen_contexts) {
18823:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
18824:                 foreach my $auth (@authtypes) {
18825:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
18826:                         push(@{$changes{'authtypes'}},$item);
18827:                         last;
18828:                     }
18829:                 }
18830:             }
18831:         }
18832:     } else {
18833:         foreach my $item (@authen_contexts) {
18834:             push(@{$changes{'authtypes'}},$item);
18835:         }
18836:     }
18837: 
18838:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
18839:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
18840:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
18841:     $save_usercreate{'id_rule'} = \@id_rule;
18842:     $save_usercreate{'username_rule'} = \@username_rule,
18843:     $save_usercreate{'authtypes'} = \%authhash;
18844: 
18845:     my %usercreation_hash =  (
18846:         usercreation     => \%save_usercreate,
18847:     );
18848: 
18849:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
18850:                                              $dom);
18851: 
18852:     if ($putresult eq 'ok') {
18853:         if (keys(%changes) > 0) {
18854:             $resulttext = &mt('Changes made:').'<ul>';
18855:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18856:                 my %lt = &usercreation_types();
18857:                 foreach my $type (@{$changes{'cancreate'}}) {
18858:                     my $chgtext = $lt{$type}.', ';
18859:                     if ($cancreate{$type} eq 'none') {
18860:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
18861:                     } elsif ($cancreate{$type} eq 'any') {
18862:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
18863:                     } elsif ($cancreate{$type} eq 'official') {
18864:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
18865:                     } elsif ($cancreate{$type} eq 'unofficial') {
18866:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
18867:                     }
18868:                     $resulttext .= '<li>'.$chgtext.'</li>';
18869:                 }
18870:             }
18871:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
18872:                 my ($rules,$ruleorder) = 
18873:                     &Apache::lonnet::inst_userrules($dom,'username');
18874:                 my $chgtext = '<ul>';
18875:                 foreach my $type (@username_rule) {
18876:                     if (ref($rules->{$type}) eq 'HASH') {
18877:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
18878:                     }
18879:                 }
18880:                 $chgtext .= '</ul>';
18881:                 if (@username_rule > 0) {
18882:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
18883:                 } else {
18884:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
18885:                 }
18886:             }
18887:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
18888:                 my ($idrules,$idruleorder) = 
18889:                     &Apache::lonnet::inst_userrules($dom,'id');
18890:                 my $chgtext = '<ul>';
18891:                 foreach my $type (@id_rule) {
18892:                     if (ref($idrules->{$type}) eq 'HASH') {
18893:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
18894:                     }
18895:                 }
18896:                 $chgtext .= '</ul>';
18897:                 if (@id_rule > 0) {
18898:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
18899:                 } else {
18900:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
18901:                 }
18902:             }
18903:             my %authname = &authtype_names();
18904:             my %context_title = &context_names();
18905:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
18906:                 my $chgtext = '<ul>';
18907:                 foreach my $type (@{$changes{'authtypes'}}) {
18908:                     my @allowed;
18909:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
18910:                     foreach my $auth (@authtypes) {
18911:                         if ($authhash{$type}{$auth}) {
18912:                             push(@allowed,$authname{$auth});
18913:                         }
18914:                     }
18915:                     if (@allowed > 0) {
18916:                         $chgtext .= join(', ',@allowed).'</li>';
18917:                     } else {
18918:                         $chgtext .= &mt('none').'</li>';
18919:                     }
18920:                 }
18921:                 $chgtext .= '</ul>';
18922:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
18923:                 $resulttext .= '</li>';
18924:             }
18925:             $resulttext .= '</ul>';
18926:         } else {
18927:             $resulttext = &mt('No changes made to user creation settings');
18928:         }
18929:     } else {
18930:         $resulttext = '<span class="LC_error">'.
18931:             &mt('An error occurred: [_1]',$putresult).'</span>';
18932:     }
18933:     if ($warningmsg ne '') {
18934:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18935:     }
18936:     return $resulttext;
18937: }
18938: 
18939: sub modify_selfcreation {
18940:     my ($dom,$lastactref,%domconfig) = @_;
18941:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
18942:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
18943:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18944:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
18945:     if (ref($typesref) eq 'ARRAY') {
18946:         @types = @{$typesref};
18947:     }
18948:     if (ref($usertypesref) eq 'HASH') {
18949:         %usertypes = %{$usertypesref};
18950:     }
18951:     $usertypes{'default'} = $othertitle;
18952: #
18953: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
18954: #
18955:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
18956:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
18957:             if ($key eq 'cancreate') {
18958:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
18959:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
18960:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
18961:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
18962:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
18963:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
18964:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
18965:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
18966:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18967:                         } else {
18968:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
18969:                         }
18970:                     }
18971:                 }
18972:             } elsif ($key eq 'email_rule') {
18973:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
18974:             } else {
18975:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
18976:             }
18977:         }
18978:     }
18979: #
18980: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
18981: #
18982:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18983:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18984:             if ($key eq 'selfcreate') {
18985:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
18986:             } else {
18987:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
18988:             }
18989:         }
18990:     }
18991: #
18992: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
18993: #
18994:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
18995:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
18996:             if ($key eq 'inststatusguest') {
18997:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
18998:             } else {
18999:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
19000:             }
19001:         }
19002:     }
19003: 
19004:     my @contexts = ('selfcreate');
19005:     @{$cancreate{'selfcreate'}} = ();
19006:     %{$cancreate{'emailusername'}} = ();
19007:     if (@types) {
19008:         @{$cancreate{'statustocreate'}} = ();
19009:     }
19010:     %{$cancreate{'selfcreateprocessing'}} = ();
19011:     %{$cancreate{'shibenv'}} = ();
19012:     %{$cancreate{'emailverified'}} = ();
19013:     %{$cancreate{'emailoptions'}} = ();
19014:     %{$cancreate{'emaildomain'}} = ();
19015:     my %selfcreatetypes = (
19016:                              sso   => 'users authenticated by institutional single sign on',
19017:                              login => 'users authenticated by institutional log-in',
19018:                              email => 'users verified by e-mail',
19019:                           );
19020: #
19021: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
19022: # is permitted.
19023: #
19024:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
19025: 
19026:     my (@statuses,%email_rule);
19027:     foreach my $item ('login','sso','email') {
19028:         if ($item eq 'email') {
19029:             if ($env{'form.cancreate_email'}) {
19030:                 if (@types) {
19031:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
19032:                     foreach my $status (@poss_statuses) {
19033:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
19034:                             push(@statuses,$status);
19035:                         }
19036:                     }
19037:                     $save_inststatus{'inststatusguest'} = \@statuses;
19038:                 } else {
19039:                     push(@statuses,'default');
19040:                 }
19041:                 if (@statuses) {
19042:                     my %curr_rule;
19043:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
19044:                         foreach my $type (@statuses) {
19045:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
19046:                         }
19047:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
19048:                         foreach my $type (@statuses) {
19049:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
19050:                         }
19051:                     }
19052:                     push(@{$cancreate{'selfcreate'}},'email');
19053:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
19054:                     my %curremaildom;
19055:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
19056:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
19057:                     }
19058:                     foreach my $type (@statuses) {
19059:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
19060:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
19061:                         }
19062:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
19063:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
19064:                         }
19065:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
19066: #
19067: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
19068: #
19069:                             my $chosen = $1;
19070:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
19071:                                 my $emaildom;
19072:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
19073:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type};
19074:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
19075:                                     if (ref($curremaildom{$type}) eq 'HASH') {
19076:                                         if (exists($curremaildom{$type}{$chosen})) {
19077:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
19078:                                                 push(@{$changes{'cancreate'}},'emaildomain');
19079:                                             }
19080:                                         } elsif ($emaildom ne '') {
19081:                                             push(@{$changes{'cancreate'}},'emaildomain');
19082:                                         }
19083:                                     } elsif ($emaildom ne '') {
19084:                                         push(@{$changes{'cancreate'}},'emaildomain');
19085:                                     }
19086:                                 }
19087:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19088:                             } elsif ($chosen eq 'custom') {
19089:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
19090:                                 $email_rule{$type} = [];
19091:                                 if (ref($emailrules) eq 'HASH') {
19092:                                     foreach my $rule (@possemail_rules) {
19093:                                         if (exists($emailrules->{$rule})) {
19094:                                             push(@{$email_rule{$type}},$rule);
19095:                                         }
19096:                                     }
19097:                                 }
19098:                                 if (@{$email_rule{$type}}) {
19099:                                     $cancreate{'emailoptions'}{$type} = 'custom';
19100:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
19101:                                         if (@{$curr_rule{$type}} > 0) {
19102:                                             foreach my $rule (@{$curr_rule{$type}}) {
19103:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
19104:                                                     push(@{$changes{'email_rule'}},$type);
19105:                                                 }
19106:                                             }
19107:                                         }
19108:                                         foreach my $type (@{$email_rule{$type}}) {
19109:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
19110:                                                 push(@{$changes{'email_rule'}},$type);
19111:                                             }
19112:                                         }
19113:                                     } else {
19114:                                         push(@{$changes{'email_rule'}},$type);
19115:                                     }
19116:                                 }
19117:                             } else {
19118:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
19119:                             }
19120:                         }
19121:                     }
19122:                     if (@types) {
19123:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19124:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
19125:                             if (@changed) {
19126:                                 push(@{$changes{'inststatus'}},'inststatusguest');
19127:                             }
19128:                         } else {
19129:                             push(@{$changes{'inststatus'}},'inststatusguest');
19130:                         }
19131:                     }
19132:                 } else {
19133:                     delete($env{'form.cancreate_email'});
19134:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19135:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19136:                             push(@{$changes{'inststatus'}},'inststatusguest');
19137:                         }
19138:                     }
19139:                 }
19140:             } else {
19141:                 $save_inststatus{'inststatusguest'} = [];
19142:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
19143:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
19144:                         push(@{$changes{'inststatus'}},'inststatusguest');
19145:                     }
19146:                 }
19147:             }
19148:         } else {
19149:             if ($env{'form.cancreate_'.$item}) {
19150:                 push(@{$cancreate{'selfcreate'}},$item);
19151:             }
19152:         }
19153:     }
19154:     my (%userinfo,%savecaptcha);
19155:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
19156: #
19157: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
19158: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
19159: #
19160: 
19161:     if ($env{'form.cancreate_email'}) {
19162:         push(@contexts,'emailusername');
19163:         if (@statuses) {
19164:             foreach my $type (@statuses) {
19165:                 if (ref($infofields) eq 'ARRAY') {
19166:                     foreach my $field (@{$infofields}) {
19167:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
19168:                             $cancreate{'emailusername'}{$type}{$field} = $1;
19169:                         }
19170:                     }
19171:                 }
19172:             }
19173:         }
19174: #
19175: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
19176: # queued requests for self-creation of account verified by e-mail.
19177: #
19178: 
19179:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
19180:         @approvalnotify = sort(@approvalnotify);
19181:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
19182:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19183:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
19184:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
19185:                     push(@{$changes{'cancreate'}},'notify');
19186:                 }
19187:             } else {
19188:                 if ($cancreate{'notify'}{'approval'}) {
19189:                     push(@{$changes{'cancreate'}},'notify');
19190:                 }
19191:             }
19192:         } elsif ($cancreate{'notify'}{'approval'}) {
19193:             push(@{$changes{'cancreate'}},'notify');
19194:         }
19195: 
19196:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
19197:     }
19198: #  
19199: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
19200: # institutional log-in.
19201: #
19202:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
19203:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
19204:                ($domdefaults{'auth_def'} eq 'localauth'))) {
19205:             $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.').' '.
19206:                           &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.');
19207:         }
19208:     }
19209:     my @fields = ('lastname','firstname','middlename','generation',
19210:                   'permanentemail','id');
19211:     my @shibfields = (@fields,'inststatus');
19212:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19213: #
19214: # Where usernames may created for institutional log-in and/or institutional single sign on:
19215: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
19216: # may self-create accounts 
19217: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
19218: # which the user may supply, if institutional data is unavailable.
19219: #
19220:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
19221:         if (@types) {
19222:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
19223:             push(@contexts,'statustocreate');
19224:             foreach my $type (@types) {
19225:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
19226:                 foreach my $field (@fields) {
19227:                     if (grep(/^\Q$field\E$/,@modifiable)) {
19228:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
19229:                     } else {
19230:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
19231:                     }
19232:                 }
19233:             }
19234:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
19235:                 foreach my $type (@types) {
19236:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
19237:                         foreach my $field (@fields) {
19238:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
19239:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
19240:                                 push(@{$changes{'selfcreate'}},$type);
19241:                                 last;
19242:                             }
19243:                         }
19244:                     }
19245:                 }
19246:             } else {
19247:                 foreach my $type (@types) {
19248:                     push(@{$changes{'selfcreate'}},$type);
19249:                 }
19250:             }
19251:         }
19252:         foreach my $field (@shibfields) {
19253:             if ($env{'form.shibenv_'.$field} ne '') {
19254:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
19255:             }
19256:         }
19257:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
19258:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
19259:                 foreach my $field (@shibfields) {
19260:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
19261:                         push(@{$changes{'cancreate'}},'shibenv');
19262:                     }
19263:                 }
19264:             } else {
19265:                 foreach my $field (@shibfields) {
19266:                     if ($env{'form.shibenv_'.$field}) {
19267:                         push(@{$changes{'cancreate'}},'shibenv');
19268:                         last;
19269:                     }
19270:                 }
19271:             }
19272:         }
19273:     }
19274:     foreach my $item (@contexts) {
19275:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
19276:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
19277:                 if (ref($cancreate{$item}) eq 'ARRAY') {
19278:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
19279:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19280:                             push(@{$changes{'cancreate'}},$item);
19281:                         }
19282:                     }
19283:                 }
19284:             }
19285:             if (ref($cancreate{$item}) eq 'ARRAY') {
19286:                 foreach my $type (@{$cancreate{$item}}) {
19287:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
19288:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19289:                             push(@{$changes{'cancreate'}},$item);
19290:                         }
19291:                     }
19292:                 }
19293:             }
19294:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
19295:             if (ref($cancreate{$item}) eq 'HASH') {
19296:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
19297:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19298:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
19299:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19300:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19301:                                     push(@{$changes{'cancreate'}},$item);
19302:                                 }
19303:                             }
19304:                         }
19305:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19306:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19307:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19308:                                 push(@{$changes{'cancreate'}},$item);
19309:                             }
19310:                         }
19311:                     }
19312:                 }
19313:                 foreach my $type (keys(%{$cancreate{$item}})) {
19314:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19315:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19316:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
19317:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
19318:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19319:                                         push(@{$changes{'cancreate'}},$item);
19320:                                     }
19321:                                 }
19322:                             } else {
19323:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19324:                                     push(@{$changes{'cancreate'}},$item);
19325:                                 }
19326:                             }
19327:                         }
19328:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19329:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
19330:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19331:                                 push(@{$changes{'cancreate'}},$item);
19332:                             }
19333:                         }
19334:                     }
19335:                 }
19336:             }
19337:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
19338:             if (ref($cancreate{$item}) eq 'ARRAY') {
19339:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
19340:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19341:                         push(@{$changes{'cancreate'}},$item);
19342:                     }
19343:                 }
19344:             }
19345:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
19346:             if (ref($cancreate{$item}) eq 'HASH') {
19347:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19348:                     push(@{$changes{'cancreate'}},$item);
19349:                 }
19350:             }
19351:         } elsif ($item eq 'emailusername') {
19352:             if (ref($cancreate{$item}) eq 'HASH') {
19353:                 foreach my $type (keys(%{$cancreate{$item}})) {
19354:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
19355:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
19356:                             if ($cancreate{$item}{$type}{$field}) {
19357:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
19358:                                     push(@{$changes{'cancreate'}},$item);
19359:                                 }
19360:                                 last;
19361:                             }
19362:                         }
19363:                     }
19364:                 }
19365:             }
19366:         }
19367:     }
19368: #
19369: # Populate %save_usercreate hash with updates to self-creation configuration.
19370: #
19371:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
19372:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
19373:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
19374:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
19375:     if (ref($cancreate{'notify'}) eq 'HASH') {
19376:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
19377:     }
19378:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
19379:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
19380:     }
19381:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
19382:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
19383:     }
19384:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
19385:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
19386:     }
19387:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
19388:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
19389:     }
19390:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19391:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
19392:     }
19393:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
19394:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
19395:     }
19396:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
19397:     $save_usercreate{'email_rule'} = \%email_rule;
19398: 
19399:     my %userconfig_hash = (
19400:             usercreation     => \%save_usercreate,
19401:             usermodification => \%save_usermodify,
19402:             inststatus       => \%save_inststatus,
19403:     );
19404: 
19405:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
19406:                                              $dom);
19407: #
19408: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
19409: #
19410:     if ($putresult eq 'ok') {
19411:         if (keys(%changes) > 0) {
19412:             $resulttext = &mt('Changes made:').'<ul>';
19413:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
19414:                 my %lt = &selfcreation_types();
19415:                 foreach my $type (@{$changes{'cancreate'}}) {
19416:                     my $chgtext = '';
19417:                     if ($type eq 'selfcreate') {
19418:                         if (@{$cancreate{$type}} == 0) {
19419:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
19420:                         } else {
19421:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
19422:                                         '<ul>';
19423:                             foreach my $case (@{$cancreate{$type}}) {
19424:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
19425:                             }
19426:                             $chgtext .= '</ul>';
19427:                             if (ref($cancreate{$type}) eq 'ARRAY') {
19428:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
19429:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
19430:                                         if (@{$cancreate{'statustocreate'}} == 0) {
19431:                                             $chgtext .= '<span class="LC_warning">'.
19432:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
19433:                                                         '</span><br />';
19434:                                         }
19435:                                     }
19436:                                 }
19437:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
19438:                                     if (!@statuses) {
19439:                                         $chgtext .= '<span class="LC_warning">'.
19440:                                                     &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.").
19441:                                                     '</span><br />';
19442: 
19443:                                     }
19444:                                 }
19445:                             }
19446:                         }
19447:                     } elsif ($type eq 'shibenv') {
19448:                         if (keys(%{$cancreate{$type}}) == 0) {
19449:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
19450:                         } else {
19451:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
19452:                                         '<ul>';
19453:                             foreach my $field (@shibfields) {
19454:                                 next if ($cancreate{$type}{$field} eq '');
19455:                                 if ($field eq 'inststatus') {
19456:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
19457:                                 } else {
19458:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
19459:                                 }
19460:                             }
19461:                             $chgtext .= '</ul>';
19462:                         }
19463:                     } elsif ($type eq 'statustocreate') {
19464:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
19465:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
19466:                             if (@{$cancreate{'selfcreate'}} > 0) {
19467:                                 if (@{$cancreate{'statustocreate'}} == 0) {
19468:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
19469:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
19470:                                         $chgtext .= '<br />'.
19471:                                                     '<span class="LC_warning">'.
19472:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
19473:                                                     '</span>';
19474:                                     }
19475:                                 } elsif (keys(%usertypes) > 0) {
19476:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19477:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
19478:                                     } else {
19479:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
19480:                                     }
19481:                                     $chgtext .= '<ul>';
19482:                                     foreach my $case (@{$cancreate{$type}}) {
19483:                                         if ($case eq 'default') {
19484:                                             $chgtext .= '<li>'.$othertitle.'</li>';
19485:                                         } else {
19486:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
19487:                                         }
19488:                                     }
19489:                                     $chgtext .= '</ul>';
19490:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
19491:                                         $chgtext .= '<span class="LC_warning">'.
19492:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
19493:                                                     '</span>';
19494:                                     }
19495:                                 }
19496:                             } else {
19497:                                 if (@{$cancreate{$type}} == 0) {
19498:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
19499:                                 } else {
19500:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
19501:                                 }
19502:                             }
19503:                             $chgtext .= '<br />';
19504:                         }
19505:                     } elsif ($type eq 'selfcreateprocessing') {
19506:                         my %choices = &Apache::lonlocal::texthash (
19507:                                                                     automatic => 'Automatic approval',
19508:                                                                     approval  => 'Queued for approval',
19509:                                                                   );
19510:                         if (@types) {
19511:                             if (@statuses) {
19512:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:').
19513:                                             '<ul>';
19514:                                 foreach my $status (@statuses) {
19515:                                     if ($status eq 'default') {
19516:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19517:                                     } else {
19518:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
19519:                                     }
19520:                                 }
19521:                                 $chgtext .= '</ul>';
19522:                             }
19523:                         } else {
19524:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
19525:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
19526:                         }
19527:                     } elsif ($type eq 'emailverified') {
19528:                         my %options = &Apache::lonlocal::texthash (
19529:                                                                     all   => 'Same as e-mail',
19530:                                                                     first => 'Omit @domain',
19531:                                                                     free  => 'Free to choose',
19532:                                                                   );
19533:                         if (@types) {
19534:                             if (@statuses) {
19535:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
19536:                                             '<ul>';
19537:                                 foreach my $status (@statuses) {
19538:                                     if ($status eq 'default') {
19539:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19540:                                     } else {
19541:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
19542:                                     }
19543:                                 }
19544:                                 $chgtext .= '</ul>';
19545:                             }
19546:                         } else {
19547:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
19548:                                             $options{$cancreate{'emailverified'}{'default'}});
19549:                         }
19550:                     } elsif ($type eq 'emailoptions') {
19551:                         my %options = &Apache::lonlocal::texthash (
19552:                                                                     any     => 'Any e-mail',
19553:                                                                     inst    => 'Institutional only',
19554:                                                                     noninst => 'Non-institutional only',
19555:                                                                     custom  => 'Custom restrictions',
19556:                                                                   );
19557:                         if (@types) {
19558:                             if (@statuses) {
19559:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
19560:                                             '<ul>';
19561:                                 foreach my $status (@statuses) {
19562:                                     if ($type eq 'default') {
19563:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19564:                                     } else {
19565:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
19566:                                     }
19567:                                 }
19568:                                 $chgtext .= '</ul>';
19569:                             }
19570:                         } else {
19571:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
19572:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
19573:                             } else {
19574:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
19575:                                                 $options{$cancreate{'emailoptions'}{'default'}});
19576:                             }
19577:                         }
19578:                     } elsif ($type eq 'emaildomain') {
19579:                         my $output;
19580:                         if (@statuses) {
19581:                             foreach my $type (@statuses) {
19582:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
19583:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
19584:                                         if ($type eq 'default') {
19585:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19586:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19587:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19588:                                             } else {
19589:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
19590:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19591:                                             }
19592:                                         } else {
19593:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19594:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
19595:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19596:                                             } else {
19597:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
19598:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
19599:                                             }
19600:                                         }
19601:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
19602:                                         if ($type eq 'default') {
19603:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19604:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19605:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
19606:                                             } else {
19607:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
19608:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19609:                                             }
19610:                                         } else {
19611:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
19612:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
19613:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
19614:                                             } else {
19615:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
19616:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
19617:                                             }
19618:                                         }
19619:                                     }
19620:                                 }
19621:                             }
19622:                         }
19623:                         if ($output ne '') {
19624:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
19625:                                         '<ul>'.$output.'</ul>';
19626:                         }
19627:                     } elsif ($type eq 'captcha') {
19628:                         if ($savecaptcha{$type} eq 'notused') {
19629:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
19630:                         } else {
19631:                             my %captchas = &captcha_phrases();
19632:                             if ($captchas{$savecaptcha{$type}}) {
19633:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
19634:                             } else {
19635:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
19636:                             }
19637:                         }
19638:                     } elsif ($type eq 'recaptchakeys') {
19639:                         my ($privkey,$pubkey);
19640:                         if (ref($savecaptcha{$type}) eq 'HASH') {
19641:                             $pubkey = $savecaptcha{$type}{'public'};
19642:                             $privkey = $savecaptcha{$type}{'private'};
19643:                         }
19644:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
19645:                         if (!$pubkey) {
19646:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
19647:                         } else {
19648:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
19649:                         }
19650:                         if (!$privkey) {
19651:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
19652:                         } else {
19653:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
19654:                         }
19655:                         $chgtext .= '</ul>';
19656:                     } elsif ($type eq 'recaptchaversion') {
19657:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
19658:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
19659:                         }
19660:                     } elsif ($type eq 'emailusername') {
19661:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
19662:                             if (@statuses) {
19663:                                 foreach my $type (@statuses) {
19664:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
19665:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
19666:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
19667:                                                     '<ul>';
19668:                                             foreach my $field (@{$infofields}) {
19669:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
19670:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
19671:                                                 }
19672:                                             }
19673:                                             $chgtext .= '</ul>';
19674:                                         } else {
19675:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19676:                                         }
19677:                                     } else {
19678:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
19679:                                     }
19680:                                 }
19681:                             }
19682:                         }
19683:                     } elsif ($type eq 'notify') {
19684:                         my $numapprove = 0;
19685:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
19686:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
19687:                                 if ($cancreate{'notify'}{'approval'}) {
19688:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
19689:                                     $numapprove ++;
19690:                                 }
19691:                             }
19692:                         }
19693:                         unless ($numapprove) {
19694:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
19695:                         }
19696:                     }
19697:                     if ($chgtext) {
19698:                         $resulttext .= '<li>'.$chgtext.'</li>';
19699:                     }
19700:                 }
19701:             }
19702:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
19703:                 my ($emailrules,$emailruleorder) =
19704:                     &Apache::lonnet::inst_userrules($dom,'email');
19705:                 foreach my $type (@{$changes{'email_rule'}}) {
19706:                     if (ref($email_rule{$type}) eq 'ARRAY') {
19707:                         my $chgtext = '<ul>';
19708:                         foreach my $rule (@{$email_rule{$type}}) {
19709:                             if (ref($emailrules->{$rule}) eq 'HASH') {
19710:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
19711:                             }
19712:                         }
19713:                         $chgtext .= '</ul>';
19714:                         my $typename;
19715:                         if (@types) {
19716:                             if ($type eq 'default') {
19717:                                 $typename = $othertitle;
19718:                             } else {
19719:                                 $typename = $usertypes{$type};
19720:                             }
19721:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
19722:                         }
19723:                         if (@{$email_rule{$type}} > 0) {
19724:                             $resulttext .= '<li>'.
19725:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
19726:                                                $usertypes{$type}).
19727:                                            $chgtext.
19728:                                            '</li>';
19729:                         } else {
19730:                             $resulttext .= '<li>'.
19731:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
19732:                                            '</li>'.
19733:                                            &mt('(Affiliation: [_1])',$typename);
19734:                         }
19735:                     }
19736:                 }
19737:             }
19738:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
19739:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
19740:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
19741:                         my $chgtext = '<ul>';
19742:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
19743:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
19744:                         }
19745:                         $chgtext .= '</ul>';
19746:                         $resulttext .= '<li>'.
19747:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
19748:                                           $chgtext.
19749:                                        '</li>';
19750:                     } else {
19751:                         $resulttext .= '<li>'.
19752:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
19753:                                        '</li>';
19754:                     }
19755:                 }
19756:             }
19757:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
19758:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
19759:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19760:                 foreach my $type (@{$changes{'selfcreate'}}) {
19761:                     my $typename = $type;
19762:                     if (keys(%usertypes) > 0) {
19763:                         if ($usertypes{$type} ne '') {
19764:                             $typename = $usertypes{$type};
19765:                         }
19766:                     }
19767:                     my @modifiable;
19768:                     $resulttext .= '<li>'.
19769:                                     &mt('Self-creation of account by users with status: [_1]',
19770:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
19771:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
19772:                     foreach my $field (@fields) {
19773:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
19774:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
19775:                         }
19776:                     }
19777:                     if (@modifiable > 0) {
19778:                         $resulttext .= join(', ',@modifiable);
19779:                     } else {
19780:                         $resulttext .= &mt('none');
19781:                     }
19782:                     $resulttext .= '</li>';
19783:                 }
19784:                 $resulttext .= '</ul></li>';
19785:             }
19786:             $resulttext .= '</ul>';
19787:             my $cachetime = 24*60*60;
19788:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
19789:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19790:             if (ref($lastactref) eq 'HASH') {
19791:                 $lastactref->{'domdefaults'} = 1;
19792:             }
19793:         } else {
19794:             $resulttext = &mt('No changes made to self-creation settings');
19795:         }
19796:     } else {
19797:         $resulttext = '<span class="LC_error">'.
19798:             &mt('An error occurred: [_1]',$putresult).'</span>';
19799:     }
19800:     if ($warningmsg ne '') {
19801:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
19802:     }
19803:     return $resulttext;
19804: }
19805: 
19806: sub process_captcha {
19807:     my ($container,$changes,$newsettings,$currsettings) = @_;
19808:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
19809:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
19810:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
19811:         $newsettings->{'captcha'} = 'original';
19812:     }
19813:     my %current;
19814:     if (ref($currsettings) eq 'HASH') {
19815:         %current = %{$currsettings};
19816:     }
19817:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
19818:         if ($container eq 'cancreate') {
19819:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19820:                 push(@{$changes->{'cancreate'}},'captcha');
19821:             } elsif (!defined($changes->{'cancreate'})) {
19822:                 $changes->{'cancreate'} = ['captcha'];
19823:             }
19824:         } elsif ($container eq 'passwords') {
19825:             $changes->{'reset'} = 1;
19826:         } else {
19827:             $changes->{'captcha'} = 1;
19828:         }
19829:     }
19830:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
19831:     if ($newsettings->{'captcha'} eq 'recaptcha') {
19832:         $newpub = $env{'form.'.$container.'_recaptchapub'};
19833:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
19834:         $newpub =~ s/[^\w\-]//g;
19835:         $newpriv =~ s/[^\w\-]//g;
19836:         $newsettings->{'recaptchakeys'} = {
19837:                                              public  => $newpub,
19838:                                              private => $newpriv,
19839:                                           };
19840:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
19841:         $newversion =~ s/\D//g;
19842:         if ($newversion ne '2') {
19843:             $newversion = 1;
19844:         }
19845:         $newsettings->{'recaptchaversion'} = $newversion;
19846:     }
19847:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
19848:         $currpub = $current{'recaptchakeys'}{'public'};
19849:         $currpriv = $current{'recaptchakeys'}{'private'};
19850:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
19851:             $newsettings->{'recaptchakeys'} = {
19852:                                                  public  => '',
19853:                                                  private => '',
19854:                                               }
19855:         }
19856:     }
19857:     if ($current{'captcha'} eq 'recaptcha') {
19858:         $currversion = $current{'recaptchaversion'};
19859:         if ($currversion ne '2') {
19860:             $currversion = 1;
19861:         }
19862:     }
19863:     if ($currversion ne $newversion) {
19864:         if ($container eq 'cancreate') {
19865:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19866:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
19867:             } elsif (!defined($changes->{'cancreate'})) {
19868:                 $changes->{'cancreate'} = ['recaptchaversion'];
19869:             }
19870:         } elsif ($container eq 'passwords') {
19871:             $changes->{'reset'} = 1;
19872:         } else {
19873:             $changes->{'recaptchaversion'} = 1;
19874:         }
19875:     }
19876:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
19877:         if ($container eq 'cancreate') {
19878:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
19879:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
19880:             } elsif (!defined($changes->{'cancreate'})) {
19881:                 $changes->{'cancreate'} = ['recaptchakeys'];
19882:             }
19883:         } elsif ($container eq 'passwords') {
19884:             $changes->{'reset'} = 1;
19885:         } else {
19886:             $changes->{'recaptchakeys'} = 1;
19887:         }
19888:     }
19889:     return;
19890: }
19891: 
19892: sub modify_usermodification {
19893:     my ($dom,%domconfig) = @_;
19894:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
19895:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
19896:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
19897:             if ($key eq 'selfcreate') {
19898:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
19899:             } else {  
19900:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
19901:             }
19902:         }
19903:     }
19904:     my @contexts = ('author','coauthor','course');
19905:     my %context_title = (
19906:                            author => 'In author context',
19907:                            coauthor => 'As co-author manager',
19908:                            course => 'In course context',
19909:                         );
19910:     my @fields = ('lastname','firstname','middlename','generation',
19911:                   'permanentemail','id');
19912:     my %roles = (
19913:                   author => ['ca','aa'],
19914:                   coauthor => ['ca','aa'],
19915:                   course => ['st','ep','ta','in','cr'],
19916:                 );
19917:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
19918:     foreach my $context (@contexts) {
19919:         my $prefix = 'canmodify';
19920:         if ($context eq 'coauthor') {
19921:             $prefix = 'cacanmodify';
19922:         }
19923:         foreach my $role (@{$roles{$context}}) {
19924:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$role);
19925:             foreach my $item (@fields) {
19926:                 if (grep(/^\Q$item\E$/,@modifiable)) {
19927:                     $modifyhash{$context}{$role}{$item} = 1;
19928:                 } else {
19929:                     $modifyhash{$context}{$role}{$item} = 0;
19930:                 }
19931:             }
19932:         }
19933:         if (ref($curr_usermodification{$context}) eq 'HASH') {
19934:             foreach my $role (@{$roles{$context}}) {
19935:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
19936:                     foreach my $field (@fields) {
19937:                         if ($modifyhash{$context}{$role}{$field} ne 
19938:                                 $curr_usermodification{$context}{$role}{$field}) {
19939:                             push(@{$changes{$context}},$role);
19940:                             last;
19941:                         }
19942:                     }
19943:                 }
19944:             }
19945:         } else {
19946:             foreach my $context (@contexts) {
19947:                 foreach my $role (@{$roles{$context}}) {
19948:                     push(@{$changes{$context}},$role);
19949:                 }
19950:             }
19951:         }
19952:     }
19953:     my %usermodification_hash =  (
19954:                                    usermodification => \%modifyhash,
19955:                                  );
19956:     my $putresult = &Apache::lonnet::put_dom('configuration',
19957:                                              \%usermodification_hash,$dom);
19958:     if ($putresult eq 'ok') {
19959:         if (keys(%changes) > 0) {
19960:             $resulttext = &mt('Changes made: ').'<ul>';
19961:             foreach my $context (@contexts) {
19962:                 if (ref($changes{$context}) eq 'ARRAY') {
19963:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
19964:                     if (ref($changes{$context}) eq 'ARRAY') {
19965:                         foreach my $role (@{$changes{$context}}) {
19966:                             my $rolename;
19967:                             if ($role eq 'cr') {
19968:                                 $rolename = &mt('Custom');
19969:                             } else {
19970:                                 $rolename = &Apache::lonnet::plaintext($role);
19971:                             }
19972:                             my @modifiable;
19973:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
19974:                             foreach my $field (@fields) {
19975:                                 if ($modifyhash{$context}{$role}{$field}) {
19976:                                     push(@modifiable,$fieldtitles{$field});
19977:                                 }
19978:                             }
19979:                             if (@modifiable > 0) {
19980:                                 $resulttext .= join(', ',@modifiable);
19981:                             } else {
19982:                                 $resulttext .= &mt('none'); 
19983:                             }
19984:                             $resulttext .= '</li>';
19985:                         }
19986:                         $resulttext .= '</ul></li>';
19987:                     }
19988:                 }
19989:             }
19990:             $resulttext .= '</ul>';
19991:         } else {
19992:             $resulttext = &mt('No changes made to user modification settings');
19993:         }
19994:     } else {
19995:         $resulttext = '<span class="LC_error">'.
19996:             &mt('An error occurred: [_1]',$putresult).'</span>';
19997:     }
19998:     return $resulttext;
19999: }
20000: 
20001: sub modify_defaults {
20002:     my ($dom,$lastactref,%domconfig) = @_;
20003:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
20004:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20005:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
20006:                  'portal_def');
20007:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
20008:     foreach my $item (@items) {
20009:         $newvalues{$item} = $env{'form.'.$item};
20010:         if ($item eq 'auth_def') {
20011:             if ($newvalues{$item} ne '') {
20012:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
20013:                     push(@errors,$item);
20014:                 }
20015:             }
20016:         } elsif ($item eq 'lang_def') {
20017:             if ($newvalues{$item} ne '') {
20018:                 if ($newvalues{$item} =~ /^(\w+)/) {
20019:                     my $langcode = $1;
20020:                     if ($langcode ne 'x_chef') {
20021:                         if (code2language($langcode) eq '') {
20022:                             push(@errors,$item);
20023:                         }
20024:                     }
20025:                 } else {
20026:                     push(@errors,$item);
20027:                 }
20028:             }
20029:         } elsif ($item eq 'timezone_def') {
20030:             if ($newvalues{$item} ne '') {
20031:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
20032:                     push(@errors,$item);   
20033:                 }
20034:             }
20035:         } elsif ($item eq 'datelocale_def') {
20036:             if ($newvalues{$item} ne '') {
20037:                 my @datelocale_ids = DateTime::Locale->ids();
20038:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
20039:                     push(@errors,$item);
20040:                 }
20041:             }
20042:         } elsif ($item eq 'portal_def') {
20043:             if ($newvalues{$item} ne '') {
20044:                 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])\/?$/) {
20045:                     foreach my $field ('email','web') {
20046:                         if ($env{'form.'.$item.'_'.$field}) {
20047:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
20048:                         }
20049:                     }
20050:                 } else {
20051:                     push(@errors,$item);
20052:                 }
20053:             }
20054:         }
20055:         if (grep(/^\Q$item\E$/,@errors)) {
20056:             $newvalues{$item} = $domdefaults{$item};
20057:             if ($item eq 'portal_def') {
20058:                 if ($domdefaults{$item}) {
20059:                     foreach my $field ('email','web') {
20060:                         if (exists($domdefaults{$item.'_'.$field})) {
20061:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
20062:                         }
20063:                     }
20064:                 }
20065:             }
20066:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
20067:             $changes{$item} = 1;
20068:         }
20069:         if ($item eq 'portal_def') {
20070:             unless (grep(/^\Q$item\E$/,@errors)) {
20071:                 if ($newvalues{$item} eq '') {
20072:                     foreach my $field ('email','web') {
20073:                         if (exists($domdefaults{$item.'_'.$field})) {
20074:                             delete($domdefaults{$item.'_'.$field});
20075:                         }
20076:                     }
20077:                 } else {
20078:                     unless ($changes{$item}) {
20079:                         foreach my $field ('email','web') {
20080:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
20081:                                 $changes{$item} = 1;
20082:                                 last;
20083:                             }
20084:                         }
20085:                     }
20086:                     foreach my $field ('email','web') {
20087:                         if ($newvalues{$item.'_'.$field}) {
20088:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
20089:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
20090:                             delete($domdefaults{$item.'_'.$field});
20091:                         }
20092:                     }
20093:                 }
20094:             }
20095:         }
20096:         $domdefaults{$item} = $newvalues{$item};
20097:     }
20098:     my %staticdefaults = (
20099:                            'intauth_cost'   => 10,
20100:                            'intauth_check'  => 0,
20101:                            'intauth_switch' => 0,
20102:                          );
20103:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
20104:         if (exists($domdefaults{$item})) {
20105:             $newvalues{$item} = $domdefaults{$item};
20106:         } else {
20107:             $newvalues{$item} = $staticdefaults{$item};
20108:         }
20109:     }
20110:     my ($unamemaprules,$ruleorder);
20111:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
20112:     if (@possunamemaprules) {
20113:         ($unamemaprules,$ruleorder) =
20114:             &Apache::lonnet::inst_userrules($dom,'unamemap');
20115:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
20116:             if (@{$ruleorder} > 0) {
20117:                 my %possrules;
20118:                 map { $possrules{$_} = 1; } @possunamemaprules;
20119:                 foreach my $rule (@{$ruleorder}) {
20120:                     if ($possrules{$rule}) {
20121:                         push(@{$newvalues{'unamemap_rule'}},$rule);
20122:                     }
20123:                 }
20124:             }
20125:         }
20126:     }
20127:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
20128:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20129:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
20130:                                                                $newvalues{'unamemap_rule'});
20131:             if (@rulediffs) {
20132:                 $changes{'unamemap_rule'} = 1;
20133:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20134:             }
20135:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
20136:             $changes{'unamemap_rule'} = 1;
20137:             delete($domdefaults{'unamemap_rule'});
20138:         }
20139:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20140:         if (@{$newvalues{'unamemap_rule'}} > 0) {
20141:             $changes{'unamemap_rule'} = 1;
20142:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
20143:         }
20144:     }
20145:     my %defaults_hash = (
20146:                          defaults => \%newvalues,
20147:                         );
20148:     my $title = &defaults_titles();
20149: 
20150:     my $currinststatus;
20151:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
20152:         $currinststatus = $domconfig{'inststatus'};
20153:     } else {
20154:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20155:         $currinststatus = {
20156:                              inststatustypes => $usertypes,
20157:                              inststatusorder => $types,
20158:                              inststatusguest => [],
20159:                           };
20160:     }
20161:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
20162:     my @allpos;
20163:     my %alltypes;
20164:     my @inststatusguest;
20165:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
20166:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
20167:             unless (grep(/^\Q$type\E$/,@todelete)) {
20168:                 push(@inststatusguest,$type);
20169:             }
20170:         }
20171:     }
20172:     my ($currtitles,$currorder);
20173:     if (ref($currinststatus) eq 'HASH') {
20174:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
20175:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
20176:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
20177:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
20178:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
20179:                     }
20180:                 }
20181:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
20182:                     my $position = $env{'form.inststatus_pos_'.$type};
20183:                     $position =~ s/\D+//g;
20184:                     $allpos[$position] = $type;
20185:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
20186:                     $alltypes{$type} =~ s/`//g;
20187:                 }
20188:             }
20189:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
20190:             $currtitles =~ s/,$//;
20191:         }
20192:     }
20193:     if ($env{'form.addinststatus'}) {
20194:         my $newtype = $env{'form.addinststatus'};
20195:         $newtype =~ s/\W//g;
20196:         unless (exists($alltypes{$newtype})) {
20197:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
20198:             $alltypes{$newtype} =~ s/`//g; 
20199:             my $position = $env{'form.addinststatus_pos'};
20200:             $position =~ s/\D+//g;
20201:             if ($position ne '') {
20202:                 $allpos[$position] = $newtype;
20203:             }
20204:         }
20205:     }
20206:     my @orderedstatus;
20207:     foreach my $type (@allpos) {
20208:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
20209:             push(@orderedstatus,$type);
20210:         }
20211:     }
20212:     foreach my $type (keys(%alltypes)) {
20213:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
20214:             delete($alltypes{$type});
20215:         }
20216:     }
20217:     $defaults_hash{'inststatus'} = {
20218:                                      inststatustypes => \%alltypes,
20219:                                      inststatusorder => \@orderedstatus,
20220:                                      inststatusguest => \@inststatusguest,
20221:                                    };
20222:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
20223:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
20224:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
20225:         }
20226:     }
20227:     if ($currorder ne join(',',@orderedstatus)) {
20228:         $changes{'inststatus'}{'inststatusorder'} = 1;
20229:     }
20230:     my $newtitles;
20231:     foreach my $item (@orderedstatus) {
20232:         $newtitles .= $alltypes{$item}.',';
20233:     }
20234:     $newtitles =~ s/,$//;
20235:     if ($currtitles ne $newtitles) {
20236:         $changes{'inststatus'}{'inststatustypes'} = 1;
20237:     }
20238:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
20239:                                              $dom);
20240:     if ($putresult eq 'ok') {
20241:         if (keys(%changes) > 0) {
20242:             $resulttext = &mt('Changes made:').'<ul>';
20243:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
20244:             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";
20245:             foreach my $item (sort(keys(%changes))) {
20246:                 if ($item eq 'inststatus') {
20247:                     if (ref($changes{'inststatus'}) eq 'HASH') {
20248:                         if (@orderedstatus) {
20249:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
20250:                             foreach my $type (@orderedstatus) { 
20251:                                 $resulttext .= $alltypes{$type}.', ';
20252:                             }
20253:                             $resulttext =~ s/, $//;
20254:                             $resulttext .= '</li>';
20255:                         } else {
20256:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>';
20257:                         }
20258:                     }
20259:                 } elsif ($item eq 'unamemap_rule') {
20260:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
20261:                         my @rulenames;
20262:                         if (ref($unamemaprules) eq 'HASH') {
20263:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
20264:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
20265:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
20266:                                 }
20267:                             }
20268:                         }
20269:                         if (@rulenames) {
20270:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
20271:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
20272:                                            '</li>';
20273:                         } else {
20274:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
20275:                         }
20276:                     } else {
20277:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
20278:                     }
20279:                 } else {
20280:                     my $value = $env{'form.'.$item};
20281:                     if ($value eq '') {
20282:                         $value = &mt('none');
20283:                     } elsif ($item eq 'auth_def') {
20284:                         my %authnames = &authtype_names();
20285:                         my %shortauth = (
20286:                                           internal   => 'int',
20287:                                           krb4       => 'krb4',
20288:                                           krb5       => 'krb5',
20289:                                           localauth  => 'loc',
20290:                                           lti        => 'lti',
20291:                         );
20292:                         $value = $authnames{$shortauth{$value}};
20293:                     }
20294:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
20295:                     $mailmsgtext .= "$title->{$item} set to $value\n";
20296:                     if ($item eq 'portal_def') {
20297:                         if ($env{'form.'.$item} ne '') {
20298:                             foreach my $field ('email','web') {
20299:                                 $value = $env{'form.'.$item.'_'.$field};
20300:                                 if ($value) {
20301:                                     $value = &mt('Yes');
20302:                                 } else {
20303:                                     $value = &mt('No');
20304:                                 }
20305:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
20306:                             }
20307:                         }
20308:                     }
20309:                 }
20310:             }
20311:             $resulttext .= '</ul>';
20312:             $mailmsgtext .= "\n";
20313:             my $cachetime = 24*60*60;
20314:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20315:             if (ref($lastactref) eq 'HASH') {
20316:                 $lastactref->{'domdefaults'} = 1;
20317:             }
20318:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
20319:                 my $notify = 1;
20320:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
20321:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
20322:                         $notify = 0;
20323:                     }
20324:                 }
20325:                 if ($notify) {
20326:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
20327:                                                "LON-CAPA Domain Settings Change - $dom",
20328:                                                $mailmsgtext);
20329:                 }
20330:             }
20331:         } else {
20332:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
20333:         }
20334:     } else {
20335:         $resulttext = '<span class="LC_error">'.
20336:             &mt('An error occurred: [_1]',$putresult).'</span>';
20337:     }
20338:     if (@errors > 0) {
20339:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
20340:         foreach my $item (@errors) {
20341:             $resulttext .= ' "'.$title->{$item}.'",';
20342:         }
20343:         $resulttext =~ s/,$//;
20344:     }
20345:     return $resulttext;
20346: }
20347: 
20348: sub modify_scantron {
20349:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20350:     my ($resulttext,%confhash,%changes,$errors);
20351:     my $custom = 'custom.tab';
20352:     my $default = 'default.tab';
20353:     my $servadm = $r->dir_config('lonAdmEMail');
20354:     my ($configuserok,$author_ok,$switchserver) =
20355:         &config_check($dom,$confname,$servadm);
20356:     if ($env{'form.scantronformat.filename'} ne '') {
20357:         my $error;
20358:         if ($configuserok eq 'ok') {
20359:             if ($switchserver) {
20360:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
20361:             } else {
20362:                 if ($author_ok eq 'ok') {
20363:                     my $modified = [];
20364:                     my ($result,$scantronurl) =
20365:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
20366:                                                                 $confname,'scantron','','',$custom,
20367:                                                                 $modified);
20368:                     if ($result eq 'ok') {
20369:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
20370:                         $changes{'scantronformat'} = 1;
20371:                         &update_modify_urls($r,$modified);
20372:                     } else {
20373:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
20374:                     }
20375:                 } else {
20376:                     $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);
20377:                 }
20378:             }
20379:         } else {
20380:             $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);
20381:         }
20382:         if ($error) {
20383:             &Apache::lonnet::logthis($error);
20384:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
20385:         }
20386:     }
20387:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20388:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
20389:             if ($env{'form.scantronformat_del'}) {
20390:                 $confhash{'scantron'}{'scantronformat'} = '';
20391:                 $changes{'scantronformat'} = 1;
20392:             } else {
20393:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
20394:             }
20395:         }
20396:     }
20397:     my @options = ('hdr','pad','rem');
20398:     my @fields = &scantroncsv_fields();
20399:     my %titles = &scantronconfig_titles();
20400:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
20401:     my ($newdat,$currdat,%newcol,%currcol);
20402:     if (grep(/^dat$/,@formats)) {
20403:         $confhash{'scantron'}{config}{dat} = 1;
20404:         $newdat = 1;
20405:     } else {
20406:         $newdat = 0;
20407:     }
20408:     if (grep(/^csv$/,@formats)) {
20409:         my %bynum;
20410:         foreach my $field (@fields) {
20411:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
20412:                 my $posscol = $1;
20413:                 if (($posscol < 20) && (!$bynum{$posscol})) {
20414:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
20415:                     $bynum{$posscol} = $field;
20416:                     $newcol{$field} = $posscol;
20417:                 }
20418:             }
20419:         }
20420:         if (keys(%newcol)) {
20421:             foreach my $option (@options) {
20422:                 if ($env{'form.scantroncsv_'.$option}) {
20423:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
20424:                 }
20425:             }
20426:         }
20427:     }
20428:     $currdat = 1;
20429:     if (ref($domconfig{'scantron'}) eq 'HASH') {
20430:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
20431:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
20432:                 $currdat = 0;
20433:             }
20434:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20435:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20436:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
20437:                 }
20438:             }
20439:         }
20440:     }
20441:     if ($currdat != $newdat) {
20442:         $changes{'config'} = 1;
20443:     } else {
20444:         foreach my $field (@fields) {
20445:             if ($currcol{$field} ne '') {
20446:                 if ($currcol{$field} ne $newcol{$field}) {
20447:                     $changes{'config'} = 1;
20448:                     last;
20449:                 }
20450:             } elsif ($newcol{$field} ne '') {
20451:                 $changes{'config'} = 1;
20452:                 last;
20453:             }
20454:         }
20455:     }
20456:     if (keys(%confhash) > 0) {
20457:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
20458:                                                  $dom);
20459:         if ($putresult eq 'ok') {
20460:             if (keys(%changes) > 0) {
20461:                 if (ref($confhash{'scantron'}) eq 'HASH') {
20462:                     $resulttext = &mt('Changes made:').'<ul>';
20463:                     if ($changes{'scantronformat'}) {
20464:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
20465:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
20466:                         } else {
20467:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
20468:                         }
20469:                     }
20470:                     if ($changes{'config'}) {
20471:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
20472:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
20473:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
20474:                             }
20475:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
20476:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
20477:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
20478:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
20479:                                         foreach my $field (@fields) {
20480:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
20481:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
20482:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
20483:                                             }
20484:                                         }
20485:                                         $resulttext .= '</ul></li>';
20486:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
20487:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
20488:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
20489:                                                 foreach my $option (@options) {
20490:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
20491:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
20492:                                                     }
20493:                                                 }
20494:                                                 $resulttext .= '</ul></li>';
20495:                                             }
20496:                                         }
20497:                                     }
20498:                                 }
20499:                             }
20500:                         } else {
20501:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
20502:                         }
20503:                     }
20504:                     $resulttext .= '</ul>';
20505:                 } else {
20506:                     $resulttext = &mt('Changes made to bubblesheet format file.');
20507:                 }
20508:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
20509:                 if (ref($lastactref) eq 'HASH') {
20510:                     $lastactref->{'domainconfig'} = 1;
20511:                 }
20512:             } else {
20513:                 $resulttext = &mt('No changes made to bubblesheet format settings');
20514:             }
20515:         } else {
20516:             $resulttext = '<span class="LC_error">'.
20517:                 &mt('An error occurred: [_1]',$putresult).'</span>';
20518:         }
20519:     } else {
20520:         $resulttext = &mt('No changes made to bubblesheet format file'); 
20521:     }
20522:     if ($errors) {
20523:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
20524:                        $errors.'</ul></p>';
20525:     }
20526:     return $resulttext;
20527: }
20528: 
20529: sub modify_coursecategories {
20530:     my ($dom,$lastactref,%domconfig) = @_;
20531:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
20532:         $cathash);
20533:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
20534:     my @catitems = ('unauth','auth');
20535:     my @cattypes = ('std','domonly','codesrch','none');
20536:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20537:         $cathash = $domconfig{'coursecategories'}{'cats'};
20538:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
20539:             $changes{'togglecats'} = 1;
20540:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
20541:         }
20542:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
20543:             $changes{'categorize'} = 1;
20544:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
20545:         }
20546:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
20547:             $changes{'togglecatscomm'} = 1;
20548:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
20549:         }
20550:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
20551:             $changes{'categorizecomm'} = 1;
20552:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
20553: 
20554:         }
20555:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
20556:             $changes{'togglecatsplace'} = 1;
20557:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
20558:         }
20559:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
20560:             $changes{'categorizeplace'} = 1;
20561:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
20562:         }
20563:         foreach my $item (@catitems) {
20564:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20565:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
20566:                     $changes{$item} = 1;
20567:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20568:                 }
20569:             }
20570:         }
20571:     } else {
20572:         $changes{'togglecats'} = 1;
20573:         $changes{'categorize'} = 1;
20574:         $changes{'togglecatscomm'} = 1;
20575:         $changes{'categorizecomm'} = 1;
20576:         $changes{'togglecatsplace'} = 1;
20577:         $changes{'categorizeplace'} = 1;
20578:         $domconfig{'coursecategories'} = {
20579:                                              togglecats => $env{'form.togglecats'},
20580:                                              categorize => $env{'form.categorize'},
20581:                                              togglecatscomm => $env{'form.togglecatscomm'},
20582:                                              categorizecomm => $env{'form.categorizecomm'},
20583:                                              togglecatsplace => $env{'form.togglecatsplace'},
20584:                                              categorizeplace => $env{'form.categorizeplace'},
20585:                                          };
20586:         foreach my $item (@catitems) {
20587:             if ($env{'form.coursecat_'.$item} ne 'std') {
20588:                 $changes{$item} = 1;
20589:             }
20590:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
20591:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
20592:             }
20593:         }
20594:     }
20595:     if (ref($cathash) eq 'HASH') {
20596:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
20597:             push (@deletecategory,'instcode::0');
20598:         }
20599:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
20600:             push(@deletecategory,'communities::0');
20601:         }
20602:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
20603:             push(@deletecategory,'placement::0');
20604:         }
20605:     }
20606:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
20607:     if (ref($cathash) eq 'HASH') {
20608:         if (@deletecategory > 0) {
20609:             #FIXME Need to remove category from all courses using a deleted category 
20610:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
20611:             foreach my $item (@deletecategory) {
20612:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
20613:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
20614:                     $deletions{$item} = 1;
20615:                     &recurse_cat_deletes($item,$cathash,\%deletions);
20616:                 }
20617:             }
20618:         }
20619:         foreach my $item (keys(%{$cathash})) {
20620:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20621:             if ($cathash->{$item} ne $env{'form.'.$item}) {
20622:                 $reorderings{$item} = 1;
20623:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
20624:             }
20625:             if ($env{'form.addcategory_name_'.$item} ne '') {
20626:                 my $newcat = $env{'form.addcategory_name_'.$item};
20627:                 my $newdepth = $depth+1;
20628:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20629:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
20630:                 $adds{$newitem} = 1; 
20631:             }
20632:             if ($env{'form.subcat_'.$item} ne '') {
20633:                 my $newcat = $env{'form.subcat_'.$item};
20634:                 my $newdepth = $depth+1;
20635:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
20636:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
20637:                 $adds{$newitem} = 1;
20638:             }
20639:         }
20640:     }
20641:     if ($env{'form.instcode'} eq '1') {
20642:         if (ref($cathash) eq 'HASH') {
20643:             my $newitem = 'instcode::0';
20644:             if ($cathash->{$newitem} eq '') {  
20645:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20646:                 $adds{$newitem} = 1;
20647:             }
20648:         } else {
20649:             my $newitem = 'instcode::0';
20650:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
20651:             $adds{$newitem} = 1;
20652:         }
20653:     }
20654:     if ($env{'form.communities'} eq '1') {
20655:         if (ref($cathash) eq 'HASH') {
20656:             my $newitem = 'communities::0';
20657:             if ($cathash->{$newitem} eq '') {
20658:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20659:                 $adds{$newitem} = 1;
20660:             }
20661:         } else {
20662:             my $newitem = 'communities::0';
20663:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
20664:             $adds{$newitem} = 1;
20665:         }
20666:     }
20667:     if ($env{'form.placement'} eq '1') {
20668:         if (ref($cathash) eq 'HASH') {
20669:             my $newitem = 'placement::0';
20670:             if ($cathash->{$newitem} eq '') {
20671:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20672:                 $adds{$newitem} = 1;
20673:             }
20674:         } else {
20675:             my $newitem = 'placement::0';
20676:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
20677:             $adds{$newitem} = 1;
20678:         }
20679:     }
20680:     if ($env{'form.addcategory_name'} ne '') {
20681:         if (($env{'form.addcategory_name'} ne 'instcode') &&
20682:             ($env{'form.addcategory_name'} ne 'communities') &&
20683:             ($env{'form.addcategory_name'} ne 'placement')) {
20684:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
20685:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
20686:             $adds{$newitem} = 1;
20687:         }
20688:     }
20689:     my $putresult;
20690:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20691:         if (keys(%deletions) > 0) {
20692:             foreach my $key (keys(%deletions)) {
20693:                 if ($predelallitems{$key} ne '') {
20694:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
20695:                 }
20696:             }
20697:         }
20698:         my (@chkcats,@chktrails,%chkallitems);
20699:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
20700:         if (ref($chkcats[0]) eq 'ARRAY') {
20701:             my $depth = 0;
20702:             my $chg = 0;
20703:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
20704:                 my $name = $chkcats[0][$i];
20705:                 my $item;
20706:                 if ($name eq '') {
20707:                     $chg ++;
20708:                 } else {
20709:                     $item = &escape($name).'::0';
20710:                     if ($chg) {
20711:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
20712:                     }
20713:                     $depth ++; 
20714:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
20715:                     $depth --;
20716:                 }
20717:             }
20718:         }
20719:     }
20720:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20721:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
20722:         if ($putresult eq 'ok') {
20723:             my %title = (
20724:                          togglecats     => 'Show/Hide a course in catalog',
20725:                          categorize     => 'Assign a category to a course',
20726:                          togglecatscomm => 'Show/Hide a community in catalog',
20727:                          categorizecomm => 'Assign a category to a community',
20728:                         );
20729:             my %level = (
20730:                          dom  => 'set in Domain ("Modify Course/Community")',
20731:                          crs  => 'set in Course ("Course Configuration")',
20732:                          comm => 'set in Community ("Community Configuration")',
20733:                          none     => 'No catalog',
20734:                          std      => 'Standard catalog',
20735:                          domonly  => 'Domain-only catalog',
20736:                          codesrch => 'Code search form',
20737:                         );
20738:             $resulttext = &mt('Changes made:').'<ul>';
20739:             if ($changes{'togglecats'}) {
20740:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
20741:             }
20742:             if ($changes{'categorize'}) {
20743:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
20744:             }
20745:             if ($changes{'togglecatscomm'}) {
20746:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
20747:             }
20748:             if ($changes{'categorizecomm'}) {
20749:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
20750:             }
20751:             if ($changes{'unauth'}) {
20752:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
20753:             }
20754:             if ($changes{'auth'}) {
20755:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
20756:             }
20757:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
20758:                 my $cathash;
20759:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
20760:                     $cathash = $domconfig{'coursecategories'}{'cats'};
20761:                 } else {
20762:                     $cathash = {};
20763:                 } 
20764:                 my (@cats,@trails,%allitems);
20765:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
20766:                 if (keys(%deletions) > 0) {
20767:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
20768:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
20769:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
20770:                     }
20771:                     $resulttext .= '</ul></li>';
20772:                 }
20773:                 if (keys(%reorderings) > 0) {
20774:                     my %sort_by_trail;
20775:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
20776:                     foreach my $key (keys(%reorderings)) {
20777:                         if ($allitems{$key} ne '') {
20778:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20779:                         }
20780:                     }
20781:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20782:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20783:                     }
20784:                     $resulttext .= '</ul></li>';
20785:                 }
20786:                 if (keys(%adds) > 0) {
20787:                     my %sort_by_trail;
20788:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
20789:                     foreach my $key (keys(%adds)) {
20790:                         if ($allitems{$key} ne '') {
20791:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
20792:                         }
20793:                     }
20794:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
20795:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
20796:                     }
20797:                     $resulttext .= '</ul></li>';
20798:                 }
20799:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
20800:                 if (ref($lastactref) eq 'HASH') {
20801:                     $lastactref->{'cats'} = 1;
20802:                 }
20803:             }
20804:             $resulttext .= '</ul>';
20805:             if ($changes{'unauth'} || $changes{'auth'}) {
20806:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
20807:                 if ($changes{'auth'}) {
20808:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
20809:                 }
20810:                 if ($changes{'unauth'}) {
20811:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
20812:                 }
20813:                 my $cachetime = 24*60*60;
20814:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20815:                 if (ref($lastactref) eq 'HASH') {
20816:                     $lastactref->{'domdefaults'} = 1;
20817:                 }
20818:             }
20819:         } else {
20820:             $resulttext = '<span class="LC_error">'.
20821:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20822:         }
20823:     } else {
20824:         $resulttext = &mt('No changes made to course and community categories');
20825:     }
20826:     return $resulttext;
20827: }
20828: 
20829: sub modify_serverstatuses {
20830:     my ($dom,%domconfig) = @_;
20831:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
20832:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
20833:         %currserverstatus = %{$domconfig{'serverstatuses'}};
20834:     }
20835:     my @pages = &serverstatus_pages();
20836:     foreach my $type (@pages) {
20837:         $newserverstatus{$type}{'namedusers'} = '';
20838:         $newserverstatus{$type}{'machines'} = '';
20839:         if (defined($env{'form.'.$type.'_namedusers'})) {
20840:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
20841:             my @okusers;
20842:             foreach my $user (@users) {
20843:                 my ($uname,$udom) = split(/:/,$user);
20844:                 if (($udom =~ /^$match_domain$/) &&   
20845:                     (&Apache::lonnet::domain($udom)) &&
20846:                     ($uname =~ /^$match_username$/)) {
20847:                     if (!grep(/^\Q$user\E/,@okusers)) {
20848:                         push(@okusers,$user);
20849:                     }
20850:                 }
20851:             }
20852:             if (@okusers > 0) {
20853:                  @okusers = sort(@okusers);
20854:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
20855:             }
20856:         }
20857:         if (defined($env{'form.'.$type.'_machines'})) {
20858:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
20859:             my @okmachines;
20860:             foreach my $ip (@machines) {
20861:                 my @parts = split(/\./,$ip);
20862:                 next if (@parts < 4);
20863:                 my $badip = 0;
20864:                 for (my $i=0; $i<4; $i++) {
20865:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
20866:                         $badip = 1;
20867:                         last;
20868:                     }
20869:                 }
20870:                 if (!$badip) {
20871:                     push(@okmachines,$ip);     
20872:                 }
20873:             }
20874:             @okmachines = sort(@okmachines);
20875:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
20876:         }
20877:     }
20878:     my %serverstatushash =  (
20879:                                 serverstatuses => \%newserverstatus,
20880:                             );
20881:     foreach my $type (@pages) {
20882:         foreach my $setting ('namedusers','machines') {
20883:             my (@current,@new);
20884:             if (ref($currserverstatus{$type}) eq 'HASH') {
20885:                 if ($currserverstatus{$type}{$setting} ne '') { 
20886:                     @current = split(/,/,$currserverstatus{$type}{$setting});
20887:                 }
20888:             }
20889:             if ($newserverstatus{$type}{$setting} ne '') {
20890:                 @new = split(/,/,$newserverstatus{$type}{$setting});
20891:             }
20892:             if (@current > 0) {
20893:                 if (@new > 0) {
20894:                     foreach my $item (@current) {
20895:                         if (!grep(/^\Q$item\E$/,@new)) {
20896:                             $changes{$type}{$setting} = 1;
20897:                             last;
20898:                         }
20899:                     }
20900:                     foreach my $item (@new) {
20901:                         if (!grep(/^\Q$item\E$/,@current)) {
20902:                             $changes{$type}{$setting} = 1;
20903:                             last;
20904:                         }
20905:                     }
20906:                 } else {
20907:                     $changes{$type}{$setting} = 1;
20908:                 }
20909:             } elsif (@new > 0) {
20910:                 $changes{$type}{$setting} = 1;
20911:             }
20912:         }
20913:     }
20914:     if (keys(%changes) > 0) {
20915:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
20916:         my $putresult = &Apache::lonnet::put_dom('configuration',
20917:                                                  \%serverstatushash,$dom);
20918:         if ($putresult eq 'ok') {
20919:             $resulttext .= &mt('Changes made:').'<ul>';
20920:             foreach my $type (@pages) {
20921:                 if (ref($changes{$type}) eq 'HASH') {
20922:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
20923:                     if ($changes{$type}{'namedusers'}) {
20924:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
20925:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
20926:                         } else {
20927:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
20928:                         }
20929:                     }
20930:                     if ($changes{$type}{'machines'}) {
20931:                         if ($newserverstatus{$type}{'machines'} eq '') {
20932:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
20933:                         } else {
20934:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
20935:                         }
20936: 
20937:                     }
20938:                     $resulttext .= '</ul></li>';
20939:                 }
20940:             }
20941:             $resulttext .= '</ul>';
20942:         } else {
20943:             $resulttext = '<span class="LC_error">'.
20944:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
20945: 
20946:         }
20947:     } else {
20948:         $resulttext = &mt('No changes made to access to server status pages');
20949:     }
20950:     return $resulttext;
20951: }
20952: 
20953: sub modify_helpsettings {
20954:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
20955:     my ($resulttext,$errors,%changes,%helphash);
20956:     my %defaultchecked = ('submitbugs' => 'on');
20957:     my @offon = ('off','on');
20958:     my @toggles = ('submitbugs');
20959:     my %current = ('submitbugs' => '',
20960:                    'adhoc'      => {},
20961:                   );
20962:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
20963:         %current = %{$domconfig{'helpsettings'}};
20964:     }
20965:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20966:     foreach my $item (@toggles) {
20967:         if ($defaultchecked{$item} eq 'on') { 
20968:             if ($current{$item} eq '') {
20969:                 if ($env{'form.'.$item} eq '0') {
20970:                     $changes{$item} = 1;
20971:                 }
20972:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20973:                 $changes{$item} = 1;
20974:             }
20975:         } elsif ($defaultchecked{$item} eq 'off') {
20976:             if ($current{$item} eq '') {
20977:                 if ($env{'form.'.$item} eq '1') {
20978:                     $changes{$item} = 1;
20979:                 }
20980:             } elsif ($current{$item} ne $env{'form.'.$item}) {
20981:                 $changes{$item} = 1;
20982:             }
20983:         }
20984:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
20985:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
20986:         }
20987:     }
20988:     my $maxnum = $env{'form.helproles_maxnum'};
20989:     my $confname = $dom.'-domainconfig';
20990:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
20991:     my (@allpos,%newsettings,%changedprivs,$newrole);
20992:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
20993:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
20994:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
20995:     my %lt = &Apache::lonlocal::texthash(
20996:                     s      => 'system',
20997:                     d      => 'domain',
20998:                     order  => 'Display order',
20999:                     access => 'Role usage',
21000:                     all    => 'All with domain helpdesk or helpdesk assistant role',
21001:                     dh     => 'All with domain helpdesk role',
21002:                     da     => 'All with domain helpdesk assistant role',
21003:                     none   => 'None',
21004:                     status => 'Determined based on institutional status',
21005:                     inc    => 'Include all, but exclude specific personnel',
21006:                     exc    => 'Exclude all, but include specific personnel',
21007:     );
21008:     for (my $num=0; $num<=$maxnum; $num++) {
21009:         my ($prefix,$identifier,$rolename,%curr);
21010:         if ($num == $maxnum) {
21011:             next unless ($env{'form.newcusthelp'} == $maxnum);
21012:             $identifier = 'custhelp'.$num;
21013:             $prefix = 'helproles_'.$num;
21014:             $rolename = $env{'form.custhelpname'.$num};
21015:             $rolename=~s/[^A-Za-z0-9]//gs;
21016:             next if ($rolename eq '');
21017:             next if (exists($existing{'rolesdef_'.$rolename}));
21018:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
21019:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
21020:                                                      $newprivs{'c'},$confname,$dom);
21021:             if ($result ne 'ok') {
21022:                 $errors .= '<li><span class="LC_error">'.
21023:                            &mt('An error occurred storing the new custom role: [_1]',
21024:                            $result).'</span></li>';
21025:                 next;
21026:             } else {
21027:                 $changedprivs{$rolename} = \%newprivs;
21028:                 $newrole = $rolename;
21029:             }
21030:         } else {
21031:             $prefix = 'helproles_'.$num;
21032:             $rolename = $env{'form.'.$prefix};
21033:             next if ($rolename eq '');
21034:             next unless (exists($existing{'rolesdef_'.$rolename}));
21035:             $identifier = 'custhelp'.$num;
21036:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
21037:             my %currprivs;
21038:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
21039:                 split(/\_/,$existing{'rolesdef_'.$rolename});
21040:             foreach my $level ('c','d','s') {
21041:                 if ($newprivs{$level} ne $currprivs{$level}) {
21042:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
21043:                                                              $newprivs{'c'},$confname,$dom);
21044:                     if ($result ne 'ok') {
21045:                         $errors .= '<li><span class="LC_error">'.
21046:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
21047:                                        $rolename,$result).'</span></li>';
21048:                     } else {
21049:                         $changedprivs{$rolename} = \%newprivs;
21050:                     }
21051:                     last;
21052:                 }
21053:             }
21054:             if (ref($current{'adhoc'}) eq 'HASH') {
21055:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21056:                     %curr = %{$current{'adhoc'}{$rolename}};
21057:                 }
21058:             }
21059:         }
21060:         my $newpos = $env{'form.'.$prefix.'_pos'};
21061:         $newpos =~ s/\D+//g;
21062:         $allpos[$newpos] = $rolename;
21063:         my $newdesc = $env{'form.'.$prefix.'_desc'};
21064:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
21065:         if ($curr{'desc'}) {
21066:             if ($curr{'desc'} ne $newdesc) {
21067:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
21068:                 $newsettings{$rolename}{'desc'} = $newdesc;
21069:             }
21070:         } elsif ($newdesc ne '') {
21071:             $changes{'customrole'}{$rolename}{'desc'} = 1;
21072:             $newsettings{$rolename}{'desc'} = $newdesc;
21073:         }
21074:         my $access = $env{'form.'.$prefix.'_access'};
21075:         if (grep(/^\Q$access\E$/,@accesstypes)) {
21076:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
21077:             if ($access eq 'status') {
21078:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
21079:                 if (scalar(@statuses) == 0) {
21080:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
21081:                 } else {
21082:                     my (@shownstatus,$numtypes);
21083:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21084:                     if (ref($types) eq 'ARRAY') {
21085:                         $numtypes = scalar(@{$types});
21086:                         foreach my $type (sort(@statuses)) {
21087:                             if ($type eq 'default') {
21088:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21089:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
21090:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
21091:                                 push(@shownstatus,$usertypes->{$type});
21092:                             }
21093:                         }
21094:                     }
21095:                     if (grep(/^default$/,@statuses)) {
21096:                         push(@shownstatus,$othertitle);
21097:                     }
21098:                     if (scalar(@shownstatus) == 1+$numtypes) {
21099:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
21100:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
21101:                     } else {
21102:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
21103:                         if (ref($curr{'status'}) eq 'ARRAY') {
21104:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21105:                             if (@diffs) {
21106:                                 $changes{'customrole'}{$rolename}{$access} = 1;
21107:                             }
21108:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21109:                             $changes{'customrole'}{$rolename}{$access} = 1;
21110:                         }
21111:                     }
21112:                 }
21113:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
21114:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
21115:                 my @newspecstaff;
21116:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
21117:                 foreach my $person (sort(@personnel)) {
21118:                     if ($domhelpdesk{$person}) {
21119:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
21120:                     }
21121:                 }
21122:                 if (ref($curr{$access}) eq 'ARRAY') {
21123:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
21124:                     if (@diffs) {
21125:                         $changes{'customrole'}{$rolename}{$access} = 1;
21126:                     }
21127:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21128:                     $changes{'customrole'}{$rolename}{$access} = 1;
21129:                 }
21130:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
21131:                     my ($uname,$udom) = split(/:/,$person);
21132:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
21133:                 }
21134:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
21135:             }
21136:         } else {
21137:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
21138:         }
21139:         unless ($curr{'access'} eq $access) {
21140:             $changes{'customrole'}{$rolename}{'access'} = 1;
21141:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
21142:         }
21143:     }
21144:     if (@allpos > 0) {
21145:         my $idx = 0;
21146:         foreach my $rolename (@allpos) {
21147:             if ($rolename ne '') {
21148:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
21149:                 if (ref($current{'adhoc'}) eq 'HASH') {
21150:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
21151:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
21152:                             $changes{'customrole'}{$rolename}{'order'} = 1;
21153:                             $newsettings{$rolename}{'order'} = $idx+1;
21154:                         }
21155:                     }
21156:                 }
21157:                 $idx ++;
21158:             }
21159:         }
21160:     }
21161:     my $putresult;
21162:     if (keys(%changes) > 0) {
21163:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
21164:         if ($putresult eq 'ok') {
21165:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
21166:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
21167:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
21168:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
21169:                 }
21170:             }
21171:             my $cachetime = 24*60*60;
21172:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21173:             if (ref($lastactref) eq 'HASH') {
21174:                 $lastactref->{'domdefaults'} = 1;
21175:             }
21176:         } else {
21177:             $errors .= '<li><span class="LC_error">'.
21178:                        &mt('An error occurred storing the settings: [_1]',
21179:                            $putresult).'</span></li>';
21180:         }
21181:     }
21182:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
21183:         $resulttext = &mt('Changes made:').'<ul>';
21184:         my (%shownprivs,@levelorder);
21185:         @levelorder = ('c','d','s');
21186:         if ((keys(%changes)) && ($putresult eq 'ok')) {
21187:             foreach my $item (sort(keys(%changes))) {
21188:                 if ($item eq 'submitbugs') {
21189:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
21190:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
21191:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
21192:                 } elsif ($item eq 'customrole') {
21193:                     if (ref($changes{'customrole'}) eq 'HASH') {
21194:                         my @keyorder = ('order','desc','access','status','exc','inc');
21195:                         my %keytext = &Apache::lonlocal::texthash(
21196:                                                                    order  => 'Order',
21197:                                                                    desc   => 'Role description',
21198:                                                                    access => 'Role usage',
21199:                                                                    status => 'Allowed institutional types',
21200:                                                                    exc    => 'Allowed personnel',
21201:                                                                    inc    => 'Disallowed personnel',
21202:                         );
21203:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
21204:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
21205:                                 if ($role eq $newrole) {
21206:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
21207:                                                               $role).'<ul>';
21208:                                 } else {
21209:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21210:                                                               $role).'<ul>';
21211:                                 }
21212:                                 foreach my $key (@keyorder) {
21213:                                     if ($changes{'customrole'}{$role}{$key}) {
21214:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
21215:                                                                   $keytext{$key},$newsettings{$role}{$key}).
21216:                                                        '</li>';
21217:                                     }
21218:                                 }
21219:                                 if (ref($changedprivs{$role}) eq 'HASH') {
21220:                                     $shownprivs{$role} = 1;
21221:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
21222:                                     foreach my $level (@levelorder) {
21223:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21224:                                             next if ($item eq '');
21225:                                             my ($priv) = split(/\&/,$item,2);
21226:                                             if (&Apache::lonnet::plaintext($priv)) {
21227:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21228:                                                 unless ($level eq 'c') {
21229:                                                     $resulttext .= ' ('.$lt{$level}.')';
21230:                                                 }
21231:                                                 $resulttext .= '</li>';
21232:                                             }
21233:                                         }
21234:                                     }
21235:                                     $resulttext .= '</ul>';
21236:                                 }
21237:                                 $resulttext .= '</ul></li>';
21238:                             }
21239:                         }
21240:                     }
21241:                 }
21242:             }
21243:         }
21244:         if (keys(%changedprivs)) {
21245:             foreach my $role (sort(keys(%changedprivs))) {
21246:                 unless ($shownprivs{$role}) {
21247:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
21248:                                               $role).'<ul>'.
21249:                                    '<li>'.&mt('Privileges set to :').'<ul>';
21250:                     foreach my $level (@levelorder) {
21251:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
21252:                             next if ($item eq '');
21253:                             my ($priv) = split(/\&/,$item,2);
21254:                             if (&Apache::lonnet::plaintext($priv)) {
21255:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
21256:                                 unless ($level eq 'c') {
21257:                                     $resulttext .= ' ('.$lt{$level}.')';
21258:                                 }
21259:                                 $resulttext .= '</li>';
21260:                             }
21261:                         }
21262:                     }
21263:                     $resulttext .= '</ul></li></ul></li>';
21264:                 }
21265:             }
21266:         }
21267:         $resulttext .= '</ul>';
21268:     } else {
21269:         $resulttext = &mt('No changes made to help settings');
21270:     }
21271:     if ($errors) {
21272:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
21273:                                     $errors.'</ul>';
21274:     }
21275:     return $resulttext;
21276: }
21277: 
21278: sub modify_coursedefaults {
21279:     my ($dom,$lastactref,%domconfig) = @_;
21280:     my ($resulttext,$errors,%changes,%defaultshash);
21281:     my %defaultchecked = (
21282:                            'canuse_pdfforms' => 'off',
21283:                            'uselcmath'       => 'on',
21284:                            'usejsme'         => 'on',
21285:                            'inline_chem'     => 'on',
21286:                            'ltiauth'         => 'off',
21287:                          );
21288:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
21289:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
21290:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
21291:                    'coursequota_official','coursequota_unofficial','coursequota_community',
21292:                    'coursequota_textbook','coursequota_placement','mysqltables_official',
21293:                    'mysqltables_unofficial','mysqltables_community','mysqltables_textbook',
21294:                    'mysqltables_placement');
21295:     my @types = ('official','unofficial','community','textbook','placement');
21296:     my %staticdefaults = (
21297:                            anonsurvey_threshold => 10,
21298:                            uploadquota          => 500,
21299:                            coursequota          => 20,
21300:                            postsubmit           => 60,
21301:                            mysqltables          => 172800,
21302:                            domexttool           => 1,
21303:                            crsauthor            => 1,
21304:                            crseditors           => ['edit','xml'],
21305:                          );
21306:     my %texoptions = (
21307:                         MathJax  => 'MathJax',
21308:                         mimetex  => &mt('Convert to Images'),
21309:                         tth      => &mt('TeX to HTML'),
21310:                      );
21311: 
21312:     my @editors = ('edit','xml','daxe');
21313:     my %editornames = &crseditor_titles();
21314: 
21315:     $defaultshash{'coursedefaults'} = {};
21316: 
21317:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
21318:         if ($domconfig{'coursedefaults'} eq '') {
21319:             $domconfig{'coursedefaults'} = {};
21320:         }
21321:     }
21322: 
21323:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
21324:         foreach my $item (@toggles) {
21325:             if ($defaultchecked{$item} eq 'on') {
21326:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21327:                     ($env{'form.'.$item} eq '0')) {
21328:                     $changes{$item} = 1;
21329:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21330:                     $changes{$item} = 1;
21331:                 }
21332:             } elsif ($defaultchecked{$item} eq 'off') {
21333:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
21334:                     ($env{'form.'.$item} eq '1')) {
21335:                     $changes{$item} = 1;
21336:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
21337:                     $changes{$item} = 1;
21338:                 }
21339:             }
21340:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
21341:         }
21342:         foreach my $item (@numbers) {
21343:             my ($currdef,$newdef);
21344:             $newdef = $env{'form.'.$item};
21345:             if ($item eq 'anonsurvey_threshold') {
21346:                 $currdef = $domconfig{'coursedefaults'}{$item};
21347:                 $newdef =~ s/\D//g;
21348:                 if ($newdef eq '' || $newdef < 1) {
21349:                     $newdef = 1;
21350:                 }
21351:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
21352:             } else {
21353:                 my ($setting,$type) = ($item =~ /^(uploadquota|coursequota|mysqltables)_(\w+)$/);
21354:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
21355:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
21356:                 }
21357:                 $newdef =~ s/[^\w.\-]//g;
21358:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
21359:             }
21360:             if ($currdef ne $newdef) {
21361:                 if ($item eq 'anonsurvey_threshold') {
21362:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
21363:                         $changes{$item} = 1;
21364:                     }
21365:                 } elsif ($item =~ /^(uploadquota|coursequota|mysqltables)_/) {
21366:                     my $setting = $1;
21367:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
21368:                         $changes{$setting} = 1;
21369:                     }
21370:                 }
21371:             }
21372:         }
21373:         my $texengine;
21374:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
21375:             $texengine = $env{'form.texengine'};
21376:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
21377:             if ($currdef eq '') {
21378:                 unless ($texengine eq $Apache::lonnet::deftex) {
21379:                     $changes{'texengine'} = 1;
21380:                 }
21381:             } elsif ($currdef ne $texengine) {
21382:                 $changes{'texengine'} = 1;
21383:             }
21384:         }
21385:         if ($texengine ne '') {
21386:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
21387:         }
21388:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
21389:         my @currclonecode;
21390:         if (ref($currclone) eq 'HASH') {
21391:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
21392:                 @currclonecode = @{$currclone->{'instcode'}};
21393:             }
21394:         }
21395:         my $newclone;
21396:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
21397:             $newclone = $env{'form.canclone'};
21398:         }
21399:         if ($newclone eq 'instcode') {
21400:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
21401:             my (%codedefaults,@code_order,@clonecode);
21402:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
21403:                                                     \@code_order);
21404:             foreach my $item (@code_order) {
21405:                 if (grep(/^\Q$item\E$/,@newcodes)) {
21406:                     push(@clonecode,$item);
21407:                 }
21408:             }
21409:             if (@clonecode) {
21410:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
21411:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
21412:                 if (@diffs) {
21413:                     $changes{'canclone'} = 1;
21414:                 }
21415:             } else {
21416:                 $newclone eq '';
21417:             }
21418:         } elsif ($newclone ne '') {
21419:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
21420:         }
21421:         if ($newclone ne $currclone) {
21422:             $changes{'canclone'} = 1;
21423:         }
21424:         my %credits;
21425:         foreach my $type (@types) {
21426:             unless ($type eq 'community') {
21427:                 $credits{$type} = $env{'form.'.$type.'_credits'};
21428:                 $credits{$type} =~ s/[^\d.]+//g;
21429:             }
21430:         }
21431:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
21432:             ($env{'form.coursecredits'} eq '1')) {
21433:             $changes{'coursecredits'} = 1;
21434:             foreach my $type (keys(%credits)) {
21435:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21436:             }
21437:         } else {
21438:             if ($env{'form.coursecredits'} eq '1') {
21439:                 foreach my $type (@types) {
21440:                     unless ($type eq 'community') {
21441:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
21442:                             $changes{'coursecredits'} = 1;
21443:                         }
21444:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
21445:                     }
21446:                 }
21447:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21448:                 foreach my $type (@types) {
21449:                     unless ($type eq 'community') {
21450:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
21451:                             $changes{'coursecredits'} = 1;
21452:                             last;
21453:                         }
21454:                     }
21455:                 }
21456:             }
21457:         }
21458:         if ($env{'form.postsubmit'} eq '1') {
21459:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
21460:             my %currtimeout;
21461:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21462:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
21463:                     $changes{'postsubmit'} = 1;
21464:                 }
21465:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21466:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
21467:                 }
21468:             } else {
21469:                 $changes{'postsubmit'} = 1;
21470:             }
21471:             foreach my $type (@types) {
21472:                 my $timeout = $env{'form.'.$type.'_timeout'};
21473:                 $timeout =~ s/\D//g;
21474:                 if ($timeout == $staticdefaults{'postsubmit'}) {
21475:                     $timeout = '';
21476:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
21477:                     $timeout = '0';
21478:                 }
21479:                 unless ($timeout eq '') {
21480:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
21481:                 }
21482:                 if (exists($currtimeout{$type})) {
21483:                     if ($timeout ne $currtimeout{$type}) {
21484:                         $changes{'postsubmit'} = 1;
21485:                     }
21486:                 } elsif ($timeout ne '') {
21487:                     $changes{'postsubmit'} = 1;
21488:                 }
21489:             }
21490:         } else {
21491:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
21492:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21493:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
21494:                     $changes{'postsubmit'} = 1;
21495:                 }
21496:             } else {
21497:                 $changes{'postsubmit'} = 1;
21498:             }
21499:         }
21500:         my (%newdomexttool,%newexttool,%newcrsauthor,%olddomexttool,%oldexttool,%oldcrsauthor,
21501:             %posscrseditors);
21502:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
21503:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool');
21504:         map { $newcrsauthor{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crsauthor');
21505:         map { $posscrseditors{$_} = 1; } &Apache::loncommon::get_env_multiple('form.crseditors');
21506:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21507:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
21508:         } else {
21509:            foreach my $type (@types) {
21510:                if ($staticdefaults{'domexttool'}) {
21511:                    $olddomexttool{$type} = 1;
21512:                } else {
21513:                    $olddomexttool{$type} = 0;
21514:                }
21515:             }
21516:         }
21517:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
21518:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};
21519:         } else {
21520:             foreach my $type (@types) {
21521:                if ($staticdefaults{'exttool'}) {
21522:                    $oldexttool{$type} = 1;
21523:                } else {
21524:                    $oldexttool{$type} = 0;
21525:                }
21526:             }
21527:         }
21528:         if (ref($domconfig{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21529:             %oldcrsauthor = %{$domconfig{'coursedefaults'}{'crsauthor'}};
21530:         } else {
21531:             foreach my $type (@types) {
21532:                if ($staticdefaults{'crsauthor'}) {
21533:                    $oldcrsauthor{$type} = 1;
21534:                } else {
21535:                    $oldcrsauthor{$type} = 0;
21536:                }
21537:             }
21538:         }
21539:         my @newcrseditors = ();
21540:         foreach my $editor (@editors) {
21541:             if ($posscrseditors{$editor}) {
21542:                 push(@newcrseditors,$editor);
21543:             }
21544:         }
21545:         if (ref($domconfig{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21546:             my @diffs =
21547:                 &Apache::loncommon::compare_arrays($domconfig{'coursedefaults'}{'crseditors'},
21548:                                                    \@newcrseditors);
21549:             if (@diffs) {
21550:                 $changes{'crseditors'} = 1; 
21551:             }
21552:         } else {
21553:             my @diffs =
21554:                 &Apache::loncommon::compare_arrays($staticdefaults{'crseditors'},
21555:                                                    \@newcrseditors);
21556:             unless (@diffs == 0) {
21557:                 $changes{'crseditors'} = 1;
21558:             }       
21559:         }
21560:         foreach my $type (@types) {
21561:             unless ($newdomexttool{$type}) {
21562:                 $newdomexttool{$type} = 0;
21563:             }
21564:             unless ($newexttool{$type}) {
21565:                 $newexttool{$type} = 0;
21566:             }
21567:             unless ($newcrsauthor{$type}) {
21568:                 $newcrsauthor{$type} = 0;
21569:             }
21570:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
21571:                 $changes{'domexttool'} = 1;
21572:             }
21573:             if ($newexttool{$type} != $oldexttool{$type}) {
21574:                 $changes{'exttool'} = 1;
21575:             }
21576:             if ($newcrsauthor{$type} != $oldcrsauthor{$type}) {
21577:                 $changes{'crsauthor'} = 1;
21578:             }
21579:         }
21580:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
21581:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
21582:         $defaultshash{'coursedefaults'}{'crsauthor'} = \%newcrsauthor;
21583:         $defaultshash{'coursedefaults'}{'crseditors'} = \@newcrseditors;
21584:     }
21585:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21586:                                              $dom);
21587:     if ($putresult eq 'ok') {
21588:         if (keys(%changes) > 0) {
21589:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21590:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
21591:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
21592:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
21593:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
21594:                 ($changes{'exttool'}) || ($changes{'coursequota'}) || ($changes{'crsauthor'})) {
21595:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
21596:                                   'ltiauth') {
21597:                     if ($changes{$item}) {
21598:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
21599:                     }
21600:                 }
21601:                 if ($changes{'coursecredits'}) {
21602:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21603:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
21604:                             $domdefaults{$type.'credits'} =
21605:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
21606:                         }
21607:                     }
21608:                 }
21609:                 if ($changes{'postsubmit'}) {
21610:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21611:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
21612:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21613:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
21614:                                 $domdefaults{$type.'postsubtimeout'} =
21615:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21616:                             }
21617:                         }
21618:                     }
21619:                 }
21620:                 if ($changes{'uploadquota'}) {
21621:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21622:                         foreach my $type (@types) {
21623:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
21624:                         }
21625:                     }
21626:                 }
21627:                 if ($changes{'coursequota'}) {
21628:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21629:                         foreach my $type (@types) {
21630:                             $domdefaults{$type.'coursequota'}=$defaultshash{'coursedefaults'}{'coursequota'}{$type};
21631:                         }
21632:                     }
21633:                 }
21634:                 if ($changes{'canclone'}) {
21635:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21636:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21637:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
21638:                             if (@clonecodes) {
21639:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
21640:                             }
21641:                         }
21642:                     } else {
21643:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
21644:                     }
21645:                 }
21646:                 if ($changes{'domexttool'}) {
21647:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
21648:                         foreach my $type (@types) {
21649:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
21650:                         }
21651:                     }
21652:                 }
21653:                 if ($changes{'exttool'}) {
21654:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
21655:                         foreach my $type (@types) {
21656:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
21657:                         }
21658:                     }
21659:                 }
21660:                 if ($changes{'crsauthor'}) {
21661:                     if (ref($defaultshash{'coursedefaults'}{'crsauthor'}) eq 'HASH') {
21662:                         foreach my $type (@types) {
21663:                             $domdefaults{$type.'crsauthor'}=$defaultshash{'coursedefaults'}{'crsauthor'}{$type};
21664:                         }
21665:                     }
21666:                 }
21667:                 if ($changes{'crseditors'}) {
21668:                     if (ref($defaultshash{'coursedefaults'}{'crseditors'}) eq 'ARRAY') {
21669:                         $domdefaults{'crseditors'}=join(',',@{$defaultshash{'coursedefaults'}{'crseditors'}});
21670:                     }
21671:                 }
21672:                 my $cachetime = 24*60*60;
21673:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21674:                 if (ref($lastactref) eq 'HASH') {
21675:                     $lastactref->{'domdefaults'} = 1;
21676:                 }
21677:             }
21678:             $resulttext = &mt('Changes made:').'<ul>';
21679:             foreach my $item (sort(keys(%changes))) {
21680:                 if ($item eq 'canuse_pdfforms') {
21681:                     if ($env{'form.'.$item} eq '1') {
21682:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
21683:                     } else {
21684:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
21685:                     }
21686:                 } elsif ($item eq 'uselcmath') {
21687:                     if ($env{'form.'.$item} eq '1') {
21688:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
21689:                     } else {
21690:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
21691:                     }
21692:                 } elsif ($item eq 'usejsme') {
21693:                     if ($env{'form.'.$item} eq '1') {
21694:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
21695:                     } else {
21696:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
21697:                     }
21698:                 } elsif ($item eq 'inline_chem') {
21699:                     if ($env{'form.'.$item} eq '1') {
21700:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
21701:                     } else {
21702:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
21703:                     }
21704:                 } elsif ($item eq 'texengine') {
21705:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
21706:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
21707:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
21708:                     }
21709:                 } elsif ($item eq 'anonsurvey_threshold') {
21710:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
21711:                 } elsif ($item eq 'uploadquota') {
21712:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
21713:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
21714:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
21715:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
21716:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
21717:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
21718:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
21719:                                        '</ul>'.
21720:                                        '</li>';
21721:                     } else {
21722:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
21723:                     }
21724:                 } elsif ($item eq 'coursequota') {
21725:                     if (ref($defaultshash{'coursedefaults'}{'coursequota'}) eq 'HASH') {
21726:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course set as follows:').'<ul>'.
21727:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'official'}.'</b>').'</li>'.
21728:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'unofficial'}.'</b>').'</li>'.
21729:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'textbook'}.'</b>').'</li>'.
21730:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'placement'}.'</b>').'</li>'.
21731:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'coursequota'}{'community'}.'</b>').'</li>'.
21732:                                        '</ul>'.
21733:                                        '</li>';
21734:                     } else {
21735:                         $resulttext .= '<li>'.&mt('Default cumulative quota for all group portfolio spaces in course remains default: [_1] MB',$staticdefaults{'coursequota'}).'</li>';
21736:                     }
21737:                 } elsif ($item eq 'mysqltables') {
21738:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
21739:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
21740:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
21741:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
21742:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
21743:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
21744:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
21745:                                        '</ul>'.
21746:                                        '</li>';
21747:                     } else {
21748:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
21749:                     }
21750:                 } elsif ($item eq 'postsubmit') {
21751:                     if ($domdefaults{'postsubmit'} eq 'off') {
21752:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
21753:                     } else {
21754:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
21755:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
21756:                             $resulttext .= &mt('durations:').'<ul>';
21757:                             foreach my $type (@types) {
21758:                                 $resulttext .= '<li>';
21759:                                 my $timeout;
21760:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
21761:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
21762:                                 }
21763:                                 my $display;
21764:                                 if ($timeout eq '0') {
21765:                                     $display = &mt('unlimited');
21766:                                 } elsif ($timeout eq '') {
21767:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
21768:                                 } else {
21769:                                     $display = &mt('[quant,_1,second]',$timeout);
21770:                                 }
21771:                                 if ($type eq 'community') {
21772:                                     $resulttext .= &mt('Communities');
21773:                                 } elsif ($type eq 'official') {
21774:                                     $resulttext .= &mt('Official courses');
21775:                                 } elsif ($type eq 'unofficial') {
21776:                                     $resulttext .= &mt('Unofficial courses');
21777:                                 } elsif ($type eq 'textbook') {
21778:                                     $resulttext .= &mt('Textbook courses');
21779:                                 } elsif ($type eq 'placement') {
21780:                                     $resulttext .= &mt('Placement tests');
21781:                                 }
21782:                                 $resulttext .= ' -- '.$display.'</li>';
21783:                             }
21784:                             $resulttext .= '</ul>';
21785:                         }
21786:                         $resulttext .= '</li>';
21787:                     }
21788:                 } elsif ($item eq 'coursecredits') {
21789:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
21790:                         if (($domdefaults{'officialcredits'} eq '') &&
21791:                             ($domdefaults{'unofficialcredits'} eq '') &&
21792:                             ($domdefaults{'textbookcredits'} eq '')) {
21793:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21794:                         } else {
21795:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
21796:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
21797:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
21798:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
21799:                                            '</ul>'.
21800:                                            '</li>';
21801:                         }
21802:                     } else {
21803:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
21804:                     }
21805:                 } elsif ($item eq 'canclone') {
21806:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
21807:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
21808:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
21809:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
21810:                         }
21811:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
21812:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
21813:                     } else {
21814:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
21815:                     }
21816:                 } elsif ($item eq 'ltiauth') {
21817:                     if ($env{'form.'.$item} eq '1') {
21818:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
21819:                     } else {
21820:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
21821:                     }
21822:                 } elsif (($item eq 'domexttool') || ($item eq 'exttool') || ($item eq 'crsauthor')) {
21823:                     my @noyes = (&mt('no'),&mt('yes'));
21824:                     my %status = (
21825:                                    domexttool => {
21826:                                                   ishash => &mt('External Tools defined in the domain may be used as follows:'), 
21827:                                                   default => &mt('External Tools defined in the domain may be used in all course types, by default'),
21828:                                                  },
21829:                                    exttool => {
21830:                                                   ishash => &mt('External Tools can be defined and configured in course containers as follows:'),
21831:                                                   default => &mt('External Tools can not be defined in any course types, by default'),
21832:                                               },
21833:                                    crsauthor => {
21834:                                                   ishash => &mt('Standard Problems can be created within course containers as follows:'),
21835:                                                   default => &mt('Standard Problems can be created within any course type, by default'),
21836:                                                 },
21837:                                  );
21838:                                    
21839:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'HASH') {
21840:                         $resulttext .= '<li>'.$status{$item}{'ishash'}.'<ul>'.
21841:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'official'}].'</b>').'</li>'.
21842:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'unofficial'}].'</b>').'</li>'.
21843:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'textbook'}].'</b>').'</li>'.
21844:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'placement'}].'</b>').'</li>'.
21845:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{$item}{'community'}].'</b>').'</li>'.
21846:                                        '</ul>'.
21847:                                        '</li>';
21848:                     } else {
21849:                         $resulttext .= '<li>'.$status{$item}{'default'}.'</li>';
21850:                     }
21851:                 } elsif ($item eq 'crseditors') {
21852:                     if (ref($defaultshash{'coursedefaults'}{$item}) eq 'ARRAY') {
21853:                         my $shown;
21854:                         if (@{$defaultshash{'coursedefaults'}{$item}}) {
21855:                             $shown = join(', ', map { $editornames{$_} } @{$defaultshash{'coursedefaults'}{$item}});
21856:                         } else {
21857:                             $shown = &mt('None');
21858:                         }
21859:                         $resulttext .= '<li>'.&mt('Available editors for course/community resources: [_1]',$shown).'</li>';
21860:                     }
21861:                 }
21862:             }
21863:             $resulttext .= '</ul>';
21864:         } else {
21865:             $resulttext = &mt('No changes made to course defaults');
21866:         }
21867:     } else {
21868:         $resulttext = '<span class="LC_error">'.
21869:             &mt('An error occurred: [_1]',$putresult).'</span>';
21870:     }
21871:     return $resulttext;
21872: }
21873: 
21874: sub modify_selfenrollment {
21875:     my ($dom,$lastactref,%domconfig) = @_;
21876:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
21877:     my @types = ('official','unofficial','community','textbook','placement');
21878:     my %titles = &tool_titles();
21879:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
21880:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
21881:     $ordered{'default'} = ['types','registered','approval','limit'];
21882: 
21883:     my (%roles,%shown,%toplevel);
21884:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
21885: 
21886:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
21887:         if ($domconfig{'selfenrollment'} eq '') {
21888:             $domconfig{'selfenrollment'} = {};
21889:         }
21890:     }
21891:     %toplevel = (
21892:                   admin      => 'Configuration Rights',
21893:                   default    => 'Default settings',
21894:                   validation => 'Validation of self-enrollment requests',
21895:                 );
21896:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
21897: 
21898:     if (ref($ordered{'admin'}) eq 'ARRAY') {
21899:         foreach my $item (@{$ordered{'admin'}}) {
21900:             foreach my $type (@types) {
21901:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
21902:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
21903:                 } else {
21904:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
21905:                 }
21906:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
21907:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
21908:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
21909:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
21910:                             push(@{$changes{'admin'}{$type}},$item);
21911:                         }
21912:                     } else {
21913:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
21914:                             push(@{$changes{'admin'}{$type}},$item);
21915:                         }
21916:                     }
21917:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
21918:                     push(@{$changes{'admin'}{$type}},$item);
21919:                 }
21920:             }
21921:         }
21922:     }
21923: 
21924:     foreach my $item (@{$ordered{'default'}}) {
21925:         foreach my $type (@types) {
21926:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
21927:             if ($item eq 'types') {
21928:                 unless (($value eq 'all') || ($value eq 'dom')) {
21929:                     $value = '';
21930:                 }
21931:             } elsif ($item eq 'registered') {
21932:                 unless ($value eq '1') {
21933:                     $value = 0;
21934:                 }
21935:             } elsif ($item eq 'approval') {
21936:                 unless ($value =~ /^[012]$/) {
21937:                     $value = 0;
21938:                 }
21939:             } else {
21940:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21941:                     $value = 'none';
21942:                 }
21943:             }
21944:             $selfenrollhash{'default'}{$type}{$item} = $value;
21945:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
21946:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21947:                     if ($selfenrollhash{'default'}{$type}{$item} ne
21948:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
21949:                          push(@{$changes{'default'}{$type}},$item);
21950:                     }
21951:                 } else {
21952:                     push(@{$changes{'default'}{$type}},$item);
21953:                 }
21954:             } else {
21955:                 push(@{$changes{'default'}{$type}},$item);
21956:             }
21957:             if ($item eq 'limit') {
21958:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
21959:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
21960:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
21961:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
21962:                     }
21963:                 } else {
21964:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
21965:                 }
21966:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
21967:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
21968:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
21969:                          push(@{$changes{'default'}{$type}},'cap');
21970:                     }
21971:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
21972:                     push(@{$changes{'default'}{$type}},'cap');
21973:                 }
21974:             }
21975:         }
21976:     }
21977: 
21978:     foreach my $item (@{$itemsref}) {
21979:         if ($item eq 'fields') {
21980:             my @changed;
21981:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
21982:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
21983:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
21984:             }
21985:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
21986:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
21987:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
21988:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
21989:                 } else {
21990:                     @changed = @{$selfenrollhash{'validation'}{$item}};
21991:                 }
21992:             } else {
21993:                 @changed = @{$selfenrollhash{'validation'}{$item}};
21994:             }
21995:             if (@changed) {
21996:                 if ($selfenrollhash{'validation'}{$item}) { 
21997:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
21998:                 } else {
21999:                     $changes{'validation'}{$item} = &mt('None');
22000:                 }
22001:             }
22002:         } else {
22003:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
22004:             if ($item eq 'markup') {
22005:                if ($env{'form.selfenroll_validation_'.$item}) {
22006:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
22007:                }
22008:             }
22009:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
22010:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
22011:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
22012:                 }
22013:             }
22014:         }
22015:     }
22016: 
22017:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
22018:                                              $dom);
22019:     if ($putresult eq 'ok') {
22020:         if (keys(%changes) > 0) {
22021:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22022:             $resulttext = &mt('Changes made:').'<ul>';
22023:             foreach my $key ('admin','default','validation') {
22024:                 if (ref($changes{$key}) eq 'HASH') {
22025:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
22026:                     if ($key eq 'validation') {
22027:                         foreach my $item (@{$itemsref}) {
22028:                             if (exists($changes{$key}{$item})) {
22029:                                 if ($item eq 'markup') {
22030:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
22031:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
22032:                                 } else {  
22033:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
22034:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
22035:                                 }
22036:                             }
22037:                         }
22038:                     } else {
22039:                         foreach my $type (@types) {
22040:                             if ($type eq 'community') {
22041:                                 $roles{'1'} = &mt('Community personnel');
22042:                             } else {
22043:                                 $roles{'1'} = &mt('Course personnel');
22044:                             }
22045:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
22046:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
22047:                                     if ($key eq 'admin') {
22048:                                         my @mgrdc = ();
22049:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22050:                                             foreach my $item (@{$ordered{'admin'}}) {
22051:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
22052:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
22053:                                                         push(@mgrdc,$item);
22054:                                                     }
22055:                                                 }
22056:                                             }
22057:                                             if (@mgrdc) {
22058:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
22059:                                             } else {
22060:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
22061:                                             }
22062:                                         }
22063:                                     } else {
22064:                                         if (ref($ordered{$key}) eq 'ARRAY') {
22065:                                             foreach my $item (@{$ordered{$key}}) {
22066:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22067:                                                     $domdefaults{$type.'selfenroll'.$item} =
22068:                                                         $selfenrollhash{$key}{$type}{$item};
22069:                                                 }
22070:                                             }
22071:                                         }
22072:                                     }
22073:                                 }
22074:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
22075:                                 foreach my $item (@{$ordered{$key}}) {
22076:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
22077:                                         $resulttext .= '<li>';
22078:                                         if ($key eq 'admin') {
22079:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
22080:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
22081:                                         } else {
22082:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
22083:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
22084:                                         }
22085:                                         $resulttext .= '</li>';
22086:                                     }
22087:                                 }
22088:                                 $resulttext .= '</ul></li>';
22089:                             }
22090:                         }
22091:                         $resulttext .= '</ul></li>'; 
22092:                     }
22093:                 }
22094:             }
22095:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
22096:                 my $cachetime = 24*60*60;
22097:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22098:                 if (ref($lastactref) eq 'HASH') {
22099:                     $lastactref->{'domdefaults'} = 1;
22100:                 }
22101:             }
22102:             $resulttext .= '</ul>';
22103:         } else {
22104:             $resulttext = &mt('No changes made to self-enrollment settings');
22105:         }
22106:     } else {
22107:         $resulttext = '<span class="LC_error">'.
22108:             &mt('An error occurred: [_1]',$putresult).'</span>';
22109:     }
22110:     return $resulttext;
22111: }
22112: 
22113: sub modify_wafproxy {
22114:     my ($dom,$action,$lastactref,%domconfig) = @_;
22115:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22116:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
22117:         %wafproxy,%changes,%expirecache,%expiresaml);
22118:     foreach my $server (sort(keys(%servers))) {
22119:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22120:         if ($serverhome eq $server) {
22121:             my $serverdom = &Apache::lonnet::host_domain($server);
22122:             if ($serverdom eq $dom) {
22123:                 $canset{$server} = 1;
22124:             }
22125:         }
22126:     }
22127:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
22128:         %{$values{$dom}} = ();
22129:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
22130:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
22131:         }
22132:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
22133:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
22134:         }
22135:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22136:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
22137:         }
22138:     }
22139:     my $output;
22140:     if (keys(%canset)) {
22141:         %{$wafproxy{'alias'}} = ();
22142:         %{$wafproxy{'saml'}} = ();
22143:         foreach my $key (sort(keys(%canset))) {
22144:             if ($env{'form.wafproxy_'.$dom}) {
22145:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
22146:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
22147:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
22148:                     $changes{'alias'} = 1;
22149:                 }
22150:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
22151:                     $wafproxy{'saml'}{$key} = 1;
22152:                 }
22153:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
22154:                     $changes{'saml'} = 1;
22155:                 }
22156:             } else {
22157:                 $wafproxy{'alias'}{$key} = '';
22158:                 $wafproxy{'saml'}{$key} = '';
22159:                 if ($curralias{$key}) {
22160:                     $changes{'alias'} = 1;
22161:                 }
22162:                 if ($currsaml{$key}) {
22163:                     $changes{'saml'} = 1;
22164:                 }
22165:             }
22166:             if ($wafproxy{'alias'}{$key} eq '') {
22167:                 if ($curralias{$key}) {
22168:                     $expirecache{$key} = 1;
22169:                 }
22170:                 delete($wafproxy{'alias'}{$key});
22171:             }
22172:             if ($wafproxy{'saml'}{$key} eq '') {
22173:                 if ($currsaml{$key}) {
22174:                     $expiresaml{$key} = 1;
22175:                 }
22176:                 delete($wafproxy{'saml'}{$key});
22177:             }
22178:         }
22179:         unless (keys(%{$wafproxy{'alias'}})) {
22180:             delete($wafproxy{'alias'});
22181:         }
22182:         unless (keys(%{$wafproxy{'saml'}})) {
22183:             delete($wafproxy{'saml'});
22184:         }
22185:         # Localization for values in %warn occurs in &mt() calls separately.
22186:         my %warn = (
22187:                      trusted => 'trusted IP range(s)',
22188:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
22189:                      vpnext => 'IP range(s) for backend WAF connections',
22190:                    );
22191:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22192:             my $possible = $env{'form.wafproxy_'.$item};
22193:             $possible =~ s/^\s+|\s+$//g;
22194:             if ($possible ne '') {
22195:                 if ($item eq 'remoteip') {
22196:                     if ($possible =~ /^[mhn]$/) {
22197:                         $wafproxy{$item} = $possible;
22198:                     }
22199:                 } elsif ($item eq 'ipheader') {
22200:                     if ($wafproxy{'remoteip'} eq 'h') {
22201:                         $wafproxy{$item} = $possible;
22202:                     }
22203:                 } elsif ($item eq 'sslopt') {
22204:                     if ($possible =~ /^0|1$/) {
22205:                         $wafproxy{$item} = $possible;
22206:                     }
22207:                 } else {
22208:                     my (@ok,$count);
22209:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
22210:                         unless ($env{'form.wafproxy_vpnaccess'}) {
22211:                             $possible = '';
22212:                         }
22213:                     } elsif ($item eq 'trusted') {
22214:                         unless ($wafproxy{'remoteip'} eq 'h') {
22215:                             $possible = '';
22216:                         }
22217:                     }
22218:                     unless ($possible eq '') {
22219:                         $possible =~ s/[\r\n]+/\s/g;
22220:                         $possible =~ s/\s*-\s*/-/g;
22221:                         $possible =~ s/\s+/,/g;
22222:                         $possible =~ s/,+/,/g;
22223:                     }
22224:                     $count = 0;
22225:                     if ($possible ne '') {
22226:                         foreach my $poss (split(/\,/,$possible)) {
22227:                             $count ++;
22228:                             $poss = &validate_ip_pattern($poss);
22229:                             if ($poss ne '') {
22230:                                 push(@ok,$poss);
22231:                             }
22232:                         }
22233:                         my $diff = $count - scalar(@ok);
22234:                         if ($diff) {
22235:                             push(@warnings,'<li>'.
22236:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
22237:                                      $diff,$warn{$item}).
22238:                                  '</li>');
22239:                         }
22240:                         if (@ok) {
22241:                             my @cidr_list;
22242:                             foreach my $item (@ok) {
22243:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
22244:                             }
22245:                             $wafproxy{$item} = join(',',@cidr_list);
22246:                         }
22247:                     }
22248:                 }
22249:                 if ($wafproxy{$item} ne $currvalue{$item}) {
22250:                     $changes{$item} = 1;
22251:                 }
22252:             } elsif ($currvalue{$item}) {
22253:                 $changes{$item} = 1;
22254:             }
22255:         }
22256:     } else {
22257:         if (keys(%curralias)) {
22258:             $changes{'alias'} = 1;
22259:         }
22260:         if (keys(%currsaml)) {
22261:             $changes{'saml'} = 1;
22262:         }
22263:         if (keys(%currvalue)) {
22264:             foreach my $key (keys(%currvalue)) {
22265:                 $changes{$key} = 1;
22266:             }
22267:         }
22268:     }
22269:     if (keys(%changes)) {
22270:         my %defaultshash = (
22271:                               wafproxy => \%wafproxy,
22272:                            );
22273:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22274:                                                  $dom);
22275:         if ($putresult eq 'ok') {
22276:             my $cachetime = 24*60*60;
22277:             my (%domdefaults,$updatedomdefs);
22278:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
22279:                 if ($changes{$item}) {
22280:                     unless ($updatedomdefs) {
22281:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
22282:                         $updatedomdefs = 1;
22283:                     }
22284:                     if ($wafproxy{$item}) {
22285:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
22286:                     } elsif (exists($domdefaults{'waf_'.$item})) {
22287:                         delete($domdefaults{'waf_'.$item});
22288:                     }
22289:                 }
22290:             }
22291:             if ($updatedomdefs) {
22292:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22293:                 if (ref($lastactref) eq 'HASH') {
22294:                     $lastactref->{'domdefaults'} = 1;
22295:                 }
22296:             }
22297:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
22298:                 my %updates = %expirecache;
22299:                 foreach my $key (keys(%expirecache)) {
22300:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
22301:                 }
22302:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
22303:                     my $cachetime = 24*60*60;
22304:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
22305:                         $updates{$key} = 1;
22306:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
22307:                                                       $cachetime);
22308:                     }
22309:                 }
22310:                 if (ref($lastactref) eq 'HASH') {
22311:                     $lastactref->{'proxyalias'} = \%updates;
22312:                 }
22313:             }
22314:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
22315:                 my %samlupdates = %expiresaml;
22316:                 foreach my $key (keys(%expiresaml)) {
22317:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
22318:                 }
22319:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
22320:                     my $cachetime = 24*60*60;
22321:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
22322:                         $samlupdates{$key} = 1;
22323:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
22324:                                                       $cachetime);
22325:                     }
22326:                 }
22327:                 if (ref($lastactref) eq 'HASH') {
22328:                     $lastactref->{'proxysaml'} = \%samlupdates;
22329:                 }
22330:             }
22331:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
22332:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
22333:                 if ($changes{$item}) {
22334:                     if ($item eq 'alias') {
22335:                         my $numaliased = 0;
22336:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
22337:                             my $shown;
22338:                             if (keys(%{$wafproxy{'alias'}})) {
22339:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
22340:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
22341:                                                          &Apache::lonnet::hostname($server),
22342:                                                          $wafproxy{'alias'}{$server}).'</li>';
22343:                                     $numaliased ++;
22344:                                 }
22345:                                 if ($numaliased) {
22346:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
22347:                                                           '<ul>'.$shown.'</ul>').'</li>';
22348:                                 }
22349:                             }
22350:                         }
22351:                         unless ($numaliased) {
22352:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
22353:                         }
22354:                     } elsif ($item eq 'saml') {
22355:                         my $shown;
22356:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
22357:                             if (keys(%{$wafproxy{'saml'}})) {
22358:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
22359:                             }
22360:                         }
22361:                         if ($shown) {
22362:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
22363:                                                   $shown).'</li>';
22364:                         } else {
22365:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
22366:                         }
22367:                     } else {
22368:                         if ($item eq 'remoteip') {
22369:                             my %ip_methods = &remoteip_methods();
22370:                             if ($wafproxy{$item} =~ /^[mh]$/) {
22371:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
22372:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
22373:                             } else {
22374:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
22375:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
22376:                                                '</li>';
22377:                                 } else {
22378:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
22379:                                 }
22380:                             }
22381:                         } elsif ($item eq 'ipheader') {
22382:                             if ($wafproxy{$item}) {
22383:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
22384:                                                       $wafproxy{$item}).'</li>';
22385:                             } else {
22386:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
22387:                             }
22388:                         } elsif ($item eq 'trusted') {
22389:                             if ($wafproxy{$item}) {
22390:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
22391:                                                       $wafproxy{$item}).'</li>';
22392:                             } else {
22393:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
22394:                             }
22395:                         } elsif ($item eq 'vpnint') {
22396:                             if ($wafproxy{$item}) {
22397:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
22398:                                                        $wafproxy{$item}).'</li>';
22399:                             } else {
22400:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
22401:                             }
22402:                         } elsif ($item eq 'vpnext') {
22403:                             if ($wafproxy{$item}) {
22404:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
22405:                                                        $wafproxy{$item}).'</li>';
22406:                             } else {
22407:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
22408:                             }
22409:                         } elsif ($item eq 'sslopt') {
22410:                             if ($wafproxy{$item}) {
22411:                                 $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>';
22412:                             } else {
22413:                                 $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>';
22414:                             }
22415:                         }
22416:                     }
22417:                 }
22418:             }
22419:             $output .= '</ul>';
22420:         } else {
22421:             $output = '<span class="LC_error">'.
22422:                       &mt('An error occurred: [_1]',$putresult).'</span>';
22423:         }
22424:     } elsif (keys(%canset)) {
22425:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
22426:     }
22427:     if (@warnings) {
22428:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
22429:                        join("\n",@warnings).'</ul>';
22430:     }
22431:     return $output;
22432: }
22433: 
22434: sub validate_ip_pattern {
22435:     my ($pattern) = @_;
22436:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
22437:         my ($start,$end) = ($1,$2);
22438:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
22439:             if (($start !~ m{/}) && ($end !~ m{/})) {
22440:                 return $start.'-'.$end;
22441:             }
22442:         }
22443:     } elsif ($pattern ne '') {
22444:         $pattern = &Net::CIDR::cidrvalidate($pattern);
22445:         if ($pattern ne '') {
22446:             return $pattern;
22447:         }
22448:     }
22449:     return;
22450: }
22451: 
22452: sub modify_usersessions {
22453:     my ($dom,$lastactref,%domconfig) = @_;
22454:     my @hostingtypes = ('version','excludedomain','includedomain');
22455:     my @offloadtypes = ('primary','default');
22456:     my %types = (
22457:                   remote => \@hostingtypes,
22458:                   hosted => \@hostingtypes,
22459:                   spares => \@offloadtypes,
22460:                 );
22461:     my @prefixes = ('remote','hosted','spares');
22462:     my @lcversions = &Apache::lonnet::all_loncaparevs();
22463:     my (%by_ip,%by_location,@intdoms,@instdoms);
22464:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22465:     my @locations = sort(keys(%by_location));
22466:     my (%defaultshash,%changes);
22467:     foreach my $prefix (@prefixes) {
22468:         $defaultshash{'usersessions'}{$prefix} = {};
22469:     }
22470:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22471:     my $resulttext;
22472:     my %iphost = &Apache::lonnet::get_iphost();
22473:     foreach my $prefix (@prefixes) {
22474:         next if ($prefix eq 'spares');
22475:         foreach my $type (@{$types{$prefix}}) {
22476:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22477:             if ($type eq 'version') {
22478:                 my $value = $env{'form.'.$prefix.'_'.$type};
22479:                 my $okvalue;
22480:                 if ($value ne '') {
22481:                     if (grep(/^\Q$value\E$/,@lcversions)) {
22482:                         $okvalue = $value;
22483:                     }
22484:                 }
22485:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22486:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22487:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
22488:                             if ($inuse == 0) {
22489:                                 $changes{$prefix}{$type} = 1;
22490:                             } else {
22491:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
22492:                                     $changes{$prefix}{$type} = 1;
22493:                                 }
22494:                                 if ($okvalue ne '') {
22495:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22496:                                 } 
22497:                             }
22498:                         } else {
22499:                             if (($inuse == 1) && ($okvalue ne '')) {
22500:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22501:                                 $changes{$prefix}{$type} = 1;
22502:                             }
22503:                         }
22504:                     } else {
22505:                         if (($inuse == 1) && ($okvalue ne '')) {
22506:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22507:                             $changes{$prefix}{$type} = 1;
22508:                         }
22509:                     }
22510:                 } else {
22511:                     if (($inuse == 1) && ($okvalue ne '')) {
22512:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
22513:                         $changes{$prefix}{$type} = 1;
22514:                     }
22515:                 }
22516:             } else {
22517:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22518:                 my @okvals;
22519:                 foreach my $val (@vals) {
22520:                     if ($val =~ /:/) {
22521:                         my @items = split(/:/,$val);
22522:                         foreach my $item (@items) {
22523:                             if (ref($by_location{$item}) eq 'ARRAY') {
22524:                                 push(@okvals,$item);
22525:                             }
22526:                         }
22527:                     } else {
22528:                         if (ref($by_location{$val}) eq 'ARRAY') {
22529:                             push(@okvals,$val);
22530:                         }
22531:                     }
22532:                 }
22533:                 @okvals = sort(@okvals);
22534:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
22535:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
22536:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22537:                             if ($inuse == 0) {
22538:                                 $changes{$prefix}{$type} = 1; 
22539:                             } else {
22540:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22541:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
22542:                                 if (@changed > 0) {
22543:                                     $changes{$prefix}{$type} = 1;
22544:                                 }
22545:                             }
22546:                         } else {
22547:                             if ($inuse == 1) {
22548:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22549:                                 $changes{$prefix}{$type} = 1;
22550:                             }
22551:                         } 
22552:                     } else {
22553:                         if ($inuse == 1) {
22554:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22555:                             $changes{$prefix}{$type} = 1;
22556:                         }
22557:                     }
22558:                 } else {
22559:                     if ($inuse == 1) {
22560:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
22561:                         $changes{$prefix}{$type} = 1;
22562:                     }
22563:                 }
22564:             }
22565:         }
22566:     }
22567: 
22568:     my @alldoms = &Apache::lonnet::all_domains();
22569:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22570:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
22571:     my $savespares;
22572: 
22573:     foreach my $lonhost (sort(keys(%servers))) {
22574:         my $serverhomeID =
22575:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
22576:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
22577:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
22578:         my %spareschg;
22579:         foreach my $type (@{$types{'spares'}}) {
22580:             my @okspares;
22581:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
22582:             foreach my $server (@checked) {
22583:                 if (&Apache::lonnet::hostname($server) ne '') {
22584:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
22585:                         unless (grep(/^\Q$server\E$/,@okspares)) {
22586:                             push(@okspares,$server);
22587:                         }
22588:                     }
22589:                 }
22590:             }
22591:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
22592:             my $newspare;
22593:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
22594:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
22595:                     $newspare = $new;
22596:                 }
22597:             }
22598:             my @spares;
22599:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
22600:                 @spares = sort(@okspares,$newspare);
22601:             } else {
22602:                 @spares = sort(@okspares);
22603:             }
22604:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
22605:             if (ref($spareid{$lonhost}) eq 'HASH') {
22606:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
22607:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
22608:                     if (@diffs > 0) {
22609:                         $spareschg{$type} = 1;
22610:                     }
22611:                 }
22612:             }
22613:         }
22614:         if (keys(%spareschg) > 0) {
22615:             $changes{'spares'}{$lonhost} = \%spareschg;
22616:         }
22617:     }
22618:     $defaultshash{'usersessions'}{'offloadnow'} = {};
22619:     $defaultshash{'usersessions'}{'offloadoth'} = {};
22620:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
22621:     my @okoffload;
22622:     if (@offloadnow) {
22623:         foreach my $server (@offloadnow) {
22624:             if (&Apache::lonnet::hostname($server) ne '') {
22625:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
22626:                     push(@okoffload,$server);
22627:                 }
22628:             }
22629:         }
22630:         if (@okoffload) {
22631:             foreach my $lonhost (@okoffload) {
22632:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
22633:             }
22634:         }
22635:     }
22636:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
22637:     my @okoffloadoth;
22638:     if (@offloadoth) {
22639:         foreach my $server (@offloadoth) {
22640:             if (&Apache::lonnet::hostname($server) ne '') {
22641:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
22642:                     push(@okoffloadoth,$server);
22643:                 }
22644:             }
22645:         }
22646:         if (@okoffloadoth) {
22647:             foreach my $lonhost (@okoffloadoth) {
22648:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
22649:             }
22650:         }
22651:     }
22652:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
22653:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
22654:             if (ref($changes{'spares'}) eq 'HASH') {
22655:                 if (keys(%{$changes{'spares'}}) > 0) {
22656:                     $savespares = 1;
22657:                 }
22658:             }
22659:         } else {
22660:             $savespares = 1;
22661:         }
22662:         foreach my $offload ('offloadnow','offloadoth') {
22663:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
22664:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
22665:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
22666:                         $changes{$offload} = 1;
22667:                         last;
22668:                     }
22669:                 }
22670:                 unless ($changes{$offload}) {
22671:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
22672:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
22673:                             $changes{$offload} = 1;
22674:                             last;
22675:                         }
22676:                     }
22677:                 }
22678:             } else {
22679:                 if (($offload eq 'offloadnow') && (@okoffload)) {
22680:                      $changes{'offloadnow'} = 1;
22681:                 }
22682:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
22683:                     $changes{'offloadoth'} = 1;
22684:                 }
22685:             }
22686:         }
22687:     } else {
22688:         if (@okoffload) {
22689:             $changes{'offloadnow'} = 1;
22690:         }
22691:         if (@okoffloadoth) {
22692:             $changes{'offloadoth'} = 1;
22693:         }
22694:     }
22695:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
22696:     if ((keys(%changes) > 0) || ($savespares)) {
22697:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22698:                                                  $dom);
22699:         if ($putresult eq 'ok') {
22700:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22701:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
22702:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
22703:                 }
22704:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
22705:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
22706:                 }
22707:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22708:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
22709:                 }
22710:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22711:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
22712:                 }
22713:             }
22714:             my $cachetime = 24*60*60;
22715:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22716:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
22717:             if (ref($lastactref) eq 'HASH') {
22718:                 $lastactref->{'domdefaults'} = 1;
22719:                 $lastactref->{'usersessions'} = 1;
22720:             }
22721:             if (keys(%changes) > 0) {
22722:                 my %lt = &usersession_titles();
22723:                 $resulttext = &mt('Changes made:').'<ul>';
22724:                 foreach my $prefix (@prefixes) {
22725:                     if (ref($changes{$prefix}) eq 'HASH') {
22726:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
22727:                         if ($prefix eq 'spares') {
22728:                             if (ref($changes{$prefix}) eq 'HASH') {
22729:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
22730:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
22731:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
22732:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
22733:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22734:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
22735:                                         foreach my $type (@{$types{$prefix}}) {
22736:                                             if ($changes{$prefix}{$lonhost}{$type}) {
22737:                                                 my $offloadto = &mt('None');
22738:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
22739:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
22740:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
22741:                                                     }
22742:                                                 }
22743:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
22744:                                             }
22745:                                         }
22746:                                     }
22747:                                     $resulttext .= '</li>';
22748:                                 }
22749:                             }
22750:                         } else {
22751:                             foreach my $type (@{$types{$prefix}}) {
22752:                                 if (defined($changes{$prefix}{$type})) {
22753:                                     my ($newvalue,$notinuse);
22754:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
22755:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
22756:                                             if ($type eq 'version') {
22757:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
22758:                                             } else {
22759:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
22760:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
22761:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
22762:                                                     }
22763:                                                 } else {
22764:                                                     $notinuse = 1;
22765:                                                 }
22766:                                             }
22767:                                         }
22768:                                     }
22769:                                     if ($newvalue eq '') {
22770:                                         if ($type eq 'version') {
22771:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
22772:                                         } elsif ($notinuse) {
22773:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22774:                                         } else {
22775:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22776:                                         }
22777:                                     } else {
22778:                                         if ($type eq 'version') {
22779:                                             $newvalue .= ' '.&mt('(or later)');
22780:                                         }
22781:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22782:                                     }
22783:                                 }
22784:                             }
22785:                         }
22786:                         $resulttext .= '</ul>';
22787:                     }
22788:                 }
22789:                 if ($changes{'offloadnow'}) {
22790:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
22791:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
22792:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
22793:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
22794:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22795:                             }
22796:                             $resulttext .= '</ul>';
22797:                         } else {
22798:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
22799:                         }
22800:                     } else {
22801:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
22802:                     }
22803:                 }
22804:                 if ($changes{'offloadoth'}) {
22805:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
22806:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
22807:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
22808:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
22809:                                 $resulttext .= '<li>'.$lonhost.'</li>';
22810:                             }
22811:                             $resulttext .= '</ul>';
22812:                         } else {
22813:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
22814:                         }
22815:                     } else {
22816:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
22817:                     }
22818:                 }
22819:                 $resulttext .= '</ul>';
22820:             } else {
22821:                 $resulttext = $nochgmsg;
22822:             }
22823:         } else {
22824:             $resulttext = '<span class="LC_error">'.
22825:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22826:         }
22827:     } else {
22828:         $resulttext = $nochgmsg;
22829:     }
22830:     return $resulttext;
22831: }
22832: 
22833: sub modify_ssl {
22834:     my ($dom,$lastactref,%domconfig) = @_;
22835:     my (%by_ip,%by_location,@intdoms,@instdoms);
22836:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
22837:     my @locations = sort(keys(%by_location));
22838:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22839:     my (%defaultshash,%changes);
22840:     my $action = 'ssl';
22841:     my @prefixes = ('connto','connfrom','replication');
22842:     foreach my $prefix (@prefixes) {
22843:         $defaultshash{$action}{$prefix} = {};
22844:     }
22845:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
22846:     my $resulttext;
22847:     my %iphost = &Apache::lonnet::get_iphost();
22848:     my @reptypes = ('certreq','nocertreq');
22849:     my @connecttypes = ('dom','intdom','other');
22850:     my %types = (
22851:                   connto      => \@connecttypes,
22852:                   connfrom    => \@connecttypes,
22853:                   replication => \@reptypes,
22854:                 );
22855:     foreach my $prefix (sort(keys(%types))) {
22856:         foreach my $type (@{$types{$prefix}}) {
22857:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22858:                 my $value = 'yes';
22859:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
22860:                     $value = $env{'form.'.$prefix.'_'.$type};
22861:                 }
22862:                 if (ref($domconfig{$action}) eq 'HASH') {
22863:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22864:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
22865:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
22866:                                 $changes{$prefix}{$type} = 1;
22867:                             }
22868:                             $defaultshash{$action}{$prefix}{$type} = $value;
22869:                         } else {
22870:                             $defaultshash{$action}{$prefix}{$type} = $value;
22871:                             $changes{$prefix}{$type} = 1;
22872:                         }
22873:                     } else {
22874:                         $defaultshash{$action}{$prefix}{$type} = $value;
22875:                         $changes{$prefix}{$type} = 1;
22876:                     }
22877:                 } else {
22878:                     $defaultshash{$action}{$prefix}{$type} = $value;
22879:                     $changes{$prefix}{$type} = 1;
22880:                 }
22881:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
22882:                     delete($changes{$prefix}{$type});
22883:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
22884:                     delete($changes{$prefix}{$type});
22885:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
22886:                     delete($changes{$prefix}{$type});
22887:                 }
22888:             } elsif ($prefix eq 'replication') {
22889:                 if (@locations > 0) {
22890:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
22891:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
22892:                     my @okvals;
22893:                     foreach my $val (@vals) {
22894:                         if ($val =~ /:/) {
22895:                             my @items = split(/:/,$val);
22896:                             foreach my $item (@items) {
22897:                                 if (ref($by_location{$item}) eq 'ARRAY') {
22898:                                     push(@okvals,$item);
22899:                                 }
22900:                             }
22901:                         } else {
22902:                             if (ref($by_location{$val}) eq 'ARRAY') {
22903:                                 push(@okvals,$val);
22904:                             }
22905:                         }
22906:                     }
22907:                     @okvals = sort(@okvals);
22908:                     if (ref($domconfig{$action}) eq 'HASH') {
22909:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
22910:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
22911:                                 if ($inuse == 0) {
22912:                                     $changes{$prefix}{$type} = 1;
22913:                                 } else {
22914:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22915:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
22916:                                     if (@changed > 0) {
22917:                                         $changes{$prefix}{$type} = 1;
22918:                                     }
22919:                                 }
22920:                             } else {
22921:                                 if ($inuse == 1) {
22922:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
22923:                                     $changes{$prefix}{$type} = 1;
22924:                                 }
22925:                             }
22926:                         } else {
22927:                             if ($inuse == 1) {
22928:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
22929:                                 $changes{$prefix}{$type} = 1;
22930:                             }
22931:                         }
22932:                     } else {
22933:                         if ($inuse == 1) {
22934:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
22935:                             $changes{$prefix}{$type} = 1;
22936:                         }
22937:                     }
22938:                 }
22939:             }
22940:         }
22941:     }
22942:     if (keys(%changes)) {
22943:         foreach my $prefix (keys(%changes)) {
22944:             if (ref($changes{$prefix}) eq 'HASH') {
22945:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
22946:                     delete($changes{$prefix});
22947:                 }
22948:             } else {
22949:                 delete($changes{$prefix});
22950:             }
22951:         }
22952:     }
22953:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
22954:     if (keys(%changes) > 0) {
22955:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
22956:                                                  $dom);
22957:         if ($putresult eq 'ok') {
22958:             if (ref($defaultshash{$action}) eq 'HASH') {
22959:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
22960:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
22961:                 }
22962:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
22963:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
22964:                 }
22965:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
22966:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
22967:                 }
22968:             }
22969:             my $cachetime = 24*60*60;
22970:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
22971:             if (ref($lastactref) eq 'HASH') {
22972:                 $lastactref->{'domdefaults'} = 1;
22973:             }
22974:             if (keys(%changes) > 0) {
22975:                 my %titles = &ssl_titles();
22976:                 $resulttext = &mt('Changes made:').'<ul>';
22977:                 foreach my $prefix (@prefixes) {
22978:                     if (ref($changes{$prefix}) eq 'HASH') {
22979:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
22980:                         foreach my $type (@{$types{$prefix}}) {
22981:                             if (defined($changes{$prefix}{$type})) {
22982:                                 my ($newvalue,$notinuse);
22983:                                 if (ref($defaultshash{$action}) eq 'HASH') {
22984:                                     if (ref($defaultshash{$action}{$prefix})) {
22985:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
22986:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
22987:                                         } else {
22988:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
22989:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
22990:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
22991:                                                 }
22992:                                             } else {
22993:                                                 $notinuse = 1;
22994:                                             }
22995:                                         }
22996:                                     }
22997:                                     if ($notinuse) {
22998:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
22999:                                     } elsif ($newvalue eq '') {
23000:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
23001:                                     } else {
23002:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
23003:                                     }
23004:                                 }
23005:                             }
23006:                         }
23007:                         $resulttext .= '</ul>';
23008:                     }
23009:                 }
23010:             } else {
23011:                 $resulttext = $nochgmsg;
23012:             }
23013:         } else {
23014:             $resulttext = '<span class="LC_error">'.
23015:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23016:         }
23017:     } else {
23018:         $resulttext = $nochgmsg;
23019:     }
23020:     return $resulttext;
23021: }
23022: 
23023: sub modify_trust {
23024:     my ($dom,$lastactref,%domconfig) = @_;
23025:     my (%by_ip,%by_location,@intdoms,@instdoms);
23026:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
23027:     my @locations = sort(keys(%by_location));
23028:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
23029:     my @types = ('exc','inc');
23030:     my (%defaultshash,%changes);
23031:     foreach my $prefix (@prefixes) {
23032:         $defaultshash{'trust'}{$prefix} = {};
23033:     }
23034:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
23035:     my $resulttext;
23036:     foreach my $prefix (@prefixes) {
23037:         foreach my $type (@types) {
23038:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
23039:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
23040:             my @okvals;
23041:             foreach my $val (@vals) {
23042:                 if ($val =~ /:/) {
23043:                     my @items = split(/:/,$val);
23044:                     foreach my $item (@items) {
23045:                         if (ref($by_location{$item}) eq 'ARRAY') {
23046:                             push(@okvals,$item);
23047:                         }
23048:                     }
23049:                 } else {
23050:                     if (ref($by_location{$val}) eq 'ARRAY') {
23051:                         push(@okvals,$val);
23052:                     }
23053:                 }
23054:             }
23055:             @okvals = sort(@okvals);
23056:             if (ref($domconfig{'trust'}) eq 'HASH') {
23057:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
23058:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23059:                         if ($inuse == 0) {
23060:                             $changes{$prefix}{$type} = 1;
23061:                         } else {
23062:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23063:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
23064:                             if (@changed > 0) {
23065:                                 $changes{$prefix}{$type} = 1;
23066:                             }
23067:                         }
23068:                     } else {
23069:                         if ($inuse == 1) {
23070:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23071:                             $changes{$prefix}{$type} = 1;
23072:                         }
23073:                     }
23074:                 } else {
23075:                     if ($inuse == 1) {
23076:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23077:                         $changes{$prefix}{$type} = 1;
23078:                     }
23079:                 }
23080:             } else {
23081:                 if ($inuse == 1) {
23082:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
23083:                     $changes{$prefix}{$type} = 1;
23084:                 }
23085:             }
23086:         }
23087:     }
23088:     my $nochgmsg = &mt('No changes made to trust settings.');
23089:     if (keys(%changes) > 0) {
23090:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
23091:                                                  $dom);
23092:         if ($putresult eq 'ok') {
23093:             if (ref($defaultshash{'trust'}) eq 'HASH') {
23094:                 foreach my $prefix (@prefixes) {
23095:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
23096:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
23097:                     }
23098:                 }
23099:             }
23100:             my $cachetime = 24*60*60;
23101:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
23102:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
23103:             if (ref($lastactref) eq 'HASH') {
23104:                 $lastactref->{'domdefaults'} = 1;
23105:                 $lastactref->{'trust'} = 1;
23106:             }
23107:             if (keys(%changes) > 0) {
23108:                 my %lt = &trust_titles();
23109:                 $resulttext = &mt('Changes made:').'<ul>';
23110:                 foreach my $prefix (@prefixes) {
23111:                     if (ref($changes{$prefix}) eq 'HASH') {
23112:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
23113:                         foreach my $type (@types) {
23114:                             if (defined($changes{$prefix}{$type})) {
23115:                                 my ($newvalue,$notinuse);
23116:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
23117:                                     if (ref($defaultshash{'trust'}{$prefix})) {
23118:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
23119:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
23120:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
23121:                                             }
23122:                                         } else {
23123:                                             $notinuse = 1;
23124:                                         }
23125:                                     }
23126:                                 }
23127:                                 if ($notinuse) {
23128:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
23129:                                 } elsif ($newvalue eq '') {
23130:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
23131:                                 } else {
23132:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
23133:                                 }
23134:                             }
23135:                         }
23136:                         $resulttext .= '</ul>';
23137:                     }
23138:                 }
23139:                 $resulttext .= '</ul>';
23140:             } else {
23141:                 $resulttext = $nochgmsg;
23142:             }
23143:         } else {
23144:             $resulttext = '<span class="LC_error">'.
23145:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23146:         }
23147:     } else {
23148:         $resulttext = $nochgmsg;
23149:     }
23150:     return $resulttext;
23151: }
23152: 
23153: sub modify_loadbalancing {
23154:     my ($dom,%domconfig) = @_;
23155:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
23156:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
23157:     my ($othertitle,$usertypes,$types) =
23158:         &Apache::loncommon::sorted_inst_types($dom);
23159:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23160:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
23161:     my @sparestypes = ('primary','default');
23162:     my %typetitles = &sparestype_titles();
23163:     my $resulttext;
23164:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23165:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23166:         %existing = %{$domconfig{'loadbalancing'}};
23167:     }
23168:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
23169:                               \%currtargets,\%currrules,\%currcookies);
23170:     my ($saveloadbalancing,%defaultshash,%changes);
23171:     my ($alltypes,$othertypes,$titles) =
23172:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
23173:     my %ruletitles = &offloadtype_text();
23174:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
23175:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
23176:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
23177:         if ($balancer eq '') {
23178:             next;
23179:         }
23180:         if (!exists($servers{$balancer})) {
23181:             if (exists($currbalancer{$balancer})) {
23182:                 push(@{$changes{'delete'}},$balancer);
23183:             }
23184:             next;
23185:         }
23186:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
23187:             push(@{$changes{'delete'}},$balancer);
23188:             next;
23189:         }
23190:         if (!exists($currbalancer{$balancer})) {
23191:             push(@{$changes{'add'}},$balancer);
23192:         }
23193:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
23194:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
23195:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
23196:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
23197:             $saveloadbalancing = 1;
23198:         }
23199:         foreach my $sparetype (@sparestypes) {
23200:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
23201:             my @offloadto;
23202:             foreach my $target (@targets) {
23203:                 if (($servers{$target}) && ($target ne $balancer)) {
23204:                     if ($sparetype eq 'default') {
23205:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
23206:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
23207:                         }
23208:                     }
23209:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
23210:                         push(@offloadto,$target);
23211:                     }
23212:                 }
23213:             }
23214:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
23215:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
23216:                     push(@offloadto,$balancer);
23217:                 }
23218:             }
23219:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
23220:         }
23221:         if ($env{'form.loadbalancing_cookie_'.$i}) {
23222:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
23223:             if (exists($currbalancer{$balancer})) {
23224:                 unless ($currcookies{$balancer}) {
23225:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
23226:                 }
23227:             }
23228:         } elsif (exists($currbalancer{$balancer})) {
23229:             if ($currcookies{$balancer}) {
23230:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
23231:             }
23232:         }
23233:         if (ref($currtargets{$balancer}) eq 'HASH') {
23234:             foreach my $sparetype (@sparestypes) {
23235:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
23236:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
23237:                     if (@targetdiffs > 0) {
23238:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23239:                     }
23240:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23241:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23242:                         $changes{'curr'}{$balancer}{'targets'} = 1;
23243:                     }
23244:                 }
23245:             }
23246:         } else {
23247:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
23248:                 foreach my $sparetype (@sparestypes) {
23249:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23250:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23251:                             $changes{'curr'}{$balancer}{'targets'} = 1;
23252:                         }
23253:                     }
23254:                 }
23255:             }
23256:         }
23257:         my $ishomedom;
23258:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
23259:             $ishomedom = 1;
23260:         }
23261:         if (ref($alltypes) eq 'ARRAY') {
23262:             foreach my $type (@{$alltypes}) {
23263:                 my $rule;
23264:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
23265:                          (!$ishomedom)) {
23266:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
23267:                 }
23268:                 if ($rule eq 'specific') {
23269:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
23270:                     if (exists($servers{$specifiedhost})) {
23271:                         $rule = $specifiedhost;
23272:                     }
23273:                 }
23274:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
23275:                 if (ref($currrules{$balancer}) eq 'HASH') {
23276:                     if ($rule ne $currrules{$balancer}{$type}) {
23277:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23278:                     }
23279:                 } elsif ($rule ne '') {
23280:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
23281:                 }
23282:             }
23283:         }
23284:     }
23285:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
23286:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
23287:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
23288:             $defaultshash{'loadbalancing'} = {};
23289:         }
23290:         my $putresult = &Apache::lonnet::put_dom('configuration',
23291:                                                  \%defaultshash,$dom);
23292:         if ($putresult eq 'ok') {
23293:             if (keys(%changes) > 0) {
23294:                 my %toupdate;
23295:                 if (ref($changes{'delete'}) eq 'ARRAY') {
23296:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
23297:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
23298:                         $toupdate{$balancer} = 1;
23299:                     }
23300:                 }
23301:                 if (ref($changes{'add'}) eq 'ARRAY') {
23302:                     foreach my $balancer (sort(@{$changes{'add'}})) {
23303:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
23304:                         $toupdate{$balancer} = 1;
23305:                     }
23306:                 }
23307:                 if (ref($changes{'curr'}) eq 'HASH') {
23308:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
23309:                         $toupdate{$balancer} = 1;
23310:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
23311:                             if ($changes{'curr'}{$balancer}{'targets'}) {
23312:                                 my %offloadstr;
23313:                                 foreach my $sparetype (@sparestypes) {
23314:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23315:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
23316:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23317:                                         }
23318:                                     }
23319:                                 }
23320:                                 if (keys(%offloadstr) == 0) {
23321:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
23322:                                 } else {
23323:                                     my $showoffload;
23324:                                     foreach my $sparetype (@sparestypes) {
23325:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
23326:                                         if (defined($offloadstr{$sparetype})) {
23327:                                             $showoffload .= $offloadstr{$sparetype};
23328:                                         } else {
23329:                                             $showoffload .= &mt('None');
23330:                                         }
23331:                                         $showoffload .= ('&nbsp;'x3);
23332:                                     }
23333:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
23334:                                 }
23335:                             }
23336:                         }
23337:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
23338:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
23339:                                 foreach my $type (@{$alltypes}) {
23340:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
23341:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23342:                                         my $balancetext;
23343:                                         if ($rule eq '') {
23344:                                             $balancetext =  $ruletitles{'default'};
23345:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
23346:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23347:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
23348:                                                 foreach my $sparetype (@sparestypes) {
23349:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
23350:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
23351:                                                     }
23352:                                                 }
23353:                                                 foreach my $item (@{$alltypes}) {
23354:                                                     next if ($item =~  /^_LC_ipchange/);
23355:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
23356:                                                     if ($hasrule eq 'homeserver') {
23357:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
23358:                                                     } else {
23359:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
23360:                                                             if ($servers{$hasrule}) {
23361:                                                                 $toupdate{$hasrule} = 1;
23362:                                                             }
23363:                                                         }
23364:                                                     }
23365:                                                 }
23366:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
23367:                                                     $balancetext =  $ruletitles{$rule};
23368:                                                 } else {
23369:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
23370:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
23371:                                                     if ($receiver) {
23372:                                                         $toupdate{$receiver};
23373:                                                     }
23374:                                                 }
23375:                                             } else {
23376:                                                 $balancetext =  $ruletitles{$rule};
23377:                                             }
23378:                                         } else {
23379:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
23380:                                         }
23381:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
23382:                                     }
23383:                                 }
23384:                             }
23385:                         }
23386:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
23387:                             if ($currcookies{$balancer}) {
23388:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
23389:                                                           $balancer).'</li>';
23390:                             } else {
23391:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
23392:                                                           $balancer).'</li>';
23393:                             }
23394:                         }
23395:                     }
23396:                 }
23397:                 if (keys(%toupdate)) {
23398:                     my %thismachine;
23399:                     my $updatedhere;
23400:                     my $cachetime = 60*60*24;
23401:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23402:                     foreach my $lonhost (keys(%toupdate)) {
23403:                         if ($thismachine{$lonhost}) {
23404:                             unless ($updatedhere) {
23405:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
23406:                                                               $defaultshash{'loadbalancing'},
23407:                                                               $cachetime);
23408:                                 $updatedhere = 1;
23409:                             }
23410:                         } else {
23411:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
23412:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
23413:                         }
23414:                     }
23415:                 }
23416:                 if ($resulttext ne '') {
23417:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
23418:                 } else {
23419:                     $resulttext = $nochgmsg;
23420:                 }
23421:             } else {
23422:                 $resulttext = $nochgmsg;
23423:             }
23424:         } else {
23425:             $resulttext = '<span class="LC_error">'.
23426:                           &mt('An error occurred: [_1]',$putresult).'</span>';
23427:         }
23428:     } else {
23429:         $resulttext = $nochgmsg;
23430:     }
23431:     return $resulttext;
23432: }
23433: 
23434: sub recurse_check {
23435:     my ($chkcats,$categories,$depth,$name) = @_;
23436:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
23437:         my $chg = 0;
23438:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
23439:             my $category = $chkcats->[$depth]{$name}[$j];
23440:             my $item;
23441:             if ($category eq '') {
23442:                 $chg ++;
23443:             } else {
23444:                 my $deeper = $depth + 1;
23445:                 $item = &escape($category).':'.&escape($name).':'.$depth;
23446:                 if ($chg) {
23447:                     $categories->{$item} -= $chg;
23448:                 }
23449:                 &recurse_check($chkcats,$categories,$deeper,$category);
23450:                 $deeper --;
23451:             }
23452:         }
23453:     }
23454:     return;
23455: }
23456: 
23457: sub recurse_cat_deletes {
23458:     my ($item,$coursecategories,$deletions) = @_;
23459:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
23460:     my $subdepth = $depth + 1;
23461:     if (ref($coursecategories) eq 'HASH') {
23462:         foreach my $subitem (keys(%{$coursecategories})) {
23463:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
23464:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
23465:                 delete($coursecategories->{$subitem});
23466:                 $deletions->{$subitem} = 1;
23467:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
23468:             }
23469:         }
23470:     }
23471:     return;
23472: }
23473: 
23474: sub active_dc_picker {
23475:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
23476:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
23477:     my @domcoord = keys(%domcoords);
23478:     if (keys(%currhash)) {
23479:         foreach my $dc (keys(%currhash)) {
23480:             unless (exists($domcoords{$dc})) {
23481:                 push(@domcoord,$dc);
23482:             }
23483:         }
23484:     }
23485:     @domcoord = sort(@domcoord);
23486:     my $numdcs = scalar(@domcoord);
23487:     my $rows = 0;
23488:     my $table;
23489:     if ($numdcs > 1) {
23490:         $table = '<table>';
23491:         for (my $i=0; $i<@domcoord; $i++) {
23492:             my $rem = $i%($numinrow);
23493:             if ($rem == 0) {
23494:                 if ($i > 0) {
23495:                     $table .= '</tr>';
23496:                 }
23497:                 $table .= '<tr>';
23498:                 $rows ++;
23499:             }
23500:             my $check = '';
23501:             if ($inputtype eq 'radio') {
23502:                 if (keys(%currhash) == 0) {
23503:                     if (!$i) {
23504:                         $check = ' checked="checked"';
23505:                     }
23506:                 } elsif (exists($currhash{$domcoord[$i]})) {
23507:                     $check = ' checked="checked"';
23508:                 }
23509:             } else {
23510:                 if (exists($currhash{$domcoord[$i]})) {
23511:                     $check = ' checked="checked"';
23512:                 }
23513:             }
23514:             if ($i == @domcoord - 1) {
23515:                 my $colsleft = $numinrow - $rem;
23516:                 if ($colsleft > 1) {
23517:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
23518:                 } else {
23519:                     $table .= '<td class="LC_left_item">';
23520:                 }
23521:             } else {
23522:                 $table .= '<td class="LC_left_item">';
23523:             }
23524:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
23525:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23526:             $table .= '<span class="LC_nobreak"><label>'.
23527:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
23528:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
23529:             if ($user ne $dcname.':'.$dcdom) {
23530:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23531:             }
23532:             $table .= '</label></span></td>';
23533:         }
23534:         $table .= '</tr></table>';
23535:     } elsif ($numdcs == 1) {
23536:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
23537:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
23538:         if ($inputtype eq 'radio') {
23539:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
23540:             if ($user ne $dcname.':'.$dcdom) {
23541:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23542:             }
23543:         } else {
23544:             my $check;
23545:             if (exists($currhash{$domcoord[0]})) {
23546:                 $check = ' checked="checked"';
23547:             }
23548:             $table = '<span class="LC_nobreak"><label>'.
23549:                      '<input type="checkbox" name="'.$name.'" '.
23550:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
23551:             if ($user ne $dcname.':'.$dcdom) {
23552:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
23553:             }
23554:             $table .= '</label></span>';
23555:             $rows ++;
23556:         }
23557:     }
23558:     return ($numdcs,$table,$rows);
23559: }
23560: 
23561: sub usersession_titles {
23562:     return &Apache::lonlocal::texthash(
23563:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
23564:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
23565:                spares => 'Servers offloaded to, when busy',
23566:                version => 'LON-CAPA version requirement',
23567:                excludedomain => 'Allow all, but exclude specific domains',
23568:                includedomain => 'Deny all, but include specific domains',
23569:                primary => 'Primary (checked first)',
23570:                default => 'Default',
23571:            );
23572: }
23573: 
23574: sub id_for_thisdom {
23575:     my (%servers) = @_;
23576:     my %altids;
23577:     foreach my $server (keys(%servers)) {
23578:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
23579:         if ($serverhome ne $server) {
23580:             $altids{$serverhome} = $server;
23581:         }
23582:     }
23583:     return %altids;
23584: }
23585: 
23586: sub count_servers {
23587:     my ($currbalancer,%servers) = @_;
23588:     my (@spares,$numspares);
23589:     foreach my $lonhost (sort(keys(%servers))) {
23590:         next if ($currbalancer eq $lonhost);
23591:         push(@spares,$lonhost);
23592:     }
23593:     if ($currbalancer) {
23594:         $numspares = scalar(@spares);
23595:     } else {
23596:         $numspares = scalar(@spares) - 1;
23597:     }
23598:     return ($numspares,@spares);
23599: }
23600: 
23601: sub lonbalance_targets_js {
23602:     my ($dom,$types,$servers,$settings) = @_;
23603:     my $select = &mt('Select');
23604:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
23605:     if (ref($servers) eq 'HASH') {
23606:         $alltargets = join("','",sort(keys(%{$servers})));
23607:         my @homedoms;
23608:         foreach my $server (sort(keys(%{$servers}))) {
23609:             if (&Apache::lonnet::host_domain($server) eq $dom) {
23610:                 push(@homedoms,'1');
23611:             } else {
23612:                 push(@homedoms,'0');
23613:             }
23614:         }
23615:         $allishome = join("','",@homedoms);
23616:     }
23617:     if (ref($types) eq 'ARRAY') {
23618:         if (@{$types} > 0) {
23619:             @alltypes = @{$types};
23620:         }
23621:     }
23622:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
23623:     $allinsttypes = join("','",@alltypes);
23624:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
23625:     if (ref($settings) eq 'HASH') {
23626:         %existing = %{$settings};
23627:     }
23628:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
23629:                               \%currtargets,\%currrules,\%currcookies);
23630:     my $balancers = join("','",sort(keys(%currbalancer)));
23631:     return <<"END";
23632: 
23633: <script type="text/javascript">
23634: // <![CDATA[
23635: 
23636: currBalancers = new Array('$balancers');
23637: 
23638: function toggleTargets(balnum) {
23639:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23640:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
23641:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
23642:     var prevbalancer = prevhostitem.value;
23643:     var baltotal = document.getElementById('loadbalancing_total').value;
23644:     prevhostitem.value = balancer;
23645:     if (prevbalancer != '') {
23646:         var prevIdx = currBalancers.indexOf(prevbalancer);
23647:         if (prevIdx != -1) {
23648:             currBalancers.splice(prevIdx,1);
23649:         }
23650:     }
23651:     if (balancer == '') {
23652:         hideSpares(balnum);
23653:     } else {
23654:         var currIdx = currBalancers.indexOf(balancer);
23655:         if (currIdx == -1) {
23656:             currBalancers.push(balancer);
23657:         }
23658:         var homedoms = new Array('$allishome');
23659:         var ishomedom = homedoms[lonhostitem.selectedIndex];
23660:         showSpares(balancer,ishomedom,balnum);
23661:     }
23662:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
23663:     return;
23664: }
23665: 
23666: function showSpares(balancer,ishomedom,balnum) {
23667:     var alltargets = new Array('$alltargets');
23668:     var insttypes = new Array('$allinsttypes');
23669:     var offloadtypes = new Array('primary','default');
23670: 
23671:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
23672:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
23673:  
23674:     for (var i=0; i<offloadtypes.length; i++) {
23675:         var count = 0;
23676:         for (var j=0; j<alltargets.length; j++) {
23677:             if (alltargets[j] != balancer) {
23678:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
23679:                 item.value = alltargets[j];
23680:                 item.style.textAlign='left';
23681:                 item.style.textFace='normal';
23682:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
23683:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
23684:                     item.disabled = '';
23685:                 } else {
23686:                     item.disabled = 'disabled';
23687:                     item.checked = false;
23688:                 }
23689:                 count ++;
23690:             }
23691:         }
23692:     }
23693:     for (var k=0; k<insttypes.length; k++) {
23694:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
23695:             if (ishomedom == 1) {
23696:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23697:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23698:             } else {
23699:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23700:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23701:             }
23702:         } else {
23703:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
23704:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
23705:         }
23706:         if ((insttypes[k] != '_LC_external') && 
23707:             ((insttypes[k] != '_LC_internetdom') ||
23708:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
23709:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
23710:             item.options.length = 0;
23711:             item.options[0] = new Option("","",true,true);
23712:             var idx = 0;
23713:             for (var m=0; m<alltargets.length; m++) {
23714:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
23715:                     idx ++;
23716:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23717:                 }
23718:             }
23719:         }
23720:     }
23721:     return;
23722: }
23723: 
23724: function hideSpares(balnum) {
23725:     var alltargets = new Array('$alltargets');
23726:     var insttypes = new Array('$allinsttypes');
23727:     var offloadtypes = new Array('primary','default');
23728: 
23729:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
23730:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
23731: 
23732:     var total = alltargets.length - 1;
23733:     for (var i=0; i<offloadtypes; i++) {
23734:         for (var j=0; j<total; j++) {
23735:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
23736:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
23737:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
23738:         }
23739:     }
23740:     for (var k=0; k<insttypes.length; k++) {
23741:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
23742:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
23743:         if (insttypes[k] != '_LC_external') {
23744:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
23745:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
23746:         }
23747:     }
23748:     return;
23749: }
23750: 
23751: function checkOffloads(item,balnum,type) {
23752:     var alltargets = new Array('$alltargets');
23753:     var offloadtypes = new Array('primary','default');
23754:     if (item.checked) {
23755:         var total = alltargets.length - 1;
23756:         var other;
23757:         if (type == offloadtypes[0]) {
23758:             other = offloadtypes[1];
23759:         } else {
23760:             other = offloadtypes[0];
23761:         }
23762:         for (var i=0; i<total; i++) {
23763:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
23764:             if (server == item.value) {
23765:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
23766:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
23767:                 }
23768:             }
23769:         }
23770:     }
23771:     return;
23772: }
23773: 
23774: function singleServerToggle(balnum,type) {
23775:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
23776:     if (offloadtoSelIdx == 0) {
23777:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
23778:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23779: 
23780:     } else {
23781:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
23782:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23783:     }
23784:     return;
23785: }
23786: 
23787: function balanceruleChange(formname,balnum,type) {
23788:     if (type == '_LC_external') {
23789:         return;
23790:     }
23791:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
23792:     for (var i=0; i<typesRules.length; i++) {
23793:         if (formname.elements[typesRules[i]].checked) {
23794:             if (formname.elements[typesRules[i]].value != 'specific') {
23795:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
23796:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
23797:             } else {
23798:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
23799:             }
23800:         }
23801:     }
23802:     return;
23803: }
23804: 
23805: function balancerDeleteChange(balnum) {
23806:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
23807:     var baltotal = document.getElementById('loadbalancing_total').value;
23808:     var addtarget;
23809:     var removetarget;
23810:     var action = 'delete';
23811:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
23812:         var lonhost = hostitem.value;
23813:         var currIdx = currBalancers.indexOf(lonhost);
23814:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
23815:             if (currIdx != -1) {
23816:                 currBalancers.splice(currIdx,1);
23817:             }
23818:             addtarget = lonhost;
23819:         } else {
23820:             if (currIdx == -1) {
23821:                 currBalancers.push(lonhost);
23822:             }
23823:             removetarget = lonhost;
23824:             action = 'undelete';
23825:         }
23826:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
23827:     }
23828:     return;
23829: }
23830: 
23831: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
23832:     if (baltotal > 1) {
23833:         var offloadtypes = new Array('primary','default');
23834:         var alltargets = new Array('$alltargets');
23835:         var insttypes = new Array('$allinsttypes');
23836:         for (var i=0; i<baltotal; i++) {
23837:             if (i != balnum) {
23838:                 for (var j=0; j<offloadtypes.length; j++) {
23839:                     var total = alltargets.length - 1;
23840:                     for (var k=0; k<total; k++) {
23841:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
23842:                         var server = serveritem.value;
23843:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
23844:                             if (server == addtarget) {
23845:                                 serveritem.disabled = '';
23846:                             }
23847:                         }
23848:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23849:                             if (server == removetarget) {
23850:                                 serveritem.disabled = 'disabled';
23851:                                 serveritem.checked = false;
23852:                             }
23853:                         }
23854:                     }
23855:                 }
23856:                 for (var j=0; j<insttypes.length; j++) {
23857:                     if (insttypes[j] != '_LC_external') {
23858:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
23859:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
23860:                             var currSel = singleserver.selectedIndex;
23861:                             var currVal = singleserver.options[currSel].value;
23862:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
23863:                                 var numoptions = singleserver.options.length;
23864:                                 var needsnew = 1;
23865:                                 for (var k=0; k<numoptions; k++) {
23866:                                     if (singleserver.options[k] == addtarget) {
23867:                                         needsnew = 0;
23868:                                         break;
23869:                                     }
23870:                                 }
23871:                                 if (needsnew == 1) {
23872:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
23873:                                 }
23874:                             }
23875:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
23876:                                 singleserver.options.length = 0;
23877:                                 if ((currVal) && (currVal != removetarget)) {
23878:                                     singleserver.options[0] = new Option("","",false,false);
23879:                                 } else {
23880:                                     singleserver.options[0] = new Option("","",true,true);
23881:                                 }
23882:                                 var idx = 0;
23883:                                 for (var m=0; m<alltargets.length; m++) {
23884:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
23885:                                         idx ++;
23886:                                         if (currVal == alltargets[m]) {
23887:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
23888:                                         } else {
23889:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
23890:                                         }
23891:                                     }
23892:                                 }
23893:                             }
23894:                         }
23895:                     }
23896:                 }
23897:             }
23898:         }
23899:     }
23900:     return;
23901: }
23902: 
23903: // ]]>
23904: </script>
23905: 
23906: END
23907: }
23908: 
23909: sub new_spares_js {
23910:     my @sparestypes = ('primary','default');
23911:     my $types = join("','",@sparestypes);
23912:     my $select = &mt('Select');
23913:     return <<"END";
23914: 
23915: <script type="text/javascript">
23916: // <![CDATA[
23917: 
23918: function updateNewSpares(formname,lonhost) {
23919:     var types = new Array('$types');
23920:     var include = new Array();
23921:     var exclude = new Array();
23922:     for (var i=0; i<types.length; i++) {
23923:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
23924:         for (var j=0; j<spareboxes.length; j++) {
23925:             if (formname.elements[spareboxes[j]].checked) {
23926:                 exclude.push(formname.elements[spareboxes[j]].value);
23927:             } else {
23928:                 include.push(formname.elements[spareboxes[j]].value);
23929:             }
23930:         }
23931:     }
23932:     for (var i=0; i<types.length; i++) {
23933:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
23934:         var selIdx = newSpare.selectedIndex;
23935:         var currnew = newSpare.options[selIdx].value;
23936:         var okSpares = new Array();
23937:         for (var j=0; j<newSpare.options.length; j++) {
23938:             var possible = newSpare.options[j].value;
23939:             if (possible != '') {
23940:                 if (exclude.indexOf(possible) == -1) {
23941:                     okSpares.push(possible);
23942:                 } else {
23943:                     if (currnew == possible) {
23944:                         selIdx = 0;
23945:                     }
23946:                 }
23947:             }
23948:         }
23949:         for (var k=0; k<include.length; k++) {
23950:             if (okSpares.indexOf(include[k]) == -1) {
23951:                 okSpares.push(include[k]);
23952:             }
23953:         }
23954:         okSpares.sort();
23955:         newSpare.options.length = 0;
23956:         if (selIdx == 0) {
23957:             newSpare.options[0] = new Option("$select","",true,true);
23958:         } else {
23959:             newSpare.options[0] = new Option("$select","",false,false);
23960:         }
23961:         for (var m=0; m<okSpares.length; m++) {
23962:             var idx = m+1;
23963:             var selThis = 0;
23964:             if (selIdx != 0) {
23965:                 if (okSpares[m] == currnew) {
23966:                     selThis = 1;
23967:                 }
23968:             }
23969:             if (selThis == 1) {
23970:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
23971:             } else {
23972:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
23973:             }
23974:         }
23975:     }
23976:     return;
23977: }
23978: 
23979: function checkNewSpares(lonhost,type) {
23980:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
23981:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
23982:     if (chosen != '') {
23983:         var othertype;
23984:         var othernewSpare;
23985:         if (type == 'primary') {
23986:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
23987:         }
23988:         if (type == 'default') {
23989:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
23990:         }
23991:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
23992:             othernewSpare.selectedIndex = 0;
23993:         }
23994:     }
23995:     return;
23996: }
23997: 
23998: // ]]>
23999: </script>
24000: 
24001: END
24002: 
24003: }
24004: 
24005: sub common_domprefs_js {
24006:     return <<"END";
24007: 
24008: <script type="text/javascript">
24009: // <![CDATA[
24010: 
24011: function getIndicesByName(formname,item) {
24012:     var group = new Array();
24013:     for (var i=0;i<formname.elements.length;i++) {
24014:         if (formname.elements[i].name == item) {
24015:             group.push(formname.elements[i].id);
24016:         }
24017:     }
24018:     return group;
24019: }
24020: 
24021: // ]]>
24022: </script>
24023: 
24024: END
24025: 
24026: }
24027: 
24028: sub recaptcha_js {
24029:     my %lt = &captcha_phrases();
24030:     return <<"END";
24031: 
24032: <script type="text/javascript">
24033: // <![CDATA[
24034: 
24035: function updateCaptcha(caller,context) {
24036:     var privitem;
24037:     var pubitem;
24038:     var privtext;
24039:     var pubtext;
24040:     var versionitem;
24041:     var versiontext;
24042:     if (document.getElementById(context+'_recaptchapub')) {
24043:         pubitem = document.getElementById(context+'_recaptchapub');
24044:     } else {
24045:         return;
24046:     }
24047:     if (document.getElementById(context+'_recaptchapriv')) {
24048:         privitem = document.getElementById(context+'_recaptchapriv');
24049:     } else {
24050:         return;
24051:     }
24052:     if (document.getElementById(context+'_recaptchapubtxt')) {
24053:         pubtext = document.getElementById(context+'_recaptchapubtxt');
24054:     } else {
24055:         return;
24056:     }
24057:     if (document.getElementById(context+'_recaptchaprivtxt')) {
24058:         privtext = document.getElementById(context+'_recaptchaprivtxt');
24059:     } else {
24060:         return;
24061:     }
24062:     if (document.getElementById(context+'_recaptchaversion')) {
24063:         versionitem = document.getElementById(context+'_recaptchaversion');
24064:     } else {
24065:         return;
24066:     }
24067:     if (document.getElementById(context+'_recaptchavertxt')) {
24068:         versiontext = document.getElementById(context+'_recaptchavertxt');
24069:     } else {
24070:         return;
24071:     }
24072:     if (caller.checked) {
24073:         if (caller.value == 'recaptcha') {
24074:             pubitem.type = 'text';
24075:             privitem.type = 'text';
24076:             pubitem.size = '40';
24077:             privitem.size = '40';
24078:             pubtext.innerHTML = "$lt{'pub'}";
24079:             privtext.innerHTML = "$lt{'priv'}";
24080:             versionitem.type = 'text';
24081:             versionitem.size = '3';
24082:             versiontext.innerHTML = "$lt{'ver'}";
24083:         } else {
24084:             pubitem.type = 'hidden';
24085:             privitem.type = 'hidden';
24086:             versionitem.type = 'hidden';
24087:             pubtext.innerHTML = '';
24088:             privtext.innerHTML = '';
24089:             versiontext.innerHTML = '';
24090:         }
24091:     }
24092:     return;
24093: }
24094: 
24095: // ]]>
24096: </script>
24097: 
24098: END
24099: 
24100: }
24101: 
24102: sub toggle_display_js {
24103:     return <<"END";
24104: 
24105: <script type="text/javascript">
24106: // <![CDATA[
24107: 
24108: function toggleDisplay(domForm,caller) {
24109:     if (document.getElementById(caller)) {
24110:         var divitem = document.getElementById(caller);
24111:         var optionsElement = domForm.coursecredits;
24112:         var checkval = 1;
24113:         var dispval = 'block';
24114:         var selfcreateRegExp = /^cancreate_emailverified/;
24115:         if (caller == 'emailoptions') {
24116:             optionsElement = domForm.cancreate_email;
24117:         }
24118:         if (caller == 'studentsubmission') {
24119:             optionsElement = domForm.postsubmit;
24120:         }
24121:         if (caller == 'cloneinstcode') {
24122:             optionsElement = domForm.canclone;
24123:             checkval = 'instcode';
24124:         }
24125:         if (selfcreateRegExp.test(caller)) {
24126:             optionsElement = domForm.elements[caller];
24127:             checkval = 'other';
24128:             dispval = 'inline'
24129:         }
24130:         if (optionsElement.length) {
24131:             var currval;
24132:             for (var i=0; i<optionsElement.length; i++) {
24133:                 if (optionsElement[i].checked) {
24134:                    currval = optionsElement[i].value;
24135:                 }
24136:             }
24137:             if (currval == checkval) {
24138:                 divitem.style.display = dispval;
24139:             } else {
24140:                 divitem.style.display = 'none';
24141:             }
24142:         }
24143:     }
24144:     return;
24145: }
24146: 
24147: // ]]>
24148: </script>
24149: 
24150: END
24151: 
24152: }
24153: 
24154: sub captcha_phrases {
24155:     return &Apache::lonlocal::texthash (
24156:                  priv => 'Private key',
24157:                  pub  => 'Public key',
24158:                  original  => 'original (CAPTCHA)',
24159:                  recaptcha => 'successor (ReCAPTCHA)',
24160:                  notused   => 'unused',
24161:                  ver => 'ReCAPTCHA version (1 or 2)',
24162:     );
24163: }
24164: 
24165: sub devalidate_remote_domconfs {
24166:     my ($dom,$cachekeys) = @_;
24167:     return unless (ref($cachekeys) eq 'HASH');
24168:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
24169:     my %thismachine;
24170:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
24171:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
24172:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
24173:                       'ipaccess','trust');
24174:     my %cache_by_lonhost;
24175:     if (exists($cachekeys->{'samllanding'})) {
24176:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
24177:             my %landing = %{$cachekeys->{'samllanding'}};
24178:             my %domservers = &Apache::lonnet::get_servers($dom);
24179:             if (keys(%domservers)) {
24180:                 foreach my $server (keys(%domservers)) {
24181:                     my @cached;
24182:                     next if ($thismachine{$server});
24183:                     if ($landing{$server}) {
24184:                         push(@cached,&escape('samllanding').':'.&escape($server));
24185:                     }
24186:                     if (@cached) {
24187:                         $cache_by_lonhost{$server} = \@cached;
24188:                     }
24189:                 }
24190:             }
24191:         }
24192:     }
24193:     if (keys(%servers)) {
24194:         foreach my $server (keys(%servers)) {
24195:             next if ($thismachine{$server});
24196:             my @cached;
24197:             foreach my $name (@posscached) {
24198:                 if ($cachekeys->{$name}) {
24199:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
24200:                         if (ref($cachekeys->{$name}) eq 'HASH') {
24201:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
24202:                                 push(@cached,&escape($name).':'.&escape($key));
24203:                             }
24204:                         }
24205:                     } else {
24206:                         push(@cached,&escape($name).':'.&escape($dom));
24207:                     }
24208:                 }
24209:             }
24210:             if ((exists($cache_by_lonhost{$server})) &&
24211:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
24212:                 push(@cached,@{$cache_by_lonhost{$server}});
24213:             }
24214:             if (@cached) {
24215:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
24216:             }
24217:         }
24218:     }
24219:     return;
24220: }
24221: 
24222: 1;

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