File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.422: download - view: text, annotated - select for diffs
Tue Apr 11 20:35:19 2023 UTC (13 months, 3 weeks ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6754
  - Domain configuration to set defaults for which course types can use:
    (a) External Tools defined/configured in domain, and (b) External Tools
    defined/configured in course.
  - Domain defaults can be overridden by DC for specific course(s) via:
    Main Menu > "View or modify a course or community".

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.422 2023/04/11 20:35:19 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use Apache::courseprefs();
  171: use LONCAPA qw(:DEFAULT :match);
  172: use LONCAPA::Enrollment;
  173: use LONCAPA::lonauthcgi();
  174: use LONCAPA::SSL;
  175: use File::Copy;
  176: use Locale::Language;
  177: use DateTime::TimeZone;
  178: use DateTime::Locale;
  179: use Time::HiRes qw( sleep );
  180: use Net::CIDR;
  181: 
  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','ipaccess'],$dom);
  226:     my %encconfig =
  227:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring','linkprot'],$dom,undef,1);
  228:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  229:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  230:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  231:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  232:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  233:                     $domconfig{'ltitools'}{$id}{'key'} = $encconfig{'ltitools'}{$id}{'key'};
  234:                 }
  235:             }
  236:         }
  237:     }
  238:     if (ref($domconfig{'lti'}) eq 'HASH') {
  239:         if (ref($encconfig{'lti'}) eq 'HASH') {
  240:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  241:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  243:                     foreach my $item ('key','secret') {
  244:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  245:                     }
  246:                 }
  247:             }
  248:         }
  249:     }
  250:     if (ref($domconfig{'ltisec'}) eq 'HASH') {
  251:         if (ref($domconfig{'ltisec'}{'linkprot'}) eq 'HASH') {
  252:             if (ref($encconfig{'linkprot'}) eq 'HASH') {
  253:                 foreach my $id (keys(%{$domconfig{'ltisec'}{'linkprot'}})) {
  254:                     unless ($id =~ /^\d+$/) {
  255:                         delete($domconfig{'ltisec'}{'linkprot'}{$id});
  256:                     }
  257:                     if ((ref($domconfig{'ltisec'}{'linkprot'}{$id}) eq 'HASH') &&
  258:                         (ref($encconfig{'linkprot'}{$id}) eq 'HASH')) {
  259:                         foreach my $item ('key','secret') {
  260:                             $domconfig{'ltisec'}{'linkprot'}{$id}{$item} = $encconfig{'linkprot'}{$id}{$item};
  261:                         }
  262:                     }
  263:                 }
  264:             }
  265:         }
  266:     }
  267:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  268:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  269:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  270:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  271:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  272:                     foreach my $item ('key','secret') {
  273:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  274:                     }
  275:                 }
  276:             }
  277:         }
  278:     }
  279:     my @prefs_order = ('rolecolors','login','ipaccess','defaults','wafproxy','passwords',
  280:                        'quotas','autoenroll','autoupdate','autocreate','directorysrch',
  281:                        'contacts','privacy','usercreation','selfcreation',
  282:                        'usermodification','scantron','requestcourses','requestauthor',
  283:                        'coursecategories','serverstatuses','helpsettings','coursedefaults',
  284:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  285:                        'trust','lti');
  286:     my %existing;
  287:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  288:         %existing = %{$domconfig{'loadbalancing'}};
  289:     }
  290:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  291:         push(@prefs_order,'loadbalancing');
  292:     }
  293:     my %prefs = (
  294:         'rolecolors' =>
  295:                    { text => 'Default color schemes',
  296:                      help => 'Domain_Configuration_Color_Schemes',
  297:                      header => [{col1 => 'Student Settings',
  298:                                  col2 => '',},
  299:                                 {col1 => 'Coordinator Settings',
  300:                                  col2 => '',},
  301:                                 {col1 => 'Author Settings',
  302:                                  col2 => '',},
  303:                                 {col1 => 'Administrator Settings',
  304:                                  col2 => '',}],
  305:                       print => \&print_rolecolors,
  306:                       modify => \&modify_rolecolors,
  307:                     },
  308:         'login' =>
  309:                     { text => 'Log-in page options',
  310:                       help => 'Domain_Configuration_Login_Page',
  311:                       header => [{col1 => 'Log-in Page Items',
  312:                                   col2 => '',},
  313:                                  {col1 => 'Log-in Help',
  314:                                   col2 => 'Value'},
  315:                                  {col1 => 'Custom HTML in document head',
  316:                                   col2 => 'Value'},
  317:                                  {col1 => 'SSO',
  318:                                   col2 => 'Dual login: SSO and non-SSO options'},
  319:                                 ],
  320:                       print => \&print_login,
  321:                       modify => \&modify_login,
  322:                     },
  323:         'defaults' => 
  324:                     { text => 'Default authentication/language/timezone/portal/types',
  325:                       help => 'Domain_Configuration_LangTZAuth',
  326:                       header => [{col1 => 'Setting',
  327:                                   col2 => 'Value'},
  328:                                  {col1 => 'Institutional user types',
  329:                                   col2 => 'Name displayed'},
  330:                                  {col1 => 'Mapping for missing usernames via standard log-in',
  331:                                   col2 => 'Rules in use'}],
  332:                       print => \&print_defaults,
  333:                       modify => \&modify_defaults,
  334:                     },
  335:         'wafproxy' =>
  336:                     { text => 'Web Application Firewall/Reverse Proxy',
  337:                       help => 'Domain_Configuration_WAF_Proxy',
  338:                       header => [{col1 => 'Domain(s)',
  339:                                   col2 => 'Servers and WAF/Reverse Proxy alias(es)',
  340:                                  },
  341:                                  {col1 => 'Domain(s)',
  342:                                   col2 => 'WAF Configuration',}],
  343:                       print => \&print_wafproxy,
  344:                       modify => \&modify_wafproxy,
  345:                     },
  346:         'passwords' =>
  347:                     { text => 'Passwords (Internal authentication)',
  348:                       help => 'Domain_Configuration_Passwords',
  349:                       header => [{col1 => 'Resetting Forgotten Password',
  350:                                   col2 => 'Settings'},
  351:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  352:                                   col2 => 'Settings'},
  353:                                  {col1 => 'Rules for LON-CAPA Passwords',
  354:                                   col2 => 'Settings'},
  355:                                  {col1 => 'Course Owner Changing Student Passwords',
  356:                                   col2 => 'Settings'}],
  357:                       print => \&print_passwords,
  358:                       modify => \&modify_passwords,
  359:                     },
  360:         'quotas' => 
  361:                     { text => 'Blogs, personal pages/timezones, webDAV/quotas, portfolio',
  362:                       help => 'Domain_Configuration_Quotas',
  363:                       header => [{col1 => 'User affiliation',
  364:                                   col2 => 'Available tools',
  365:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  366:                       print => \&print_quotas,
  367:                       modify => \&modify_quotas,
  368:                     },
  369:         'autoenroll' =>
  370:                    { text => 'Auto-enrollment settings',
  371:                      help => 'Domain_Configuration_Auto_Enrollment',
  372:                      header => [{col1 => 'Configuration setting',
  373:                                  col2 => 'Value(s)'}],
  374:                      print => \&print_autoenroll,
  375:                      modify => \&modify_autoenroll,
  376:                    },
  377:         'autoupdate' => 
  378:                    { text => 'Auto-update settings',
  379:                      help => 'Domain_Configuration_Auto_Updates',
  380:                      header => [{col1 => 'Setting',
  381:                                  col2 => 'Value',},
  382:                                 {col1 => 'Setting',
  383:                                  col2 => 'Affiliation'},
  384:                                 {col1 => 'User population',
  385:                                  col2 => 'Updatable user data'}],
  386:                      print => \&print_autoupdate,
  387:                      modify => \&modify_autoupdate,
  388:                   },
  389:         'autocreate' => 
  390:                   { text => 'Auto-course creation settings',
  391:                      help => 'Domain_Configuration_Auto_Creation',
  392:                      header => [{col1 => 'Configuration Setting',
  393:                                  col2 => 'Value',}],
  394:                      print => \&print_autocreate,
  395:                      modify => \&modify_autocreate,
  396:                   },
  397:         'directorysrch' => 
  398:                   { text => 'Directory searches',
  399:                     help => 'Domain_Configuration_InstDirectory_Search',
  400:                     header => [{col1 => 'Institutional Directory Setting',
  401:                                 col2 => 'Value',},
  402:                                {col1 => 'LON-CAPA Directory Setting',
  403:                                 col2 => 'Value',}],
  404:                     print => \&print_directorysrch,
  405:                     modify => \&modify_directorysrch,
  406:                   },
  407:         'contacts' =>
  408:                   { text => 'E-mail addresses and helpform',
  409:                     help => 'Domain_Configuration_Contact_Info',
  410:                     header => [{col1 => 'Default e-mail addresses',
  411:                                 col2 => 'Value',},
  412:                                {col1 => 'Recipient(s) for notifications',
  413:                                 col2 => 'Value',},
  414:                                {col1 => 'Nightly status check e-mail',
  415:                                 col2 => 'Settings',},
  416:                                {col1 => 'Ask helpdesk form settings',
  417:                                 col2 => 'Value',},],
  418:                     print => \&print_contacts,
  419:                     modify => \&modify_contacts,
  420:                   },
  421:         'usercreation' => 
  422:                   { text => 'User creation',
  423:                     help => 'Domain_Configuration_User_Creation',
  424:                     header => [{col1 => 'Format rule type',
  425:                                 col2 => 'Format rules in force'},
  426:                                {col1 => 'User account creation',
  427:                                 col2 => 'Usernames which may be created',},
  428:                                {col1 => 'Context',
  429:                                 col2 => 'Assignable authentication types'}],
  430:                     print => \&print_usercreation,
  431:                     modify => \&modify_usercreation,
  432:                   },
  433:         'selfcreation' => 
  434:                   { text => 'Users self-creating accounts',
  435:                     help => 'Domain_Configuration_Self_Creation', 
  436:                     header => [{col1 => 'Self-creation with institutional username',
  437:                                 col2 => 'Enabled?'},
  438:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  439:                                 col2 => 'Information user can enter'},
  440:                                {col1 => 'Self-creation with e-mail verification',
  441:                                 col2 => 'Settings'}],
  442:                     print => \&print_selfcreation,
  443:                     modify => \&modify_selfcreation,
  444:                   },
  445:         'usermodification' =>
  446:                   { text => 'User modification',
  447:                     help => 'Domain_Configuration_User_Modification',
  448:                     header => [{col1 => 'Target user has role',
  449:                                 col2 => 'User information updatable in author context'},
  450:                                {col1 => 'Target user has role',
  451:                                 col2 => 'User information updatable in course context'}],
  452:                     print => \&print_usermodification,
  453:                     modify => \&modify_usermodification,
  454:                   },
  455:         'scantron' =>
  456:                   { text => 'Bubblesheet format',
  457:                     help => 'Domain_Configuration_Scantron_Format',
  458:                     header => [ {col1 => 'Bubblesheet format file',
  459:                                  col2 => ''},
  460:                                 {col1 => 'Bubblesheet data upload formats',
  461:                                  col2 => 'Settings'}],
  462:                     print => \&print_scantron,
  463:                     modify => \&modify_scantron,
  464:                   },
  465:         'requestcourses' => 
  466:                  {text => 'Request creation of courses',
  467:                   help => 'Domain_Configuration_Request_Courses',
  468:                   header => [{col1 => 'User affiliation',
  469:                               col2 => 'Availability/Processing of requests',},
  470:                              {col1 => 'Setting',
  471:                               col2 => 'Value'},
  472:                              {col1 => 'Available textbooks',
  473:                               col2 => ''},
  474:                              {col1 => 'Available templates',
  475:                               col2 => ''},
  476:                              {col1 => 'Validation (not official courses)',
  477:                               col2 => 'Value'},],
  478:                   print => \&print_quotas,
  479:                   modify => \&modify_quotas,
  480:                  },
  481:         'requestauthor' =>
  482:                  {text => 'Request Authoring Space',
  483:                   help => 'Domain_Configuration_Request_Author',
  484:                   header => [{col1 => 'User affiliation',
  485:                               col2 => 'Availability/Processing of requests',},
  486:                              {col1 => 'Setting',
  487:                               col2 => 'Value'}],
  488:                   print => \&print_quotas,
  489:                   modify => \&modify_quotas,
  490:                  },
  491:         'coursecategories' =>
  492:                   { text => 'Cataloging of courses/communities',
  493:                     help => 'Domain_Configuration_Cataloging_Courses',
  494:                     header => [{col1 => 'Catalog type/availability',
  495:                                 col2 => '',},
  496:                                {col1 => 'Category settings for standard catalog',
  497:                                 col2 => '',},
  498:                                {col1 => 'Categories',
  499:                                 col2 => '',
  500:                                }],
  501:                     print => \&print_coursecategories,
  502:                     modify => \&modify_coursecategories,
  503:                   },
  504:         'serverstatuses' =>
  505:                  {text   => 'Access to server status pages',
  506:                   help   => 'Domain_Configuration_Server_Status',
  507:                   header => [{col1 => 'Status Page',
  508:                               col2 => 'Other named users',
  509:                               col3 => 'Specific IPs',
  510:                             }],
  511:                   print => \&print_serverstatuses,
  512:                   modify => \&modify_serverstatuses,
  513:                  },
  514:         'helpsettings' =>
  515:                  {text   => 'Support settings',
  516:                   help   => 'Domain_Configuration_Help_Settings',
  517:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  518:                               col2 => 'Value'},
  519:                              {col1 => 'Helpdesk Roles',
  520:                               col2 => 'Settings'},],
  521:                   print  => \&print_helpsettings,
  522:                   modify => \&modify_helpsettings,
  523:                  },
  524:         'coursedefaults' => 
  525:                  {text => 'Course/Community defaults',
  526:                   help => 'Domain_Configuration_Course_Defaults',
  527:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  528:                               col2 => 'Value',},
  529:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  530:                               col2 => 'Value',},],
  531:                   print => \&print_coursedefaults,
  532:                   modify => \&modify_coursedefaults,
  533:                  },
  534:         'selfenrollment' => 
  535:                  {text   => 'Self-enrollment in Course/Community',
  536:                   help   => 'Domain_Configuration_Selfenrollment',
  537:                   header => [{col1 => 'Configuration Rights',
  538:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  539:                              {col1 => 'Defaults',
  540:                               col2 => 'Value'},
  541:                              {col1 => 'Self-enrollment validation (optional)',
  542:                               col2 => 'Value'},],
  543:                   print => \&print_selfenrollment,
  544:                   modify => \&modify_selfenrollment,
  545:                  },
  546:         'privacy' => 
  547:                  {text   => 'Availability of User Information',
  548:                   help   => 'Domain_Configuration_User_Privacy',
  549:                   header => [{col1 => 'Role assigned in different domain',
  550:                               col2 => 'Approval options'},
  551:                              {col1 => 'Role assigned in different domain to user of type',
  552:                               col2 => 'User information available in that domain'},
  553:                              {col1 => "Role assigned in user's domain",
  554:                               col2 => 'Information viewable by privileged user'},
  555:                              {col1 => "Role assigned in user's domain",
  556:                               col2 => 'Information viewable by unprivileged user'}],
  557:                   print => \&print_privacy,
  558:                   modify => \&modify_privacy,
  559:                  },
  560:         'usersessions' =>
  561:                  {text  => 'User session hosting/offloading',
  562:                   help  => 'Domain_Configuration_User_Sessions',
  563:                   header => [{col1 => 'Domain server',
  564:                               col2 => 'Servers to offload sessions to when busy'},
  565:                              {col1 => 'Hosting of users from other domains',
  566:                               col2 => 'Rules'},
  567:                              {col1 => "Hosting domain's own users elsewhere",
  568:                               col2 => 'Rules'}],
  569:                   print => \&print_usersessions,
  570:                   modify => \&modify_usersessions,
  571:                  },
  572:         'loadbalancing' =>
  573:                  {text  => 'Dedicated Load Balancer(s)',
  574:                   help  => 'Domain_Configuration_Load_Balancing',
  575:                   header => [{col1 => 'Balancers',
  576:                               col2 => 'Default destinations',
  577:                               col3 => 'User affiliation',
  578:                               col4 => 'Overrides'},
  579:                             ],
  580:                   print => \&print_loadbalancing,
  581:                   modify => \&modify_loadbalancing,
  582:                  },
  583:         'ltitools' => 
  584:                  {text => 'External Tools (LTI)',
  585:                   help => 'Domain_Configuration_LTI_Tools',
  586:                   header => [{col1 => 'Encryption of shared secrets',
  587:                               col2 => 'Settings'},
  588:                              {col1 => 'Rules for shared secrets',
  589:                               col2 => 'Settings'},
  590:                              {col1 => 'Providers',
  591:                               col2 => 'Settings',}],
  592:                   print => \&print_ltitools,
  593:                   modify => \&modify_ltitools,
  594:                  },
  595:         'proctoring' =>
  596:                  {text => 'Remote Proctoring Integration',
  597:                   help => 'Domain_Configuration_Proctoring',
  598:                   header => [{col1 => 'Name',
  599:                               col2 => 'Configuration'}],
  600:                   print => \&print_proctoring,
  601:                   modify => \&modify_proctoring,
  602:                  },
  603:         'ssl' =>
  604:                  {text  => 'LON-CAPA Network (SSL)',
  605:                   help  => 'Domain_Configuration_Network_SSL',
  606:                   header => [{col1 => 'Server',
  607:                               col2 => 'Certificate Status'},
  608:                              {col1 => 'Connections to other servers',
  609:                               col2 => 'Rules'},
  610:                              {col1 => 'Connections from other servers',
  611:                               col2 => 'Rules'},
  612:                              {col1 => "Replicating domain's published content",
  613:                               col2 => 'Rules'}],
  614:                   print => \&print_ssl,
  615:                   modify => \&modify_ssl,
  616:                  },
  617:         'trust' =>
  618:                  {text   => 'Trust Settings',
  619:                   help   => 'Domain_Configuration_Trust',
  620:                   header => [{col1 => "Access to this domain's content by others",
  621:                               col2 => 'Rules'},
  622:                              {col1 => "Access to other domain's content by this domain",
  623:                               col2 => 'Rules'},
  624:                              {col1 => "Enrollment in this domain's courses by others",
  625:                               col2 => 'Rules',},
  626:                              {col1 => "Co-author roles in this domain for others",
  627:                               col2 => 'Rules',},
  628:                              {col1 => "Co-author roles for this domain's users elsewhere",
  629:                               col2 => 'Rules',},
  630:                              {col1 => "Domain roles in this domain assignable to others",
  631:                               col2 => 'Rules'},
  632:                              {col1 => "Course catalog for this domain displayed elsewhere",
  633:                               col2 => 'Rules'},
  634:                              {col1 => "Requests for creation of courses in this domain by others",
  635:                               col2 => 'Rules'},
  636:                              {col1 => "Users in other domains can send messages to this domain",
  637:                               col2 => 'Rules'},],
  638:                   print => \&print_trust,
  639:                   modify => \&modify_trust,
  640:                  },
  641:           'lti' =>
  642:                  {text => 'LTI Link Protection and LTI Consumers',
  643:                   help => 'Domain_Configuration_LTI_Provider',
  644:                   header => [{col1 => 'Encryption of shared secrets',
  645:                               col2 => 'Settings'},
  646:                              {col1 => 'Rules for shared secrets', 
  647:                               col2 => 'Settings'},
  648:                              {col1 => 'Link Protectors',
  649:                               col2 => 'Settings'},
  650:                              {col1 => 'Consumers',
  651:                               col2 => 'Settings'},],
  652:                   print => \&print_lti,
  653:                   modify => \&modify_lti,
  654:                  },
  655:            'ipaccess' =>
  656:                        {text => 'IP-based access control',
  657:                         help => 'Domain_Configuration_IP_Access',
  658:                         header => [{col1 => 'Setting',
  659:                                     col2 => 'Value'},],
  660:                         print  => \&print_ipaccess,
  661:                         modify => \&modify_ipaccess,
  662:                        },
  663:     );
  664:     if (keys(%servers) > 1) {
  665:         $prefs{'login'}  = { text   => 'Log-in page options',
  666:                              help   => 'Domain_Configuration_Login_Page',
  667:                             header => [{col1 => 'Log-in Service',
  668:                                         col2 => 'Server Setting',},
  669:                                        {col1 => 'Log-in Page Items',
  670:                                         col2 => 'Settings'},
  671:                                        {col1 => 'Log-in Help',
  672:                                         col2 => 'Value'},
  673:                                        {col1 => 'Custom HTML in document head',
  674:                                         col2 => 'Value'},
  675:                                        {col1 => 'SSO',
  676:                                         col2 => 'Dual login: SSO and non-SSO options'},
  677:                                       ],
  678:                             print => \&print_login,
  679:                             modify => \&modify_login,
  680:                            };
  681:     }
  682: 
  683:     my @roles = ('student','coordinator','author','admin');
  684:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  685:     &Apache::lonhtmlcommon::add_breadcrumb
  686:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  687:       text=>"Settings to display/modify"});
  688:     my $confname = $dom.'-domainconfig';
  689: 
  690:     if ($phase eq 'process') {
  691:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  692:                                                               \%prefs,\%domconfig,$confname,\@roles);
  693:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  694:             $r->rflush();
  695:             &devalidate_remote_domconfs($dom,$result);
  696:         }
  697:     } elsif ($phase eq 'display') {
  698:         my $js = &recaptcha_js().
  699:                  &toggle_display_js();
  700:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  701:             my ($othertitle,$usertypes,$types) =
  702:                 &Apache::loncommon::sorted_inst_types($dom);
  703:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  704:                                           $domconfig{'loadbalancing'}).
  705:                    &new_spares_js().
  706:                    &common_domprefs_js().
  707:                    &Apache::loncommon::javascript_array_indexof();
  708:         }
  709:         if (grep(/^requestcourses$/,@actions)) {
  710:             my $javascript_validations;
  711:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  712:             $js .= <<END;
  713: <script type="text/javascript">
  714: $javascript_validations
  715: </script>
  716: $coursebrowserjs
  717: END
  718:         } elsif (grep(/^ipaccess$/,@actions)) {
  719:             $js .= &Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'});
  720:         }
  721:         if (grep(/^selfcreation$/,@actions)) {
  722:             $js .= &selfcreate_javascript();
  723:         }
  724:         if (grep(/^contacts$/,@actions)) {
  725:             $js .= &contacts_javascript();
  726:         }
  727:         if (grep(/^scantron$/,@actions)) {
  728:             $js .= &scantron_javascript();
  729:         }
  730:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  731:     } else {
  732: # check if domconfig user exists for the domain.
  733:         my $servadm = $r->dir_config('lonAdmEMail');
  734:         my ($configuserok,$author_ok,$switchserver) =
  735:             &config_check($dom,$confname,$servadm);
  736:         unless ($configuserok eq 'ok') {
  737:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  738:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  739:                           $confname).
  740:                       '<br />'
  741:             );
  742:             if ($switchserver) {
  743:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  744:                           '<br />'.
  745:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  746:                           '<br />'.
  747:                           &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).
  748:                           '<br />'.
  749:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  750:                 );
  751:             } else {
  752:                 $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.').
  753:                           '<br />'.
  754:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  755:                 );
  756:             }
  757:             $r->print(&Apache::loncommon::end_page());
  758:             return OK;
  759:         }
  760:         if (keys(%domconfig) == 0) {
  761:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  762:             my @ids=&Apache::lonnet::current_machine_ids();
  763:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  764:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  765:                 my @loginimages = ('img','logo','domlogo','login');
  766:                 my $custom_img_count = 0;
  767:                 foreach my $img (@loginimages) {
  768:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  769:                         $custom_img_count ++;
  770:                     }
  771:                 }
  772:                 foreach my $role (@roles) {
  773:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  774:                         $custom_img_count ++;
  775:                     }
  776:                 }
  777:                 if ($custom_img_count > 0) {
  778:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  779:                     my $switch_server = &check_switchserver($dom,$confname);
  780:                     $r->print(
  781:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  782:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  783:     &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 />'.
  784:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  785:                     if ($switch_server) {
  786:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  787:                     }
  788:                     $r->print(&Apache::loncommon::end_page());
  789:                     return OK;
  790:                 }
  791:             }
  792:         }
  793:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  794:     }
  795:     return OK;
  796: }
  797: 
  798: sub process_changes {
  799:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  800:     my %domconfig;
  801:     if (ref($values) eq 'HASH') {
  802:         %domconfig = %{$values};
  803:     }
  804:     my $output;
  805:     if ($action eq 'login') {
  806:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  807:     } elsif ($action eq 'rolecolors') {
  808:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  809:                                      $lastactref,%domconfig);
  810:     } elsif ($action eq 'quotas') {
  811:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  812:     } elsif ($action eq 'autoenroll') {
  813:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  814:     } elsif ($action eq 'autoupdate') {
  815:         $output = &modify_autoupdate($dom,%domconfig);
  816:     } elsif ($action eq 'autocreate') {
  817:         $output = &modify_autocreate($dom,%domconfig);
  818:     } elsif ($action eq 'directorysrch') {
  819:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  820:     } elsif ($action eq 'usercreation') {
  821:         $output = &modify_usercreation($dom,%domconfig);
  822:     } elsif ($action eq 'selfcreation') {
  823:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  824:     } elsif ($action eq 'usermodification') {
  825:         $output = &modify_usermodification($dom,%domconfig);
  826:     } elsif ($action eq 'contacts') {
  827:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  828:     } elsif ($action eq 'defaults') {
  829:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  830:     } elsif ($action eq 'scantron') {
  831:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  832:     } elsif ($action eq 'coursecategories') {
  833:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  834:     } elsif ($action eq 'serverstatuses') {
  835:         $output = &modify_serverstatuses($dom,%domconfig);
  836:     } elsif ($action eq 'requestcourses') {
  837:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  838:     } elsif ($action eq 'requestauthor') {
  839:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  840:     } elsif ($action eq 'helpsettings') {
  841:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  842:     } elsif ($action eq 'coursedefaults') {
  843:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  844:     } elsif ($action eq 'selfenrollment') {
  845:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  846:     } elsif ($action eq 'usersessions') {
  847:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  848:     } elsif ($action eq 'loadbalancing') {
  849:         $output = &modify_loadbalancing($dom,%domconfig);
  850:     } elsif ($action eq 'ltitools') {
  851:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  852:     } elsif ($action eq 'proctoring') {
  853:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  854:     } elsif ($action eq 'ssl') {
  855:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  856:     } elsif ($action eq 'trust') {
  857:         $output = &modify_trust($dom,$lastactref,%domconfig);
  858:     } elsif ($action eq 'lti') {
  859:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  860:     } elsif ($action eq 'privacy') {
  861:         $output = &modify_privacy($dom,%domconfig);
  862:     } elsif ($action eq 'passwords') {
  863:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  864:     } elsif ($action eq 'wafproxy') {
  865:         $output = &modify_wafproxy($dom,$action,$lastactref,%domconfig);
  866:     } elsif ($action eq 'ipaccess') {
  867:         $output = &modify_ipaccess($dom,$lastactref,%domconfig);
  868:     }
  869:     return $output;
  870: }
  871: 
  872: sub print_config_box {
  873:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  874:     my $rowtotal = 0;
  875:     my $output;
  876:     if ($action eq 'coursecategories') {
  877:         $output = &coursecategories_javascript($settings);
  878:     } elsif ($action eq 'defaults') {
  879:         $output = &defaults_javascript($settings); 
  880:     } elsif ($action eq 'passwords') {
  881:         $output = &passwords_javascript($action);
  882:     } elsif ($action eq 'helpsettings') {
  883:         my (%privs,%levelscurrent);
  884:         my %full=();
  885:         my %levels=(
  886:                      course => {},
  887:                      domain => {},
  888:                      system => {},
  889:                    );
  890:         my $context = 'domain';
  891:         my $crstype = 'Course';
  892:         my $formname = 'display';
  893:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  894:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  895:         $output =
  896:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  897:                                                       \@templateroles);
  898:     } elsif ($action eq 'ltitools') {
  899:         $output .= &Apache::lonconfigsettings::ltitools_javascript($settings);
  900:     } elsif ($action eq 'lti') {
  901:         $output .= &passwords_javascript('ltisecrets')."\n".
  902:                    &lti_javascript($dom,$settings);
  903:     } elsif ($action eq 'proctoring') {
  904:         $output .= &proctoring_javascript($settings);
  905:     } elsif ($action eq 'wafproxy') {
  906:         $output .= &wafproxy_javascript($dom);
  907:     } elsif ($action eq 'autoupdate') {
  908:         $output .= &autoupdate_javascript();
  909:     } elsif ($action eq 'autoenroll') {
  910:         $output .= &autoenroll_javascript();
  911:     } elsif ($action eq 'login') {
  912:         $output .= &saml_javascript();
  913:     } elsif ($action eq 'ipaccess') {
  914:         $output .= &ipaccess_javascript($settings);
  915:     }
  916:     $output .=
  917:          '<table class="LC_nested_outer">
  918:           <tr>
  919:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  920:            &mt($item->{text}).'&nbsp;'.
  921:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  922:           '</tr>';
  923:     $rowtotal ++;
  924:     my $numheaders = 1;
  925:     if (ref($item->{'header'}) eq 'ARRAY') {
  926:         $numheaders = scalar(@{$item->{'header'}});
  927:     }
  928:     if ($numheaders > 1) {
  929:         my $colspan = '';
  930:         my $rightcolspan = '';
  931:         my $leftnobr = '';
  932:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  933:             ($action eq 'directorysrch') ||
  934:             (($action eq 'login') && ($numheaders < 5))) {
  935:             $colspan = ' colspan="2"';
  936:         }
  937:         if ($action eq 'usersessions') {
  938:             $rightcolspan = ' colspan="3"'; 
  939:         }
  940:         if ($action eq 'passwords') {
  941:             $leftnobr = ' LC_nobreak';
  942:         }
  943:         $output .= '
  944:           <tr>
  945:            <td>
  946:             <table class="LC_nested">
  947:              <tr class="LC_info_row">
  948:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  949:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  950:              </tr>';
  951:         $rowtotal ++;
  952:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  953:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  954:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  955:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  956:             ($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'wafproxy') ||
  957:             ($action eq 'lti') || ($action eq 'ltitools')) {
  958:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  959:         } elsif ($action eq 'passwords') {
  960:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  961:         } elsif ($action eq 'coursecategories') {
  962:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  963:         } elsif ($action eq 'scantron') {
  964:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  965:         } elsif ($action eq 'login') {
  966:             if ($numheaders == 5) {
  967:                 $colspan = ' colspan="2"';
  968:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  969:             } else {
  970:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  971:             }
  972:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  973:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  974:         } elsif ($action eq 'rolecolors') {
  975:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  976:         }
  977:         $output .= '
  978:            </table>
  979:           </td>
  980:          </tr>
  981:          <tr>
  982:            <td>
  983:             <table class="LC_nested">
  984:              <tr class="LC_info_row">
  985:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  986:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  987:              </tr>';
  988:             $rowtotal ++;
  989:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  990:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  991:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  992:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults') ||
  993:             ($action eq 'privacy') || ($action eq 'passwords') || ($action eq 'lti') ||
  994:             ($action eq 'ltitools')) {
  995:             if ($action eq 'coursecategories') {
  996:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  997:                 $colspan = ' colspan="2"';
  998:             } elsif ($action eq 'trust') {
  999:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
 1000:             } elsif ($action eq 'passwords') {
 1001:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
 1002:             } else {
 1003:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
 1004:             }
 1005:             if ($action eq 'trust') {
 1006:                 $output .= '
 1007:             </table>
 1008:           </td>
 1009:          </tr>';
 1010:                 my @trusthdrs = qw(2 3 4 5 6 7);
 1011:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
 1012:                 for (my $i=0; $i<@trusthdrs; $i++) {
 1013:                     $output .= '
 1014:          <tr>
 1015:            <td>
 1016:             <table class="LC_nested">
 1017:              <tr class="LC_info_row">
 1018:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
 1019:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
 1020:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
 1021:             </table>
 1022:           </td>
 1023:          </tr>';
 1024:                 }
 1025:                 $output .= '
 1026:          <tr>
 1027:            <td>
 1028:             <table class="LC_nested">
 1029:              <tr class="LC_info_row">
 1030:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
 1031:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
 1032:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1033:             } else {
 1034:                 $output .= '
 1035:            </table>
 1036:           </td>
 1037:          </tr>
 1038:          <tr>
 1039:            <td>
 1040:             <table class="LC_nested">
 1041:              <tr class="LC_info_row">
 1042:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1043:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1044:              </tr>'."\n";
 1045:                 if ($action eq 'coursecategories') {
 1046:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
 1047:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || 
 1048:                          ($action eq 'passwords') || ($action eq 'lti')) {
 1049:                     if ($action eq 'passwords') {
 1050:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
 1051:                     } else {
 1052:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
 1053:                     }
 1054:                     $output .= '
 1055:              </tr>
 1056:             </table>
 1057:            </td>
 1058:           </tr>
 1059:           <tr>
 1060:            <td>
 1061:             <table class="LC_nested">
 1062:              <tr class="LC_info_row">
 1063:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1064:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
 1065:                     if ($action eq 'passwords') {
 1066:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
 1067:                     } else {
 1068:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1069:                     }
 1070:                     $output .= '
 1071:             </table>
 1072:           </td>
 1073:          </tr>
 1074:          <tr>';
 1075:                 } else {
 1076:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1077:                 }
 1078:             }
 1079:             $rowtotal ++;
 1080:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1081:                  ($action eq 'directorysrch') || ($action eq 'helpsettings') ||
 1082:                  ($action eq 'wafproxy')) {
 1083:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1084:         } elsif ($action eq 'scantron') {
 1085:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1086:         } elsif ($action eq 'ssl') {
 1087:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1088:             </table>
 1089:           </td>
 1090:          </tr>
 1091:          <tr>
 1092:            <td>
 1093:             <table class="LC_nested">
 1094:              <tr class="LC_info_row">
 1095:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1096:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1097:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1098:             </table>
 1099:           </td>
 1100:          </tr>
 1101:          <tr>
 1102:            <td>
 1103:             <table class="LC_nested">
 1104:              <tr class="LC_info_row">
 1105:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1106:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1107:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1108:         } elsif ($action eq 'login') {
 1109:             if ($numheaders == 5) {
 1110:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1111:            </table>
 1112:           </td>
 1113:          </tr>
 1114:          <tr>
 1115:            <td>
 1116:             <table class="LC_nested">
 1117:              <tr class="LC_info_row">
 1118:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1119:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1120:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1121:                 $rowtotal ++;
 1122:             } else {
 1123:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1124:             }
 1125:             $output .= '
 1126:            </table>
 1127:           </td>
 1128:          </tr>
 1129:          <tr>
 1130:            <td>
 1131:             <table class="LC_nested">
 1132:              <tr class="LC_info_row">';
 1133:             if ($numheaders == 5) {
 1134:                 $output .= '
 1135:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1136:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1137:              </tr>';
 1138:             } else {
 1139:                 $output .= '
 1140:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1141:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1142:              </tr>';
 1143:             }
 1144:             $rowtotal ++;
 1145:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal).'
 1146:            </table>
 1147:           </td>
 1148:          </tr>
 1149:          <tr>
 1150:            <td>
 1151:             <table class="LC_nested">
 1152:              <tr class="LC_info_row">';
 1153:             if ($numheaders == 5) {
 1154:                 $output .= '
 1155:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1156:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1157:              </tr>';
 1158:             } else {
 1159:                 $output .= '
 1160:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1161:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1162:              </tr>';
 1163:             }
 1164:             $rowtotal ++;
 1165:             $output .= &print_login('saml',$dom,$confname,$phase,$settings,\$rowtotal);
 1166:         } elsif ($action eq 'requestcourses') {
 1167:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1168:             $rowtotal ++;
 1169:             $output .= &print_studentcode($settings,\$rowtotal).'
 1170:            </table>
 1171:           </td>
 1172:          </tr>
 1173:          <tr>
 1174:            <td>
 1175:             <table class="LC_nested">
 1176:              <tr class="LC_info_row">
 1177:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1178:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1179:                        &textbookcourses_javascript($settings).
 1180:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1181:             </table>
 1182:            </td>
 1183:           </tr>
 1184:          <tr>
 1185:            <td>
 1186:             <table class="LC_nested">
 1187:              <tr class="LC_info_row">
 1188:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1189:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1190:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1191:             </table>
 1192:            </td>
 1193:           </tr>
 1194:           <tr>
 1195:            <td>
 1196:             <table class="LC_nested">
 1197:              <tr class="LC_info_row">
 1198:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1199:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1200:              </tr>'.
 1201:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1202:         } elsif ($action eq 'requestauthor') {
 1203:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1204:             $rowtotal ++;
 1205:         } elsif ($action eq 'rolecolors') {
 1206:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1207:            </table>
 1208:           </td>
 1209:          </tr>
 1210:          <tr>
 1211:            <td>
 1212:             <table class="LC_nested">
 1213:              <tr class="LC_info_row">
 1214:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1215:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1216:               <td class="LC_right_item" style="vertical-align: top">'.
 1217:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1218:              </tr>'.
 1219:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1220:            </table>
 1221:           </td>
 1222:          </tr>
 1223:          <tr>
 1224:            <td>
 1225:             <table class="LC_nested">
 1226:              <tr class="LC_info_row">
 1227:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1228:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1229:              </tr>'.
 1230:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1231:             $rowtotal += 2;
 1232:         }
 1233:     } else {
 1234:         $output .= '
 1235:           <tr>
 1236:            <td>
 1237:             <table class="LC_nested">
 1238:              <tr class="LC_info_row">';
 1239:         if ($action eq 'login') {
 1240:             $output .= '  
 1241:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1242:         } elsif ($action eq 'serverstatuses') {
 1243:             $output .= '
 1244:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1245:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1246: 
 1247:         } else {
 1248:             $output .= '
 1249:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1250:         }
 1251:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1252:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1253:                        &mt($item->{'header'}->[0]->{'col2'});
 1254:             if ($action eq 'serverstatuses') {
 1255:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1256:             } 
 1257:         } else {
 1258:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1259:                        &mt($item->{'header'}->[0]->{'col2'});
 1260:         }
 1261:         $output .= '</td>';
 1262:         if ($item->{'header'}->[0]->{'col3'}) {
 1263:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1264:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1265:                             &mt($item->{'header'}->[0]->{'col3'});
 1266:             } else {
 1267:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1268:                            &mt($item->{'header'}->[0]->{'col3'});
 1269:             }
 1270:             if ($action eq 'serverstatuses') {
 1271:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1272:             }
 1273:             $output .= '</td>';
 1274:         }
 1275:         if ($item->{'header'}->[0]->{'col4'}) {
 1276:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1277:                        &mt($item->{'header'}->[0]->{'col4'});
 1278:         }
 1279:         $output .= '</tr>';
 1280:         $rowtotal ++;
 1281:         if ($action eq 'quotas') {
 1282:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1283:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1284:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1285:                  ($action eq 'proctoring') || ($action eq 'ipaccess')) {
 1286:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1287:         }
 1288:     }
 1289:     $output .= '
 1290:    </table>
 1291:   </td>
 1292:  </tr>
 1293: </table><br />';
 1294:     return ($output,$rowtotal);
 1295: }
 1296: 
 1297: sub print_login {
 1298:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1299:     my ($css_class,$datatable,$switchserver,%lt);
 1300:     my %choices = &login_choices();
 1301:     if (($caller eq 'help') || ($caller eq 'headtag') || ($caller eq 'saml')) {
 1302:         %lt = &login_file_options();
 1303:         $switchserver = &check_switchserver($dom,$confname);
 1304:     }
 1305:     if ($caller eq 'service') {
 1306:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1307:         my $choice = $choices{'disallowlogin'};
 1308:         $css_class = ' class="LC_odd_row"';
 1309:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1310:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1311:                       '<th>'.$choices{'server'}.'</th>'.
 1312:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1313:                       '<th>'.$choices{'custompath'}.'</th>'.
 1314:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1315:         my %disallowed;
 1316:         if (ref($settings) eq 'HASH') {
 1317:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1318:                %disallowed = %{$settings->{'loginvia'}};
 1319:             }
 1320:         }
 1321:         foreach my $lonhost (sort(keys(%servers))) {
 1322:             my $direct = 'selected="selected"';
 1323:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1324:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1325:                     $direct = '';
 1326:                 }
 1327:             }
 1328:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1329:                           '<td><select name="'.$lonhost.'_server">'.
 1330:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1331:                           '</option>';
 1332:             foreach my $hostid (sort(keys(%servers))) {
 1333:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1334:                 my $selected = '';
 1335:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1336:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1337:                         $selected = 'selected="selected"';
 1338:                     }
 1339:                 }
 1340:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1341:                               $servers{$hostid}.'</option>';
 1342:             }
 1343:             $datatable .= '</select></td>'.
 1344:                           '<td><select name="'.$lonhost.'_serverpath">';
 1345:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1346:                 my $pathname = $path;
 1347:                 if ($path eq 'custom') {
 1348:                     $pathname = &mt('Custom Path').' ->';
 1349:                 }
 1350:                 my $selected = '';
 1351:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1352:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1353:                         $selected = 'selected="selected"';
 1354:                     }
 1355:                 } elsif ($path eq '') {
 1356:                     $selected = 'selected="selected"';
 1357:                 }
 1358:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1359:             }
 1360:             $datatable .= '</select></td>';
 1361:             my ($custom,$exempt);
 1362:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1363:                 $custom = $disallowed{$lonhost}{'custompath'};
 1364:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1365:             }
 1366:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1367:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1368:                           '</tr>';
 1369:         }
 1370:         $datatable .= '</table></td></tr>';
 1371:         return $datatable;
 1372:     } elsif ($caller eq 'page') {
 1373:         my %defaultchecked = ( 
 1374:                                'coursecatalog' => 'on',
 1375:                                'helpdesk'      => 'on',
 1376:                                'adminmail'     => 'off',
 1377:                                'newuser'       => 'off',
 1378:                              );
 1379:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1380:         my (%checkedon,%checkedoff);
 1381:         foreach my $item (@toggles) {
 1382:             if ($defaultchecked{$item} eq 'on') { 
 1383:                 $checkedon{$item} = ' checked="checked" ';
 1384:                 $checkedoff{$item} = ' ';
 1385:             } elsif ($defaultchecked{$item} eq 'off') {
 1386:                 $checkedoff{$item} = ' checked="checked" ';
 1387:                 $checkedon{$item} = ' ';
 1388:             }
 1389:         }
 1390:         my @images = ('img','logo','domlogo','login');
 1391:         my @alttext = ('img','logo','domlogo');
 1392:         my @logintext = ('textcol','bgcol');
 1393:         my @bgs = ('pgbg','mainbg','sidebg');
 1394:         my @links = ('link','alink','vlink');
 1395:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1396:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1397:         my (%is_custom,%designs);
 1398:         my %defaults = (
 1399:                        font => $defaultdesign{'login.font'},
 1400:                        );
 1401:         foreach my $item (@images) {
 1402:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1403:             $defaults{'showlogo'}{$item} = 1;
 1404:         }
 1405:         foreach my $item (@bgs) {
 1406:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1407:         }
 1408:         foreach my $item (@logintext) {
 1409:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1410:         }
 1411:         foreach my $item (@links) {
 1412:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1413:         }
 1414:         if (ref($settings) eq 'HASH') {
 1415:             foreach my $item (@toggles) {
 1416:                 if ($settings->{$item} eq '1') {
 1417:                     $checkedon{$item} =  ' checked="checked" ';
 1418:                     $checkedoff{$item} = ' ';
 1419:                 } elsif ($settings->{$item} eq '0') {
 1420:                     $checkedoff{$item} =  ' checked="checked" ';
 1421:                     $checkedon{$item} = ' ';
 1422:                 }
 1423:             }
 1424:             foreach my $item (@images) {
 1425:                 if (defined($settings->{$item})) {
 1426:                     $designs{$item} = $settings->{$item};
 1427:                     $is_custom{$item} = 1;
 1428:                 }
 1429:                 if (defined($settings->{'showlogo'}{$item})) {
 1430:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1431:                 }
 1432:             }
 1433:             foreach my $item (@alttext) {
 1434:                 if (ref($settings->{'alttext'}) eq 'HASH') {
 1435:                     if ($settings->{'alttext'}->{$item} ne '') {
 1436:                         $designs{'alttext'}{$item} = $settings->{'alttext'}{$item};
 1437:                     }
 1438:                 }
 1439:             }
 1440:             foreach my $item (@logintext) {
 1441:                 if ($settings->{$item} ne '') {
 1442:                     $designs{'logintext'}{$item} = $settings->{$item};
 1443:                     $is_custom{$item} = 1;
 1444:                 }
 1445:             }
 1446:             if ($settings->{'font'} ne '') {
 1447:                 $designs{'font'} = $settings->{'font'};
 1448:                 $is_custom{'font'} = 1;
 1449:             }
 1450:             foreach my $item (@bgs) {
 1451:                 if ($settings->{$item} ne '') {
 1452:                     $designs{'bgs'}{$item} = $settings->{$item};
 1453:                     $is_custom{$item} = 1;
 1454:                 }
 1455:             }
 1456:             foreach my $item (@links) {
 1457:                 if ($settings->{$item} ne '') {
 1458:                     $designs{'links'}{$item} = $settings->{$item};
 1459:                     $is_custom{$item} = 1;
 1460:                 }
 1461:             }
 1462:         } else {
 1463:             if ($designhash{$dom.'.login.font'} ne '') {
 1464:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1465:                 $is_custom{'font'} = 1;
 1466:             }
 1467:             foreach my $item (@images) {
 1468:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1469:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1470:                     $is_custom{$item} = 1;
 1471:                 }
 1472:             }
 1473:             foreach my $item (@bgs) {
 1474:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1475:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1476:                     $is_custom{$item} = 1;
 1477:                 }
 1478:             }
 1479:             foreach my $item (@links) {
 1480:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1481:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1482:                     $is_custom{$item} = 1;
 1483:                 }
 1484:             }
 1485:         }
 1486:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1487:                                                       logo => 'Institution Logo',
 1488:                                                       domlogo => 'Domain Logo',
 1489:                                                       login => 'Login box');
 1490:         my $itemcount = 1;
 1491:         foreach my $item (@toggles) {
 1492:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1493:             $datatable .=  
 1494:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1495:                 '</td><td>'.
 1496:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1497:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1498:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1499:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1500:                 '</tr>';
 1501:             $itemcount ++;
 1502:         }
 1503:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1504:         $datatable .= '</tr></table></td></tr>';
 1505:     } elsif ($caller eq 'help') {
 1506:         my ($defaulturl,$defaulttype,%url,%type,%langchoices);
 1507:         my $itemcount = 1;
 1508:         $defaulturl = '/adm/loginproblems.html';
 1509:         $defaulttype = 'default';
 1510:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1511:         my @currlangs;
 1512:         if (ref($settings) eq 'HASH') {
 1513:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1514:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1515:                     next if ($settings->{'helpurl'}{$key} eq '');
 1516:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1517:                     $type{$key} = 'custom';
 1518:                     unless ($key eq 'nolang') {
 1519:                         push(@currlangs,$key);
 1520:                     }
 1521:                 }
 1522:             } elsif ($settings->{'helpurl'} ne '') {
 1523:                 $type{'nolang'} = 'custom';
 1524:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1525:             }
 1526:         }
 1527:         foreach my $lang ('nolang',sort(@currlangs)) {
 1528:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1529:             $datatable .= '<tr'.$css_class.'>';
 1530:             if ($url{$lang} eq '') {
 1531:                 $url{$lang} = $defaulturl;
 1532:             }
 1533:             if ($type{$lang} eq '') {
 1534:                 $type{$lang} = $defaulttype;
 1535:             }
 1536:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1537:             if ($lang eq 'nolang') {
 1538:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1539:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1540:             } else {
 1541:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1542:                                   $langchoices{$lang},
 1543:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1544:             }
 1545:             $datatable .= '</span></td>'."\n".
 1546:                           '<td class="LC_left_item">';
 1547:             if ($type{$lang} eq 'custom') {
 1548:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1549:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1550:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1551:             } else {
 1552:                 $datatable .= $lt{'upl'};
 1553:             }
 1554:             $datatable .='<br />';
 1555:             if ($switchserver) {
 1556:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1557:             } else {
 1558:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1559:             }
 1560:             $datatable .= '</td></tr>';
 1561:             $itemcount ++;
 1562:         }
 1563:         my @addlangs;
 1564:         foreach my $lang (sort(keys(%langchoices))) {
 1565:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1566:             push(@addlangs,$lang);
 1567:         }
 1568:         if (@addlangs > 0) {
 1569:             my %toadd;
 1570:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1571:             $toadd{''} = &mt('Select');
 1572:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1573:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1574:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1575:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1576:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1577:             if ($switchserver) {
 1578:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1579:             } else {
 1580:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1581:             }
 1582:             $datatable .= '</td></tr>';
 1583:             $itemcount ++;
 1584:         }
 1585:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1586:     } elsif ($caller eq 'headtag') {
 1587:         my %domservers = &Apache::lonnet::get_servers($dom);
 1588:         my $choice = $choices{'headtag'};
 1589:         $css_class = ' class="LC_odd_row"';
 1590:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1591:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1592:                       '<th>'.$choices{'current'}.'</th>'.
 1593:                       '<th>'.$choices{'action'}.'</th>'.
 1594:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1595:         my (%currurls,%currexempt);
 1596:         if (ref($settings) eq 'HASH') {
 1597:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1598:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1599:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1600:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1601:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1602:                     }
 1603:                 }
 1604:             }
 1605:         }
 1606:         foreach my $lonhost (sort(keys(%domservers))) {
 1607:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1608:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1609:             if ($currurls{$lonhost}) {
 1610:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1611:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1612:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1613:                               '">'.$lt{'curr'}.'</a></td>'.
 1614:                               '<td><span class="LC_nobreak"><label>'.
 1615:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1616:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1617:             } else {
 1618:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1619:             }
 1620:             $datatable .='<br />';
 1621:             if ($switchserver) {
 1622:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1623:             } else {
 1624:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1625:             }
 1626:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1627:         }
 1628:         $datatable .= '</table></td></tr>';
 1629:     } elsif ($caller eq 'saml') {
 1630:         my %domservers = &Apache::lonnet::get_servers($dom);
 1631:         $datatable .= '<tr><td colspan="3" style="text-align: left">'.
 1632:                       '<table><tr><th>'.$choices{'hostid'}.'</th>'.
 1633:                       '<th>'.$choices{'samllanding'}.'</th>'.
 1634:                       '<th>'.$choices{'samloptions'}.'</th></tr>'."\n";
 1635:         my (%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso,%styleon,%styleoff);
 1636:         foreach my $lonhost (keys(%domservers)) {
 1637:             $samlurl{$lonhost} = '/adm/sso';
 1638:             $styleon{$lonhost} = 'display:none';
 1639:             $styleoff{$lonhost} = '';
 1640:         }
 1641:         if ((ref($settings) eq 'HASH') && (ref($settings->{'saml'}) eq 'HASH')) {
 1642:             foreach my $lonhost (keys(%{$settings->{'saml'}})) {
 1643:                 if (ref($settings->{'saml'}{$lonhost}) eq 'HASH') {
 1644:                     $saml{$lonhost} = 1;
 1645:                     $samltext{$lonhost} = $settings->{'saml'}{$lonhost}{'text'};
 1646:                     $samlimg{$lonhost} = $settings->{'saml'}{$lonhost}{'img'};
 1647:                     $samlalt{$lonhost} = $settings->{'saml'}{$lonhost}{'alt'};
 1648:                     $samlurl{$lonhost} = $settings->{'saml'}{$lonhost}{'url'};
 1649:                     $samltitle{$lonhost} = $settings->{'saml'}{$lonhost}{'title'};
 1650:                     $samlwindow{$lonhost} = $settings->{'saml'}{$lonhost}{'window'};
 1651:                     $samlnotsso{$lonhost} = $settings->{'saml'}{$lonhost}{'notsso'};
 1652:                     $styleon{$lonhost} = '';
 1653:                     $styleoff{$lonhost} = 'display:none';
 1654:                 } else {
 1655:                     $styleon{$lonhost} = 'display:none';
 1656:                     $styleoff{$lonhost} = '';
 1657:                 }
 1658:             }
 1659:         }
 1660:         my $itemcount = 1;
 1661:         foreach my $lonhost (sort(keys(%domservers))) {
 1662:             my $samlon = ' ';
 1663:             my $samloff = ' checked="checked" ';
 1664:             if ($saml{$lonhost}) {
 1665:                 $samlon = $samloff;
 1666:                 $samloff = ' ';
 1667:             }
 1668:             my $samlwinon = '';
 1669:             my $samlwinoff = ' checked="checked"';
 1670:             if ($samlwindow{$lonhost}) {
 1671:                 $samlwinon = $samlwinoff;
 1672:                 $samlwinoff = '';
 1673:             }
 1674:             my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1675:             $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.$domservers{$lonhost}.'</span></td>'.
 1676:                           '<td><span class="LC_nobreak"><label><input type="radio" name="saml_'.$lonhost.'"'.$samloff.
 1677:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="0" />'.
 1678:                           &mt('No').'</label>'.('&nbsp;'x2).
 1679:                           '<label><input type="radio" name="saml_'.$lonhost.'"'.$samlon.
 1680:                           'onclick="toggleSamlOptions(this.form,'."'$lonhost'".');" value="1" />'.
 1681:                           &mt('Yes').'</label></span></td>'.
 1682:                           '<td id="samloptionson_'.$lonhost.'" style="'.$styleon{$lonhost}.'" width="100%">'.
 1683:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th></tr>'.
 1684:                           '<tr><th>'.&mt('Text').'</th><th>'.&mt('Image').'</th>'.
 1685:                           '<th>'.&mt('Alt Text').'</th></tr>'.
 1686:                           '<tr'.$css_class.'><td><input type="text" name="saml_text_'.$lonhost.'" size="20" value="'.
 1687:                           $samltext{$lonhost}.'" /></td><td>';
 1688:             if ($samlimg{$lonhost}) {
 1689:                 $datatable .= '<img src="'.$samlimg{$lonhost}.'" /><br />'.
 1690:                               '<span class="LC_nobreak"><label>'.
 1691:                               '<input type="checkbox" name="saml_img_del" value="'.$lonhost.'" />'.
 1692:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1693:             } else {
 1694:                 $datatable .= $lt{'upl'};
 1695:             }
 1696:             $datatable .='<br />';
 1697:             if ($switchserver) {
 1698:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1699:             } else {
 1700:                 $datatable .= '<input type="file" name="saml_img_'.$lonhost.'" />';
 1701:             }
 1702:             $datatable .= '</td>'.
 1703:                           '<td><input type="text" name="saml_alt_'.$lonhost.'" size="25" '.
 1704:                           'value="'.$samlalt{$lonhost}.'" /></td></tr></table><br />'.
 1705:                           '<table width="100%"><tr><th colspan="3" align="center">'.&mt('SSO').'</th><th align="center">'.
 1706:                           '<span class="LC_nobreak">'.&mt('Non-SSO').'</span></th></tr>'.
 1707:                           '<tr><th>'.&mt('URL').'</th><th>'.&mt('Tool Tip').'</th>'.
 1708:                           '<th>'.&mt('Pop-up if iframe').'</th><th>'.&mt('Text').'</th></tr>'.
 1709:                           '<tr'.$css_class.'>'.
 1710:                           '<td><input type="text" name="saml_url_'.$lonhost.'" size="30" '.
 1711:                           'value="'.$samlurl{$lonhost}.'" /></td>'.
 1712:                           '<td><textarea name="saml_title_'.$lonhost.'" rows="3" cols="20">'.
 1713:                           $samltitle{$lonhost}.'</textarea></td>'.
 1714:                           '<td><label><input type="radio" name="saml_window_'.$lonhost.'" value=""'.$samlwinoff.'>'.
 1715:                           &mt('No').'</label>'.('&nbsp;'x2).'<label><input type="radio" '.
 1716:                           'name="saml_window_'.$lonhost.'" value="1"'.$samlwinon.'>'.&mt('Yes').'</label></td>'.
 1717:                           '<td><input type="text" name="saml_notsso_'.$lonhost.'" size="12" '.
 1718:                           'value="'.$samlnotsso{$lonhost}.'" /></td></tr>'.
 1719:                           '</table></td>'.
 1720:                           '<td id="samloptionsoff_'.$lonhost.'" style="'.$styleoff{$lonhost}.'" width="100%">&nbsp;</td></tr>';
 1721:            $itemcount ++;
 1722:         }
 1723:         $datatable .= '</table></td></tr>';
 1724:     }
 1725:     return $datatable;
 1726: }
 1727: 
 1728: sub login_choices {
 1729:     my %choices =
 1730:         &Apache::lonlocal::texthash (
 1731:             coursecatalog => 'Display Course/Community Catalog link?',
 1732:             adminmail     => "Display Administrator's E-mail Address?",
 1733:             helpdesk      => 'Display "Contact Helpdesk" link',
 1734:             disallowlogin => "Login page requests redirected",
 1735:             hostid        => "Server",
 1736:             server        => "Redirect to:",
 1737:             serverpath    => "Path",
 1738:             custompath    => "Custom", 
 1739:             exempt        => "Exempt IP(s)",
 1740:             directlogin   => "No redirect",
 1741:             newuser       => "Link to create a user account",
 1742:             img           => "Header",
 1743:             logo          => "Main Logo",
 1744:             domlogo       => "Domain Logo",
 1745:             login         => "Log-in Header", 
 1746:             textcol       => "Text color",
 1747:             bgcol         => "Box color",
 1748:             bgs           => "Background colors",
 1749:             links         => "Link colors",
 1750:             font          => "Font color",
 1751:             pgbg          => "Header",
 1752:             mainbg        => "Page",
 1753:             sidebg        => "Login box",
 1754:             link          => "Link",
 1755:             alink         => "Active link",
 1756:             vlink         => "Visited link",
 1757:             headtag       => "Custom markup",
 1758:             action        => "Action",
 1759:             current       => "Current",
 1760:             samllanding   => "Dual login?",
 1761:             samloptions   => "Options",
 1762:             alttext       => "Alt text",
 1763:         );
 1764:     return %choices;
 1765: }
 1766: 
 1767: sub login_file_options {
 1768:       return &Apache::lonlocal::texthash(
 1769:                                            del     => 'Delete?',
 1770:                                            rep     => 'Replace:',
 1771:                                            upl     => 'Upload:',
 1772:                                            curr    => 'View contents',
 1773:                                            default => 'Default',
 1774:                                            custom  => 'Custom',
 1775:                                            none    => 'None',
 1776:       );
 1777: }
 1778: 
 1779: sub print_ipaccess {
 1780:     my ($dom,$settings,$rowtotal) = @_;
 1781:     my $css_class;
 1782:     my $itemcount = 0;
 1783:     my $datatable;
 1784:     my %ordered;
 1785:     if (ref($settings) eq 'HASH') {
 1786:         foreach my $item (keys(%{$settings})) {
 1787:             if (ref($settings->{$item}) eq 'HASH') {
 1788:                 my $num = $settings->{$item}{'order'};
 1789:                 if ($num eq '') {
 1790:                     $num = scalar(keys(%{$settings}));
 1791:                 }
 1792:                 $ordered{$num} = $item;
 1793:             }
 1794:         }
 1795:     }
 1796:     my $maxnum = scalar(keys(%ordered));
 1797:     if (keys(%ordered)) {
 1798:         my @items = sort { $a <=> $b } keys(%ordered);
 1799:         for (my $i=0; $i<@items; $i++) {
 1800:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1801:             my $item = $ordered{$items[$i]};
 1802:             my ($name,$ipranges,%commblocks,%courses);
 1803:             if (ref($settings->{$item}) eq 'HASH') {
 1804:                 $name = $settings->{$item}->{'name'};
 1805:                 $ipranges = $settings->{$item}->{'ip'};
 1806:                 if (ref($settings->{$item}->{'commblocks'}) eq 'HASH') {
 1807:                     %commblocks = %{$settings->{$item}->{'commblocks'}};
 1808:                 }
 1809:                 if (ref($settings->{$item}->{'courses'}) eq 'HASH') {
 1810:                     %courses = %{$settings->{$item}->{'courses'}};
 1811:                 }
 1812:             }
 1813:             my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_".$item."'".');"';
 1814:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 1815:                          .'<select name="ipaccess_pos_'.$item.'"'.$chgstr.'>';
 1816:             for (my $k=0; $k<=$maxnum; $k++) {
 1817:                 my $vpos = $k+1;
 1818:                 my $selstr;
 1819:                 if ($k == $i) {
 1820:                     $selstr = ' selected="selected" ';
 1821:                 }
 1822:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1823:             }
 1824:             $datatable .= '</select>'.('&nbsp;'x2).
 1825:                 '<label><input type="checkbox" name="ipaccess_del" value="'.$item.'" />'.
 1826:                 &mt('Delete?').'</label></span></td>'.
 1827:                 '<td colspan="2"><input type="hidden" name="ipaccess_id_'.$i.'" value="'.$item.'" />'.
 1828:                 &ipaccess_options($i,$itemcount,$dom,$name,$ipranges,\%commblocks,\%courses).
 1829:                 '</td></tr>';
 1830:             $itemcount ++;
 1831:         }
 1832:     }
 1833:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1834:     my $chgstr = ' onchange="javascript:reorderIPaccess(this.form,'."'ipaccess_pos_add'".');"';
 1835:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 1836:                   '<input type="hidden" name="ipaccess_maxnum" value="'.$maxnum.'" />'."\n".
 1837:                   '<select name="ipaccess_pos_add"'.$chgstr.'>';
 1838:     for (my $k=0; $k<$maxnum+1; $k++) {
 1839:         my $vpos = $k+1;
 1840:         my $selstr;
 1841:         if ($k == $maxnum) {
 1842:             $selstr = ' selected="selected" ';
 1843:         }
 1844:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 1845:     }
 1846:     $datatable .= '</select>&nbsp;'."\n".
 1847:                   '<input type="checkbox" name="ipaccess_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 1848:                   '<td colspan="2">'.
 1849:                   &ipaccess_options('add',$itemcount,$dom).
 1850:                   '</td>'."\n".
 1851:                   '</tr>'."\n";
 1852:     $$rowtotal ++;
 1853:     return $datatable;
 1854: }
 1855: 
 1856: sub ipaccess_options {
 1857:     my ($num,$itemcount,$dom,$name,$ipranges,$blocksref,$coursesref) = @_;
 1858:     my (%currblocks,%currcourses,$output);
 1859:     if (ref($blocksref) eq 'HASH') {
 1860:         %currblocks = %{$blocksref};
 1861:     }
 1862:     if (ref($coursesref) eq 'HASH') {
 1863:         %currcourses = %{$coursesref};
 1864:     }
 1865:     $output = '<fieldset><legend>'.&mt('Location(s)').'</legend>'.
 1866:               '<span class="LC_nobreak">'.&mt('Name').':&nbsp;'.
 1867:               '<input type="text" name="ipaccess_name_'.$num.'" value="'.$name.'" />'.
 1868:               '</span></fieldset>'.
 1869:               '<fieldset><legend>'.&mt('IP Range(s)').'</legend>'.
 1870:               &mt('Format for each IP range').': '.&mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 1871:               &mt('Range(s) will be stored as IP netblock(s) in CIDR notation (comma separated)').'<br />'.
 1872:               '<textarea name="ipaccess_range_'.$num.'" rows="3" cols="80">'.
 1873:               $ipranges.'</textarea></fieldset>'.
 1874:               '<fieldset><legend>'.&mt('Functionality Blocked?').'</legend>'.
 1875:               &blocker_checkboxes($num,$blocksref).'</fieldset>'.
 1876:               '<fieldset><legend>'.&mt('Courses/Communities allowed').'</legend>'.
 1877:               '<table>';
 1878:     foreach my $cid (sort(keys(%currcourses))) {
 1879:         my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1});
 1880:         $output .= '<tr><td><span class="LC_nobreak">'.
 1881:                    '<label><input type="checkbox" name="ipaccess_course_delete_'.$num.'" value="'.$cid.'" />'.
 1882:                    &mt('Delete?').'&nbsp;<span class="LC_cusr_emph">'.$courseinfo{'description'}.'</span></label></span>'.
 1883:                    ' <span class="LC_fontsize_medium">('.$cid.')</span></td></tr>';
 1884:     }
 1885:     $output .= '<tr><td><span class="LC_nobreak">'.&mt('Add').':&nbsp;'.
 1886:                '<input type="text" name="ipaccess_cdesc_'.$num.'" value="" onfocus="this.blur();opencrsbrowser('."'display','ipaccess_cnum_$num','ipaccess_cdom_$num','ipaccess_cdesc_$num'".');" />'.
 1887:                 &Apache::loncommon::selectcourse_link('display','ipaccess_cnum_'.$num,'ipaccess_cdom_'.$num,'ipaccess_cdesc_'.$num,$dom,undef,'Course/Community').
 1888:                '<input type="hidden" name="ipaccess_cnum_'.$num.'" value="" />'.
 1889:                '<input type="hidden" name="ipaccess_cdom_'.$num.'" value="" />'.
 1890:                '</span></td></tr></table>'."\n".
 1891:                '</fieldset>';
 1892:     return $output;
 1893: }
 1894: 
 1895: sub blocker_checkboxes {
 1896:     my ($num,$blocks) = @_;
 1897:     my ($typeorder,$types) = &commblocktype_text();
 1898:     my $numinrow = 6;
 1899:     my $output = '<table>';
 1900:     for (my $i=0; $i<@{$typeorder}; $i++) {
 1901:         my $block = $typeorder->[$i];
 1902:         my $blockstatus;
 1903:         if (ref($blocks) eq 'HASH') {
 1904:             if ($blocks->{$block} eq 'on') {
 1905:                 $blockstatus = 'checked="checked"';
 1906:             }
 1907:         }
 1908:         my $rem = $i%($numinrow);
 1909:         if ($rem == 0) {
 1910:             if ($i > 0) {
 1911:                 $output .= '</tr>';
 1912:             }
 1913:             $output .= '<tr>';
 1914:         }
 1915:         if ($i == scalar(@{$typeorder})-1) {
 1916:             my $colsleft = $numinrow-$rem;
 1917:             if ($colsleft > 1) {
 1918:                 $output .= '<td colspan="'.$colsleft.'">';
 1919:             } else {
 1920:                 $output .= '<td>';
 1921:             }
 1922:         } else {
 1923:             $output .= '<td>';
 1924:         }
 1925:         my $item = 'ipaccess_block_'.$num;
 1926:         if ($blockstatus) {
 1927:             $blockstatus = ' '.$blockstatus;
 1928:         }
 1929:         $output .= '<span class="LC_nobreak"><label>'."\n".
 1930:                    '<input type="checkbox" name="'.$item.'"'.
 1931:                    $blockstatus.' value="'.$block.'"'.' />'.
 1932:                    $types->{$block}.'</label></span>'."\n".
 1933:                    '<br /></td>';
 1934:     }
 1935:     $output .= '</tr></table>';
 1936:     return $output;
 1937: }
 1938: 
 1939: sub commblocktype_text {
 1940:     my %types = &Apache::lonlocal::texthash(
 1941:         'com' => 'Messaging',
 1942:         'chat' => 'Chat Room',
 1943:         'boards' => 'Discussion',
 1944:         'port' => 'Portfolio',
 1945:         'groups' => 'Groups',
 1946:         'blogs' => 'Blogs',
 1947:         'about' => 'User Information',
 1948:         'printout' => 'Printouts',
 1949:         'passwd' => 'Change Password',
 1950:         'grades' => 'Gradebook',
 1951:         'search' => 'Course search',
 1952:         'wishlist' => 'Stored links',
 1953:         'annotate' => 'Annotations',
 1954:     );
 1955:     my $typeorder = ['com','chat','boards','port','groups','blogs','about','wishlist','printout','grades','search','annotate','passwd'];
 1956:     return ($typeorder,\%types);
 1957: }
 1958: 
 1959: sub print_rolecolors {
 1960:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1961:     my %choices = &color_font_choices();
 1962:     my @bgs = ('pgbg','tabbg','sidebg');
 1963:     my @links = ('link','alink','vlink');
 1964:     my @images = ('img');
 1965:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1966:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1967:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1968:     my (%is_custom,%designs);
 1969:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1970:     if (ref($settings) eq 'HASH') {
 1971:         if (ref($settings->{$role}) eq 'HASH') {
 1972:             if ($settings->{$role}->{'img'} ne '') {
 1973:                 $designs{'img'} = $settings->{$role}->{'img'};
 1974:                 $is_custom{'img'} = 1;
 1975:             }
 1976:             if ($settings->{$role}->{'font'} ne '') {
 1977:                 $designs{'font'} = $settings->{$role}->{'font'};
 1978:                 $is_custom{'font'} = 1;
 1979:             }
 1980:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1981:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1982:                 $is_custom{'fontmenu'} = 1;
 1983:             }
 1984:             foreach my $item (@bgs) {
 1985:                 if ($settings->{$role}->{$item} ne '') {
 1986:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1987:                     $is_custom{$item} = 1;
 1988:                 }
 1989:             }
 1990:             foreach my $item (@links) {
 1991:                 if ($settings->{$role}->{$item} ne '') {
 1992:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1993:                     $is_custom{$item} = 1;
 1994:                 }
 1995:             }
 1996:         }
 1997:     } else {
 1998:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1999:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 2000:             $is_custom{'img'} = 1;
 2001:         }
 2002:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 2003:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 2004:             $is_custom{'fontmenu'} = 1; 
 2005:         }
 2006:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 2007:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 2008:             $is_custom{'font'} = 1;
 2009:         }
 2010:         foreach my $item (@bgs) {
 2011:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2012:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2013:                 $is_custom{$item} = 1;
 2014:             
 2015:             }
 2016:         }
 2017:         foreach my $item (@links) {
 2018:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 2019:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 2020:                 $is_custom{$item} = 1;
 2021:             }
 2022:         }
 2023:     }
 2024:     my $itemcount = 1;
 2025:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 2026:     $datatable .= '</tr></table></td></tr>';
 2027:     return $datatable;
 2028: }
 2029: 
 2030: sub role_defaults {
 2031:     my ($role,$bgs,$links,$images,$logintext) = @_;
 2032:     my %defaults;
 2033:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 2034:         return %defaults;
 2035:     }
 2036:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 2037:     if ($role eq 'login') {
 2038:         %defaults = (
 2039:                        font => $defaultdesign{$role.'.font'},
 2040:                     );
 2041:         if (ref($logintext) eq 'ARRAY') {
 2042:             foreach my $item (@{$logintext}) {
 2043:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 2044:             }
 2045:         }
 2046:         foreach my $item (@{$images}) {
 2047:             $defaults{'showlogo'}{$item} = 1;
 2048:         }
 2049:     } else {
 2050:         %defaults = (
 2051:                        img => $defaultdesign{$role.'.img'},
 2052:                        font => $defaultdesign{$role.'.font'},
 2053:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 2054:                     );
 2055:     }
 2056:     foreach my $item (@{$bgs}) {
 2057:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 2058:     }
 2059:     foreach my $item (@{$links}) {
 2060:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 2061:     }
 2062:     foreach my $item (@{$images}) {
 2063:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 2064:     }
 2065:     return %defaults;
 2066: }
 2067: 
 2068: sub display_color_options {
 2069:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 2070:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 2071:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 2072:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2073:     my $datatable = '<tr'.$css_class.'>'.
 2074:         '<td>'.$choices->{'font'}.'</td>';
 2075:     if (!$is_custom->{'font'}) {
 2076:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 2077:     } else {
 2078:         $datatable .= '<td>&nbsp;</td>';
 2079:     }
 2080:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 2081: 
 2082:     $datatable .= '<td><span class="LC_nobreak">'.
 2083:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 2084:                   ' value="'.$current_color.'" />&nbsp;'.
 2085:                   '&nbsp;</span></td></tr>';
 2086:     unless ($role eq 'login') { 
 2087:         $datatable .= '<tr'.$css_class.'>'.
 2088:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 2089:         if (!$is_custom->{'fontmenu'}) {
 2090:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 2091:         } else {
 2092:             $datatable .= '<td>&nbsp;</td>';
 2093:         }
 2094: 	$current_color = $designs->{'fontmenu'} ?
 2095: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 2096:         $datatable .= '<td><span class="LC_nobreak">'.
 2097:                       '<input class="colorchooser" type="text" size="10" name="'
 2098: 		      .$role.'_fontmenu"'.
 2099:                       ' value="'.$current_color.'" />&nbsp;'.
 2100:                       '&nbsp;</span></td></tr>';
 2101:     }
 2102:     my $switchserver = &check_switchserver($dom,$confname);
 2103:     foreach my $img (@{$images}) {
 2104: 	$itemcount ++;
 2105:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2106:         $datatable .= '<tr'.$css_class.'>'.
 2107:                       '<td>'.$choices->{$img};
 2108:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors,$alttext);
 2109:         if ($role eq 'login') {
 2110:             if ($img eq 'login') {
 2111:                 $login_hdr_pick =
 2112:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 2113:                 $logincolors =
 2114:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 2115:                                        $designs,$defaults);
 2116:             } else {
 2117:                 if ($img ne 'domlogo') {
 2118:                     $datatable.= &logo_display_options($img,$defaults,$designs);
 2119:                 }
 2120:                 if (ref($designs->{'alttext'}) eq 'HASH') {
 2121:                     $alttext = $designs->{'alttext'}{$img};
 2122:                 }
 2123:             }
 2124:         }
 2125:         $datatable .= '</td>';
 2126:         if ($designs->{$img} ne '') {
 2127:             $imgfile = $designs->{$img};
 2128: 	    $img_import = ($imgfile =~ m{^/adm/});
 2129:         } else {
 2130:             $imgfile = $defaults->{$img};
 2131:         }
 2132:         if ($imgfile) {
 2133:             my ($showfile,$fullsize);
 2134:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 2135:                 my $urldir = $1;
 2136:                 my $filename = $2;
 2137:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 2138:                 if (@info) {
 2139:                     my $thumbfile = 'tn-'.$filename;
 2140:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 2141:                     if (@thumb) {
 2142:                         $showfile = $urldir.'/'.$thumbfile;
 2143:                     } else {
 2144:                         $showfile = $imgfile;
 2145:                     }
 2146:                 } else {
 2147:                     $showfile = '';
 2148:                 }
 2149:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 2150:                 $showfile = $imgfile;
 2151:                 my $imgdir = $1;
 2152:                 my $filename = $2;
 2153:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 2154:                     $showfile = "/$imgdir/tn-".$filename;
 2155:                 } else {
 2156:                     my $input = $londocroot.$imgfile;
 2157:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 2158:                     if (!-e $output) {
 2159:                         my ($width,$height) = &thumb_dimensions();
 2160:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 2161:                         if ($fullwidth ne '' && $fullheight ne '') {
 2162:                             if ($fullwidth > $width && $fullheight > $height) { 
 2163:                                 my $size = $width.'x'.$height;
 2164:                                 my @args = ('convert','-sample',$size,$input,$output);
 2165:                                 system({$args[0]} @args);
 2166:                                 $showfile = "/$imgdir/tn-".$filename;
 2167:                             }
 2168:                         }
 2169:                     }
 2170:                 }
 2171:             }
 2172:             if ($showfile) {
 2173:                 if ($showfile =~ m{^/(adm|res)/}) {
 2174:                     if ($showfile =~ m{^/res/}) {
 2175:                         my $local_showfile =
 2176:                             &Apache::lonnet::filelocation('',$showfile);
 2177:                         &Apache::lonnet::repcopy($local_showfile);
 2178:                     }
 2179:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 2180:                 }
 2181:                 if ($imgfile) {
 2182:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 2183:                         if ($imgfile =~ m{^/res/}) {
 2184:                             my $local_imgfile =
 2185:                                 &Apache::lonnet::filelocation('',$imgfile);
 2186:                             &Apache::lonnet::repcopy($local_imgfile);
 2187:                         }
 2188:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 2189:                     } else {
 2190:                         $fullsize = $imgfile;
 2191:                     }
 2192:                 }
 2193:                 $datatable .= '<td>';
 2194:                 if ($img eq 'login') {
 2195:                     $datatable .= $login_hdr_pick;
 2196:                 } 
 2197:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 2198:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 2199:             } else {
 2200:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2201:                               &mt('Upload:').'<br />';
 2202:             }
 2203:         } else {
 2204:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 2205:                           &mt('Upload:').'<br />';
 2206:         }
 2207:         if ($switchserver) {
 2208:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2209:         } else {
 2210:             if ($img ne 'login') { # suppress file selection for Log-in header
 2211:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 2212:             }
 2213:         }
 2214:         if (($role eq 'login') && ($img ne 'login')) {
 2215:             $datatable .= ('&nbsp;' x2).' <span class="LC_nobreak"><label>'.$choices->{'alttext'}.':'.
 2216:                           '<input type="text" name="'.$role.'_alt_'.$img.'" size="10" value="'.$alttext.'" />'.
 2217:                           '</label></span>';
 2218:         }
 2219:         $datatable .= '</td></tr>';
 2220:     }
 2221:     $itemcount ++;
 2222:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2223:     $datatable .= '<tr'.$css_class.'>'.
 2224:                   '<td>'.$choices->{'bgs'}.'</td>';
 2225:     my $bgs_def;
 2226:     foreach my $item (@{$bgs}) {
 2227:         if (!$is_custom->{$item}) {
 2228:             $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>';
 2229:         }
 2230:     }
 2231:     if ($bgs_def) {
 2232:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 2233:     } else {
 2234:         $datatable .= '<td>&nbsp;</td>';
 2235:     }
 2236:     $datatable .= '<td class="LC_right_item">'.
 2237:                   '<table border="0"><tr>';
 2238: 
 2239:     foreach my $item (@{$bgs}) {
 2240:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 2241: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 2242:         if ($designs->{'bgs'}{$item}) {
 2243:             $datatable .= '&nbsp;';
 2244:         }
 2245:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2246:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2247:     }
 2248:     $datatable .= '</tr></table></td></tr>';
 2249:     $itemcount ++;
 2250:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2251:     $datatable .= '<tr'.$css_class.'>'.
 2252:                   '<td>'.$choices->{'links'}.'</td>';
 2253:     my $links_def;
 2254:     foreach my $item (@{$links}) {
 2255:         if (!$is_custom->{$item}) {
 2256:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 2257:         }
 2258:     }
 2259:     if ($links_def) {
 2260:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 2261:     } else {
 2262:         $datatable .= '<td>&nbsp;</td>';
 2263:     }
 2264:     $datatable .= '<td class="LC_right_item">'.
 2265:                   '<table border="0"><tr>';
 2266:     foreach my $item (@{$links}) {
 2267: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 2268:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 2269:         if ($designs->{'links'}{$item}) {
 2270:             $datatable.='&nbsp;';
 2271:         }
 2272:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 2273:                       '" /></td>';
 2274:     }
 2275:     $$rowtotal += $itemcount;
 2276:     return $datatable;
 2277: }
 2278: 
 2279: sub logo_display_options {
 2280:     my ($img,$defaults,$designs) = @_;
 2281:     my $checkedon;
 2282:     if (ref($defaults) eq 'HASH') {
 2283:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 2284:             if ($defaults->{'showlogo'}{$img}) {
 2285:                 $checkedon = 'checked="checked" ';     
 2286:             }
 2287:         } 
 2288:     }
 2289:     if (ref($designs) eq 'HASH') {
 2290:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 2291:             if (defined($designs->{'showlogo'}{$img})) {
 2292:                 if ($designs->{'showlogo'}{$img} == 0) {
 2293:                     $checkedon = '';
 2294:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 2295:                     $checkedon = 'checked="checked" ';
 2296:                 }
 2297:             }
 2298:         }
 2299:     }
 2300:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 2301:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 2302:            &mt('show').'</label>'."\n";
 2303: }
 2304: 
 2305: sub login_header_options  {
 2306:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 2307:     my $output = '';
 2308:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 2309:         $output .= &mt('Text default(s):').'<br />';
 2310:         if (!$is_custom->{'textcol'}) {
 2311:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 2312:                        '&nbsp;&nbsp;&nbsp;';
 2313:         }
 2314:         if (!$is_custom->{'bgcol'}) {
 2315:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 2316:                        '<span id="css_'.$role.'_font" style="background-color: '.
 2317:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 2318:         }
 2319:         $output .= '<br />';
 2320:     }
 2321:     $output .='<br />';
 2322:     return $output;
 2323: }
 2324: 
 2325: sub login_text_colors {
 2326:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 2327:     my $color_menu = '<table border="0"><tr>';
 2328:     foreach my $item (@{$logintext}) {
 2329:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 2330:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 2331:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 2332:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 2333:     }
 2334:     $color_menu .= '</tr></table><br />';
 2335:     return $color_menu;
 2336: }
 2337: 
 2338: sub image_changes {
 2339:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 2340:     my $output;
 2341:     if ($img eq 'login') {
 2342:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 2343:     } elsif (!$is_custom) {
 2344:         if ($img ne 'domlogo') {
 2345:             $output = &mt('Default image:').'<br />';
 2346:         } else {
 2347:             $output = &mt('Default in use:').'<br />';
 2348:         }
 2349:     }
 2350:     if ($img ne 'login') {
 2351:         if ($img_import) {
 2352:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 2353:         }
 2354:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 2355:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 2356:         if ($is_custom) {
 2357:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 2358:                        '<input type="checkbox" name="'.
 2359:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 2360:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 2361:         } else {
 2362:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 2363:         }
 2364:     }
 2365:     return $output;
 2366: }
 2367: 
 2368: sub print_quotas {
 2369:     my ($dom,$settings,$rowtotal,$action) = @_;
 2370:     my $context;
 2371:     if ($action eq 'quotas') {
 2372:         $context = 'tools';
 2373:     } else {
 2374:         $context = $action;
 2375:     }
 2376:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 2377:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2378:     my $typecount = 0;
 2379:     my ($css_class,%titles);
 2380:     if ($context eq 'requestcourses') {
 2381:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 2382:         @options =('norequest','approval','validate','autolimit');
 2383:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 2384:         %titles = &courserequest_titles();
 2385:     } elsif ($context eq 'requestauthor') {
 2386:         @usertools = ('author');
 2387:         @options = ('norequest','approval','automatic');
 2388:         %titles = &authorrequest_titles();
 2389:     } else {
 2390:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
 2391:         %titles = &tool_titles();
 2392:     }
 2393:     if (ref($types) eq 'ARRAY') {
 2394:         foreach my $type (@{$types}) {
 2395:             my ($currdefquota,$currauthorquota);
 2396:             unless (($context eq 'requestcourses') ||
 2397:                     ($context eq 'requestauthor')) {
 2398:                 if (ref($settings) eq 'HASH') {
 2399:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2400:                         $currdefquota = $settings->{defaultquota}->{$type};
 2401:                     } else {
 2402:                         $currdefquota = $settings->{$type};
 2403:                     }
 2404:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2405:                         $currauthorquota = $settings->{authorquota}->{$type};
 2406:                     }
 2407:                 }
 2408:             }
 2409:             if (defined($usertypes->{$type})) {
 2410:                 $typecount ++;
 2411:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2412:                 $datatable .= '<tr'.$css_class.'>'.
 2413:                               '<td>'.$usertypes->{$type}.'</td>'.
 2414:                               '<td class="LC_left_item">';
 2415:                 if ($context eq 'requestcourses') {
 2416:                     $datatable .= '<table><tr>';
 2417:                 }
 2418:                 my %cell;  
 2419:                 foreach my $item (@usertools) {
 2420:                     if ($context eq 'requestcourses') {
 2421:                         my ($curroption,$currlimit);
 2422:                         if (ref($settings) eq 'HASH') {
 2423:                             if (ref($settings->{$item}) eq 'HASH') {
 2424:                                 $curroption = $settings->{$item}->{$type};
 2425:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2426:                                     $currlimit = $1; 
 2427:                                 }
 2428:                             }
 2429:                         }
 2430:                         if (!$curroption) {
 2431:                             $curroption = 'norequest';
 2432:                         }
 2433:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2434:                         foreach my $option (@options) {
 2435:                             my $val = $option;
 2436:                             if ($option eq 'norequest') {
 2437:                                 $val = 0;  
 2438:                             }
 2439:                             if ($option eq 'validate') {
 2440:                                 my $canvalidate = 0;
 2441:                                 if (ref($validations{$item}) eq 'HASH') { 
 2442:                                     if ($validations{$item}{$type}) {
 2443:                                         $canvalidate = 1;
 2444:                                     }
 2445:                                 }
 2446:                                 next if (!$canvalidate);
 2447:                             }
 2448:                             my $checked = '';
 2449:                             if ($option eq $curroption) {
 2450:                                 $checked = ' checked="checked"';
 2451:                             } elsif ($option eq 'autolimit') {
 2452:                                 if ($curroption =~ /^autolimit/) {
 2453:                                     $checked = ' checked="checked"';
 2454:                                 }                       
 2455:                             } 
 2456:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2457:                                   '<input type="radio" name="crsreq_'.$item.
 2458:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2459:                                   $titles{$option}.'</label>';
 2460:                             if ($option eq 'autolimit') {
 2461:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2462:                                                 $item.'_limit_'.$type.'" size="1" '.
 2463:                                                 'value="'.$currlimit.'" />';
 2464:                             }
 2465:                             $cell{$item} .= '</span> ';
 2466:                             if ($option eq 'autolimit') {
 2467:                                 $cell{$item} .= $titles{'unlimited'};
 2468:                             }
 2469:                         }
 2470:                     } elsif ($context eq 'requestauthor') {
 2471:                         my $curroption;
 2472:                         if (ref($settings) eq 'HASH') {
 2473:                             $curroption = $settings->{$type};
 2474:                         }
 2475:                         if (!$curroption) {
 2476:                             $curroption = 'norequest';
 2477:                         }
 2478:                         foreach my $option (@options) {
 2479:                             my $val = $option;
 2480:                             if ($option eq 'norequest') {
 2481:                                 $val = 0;
 2482:                             }
 2483:                             my $checked = '';
 2484:                             if ($option eq $curroption) {
 2485:                                 $checked = ' checked="checked"';
 2486:                             }
 2487:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2488:                                   '<input type="radio" name="authorreq_'.$type.
 2489:                                   '" value="'.$val.'"'.$checked.' />'.
 2490:                                   $titles{$option}.'</label></span>&nbsp; ';
 2491:                         }
 2492:                     } else {
 2493:                         my $checked = 'checked="checked" ';
 2494:                         if ($item eq 'timezone') {
 2495:                             $checked = '';
 2496:                         }
 2497:                         if (ref($settings) eq 'HASH') {
 2498:                             if (ref($settings->{$item}) eq 'HASH') {
 2499:                                 if (!$settings->{$item}->{$type}) {
 2500:                                     $checked = '';
 2501:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2502:                                     $checked =  'checked="checked" ';
 2503:                                 }
 2504:                             }
 2505:                         }
 2506:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2507:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2508:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2509:                                       '</label></span>&nbsp; ';
 2510:                     }
 2511:                 }
 2512:                 if ($context eq 'requestcourses') {
 2513:                     $datatable .= '</tr><tr>';
 2514:                     foreach my $item (@usertools) {
 2515:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2516:                     }
 2517:                     $datatable .= '</tr></table>';
 2518:                 }
 2519:                 $datatable .= '</td>';
 2520:                 unless (($context eq 'requestcourses') ||
 2521:                         ($context eq 'requestauthor')) {
 2522:                     $datatable .= 
 2523:                               '<td class="LC_right_item">'.
 2524:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2525:                               '<input type="text" name="quota_'.$type.
 2526:                               '" value="'.$currdefquota.
 2527:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2528:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2529:                               '<input type="text" name="authorquota_'.$type.
 2530:                               '" value="'.$currauthorquota.
 2531:                               '" size="5" /></span></td>';
 2532:                 }
 2533:                 $datatable .= '</tr>';
 2534:             }
 2535:         }
 2536:     }
 2537:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2538:         $defaultquota = '20';
 2539:         $authorquota = '500';
 2540:         if (ref($settings) eq 'HASH') {
 2541:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2542:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2543:             } elsif (defined($settings->{'default'})) {
 2544:                 $defaultquota = $settings->{'default'};
 2545:             }
 2546:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2547:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2548:             }
 2549:         }
 2550:     }
 2551:     $typecount ++;
 2552:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2553:     $datatable .= '<tr'.$css_class.'>'.
 2554:                   '<td>'.$othertitle.'</td>'.
 2555:                   '<td class="LC_left_item">';
 2556:     if ($context eq 'requestcourses') {
 2557:         $datatable .= '<table><tr>';
 2558:     }
 2559:     my %defcell;
 2560:     foreach my $item (@usertools) {
 2561:         if ($context eq 'requestcourses') {
 2562:             my ($curroption,$currlimit);
 2563:             if (ref($settings) eq 'HASH') {
 2564:                 if (ref($settings->{$item}) eq 'HASH') {
 2565:                     $curroption = $settings->{$item}->{'default'};
 2566:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2567:                         $currlimit = $1;
 2568:                     }
 2569:                 }
 2570:             }
 2571:             if (!$curroption) {
 2572:                 $curroption = 'norequest';
 2573:             }
 2574:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2575:             foreach my $option (@options) {
 2576:                 my $val = $option;
 2577:                 if ($option eq 'norequest') {
 2578:                     $val = 0;
 2579:                 }
 2580:                 if ($option eq 'validate') {
 2581:                     my $canvalidate = 0;
 2582:                     if (ref($validations{$item}) eq 'HASH') {
 2583:                         if ($validations{$item}{'default'}) {
 2584:                             $canvalidate = 1;
 2585:                         }
 2586:                     }
 2587:                     next if (!$canvalidate);
 2588:                 }
 2589:                 my $checked = '';
 2590:                 if ($option eq $curroption) {
 2591:                     $checked = ' checked="checked"';
 2592:                 } elsif ($option eq 'autolimit') {
 2593:                     if ($curroption =~ /^autolimit/) {
 2594:                         $checked = ' checked="checked"';
 2595:                     }
 2596:                 }
 2597:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2598:                                   '<input type="radio" name="crsreq_'.$item.
 2599:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2600:                                   $titles{$option}.'</label>';
 2601:                 if ($option eq 'autolimit') {
 2602:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2603:                                        $item.'_limit_default" size="1" '.
 2604:                                        'value="'.$currlimit.'" />';
 2605:                 }
 2606:                 $defcell{$item} .= '</span> ';
 2607:                 if ($option eq 'autolimit') {
 2608:                     $defcell{$item} .= $titles{'unlimited'};
 2609:                 }
 2610:             }
 2611:         } elsif ($context eq 'requestauthor') {
 2612:             my $curroption;
 2613:             if (ref($settings) eq 'HASH') {
 2614:                 $curroption = $settings->{'default'};
 2615:             }
 2616:             if (!$curroption) {
 2617:                 $curroption = 'norequest';
 2618:             }
 2619:             foreach my $option (@options) {
 2620:                 my $val = $option;
 2621:                 if ($option eq 'norequest') {
 2622:                     $val = 0;
 2623:                 }
 2624:                 my $checked = '';
 2625:                 if ($option eq $curroption) {
 2626:                     $checked = ' checked="checked"';
 2627:                 }
 2628:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2629:                               '<input type="radio" name="authorreq_default"'.
 2630:                               ' value="'.$val.'"'.$checked.' />'.
 2631:                               $titles{$option}.'</label></span>&nbsp; ';
 2632:             }
 2633:         } else {
 2634:             my $checked = 'checked="checked" ';
 2635:             if (ref($settings) eq 'HASH') {
 2636:                 if (ref($settings->{$item}) eq 'HASH') {
 2637:                     if ($settings->{$item}->{'default'} == 0) {
 2638:                         $checked = '';
 2639:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2640:                         $checked = 'checked="checked" ';
 2641:                     }
 2642:                 }
 2643:             }
 2644:             $datatable .= '<span class="LC_nobreak"><label>'.
 2645:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2646:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2647:                           '</label></span>&nbsp; ';
 2648:         }
 2649:     }
 2650:     if ($context eq 'requestcourses') {
 2651:         $datatable .= '</tr><tr>';
 2652:         foreach my $item (@usertools) {
 2653:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2654:         }
 2655:         $datatable .= '</tr></table>';
 2656:     }
 2657:     $datatable .= '</td>';
 2658:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2659:         $datatable .= '<td class="LC_right_item">'.
 2660:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2661:                       '<input type="text" name="defaultquota" value="'.
 2662:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2663:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2664:                       '<input type="text" name="authorquota" value="'.
 2665:                       $authorquota.'" size="5" /></span></td>';
 2666:     }
 2667:     $datatable .= '</tr>';
 2668:     $typecount ++;
 2669:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2670:     $datatable .= '<tr'.$css_class.'>'.
 2671:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2672:     if ($context eq 'requestcourses') {
 2673:         $datatable .= &mt('(overrides affiliation, if set)').
 2674:                       '</td>'.
 2675:                       '<td class="LC_left_item">'.
 2676:                       '<table><tr>';
 2677:     } else {
 2678:         $datatable .= &mt('(overrides affiliation, if checked)').
 2679:                       '</td>'.
 2680:                       '<td class="LC_left_item" colspan="2">'.
 2681:                       '<br />';
 2682:     }
 2683:     my %advcell;
 2684:     foreach my $item (@usertools) {
 2685:         if ($context eq 'requestcourses') {
 2686:             my ($curroption,$currlimit);
 2687:             if (ref($settings) eq 'HASH') {
 2688:                 if (ref($settings->{$item}) eq 'HASH') {
 2689:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2690:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2691:                         $currlimit = $1;
 2692:                     }
 2693:                 }
 2694:             }
 2695:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2696:             my $checked = '';
 2697:             if ($curroption eq '') {
 2698:                 $checked = ' checked="checked"';
 2699:             }
 2700:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2701:                                '<input type="radio" name="crsreq_'.$item.
 2702:                                '__LC_adv" value=""'.$checked.' />'.
 2703:                                &mt('No override set').'</label></span>&nbsp; ';
 2704:             foreach my $option (@options) {
 2705:                 my $val = $option;
 2706:                 if ($option eq 'norequest') {
 2707:                     $val = 0;
 2708:                 }
 2709:                 if ($option eq 'validate') {
 2710:                     my $canvalidate = 0;
 2711:                     if (ref($validations{$item}) eq 'HASH') {
 2712:                         if ($validations{$item}{'_LC_adv'}) {
 2713:                             $canvalidate = 1;
 2714:                         }
 2715:                     }
 2716:                     next if (!$canvalidate);
 2717:                 }
 2718:                 my $checked = '';
 2719:                 if ($val eq $curroption) {
 2720:                     $checked = ' checked="checked"';
 2721:                 } elsif ($option eq 'autolimit') {
 2722:                     if ($curroption =~ /^autolimit/) {
 2723:                         $checked = ' checked="checked"';
 2724:                     }
 2725:                 }
 2726:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2727:                                   '<input type="radio" name="crsreq_'.$item.
 2728:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2729:                                   $titles{$option}.'</label>';
 2730:                 if ($option eq 'autolimit') {
 2731:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2732:                                        $item.'_limit__LC_adv" size="1" '.
 2733:                                        'value="'.$currlimit.'" />';
 2734:                 }
 2735:                 $advcell{$item} .= '</span> ';
 2736:                 if ($option eq 'autolimit') {
 2737:                     $advcell{$item} .= $titles{'unlimited'};
 2738:                 }
 2739:             }
 2740:         } elsif ($context eq 'requestauthor') {
 2741:             my $curroption;
 2742:             if (ref($settings) eq 'HASH') {
 2743:                 $curroption = $settings->{'_LC_adv'};
 2744:             }
 2745:             my $checked = '';
 2746:             if ($curroption eq '') {
 2747:                 $checked = ' checked="checked"';
 2748:             }
 2749:             $datatable .= '<span class="LC_nobreak"><label>'.
 2750:                           '<input type="radio" name="authorreq__LC_adv"'.
 2751:                           ' value=""'.$checked.' />'.
 2752:                           &mt('No override set').'</label></span>&nbsp; ';
 2753:             foreach my $option (@options) {
 2754:                 my $val = $option;
 2755:                 if ($option eq 'norequest') {
 2756:                     $val = 0;
 2757:                 }
 2758:                 my $checked = '';
 2759:                 if ($val eq $curroption) {
 2760:                     $checked = ' checked="checked"';
 2761:                 }
 2762:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2763:                               '<input type="radio" name="authorreq__LC_adv"'.
 2764:                               ' value="'.$val.'"'.$checked.' />'.
 2765:                               $titles{$option}.'</label></span>&nbsp; ';
 2766:             }
 2767:         } else {
 2768:             my $checked = 'checked="checked" ';
 2769:             if (ref($settings) eq 'HASH') {
 2770:                 if (ref($settings->{$item}) eq 'HASH') {
 2771:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2772:                         $checked = '';
 2773:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2774:                         $checked = 'checked="checked" ';
 2775:                     }
 2776:                 }
 2777:             }
 2778:             $datatable .= '<span class="LC_nobreak"><label>'.
 2779:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2780:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2781:                           '</label></span>&nbsp; ';
 2782:         }
 2783:     }
 2784:     if ($context eq 'requestcourses') {
 2785:         $datatable .= '</tr><tr>';
 2786:         foreach my $item (@usertools) {
 2787:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2788:         }
 2789:         $datatable .= '</tr></table>';
 2790:     }
 2791:     $datatable .= '</td></tr>';
 2792:     $$rowtotal += $typecount;
 2793:     return $datatable;
 2794: }
 2795: 
 2796: sub print_requestmail {
 2797:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2798:     my ($now,$datatable,%currapp);
 2799:     $now = time;
 2800:     if (ref($settings) eq 'HASH') {
 2801:         if (ref($settings->{'notify'}) eq 'HASH') {
 2802:             if ($settings->{'notify'}{'approval'} ne '') {
 2803:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2804:             }
 2805:         }
 2806:     }
 2807:     my $numinrow = 2;
 2808:     my $css_class;
 2809:     if ($$rowtotal%2) {
 2810:         $css_class = 'LC_odd_row';
 2811:     }
 2812:     if ($customcss) {
 2813:         $css_class .= " $customcss";
 2814:     }
 2815:     $css_class =~ s/^\s+//;
 2816:     if ($css_class) {
 2817:         $css_class = ' class="'.$css_class.'"';
 2818:     }
 2819:     if ($rowstyle) {
 2820:         $css_class .= ' style="'.$rowstyle.'"';
 2821:     }
 2822:     my $text;
 2823:     if ($action eq 'requestcourses') {
 2824:         $text = &mt('Receive notification of course requests requiring approval');
 2825:     } elsif ($action eq 'requestauthor') {
 2826:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2827:     } else {
 2828:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2829:     }
 2830:     $datatable = '<tr'.$css_class.'>'.
 2831:                  ' <td>'.$text.'</td>'.
 2832:                  ' <td class="LC_left_item">';
 2833:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2834:                                                  $action.'notifyapproval',%currapp);
 2835:     if ($numdc > 0) {
 2836:         $datatable .= $table;
 2837:     } else {
 2838:         $datatable .= &mt('There are no active Domain Coordinators');
 2839:     }
 2840:     $datatable .='</td></tr>';
 2841:     return $datatable;
 2842: }
 2843: 
 2844: sub print_studentcode {
 2845:     my ($settings,$rowtotal) = @_;
 2846:     my $rownum = 0; 
 2847:     my ($output,%current);
 2848:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2849:     if (ref($settings) eq 'HASH') {
 2850:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2851:             foreach my $type (@crstypes) {
 2852:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2853:             }
 2854:         }
 2855:     }
 2856:     $output .= '<tr>'.
 2857:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2858:                '<td class="LC_left_item">';
 2859:     foreach my $type (@crstypes) {
 2860:         my $check = ' ';
 2861:         if ($current{$type}) {
 2862:             $check = ' checked="checked" ';
 2863:         }
 2864:         $output .= '<span class="LC_nobreak"><label>'.
 2865:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2866:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2867:     }
 2868:     $output .= '</td></tr>';
 2869:     $$rowtotal ++;
 2870:     return $output;
 2871: }
 2872: 
 2873: sub print_textbookcourses {
 2874:     my ($dom,$type,$settings,$rowtotal) = @_;
 2875:     my $rownum = 0;
 2876:     my $css_class;
 2877:     my $itemcount = 1;
 2878:     my $maxnum = 0;
 2879:     my $bookshash;
 2880:     if (ref($settings) eq 'HASH') {
 2881:         $bookshash = $settings->{$type};
 2882:     }
 2883:     my %ordered;
 2884:     if (ref($bookshash) eq 'HASH') {
 2885:         foreach my $item (keys(%{$bookshash})) {
 2886:             if (ref($bookshash->{$item}) eq 'HASH') {
 2887:                 my $num = $bookshash->{$item}{'order'};
 2888:                 $ordered{$num} = $item;
 2889:             }
 2890:         }
 2891:     }
 2892:     my $confname = $dom.'-domainconfig';
 2893:     my $switchserver = &check_switchserver($dom,$confname);
 2894:     my $maxnum = scalar(keys(%ordered));
 2895:     my $datatable;
 2896:     if (keys(%ordered)) {
 2897:         my @items = sort { $a <=> $b } keys(%ordered);
 2898:         for (my $i=0; $i<@items; $i++) {
 2899:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2900:             my $key = $ordered{$items[$i]};
 2901:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2902:             my $coursetitle = $coursehash{'description'};
 2903:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2904:             if (ref($bookshash->{$key}) eq 'HASH') {
 2905:                 $subject = $bookshash->{$key}->{'subject'};
 2906:                 $title = $bookshash->{$key}->{'title'};
 2907:                 if ($type eq 'textbooks') {
 2908:                     $publisher = $bookshash->{$key}->{'publisher'};
 2909:                     $author = $bookshash->{$key}->{'author'};
 2910:                     $image = $bookshash->{$key}->{'image'};
 2911:                     if ($image ne '') {
 2912:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2913:                         my $imagethumb = "$path/tn-".$imagefile;
 2914:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2915:                     }
 2916:                 }
 2917:             }
 2918:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2919:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2920:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2921:             for (my $k=0; $k<=$maxnum; $k++) {
 2922:                 my $vpos = $k+1;
 2923:                 my $selstr;
 2924:                 if ($k == $i) {
 2925:                     $selstr = ' selected="selected" ';
 2926:                 }
 2927:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2928:             }
 2929:             $datatable .= '</select>'.('&nbsp;'x2).
 2930:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2931:                 &mt('Delete?').'</label></span></td>'.
 2932:                 '<td colspan="2">'.
 2933:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2934:                 ('&nbsp;'x2).
 2935:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2936:             if ($type eq 'textbooks') {
 2937:                 $datatable .= ('&nbsp;'x2).
 2938:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2939:                               ('&nbsp;'x2).
 2940:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2941:                               ('&nbsp;'x2).
 2942:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2943:                 if ($image) {
 2944:                     $datatable .= $imgsrc.
 2945:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2946:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2947:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2948:                 }
 2949:                 if ($switchserver) {
 2950:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2951:                 } else {
 2952:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2953:                 }
 2954:             }
 2955:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2956:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2957:                           $coursetitle.'</span></td></tr>'."\n";
 2958:             $itemcount ++;
 2959:         }
 2960:     }
 2961:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2962:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2963:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2964:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2965:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2966:     for (my $k=0; $k<$maxnum+1; $k++) {
 2967:         my $vpos = $k+1;
 2968:         my $selstr;
 2969:         if ($k == $maxnum) {
 2970:             $selstr = ' selected="selected" ';
 2971:         }
 2972:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2973:     }
 2974:     $datatable .= '</select>&nbsp;'."\n".
 2975:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2976:                   '<td colspan="2">'.
 2977:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2978:                   ('&nbsp;'x2).
 2979:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2980:                   ('&nbsp;'x2);
 2981:     if ($type eq 'textbooks') {
 2982:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2983:                       ('&nbsp;'x2).
 2984:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2985:                       ('&nbsp;'x2).
 2986:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2987:         if ($switchserver) {
 2988:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2989:         } else {
 2990:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2991:         }
 2992:         $datatable .= '</span>'."\n";
 2993:     }
 2994:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2995:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2996:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2997:                   &Apache::loncommon::selectcourse_link
 2998:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2999:                   '</span></td>'."\n".
 3000:                   '</tr>'."\n";
 3001:     $itemcount ++;
 3002:     return $datatable;
 3003: }
 3004: 
 3005: sub textbookcourses_javascript {
 3006:     my ($settings) = @_;
 3007:     return unless(ref($settings) eq 'HASH');
 3008:     my (%ordered,%total,%jstext);
 3009:     foreach my $type ('textbooks','templates') {
 3010:         $total{$type} = 0;
 3011:         if (ref($settings->{$type}) eq 'HASH') {
 3012:             foreach my $item (keys(%{$settings->{$type}})) {
 3013:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 3014:                     my $num = $settings->{$type}->{$item}{'order'};
 3015:                     $ordered{$type}{$num} = $item;
 3016:                 }
 3017:             }
 3018:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 3019:         }
 3020:         my @jsarray = ();
 3021:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 3022:             push(@jsarray,$ordered{$type}{$item});
 3023:         }
 3024:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 3025:     }
 3026:     return <<"ENDSCRIPT";
 3027: <script type="text/javascript">
 3028: // <![CDATA[
 3029: function reorderBooks(form,item,caller) {
 3030:     var changedVal;
 3031: $jstext{'textbooks'};
 3032: $jstext{'templates'};
 3033:     var newpos;
 3034:     var maxh;
 3035:     if (caller == 'textbooks') {  
 3036:         newpos = 'textbooks_addbook_pos';
 3037:         maxh = 1 + $total{'textbooks'};
 3038:     } else {
 3039:         newpos = 'templates_addbook_pos';
 3040:         maxh = 1 + $total{'templates'};
 3041:     }
 3042:     var current = new Array;
 3043:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3044:     if (item == newpos) {
 3045:         changedVal = newitemVal;
 3046:     } else {
 3047:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3048:         current[newitemVal] = newpos;
 3049:     }
 3050:     if (caller == 'textbooks') {
 3051:         for (var i=0; i<textbooks.length; i++) {
 3052:             var elementName = 'textbooks_'+textbooks[i];
 3053:             if (elementName != item) {
 3054:                 if (form.elements[elementName]) {
 3055:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3056:                     current[currVal] = elementName;
 3057:                 }
 3058:             }
 3059:         }
 3060:     }
 3061:     if (caller == 'templates') {
 3062:         for (var i=0; i<templates.length; i++) {
 3063:             var elementName = 'templates_'+templates[i];
 3064:             if (elementName != item) {
 3065:                 if (form.elements[elementName]) {
 3066:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3067:                     current[currVal] = elementName;
 3068:                 }
 3069:             }
 3070:         }
 3071:     }
 3072:     var oldVal;
 3073:     for (var j=0; j<maxh; j++) {
 3074:         if (current[j] == undefined) {
 3075:             oldVal = j;
 3076:         }
 3077:     }
 3078:     if (oldVal < changedVal) {
 3079:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3080:            var elementName = current[k];
 3081:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3082:         }
 3083:     } else {
 3084:         for (var k=changedVal; k<oldVal; k++) {
 3085:             var elementName = current[k];
 3086:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3087:         }
 3088:     }
 3089:     return;
 3090: }
 3091: 
 3092: // ]]>
 3093: </script>
 3094: 
 3095: ENDSCRIPT
 3096: }
 3097: 
 3098: sub ltitools_javascript {
 3099:     my ($settings) = @_;
 3100:     my $togglejs = &ltitools_toggle_js();
 3101:     unless (ref($settings) eq 'HASH') {
 3102:         return $togglejs;
 3103:     }
 3104:     my (%ordered,$total,%jstext);
 3105:     $total = 0;
 3106:     foreach my $item (keys(%{$settings})) {
 3107:         if (ref($settings->{$item}) eq 'HASH') {
 3108:             my $num = $settings->{$item}{'order'};
 3109:             $ordered{$num} = $item;
 3110:         }
 3111:     }
 3112:     $total = scalar(keys(%{$settings}));
 3113:     my @jsarray = ();
 3114:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3115:         push(@jsarray,$ordered{$item});
 3116:     }
 3117:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 3118:     return <<"ENDSCRIPT";
 3119: <script type="text/javascript">
 3120: // <![CDATA[
 3121: function reorderLTITools(form,item) {
 3122:     var changedVal;
 3123: $jstext
 3124:     var newpos = 'ltitools_add_pos';
 3125:     var maxh = 1 + $total;
 3126:     var current = new Array;
 3127:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3128:     if (item == newpos) {
 3129:         changedVal = newitemVal;
 3130:     } else {
 3131:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3132:         current[newitemVal] = newpos;
 3133:     }
 3134:     for (var i=0; i<ltitools.length; i++) {
 3135:         var elementName = 'ltitools_'+ltitools[i];
 3136:         if (elementName != item) {
 3137:             if (form.elements[elementName]) {
 3138:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3139:                 current[currVal] = elementName;
 3140:             }
 3141:         }
 3142:     }
 3143:     var oldVal;
 3144:     for (var j=0; j<maxh; j++) {
 3145:         if (current[j] == undefined) {
 3146:             oldVal = j;
 3147:         }
 3148:     }
 3149:     if (oldVal < changedVal) {
 3150:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3151:            var elementName = current[k];
 3152:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3153:         }
 3154:     } else {
 3155:         for (var k=changedVal; k<oldVal; k++) {
 3156:             var elementName = current[k];
 3157:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3158:         }
 3159:     }
 3160:     return;
 3161: }
 3162: 
 3163: // ]]>
 3164: </script>
 3165: 
 3166: $togglejs
 3167: 
 3168: ENDSCRIPT
 3169: }
 3170: 
 3171: sub ltitools_toggle_js {
 3172:     return <<"ENDSCRIPT";
 3173: <script type="text/javascript">
 3174: // <![CDATA[
 3175: 
 3176: function toggleLTITools(form,setting,item) {
 3177:     var radioname = '';
 3178:     var divid = '';
 3179:     if ((setting == 'passback') || (setting == 'roster')) {
 3180:         radioname = 'ltitools_'+setting+'_'+item;
 3181:         divid = 'ltitools_'+setting+'time_'+item;
 3182:         var num = form.elements[radioname].length;
 3183:         if (num) {
 3184:             var setvis = '';
 3185:             for (var i=0; i<num; i++) {
 3186:                 if (form.elements[radioname][i].checked) {
 3187:                     if (form.elements[radioname][i].value == '1') {
 3188:                         if (document.getElementById(divid)) {
 3189:                             document.getElementById(divid).style.display = 'inline-block';
 3190:                         }
 3191:                         setvis = 1;
 3192:                     }
 3193:                     break;
 3194:                 }
 3195:             }
 3196:         }
 3197:         if (!setvis) {
 3198:             if (document.getElementById(divid)) {
 3199:                 document.getElementById(divid).style.display = 'none';
 3200:             }
 3201:         }
 3202:     }
 3203:     if (setting == 'user') {
 3204:         divid = 'ltitools_'+setting+'_div_'+item;
 3205:         var checkid = 'ltitools_'+setting+'_field_'+item;
 3206:         if (document.getElementById(divid)) {
 3207:             if (document.getElementById(checkid)) {
 3208:                 if (document.getElementById(checkid).checked) {
 3209:                     document.getElementById(divid).style.display = 'inline-block';
 3210:                 } else {
 3211:                     document.getElementById(divid).style.display = 'none';
 3212:                 }
 3213:             }
 3214:         }
 3215:     }
 3216:     return;
 3217: }
 3218: // ]]>
 3219: </script>
 3220: 
 3221: ENDSCRIPT
 3222: }
 3223: 
 3224: sub wafproxy_javascript {
 3225:     my ($dom) = @_;
 3226:     return <<"ENDSCRIPT";
 3227: <script type="text/javascript">
 3228: // <![CDATA[
 3229: function updateWAF() {
 3230:     if (document.getElementById('wafproxy_remoteip')) {
 3231:         var wafremote = 0;
 3232:         if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value == 'h') {
 3233:             wafremote = 1;
 3234:         }
 3235:         var fields = new Array('header','trust');
 3236:         for (var i=0; i<fields.length; i++) {
 3237:             if (document.getElementById('wafproxy_'+fields[i])) {
 3238:                 if (wafremote == 1) {
 3239:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'table-row';
 3240:                 }
 3241:                 else {
 3242:                     document.getElementById('wafproxy_'+fields[i]).style.display = 'none';
 3243:                 }
 3244:             }
 3245:         }
 3246:         if (document.getElementById('wafproxyranges_$dom')) {
 3247:             if (wafremote == 1) {
 3248:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3249:             } else {
 3250:                 for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3251:                     if (document.display.wafproxy_vpnaccess[i].checked) {
 3252:                         if (document.display.wafproxy_vpnaccess[i].value == 0) {
 3253:                             document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3254:                         }
 3255:                     }
 3256:                 }
 3257:             }
 3258:         }
 3259:     }
 3260:     return;
 3261: }
 3262: 
 3263: function checkWAF() {
 3264:     if (document.getElementById('wafproxy_remoteip')) {
 3265:         var wafvpn = 0;
 3266:         for (var i=0; i<document.display.wafproxy_vpnaccess.length; i++) {
 3267:             if (document.display.wafproxy_vpnaccess[i].checked) {
 3268:                 if (document.display.wafproxy_vpnaccess[i].value == 1) {
 3269:                     wafvpn = 1;
 3270:                 }
 3271:                 break;
 3272:             }
 3273:         }
 3274:         var vpn = new Array('vpnint','vpnext');
 3275:         for (var i=0; i<vpn.length; i++) {
 3276:             if (document.getElementById('wafproxy_show_'+vpn[i])) {
 3277:                 if (wafvpn == 1) {
 3278:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'table-row';
 3279:                 }
 3280:                 else {
 3281:                     document.getElementById('wafproxy_show_'+vpn[i]).style.display = 'none';
 3282:                 }
 3283:             }
 3284:         }
 3285:         if (document.getElementById('wafproxyranges_$dom')) {
 3286:             if (wafvpn == 1) {
 3287:                 document.getElementById('wafproxyranges_$dom').style.display = 'inline-block';
 3288:             }
 3289:             else if (document.display.wafproxy_remoteip.options[document.display.wafproxy_remoteip.selectedIndex].value != 'h') {
 3290:                 document.getElementById('wafproxyranges_$dom').style.display = 'none';
 3291:             }
 3292:         }
 3293:     }
 3294:     return;
 3295: }
 3296: 
 3297: function toggleWAF() {
 3298:     if (document.getElementById('wafproxy_table')) {
 3299:         var wafproxy = 0;
 3300:         for (var i=0; i<document.display.wafproxy_${dom}.length; i++) {
 3301:              if (document.display.wafproxy_${dom}[i].checked) {
 3302:                  if (document.display.wafproxy_${dom}[i].value == 1) {
 3303:                      wafproxy = 1;
 3304:                      break;
 3305:                 }
 3306:             }
 3307:         }
 3308:         if (wafproxy == 1) {
 3309:             document.getElementById('wafproxy_table').style.display='inline';
 3310:         }
 3311:         else {
 3312:            document.getElementById('wafproxy_table').style.display='none';
 3313:         }
 3314:         if (document.getElementById('wafproxyrow_${dom}')) {
 3315:             if (wafproxy == 1) {
 3316:                 document.getElementById('wafproxyrow_${dom}').style.display = 'table-row';
 3317:             }
 3318:             else {
 3319:                 document.getElementById('wafproxyrow_${dom}').style.display = 'none';
 3320:             }
 3321:         }
 3322:         if (document.getElementById('nowafproxyrow_$dom')) {
 3323:             if (wafproxy == 1) {
 3324:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'none';
 3325:             }
 3326:             else {
 3327:                 document.getElementById('nowafproxyrow_${dom}').style.display = 'table-row';
 3328:             }
 3329:         }
 3330:     }
 3331:     return;
 3332: }
 3333: // ]]>
 3334: </script>
 3335: 
 3336: ENDSCRIPT
 3337: }
 3338: 
 3339: sub proctoring_javascript {
 3340:     my ($settings) = @_;
 3341:     my (%ordered,$total,%jstext);
 3342:     $total = 0;
 3343:     if (ref($settings) eq 'HASH') {
 3344:         foreach my $item (keys(%{$settings})) {
 3345:             if (ref($settings->{$item}) eq 'HASH') {
 3346:                 my $num = $settings->{$item}{'order'};
 3347:                 $ordered{$num} = $item;
 3348:             }
 3349:         }
 3350:         $total = scalar(keys(%{$settings}));
 3351:     } else {
 3352:         %ordered = (
 3353:                        0 => 'proctorio',
 3354:                        1 => 'examity',
 3355:                    );
 3356:         $total = 2; 
 3357:     }
 3358:     my @jsarray = ();
 3359:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3360:         push(@jsarray,$ordered{$item});
 3361:     }
 3362:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 3363:     return <<"ENDSCRIPT";
 3364: <script type="text/javascript">
 3365: // <![CDATA[
 3366: function reorderProctoring(form,item) {
 3367:     var changedVal;
 3368: $jstext
 3369:     var maxh = $total;
 3370:     var current = new Array;
 3371:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3372:     for (var i=0; i<proctors.length; i++) {
 3373:         var elementName = 'proctoring_pos_'+proctors[i];
 3374:         if (elementName != item) {
 3375:             if (form.elements[elementName]) {
 3376:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3377:                 current[currVal] = elementName;
 3378:             }
 3379:         }
 3380:     }
 3381:     var oldVal;
 3382:     for (var j=0; j<maxh; j++) {
 3383:         if (current[j] == undefined) {
 3384:             oldVal = j;
 3385:         }
 3386:     }
 3387:     if (oldVal < changedVal) {
 3388:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3389:            var elementName = current[k];
 3390:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3391:         }
 3392:     } else {
 3393:         for (var k=changedVal; k<oldVal; k++) {
 3394:             var elementName = current[k];
 3395:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3396:         }
 3397:     }
 3398:     return;
 3399: }
 3400: 
 3401: function toggleProctoring(form,item) {
 3402:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 3403:     if (fieldsets.length) {
 3404:         var radioname = 'proctoring_available_'+item;
 3405:         var num = form.elements[radioname].length;
 3406:         if (num) {
 3407:             var setvis = '';
 3408:             for (var i=0; i<num; i++) {
 3409:                 if (form.elements[radioname][i].checked) {
 3410:                     if (form.elements[radioname][i].value == '1') {
 3411:                        setvis = 1;
 3412:                        break;
 3413:                     }
 3414:                 }
 3415:             }
 3416:             for (var j=0; j<fieldsets.length; j++) {
 3417:                 if (setvis) {
 3418:                     fieldsets[j].style.display = 'block';
 3419:                 } else {
 3420:                     fieldsets[j].style.display = 'none';
 3421:                 }
 3422:             }
 3423:         }
 3424:     }
 3425:     return;
 3426: }
 3427: 
 3428: // ]]>
 3429: </script>
 3430: 
 3431: ENDSCRIPT
 3432: }
 3433: 
 3434: 
 3435: sub lti_javascript {
 3436:     my ($dom,$settings) = @_;
 3437:     my $togglejs = &lti_toggle_js($dom);
 3438:     my $linkprot_js = &Apache::courseprefs::linkprot_javascript();
 3439:     unless (ref($settings) eq 'HASH') {
 3440:         return $togglejs.'
 3441: <script type="text/javascript">
 3442: // <![CDATA[
 3443: 
 3444: '.$linkprot_js.'
 3445: 
 3446: // ]]>
 3447: </script>
 3448: ';
 3449:     }
 3450:     my (%ordered,$total,%jstext);
 3451:     $total = scalar(keys(%{$settings}));
 3452:     foreach my $item (keys(%{$settings})) {
 3453:         if (ref($settings->{$item}) eq 'HASH') {
 3454:             my $num = $settings->{$item}{'order'};
 3455:             if ($num eq '') {
 3456:                 $num = $total - 1;
 3457:             }
 3458:             $ordered{$num} = $item;
 3459:         }
 3460:     }
 3461:     my @jsarray = ();
 3462:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3463:         push(@jsarray,$ordered{$item});
 3464:     }
 3465:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 3466:     return <<"ENDSCRIPT";
 3467: <script type="text/javascript">
 3468: // <![CDATA[
 3469: function reorderLTI(form,item) {
 3470:     var changedVal;
 3471: $jstext
 3472:     var newpos = 'lti_pos_add';
 3473:     var maxh = 1 + $total;
 3474:     var current = new Array;
 3475:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3476:     if (item == newpos) {
 3477:         changedVal = newitemVal;
 3478:     } else {
 3479:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3480:         current[newitemVal] = newpos;
 3481:     }
 3482:     for (var i=0; i<lti.length; i++) {
 3483:         var elementName = 'lti_pos_'+lti[i];
 3484:         if (elementName != item) {
 3485:             if (form.elements[elementName]) {
 3486:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3487:                 current[currVal] = elementName;
 3488:             }
 3489:         }
 3490:     }
 3491:     var oldVal;
 3492:     for (var j=0; j<maxh; j++) {
 3493:         if (current[j] == undefined) {
 3494:             oldVal = j;
 3495:         }
 3496:     }
 3497:     if (oldVal < changedVal) {
 3498:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3499:            var elementName = current[k];
 3500:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3501:         }
 3502:     } else {
 3503:         for (var k=changedVal; k<oldVal; k++) {
 3504:             var elementName = current[k];
 3505:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3506:         }
 3507:     }
 3508:     return;
 3509: }
 3510: 
 3511: $linkprot_js
 3512: 
 3513: // ]]>
 3514: </script>
 3515: 
 3516: $togglejs
 3517: 
 3518: ENDSCRIPT
 3519: }
 3520: 
 3521: sub lti_toggle_js {
 3522:     my ($dom) = @_;
 3523:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3524:                             localauth => 'Local auth argument',
 3525:                             krb       => 'Kerberos domain',
 3526:                          );
 3527:     my $crsincalert = &mt('"User\'s identity sent" needs to be set to "Yes" first,[_1] before setting "Course\'s identity sent" to "Yes"',"\n");
 3528:     &js_escape(\$crsincalert);
 3529:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 3530:     my $primary = &Apache::lonnet::domain($dom,'primary');
 3531:     my $course_servers = "'".join("','",keys(%servers))."'";
 3532: 
 3533:     return <<"ENDSCRIPT";
 3534: <script type="text/javascript">
 3535: // <![CDATA[
 3536: 
 3537: function toggleLTI(form,setting,item) {
 3538:     if ((setting == 'requser') || (setting == 'crsinc')) {
 3539:         var usrfieldsets = document.getElementsByClassName('ltioption_usr_'+item);
 3540:         var setvis = '';
 3541:         var radioname = 'lti_requser_'+item;
 3542:         var num = form.elements[radioname].length;
 3543:         if (num) {
 3544:             for (var i=0; i<num; i++) {
 3545:                 if (form.elements[radioname][i].checked) {
 3546:                     if (form.elements[radioname][i].value == '1') {
 3547:                         setvis = 1;
 3548:                         break;
 3549:                     }
 3550:                 }
 3551:             }
 3552:         }
 3553:         if (usrfieldsets.length) {
 3554:             for (var j=0; j<usrfieldsets.length; j++) {
 3555:                 if (setvis) {
 3556:                     usrfieldsets[j].style.display = 'block';
 3557:                 } else {
 3558:                     usrfieldsets[j].style.display = 'none';
 3559:                 }
 3560:             }
 3561:         }
 3562:         var crsfieldsets = document.getElementsByClassName('ltioption_crs_'+item);
 3563:         if (crsfieldsets.length) {
 3564:             radioname = 'lti_crsinc_'+item;
 3565:             var num = form.elements[radioname].length;
 3566:             if (num) {
 3567:                 var crsvis = '';
 3568:                 for (var i=0; i<num; i++) {
 3569:                     if (form.elements[radioname][i].checked) {
 3570:                         if (form.elements[radioname][i].value == '1') {
 3571:                             if (setvis == '') {
 3572:                                 if (setting == 'crsinc'){
 3573:                                     alert("$crsincalert");
 3574:                                     form.elements[radioname][0].checked = true;
 3575:                                 }
 3576:                             } else {
 3577:                                 crsvis = 1;
 3578:                             }
 3579:                             break;
 3580:                         }
 3581:                     }
 3582:                 }
 3583:                 setvis = crsvis;
 3584:             }
 3585:             for (var j=0; j<crsfieldsets.length; j++) {
 3586:                 if (setvis) {
 3587:                     crsfieldsets[j].style.display = 'block';
 3588:                 } else {
 3589:                     crsfieldsets[j].style.display = 'none';
 3590:                 }
 3591:             }
 3592:         }
 3593:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3594:         var radioname = '';
 3595:         var divid = '';
 3596:         if (setting == 'user') {
 3597:             radioname = 'lti_mapuser_'+item;
 3598:             divid = 'lti_userfield_'+item;
 3599:         } else if (setting == 'crs') {
 3600:             radioname = 'lti_mapcrs_'+item;
 3601:             divid = 'lti_crsfield_'+item;
 3602:         } else if (setting == 'callback') {
 3603:             radioname = 'lti_callback_'+item;
 3604:             divid = 'lti_callbackfield_'+item;
 3605:         } else {
 3606:             radioname = 'lti_passback_'+item;
 3607:             divid =  'lti_passback_'+item;
 3608:         }
 3609:         var num = form.elements[radioname].length;
 3610:         if (num) {
 3611:             var setvis = '';
 3612:             for (var i=0; i<num; i++) {
 3613:                if (form.elements[radioname][i].checked) {
 3614:                    if ((setting == 'passback') || (setting == 'callback')) {
 3615:                        if (form.elements[radioname][i].value == '1') {
 3616:                            if (document.getElementById(divid)) {
 3617:                                document.getElementById(divid).style.display = 'inline-block';
 3618:                            }
 3619:                            setvis = 1;
 3620:                            break;
 3621:                        }
 3622:                    } else {
 3623:                        if (form.elements[radioname][i].value == 'other') {
 3624:                            if (document.getElementById(divid)) {
 3625:                                document.getElementById(divid).style.display = 'inline-block';
 3626:                            }
 3627:                            setvis = 1;
 3628:                            break;
 3629:                        }
 3630:                    }
 3631:                } 
 3632:             }
 3633:             if (!setvis) {
 3634:                 if (document.getElementById(divid)) {
 3635:                     document.getElementById(divid).style.display = 'none';
 3636:                 }
 3637:             }
 3638:         }
 3639:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3640:         var numsec = form.elements['lti_crssec_'+item].length;
 3641:         if (numsec) {
 3642:             var setvis = '';
 3643:             for (var i=0; i<numsec; i++) {
 3644:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3645:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3646:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3647:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3648:                             setvis = 1;
 3649:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3650:                             if (numsrcsec) {
 3651:                                 var setsrcvis = '';
 3652:                                 for (var j=0; j<numsrcsec; j++) {
 3653:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3654:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3655:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3656:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3657:                                                 setsrcvis = 1;
 3658:                                             }
 3659:                                         }
 3660:                                     }
 3661:                                 }
 3662:                                 if (!setsrcvis) {
 3663:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3664:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3665:                                     }
 3666:                                 }
 3667:                             }
 3668:                         }
 3669:                     }
 3670:                 }
 3671:             }
 3672:             if (!setvis) {
 3673:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3674:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3675:                 }
 3676:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3677:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3678:                 }
 3679:             }
 3680:         }
 3681:     } else if (setting == 'lcauth') {
 3682:         var numauth = form.elements['lti_lcauth_'+item].length;
 3683:         if (numauth) {
 3684:             for (var i=0; i<numauth; i++) {
 3685:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3686:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3687:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3688:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3689:                         } else {
 3690:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3691:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3692:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3693:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3694:                                 } else {
 3695:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3696:                                 }
 3697:                             }
 3698:                         }
 3699:                     }
 3700:                 }
 3701:             }
 3702:         }
 3703:     } else if (setting == 'lcmenu') {
 3704:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3705:         var divid = 'lti_menufield_'+item;
 3706:         var setvis = '';
 3707:         for (var i=0; i<menus.length; i++) {
 3708:             var radioname = menus[i];  
 3709:             var num = form.elements[radioname].length;
 3710:             if (num) {
 3711:                 for (var j=0; j<num; j++) {
 3712:                     if (form.elements[radioname][j].checked) {
 3713:                         if (form.elements[radioname][j].value == '1') {
 3714:                             if (document.getElementById(divid)) {
 3715:                                 document.getElementById(divid).style.display = 'inline-block';
 3716:                             }
 3717:                             setvis = 1;
 3718:                             break;
 3719:                         }
 3720:                     }
 3721:                 }
 3722:             }
 3723:             if (setvis == 1) {
 3724:                 break;
 3725:             }
 3726:         }
 3727:         if (!setvis) {
 3728:             if (document.getElementById(divid)) {
 3729:                 document.getElementById(divid).style.display = 'none';
 3730:             }
 3731:         }
 3732:     }
 3733:     return;
 3734: }
 3735: 
 3736: // ]]>
 3737: </script>
 3738: 
 3739: ENDSCRIPT
 3740: }
 3741: 
 3742: sub autoupdate_javascript {
 3743:     return <<"ENDSCRIPT";
 3744: <script type="text/javascript">
 3745: // <![CDATA[
 3746: function toggleLastActiveDays(form) {
 3747:     var radioname = 'lastactive';
 3748:     var divid = 'lastactive_div';
 3749:     var num = form.elements[radioname].length;
 3750:     if (num) {
 3751:         var setvis = '';
 3752:         for (var i=0; i<num; i++) {
 3753:             if (form.elements[radioname][i].checked) {
 3754:                 if (form.elements[radioname][i].value == '1') {
 3755:                     if (document.getElementById(divid)) {
 3756:                         document.getElementById(divid).style.display = 'inline-block';
 3757:                     }
 3758:                     setvis = 1;
 3759:                 }
 3760:                 break;
 3761:             }
 3762:         }
 3763:         if (!setvis) {
 3764:             if (document.getElementById(divid)) {
 3765:                 document.getElementById(divid).style.display = 'none';
 3766:             }
 3767:         }
 3768:     }
 3769:     return;
 3770: }
 3771: // ]]>
 3772: </script>
 3773: 
 3774: ENDSCRIPT
 3775: }
 3776: 
 3777: sub autoenroll_javascript {
 3778:     return <<"ENDSCRIPT";
 3779: <script type="text/javascript">
 3780: // <![CDATA[
 3781: function toggleFailsafe(form) {
 3782:     var radioname = 'autoenroll_failsafe';
 3783:     var divid = 'autoenroll_failsafe_div';
 3784:     var num = form.elements[radioname].length;
 3785:     if (num) {
 3786:         var setvis = '';
 3787:         for (var i=0; i<num; i++) {
 3788:             if (form.elements[radioname][i].checked) {
 3789:                 if ((form.elements[radioname][i].value == 'zero') || (form.elements[radioname][i].value == 'any')) {
 3790:                     if (document.getElementById(divid)) {
 3791:                         document.getElementById(divid).style.display = 'inline-block';
 3792:                     }
 3793:                     setvis = 1;
 3794:                 }
 3795:                 break;
 3796:             }
 3797:         }
 3798:         if (!setvis) {
 3799:             if (document.getElementById(divid)) {
 3800:                 document.getElementById(divid).style.display = 'none';
 3801:             }
 3802:         }
 3803:     }
 3804:     return;
 3805: }
 3806: // ]]>
 3807: </script>
 3808: 
 3809: ENDSCRIPT
 3810: }
 3811: 
 3812: sub saml_javascript {
 3813:     return <<"ENDSCRIPT";
 3814: <script type="text/javascript">
 3815: // <![CDATA[
 3816: function toggleSamlOptions(form,hostid) { 
 3817:     var radioname = 'saml_'+hostid;
 3818:     var tablecellon = 'samloptionson_'+hostid;
 3819:     var tablecelloff = 'samloptionsoff_'+hostid;
 3820:     var num = form.elements[radioname].length;
 3821:     if (num) {
 3822:         var setvis = ''; 
 3823:         for (var i=0; i<num; i++) {
 3824:             if (form.elements[radioname][i].checked) {
 3825:                 if (form.elements[radioname][i].value == '1') { 
 3826:                     if (document.getElementById(tablecellon)) {
 3827:                         document.getElementById(tablecellon).style.display='';
 3828:                     }
 3829:                     if (document.getElementById(tablecelloff)) {
 3830:                         document.getElementById(tablecelloff).style.display='none';
 3831:                     }
 3832:                     setvis = 1;
 3833:                 }
 3834:                 break;
 3835:             }
 3836:         }
 3837:         if (!setvis) {
 3838:             if (document.getElementById(tablecellon)) {
 3839:                 document.getElementById(tablecellon).style.display='none';
 3840:             }
 3841:             if (document.getElementById(tablecelloff)) {
 3842:                 document.getElementById(tablecelloff).style.display='';
 3843:             }
 3844:         }
 3845:     }
 3846:     return;
 3847: }
 3848: // ]]>
 3849: </script>
 3850: 
 3851: ENDSCRIPT
 3852: }
 3853: 
 3854: sub ipaccess_javascript {
 3855:     my ($settings) = @_;
 3856:     my (%ordered,$total,%jstext);
 3857:     $total = 0;
 3858:     if (ref($settings) eq 'HASH') {
 3859:         foreach my $item (keys(%{$settings})) {
 3860:             if (ref($settings->{$item}) eq 'HASH') {
 3861:                 my $num = $settings->{$item}{'order'};
 3862:                 $ordered{$num} = $item;
 3863:             }
 3864:         }
 3865:         $total = scalar(keys(%{$settings}));
 3866:     }
 3867:     my @jsarray = ();
 3868:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3869:         push(@jsarray,$ordered{$item});
 3870:     }
 3871:     my $jstext = '    var ipaccess = Array('."'".join("','",@jsarray)."'".');'."\n";
 3872:     return <<"ENDSCRIPT";
 3873: <script type="text/javascript">
 3874: // <![CDATA[
 3875: function reorderIPaccess(form,item) {
 3876:     var changedVal;
 3877: $jstext
 3878:     var newpos = 'ipaccess_pos_add';
 3879:     var maxh = 1 + $total;
 3880:     var current = new Array;
 3881:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3882:     if (item == newpos) {
 3883:         changedVal = newitemVal;
 3884:     } else {
 3885:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3886:         current[newitemVal] = newpos;
 3887:     }
 3888:     for (var i=0; i<ipaccess.length; i++) {
 3889:         var elementName = 'ipaccess_pos_'+ipaccess[i];
 3890:         if (elementName != item) {
 3891:             if (form.elements[elementName]) {
 3892:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3893:                 current[currVal] = elementName;
 3894:             }
 3895:         }
 3896:     }
 3897:     var oldVal;
 3898:     for (var j=0; j<maxh; j++) {
 3899:         if (current[j] == undefined) {
 3900:             oldVal = j;
 3901:         }
 3902:     }
 3903:     if (oldVal < changedVal) {
 3904:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3905:            var elementName = current[k];
 3906:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3907:         }
 3908:     } else {
 3909:         for (var k=changedVal; k<oldVal; k++) {
 3910:             var elementName = current[k];
 3911:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3912:         }
 3913:     }
 3914:     return;
 3915: }
 3916: // ]]>
 3917: </script>
 3918: 
 3919: ENDSCRIPT
 3920: }
 3921: 
 3922: sub print_autoenroll {
 3923:     my ($dom,$settings,$rowtotal) = @_;
 3924:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3925:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,
 3926:         $failsafe,$autofailsafe,$failsafesty,%failsafechecked);
 3927:     $failsafesty = 'none';
 3928:     %failsafechecked = (
 3929:         off => ' checked="checked"',
 3930:     );
 3931:     if (ref($settings) eq 'HASH') {
 3932:         if (exists($settings->{'run'})) {
 3933:             if ($settings->{'run'} eq '0') {
 3934:                 $runoff = ' checked="checked" ';
 3935:                 $runon = ' ';
 3936:             } else {
 3937:                 $runon = ' checked="checked" ';
 3938:                 $runoff = ' ';
 3939:             }
 3940:         } else {
 3941:             if ($autorun) {
 3942:                 $runon = ' checked="checked" ';
 3943:                 $runoff = ' ';
 3944:             } else {
 3945:                 $runoff = ' checked="checked" ';
 3946:                 $runon = ' ';
 3947:             }
 3948:         }
 3949:         if (exists($settings->{'co-owners'})) {
 3950:             if ($settings->{'co-owners'} eq '0') {
 3951:                 $coownersoff = ' checked="checked" ';
 3952:                 $coownerson = ' ';
 3953:             } else {
 3954:                 $coownerson = ' checked="checked" ';
 3955:                 $coownersoff = ' ';
 3956:             }
 3957:         } else {
 3958:             $coownersoff = ' checked="checked" ';
 3959:             $coownerson = ' ';
 3960:         }
 3961:         if (exists($settings->{'sender_domain'})) {
 3962:             $defdom = $settings->{'sender_domain'};
 3963:         }
 3964:         if (exists($settings->{'failsafe'})) {
 3965:             $failsafe = $settings->{'failsafe'};
 3966:             if ($failsafe eq 'zero') {
 3967:                 $failsafechecked{'zero'} = ' checked="checked"';
 3968:                 $failsafechecked{'off'} = '';
 3969:                 $failsafesty = 'inline-block';
 3970:             } elsif ($failsafe eq 'any') {
 3971:                 $failsafechecked{'any'} = ' checked="checked"';
 3972:                 $failsafechecked{'off'} = '';
 3973:             }
 3974:             $autofailsafe = $settings->{'autofailsafe'};
 3975:         } elsif (exists($settings->{'autofailsafe'})) {
 3976:             $autofailsafe = $settings->{'autofailsafe'};
 3977:             if ($autofailsafe ne '') {
 3978:                 $failsafechecked{'zero'} = ' checked="checked"';
 3979:                 $failsafe = 'zero';
 3980:                 $failsafechecked{'off'} = '';
 3981:             }
 3982:         }
 3983:     } else {
 3984:         if ($autorun) {
 3985:             $runon = ' checked="checked" ';
 3986:             $runoff = ' ';
 3987:         } else {
 3988:             $runoff = ' checked="checked" ';
 3989:             $runon = ' ';
 3990:         }
 3991:     }
 3992:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3993:     my $notif_sender;
 3994:     if (ref($settings) eq 'HASH') {
 3995:         $notif_sender = $settings->{'sender_uname'};
 3996:     }
 3997:     my $datatable='<tr class="LC_odd_row">'.
 3998:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3999:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4000:                   '<input type="radio" name="autoenroll_run"'.
 4001:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4002:                   '<label><input type="radio" name="autoenroll_run"'.
 4003:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4004:                   '</tr><tr>'.
 4005:                   '<td>'.&mt('Notification messages - sender').
 4006:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 4007:                   &mt('username').':&nbsp;'.
 4008:                   '<input type="text" name="sender_uname" value="'.
 4009:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 4010:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 4011:                   '<tr class="LC_odd_row">'.
 4012:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 4013:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4014:                   '<input type="radio" name="autoassign_coowners"'.
 4015:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4016:                   '<label><input type="radio" name="autoassign_coowners"'.
 4017:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4018:                   '</tr><tr>'.
 4019:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 4020:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 4021:                   '<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; '.
 4022:                   '<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 />'.
 4023:                   '<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>'.
 4024:                   '<div class="LC_floatleft" style="display:'.$failsafesty.';" id="autoenroll_failsafe_div">'.
 4025:                   '<span class="LC_nobreak">'.
 4026:                   &mt('Threshold for number of students in section to drop: [_1]',
 4027:                       '<input type="text" name="autoenroll_autofailsafe" value="'.$autofailsafe.'" size="4" />').
 4028:                   '</span></div></td></tr>';
 4029:     $$rowtotal += 4;
 4030:     return $datatable;
 4031: }
 4032: 
 4033: sub print_autoupdate {
 4034:     my ($position,$dom,$settings,$rowtotal) = @_;
 4035:     my ($enable,$datatable);
 4036:     if ($position eq 'top') {
 4037:         my %choices = &Apache::lonlocal::texthash (
 4038:                           run        => 'Auto-update active?',
 4039:                           classlists => 'Update information in classlists?',
 4040:                           unexpired  => 'Skip updates for users without active or future roles?',
 4041:                           lastactive => 'Skip updates for inactive users?',
 4042:         );
 4043:         my $itemcount = 0;
 4044:         my $updateon = ' ';
 4045:         my $updateoff = ' checked="checked" ';
 4046:         if (ref($settings) eq 'HASH') {
 4047:             if ($settings->{'run'} eq '1') {
 4048:                 $updateon = $updateoff;
 4049:                 $updateoff = ' ';
 4050:             }
 4051:         }
 4052:         $enable = '<tr class="LC_odd_row">'.
 4053:                   '<td>'.$choices{'run'}.'</td>'.
 4054:                   '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4055:                   '<input type="radio" name="autoupdate_run"'.
 4056:                   $updateoff.'value="0" />'.&mt('No').'</label>&nbsp;'.
 4057:                   '<label><input type="radio" name="autoupdate_run"'.
 4058:                   $updateon.'value="1" />'.&mt('Yes').'</label></span></td>'.
 4059:                   '</tr>';
 4060:         my @toggles = ('classlists','unexpired');
 4061:         my %defaultchecked = ('classlists' => 'off',
 4062:                               'unexpired'  => 'off'
 4063:                               );
 4064:         $$rowtotal ++;
 4065:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4066:                                                      \%choices,$itemcount,'','','left','no');
 4067:         $datatable = $enable.$datatable;
 4068:         $$rowtotal += $itemcount;
 4069:         my $lastactiveon = ' ';
 4070:         my $lastactiveoff = ' checked="checked" ';
 4071:         my $lastactivestyle = 'none';
 4072:         my $lastactivedays;
 4073:         my $onclick = ' onclick="javascript:toggleLastActiveDays(this.form);"';
 4074:         if (ref($settings) eq 'HASH') {
 4075:             if ($settings->{'lastactive'} =~ /^\d+$/) {
 4076:                 $lastactiveon = $lastactiveoff;
 4077:                 $lastactiveoff = ' ';
 4078:                 $lastactivestyle = 'inline-block';
 4079:                 $lastactivedays = $settings->{'lastactive'};
 4080:             }
 4081:         }
 4082:         my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4083:         $datatable .= '<tr'.$css_class.'>'.
 4084:                       '<td>'.$choices{'lastactive'}.'</td>'.
 4085:                       '<td class="LC_left_item"><span class="LC_nobreak"><label>'.
 4086:                       '<input type="radio" name="lastactive"'.
 4087:                       $lastactiveoff.'value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4088:                       '&nbsp;<label>'.
 4089:                       '<input type="radio" name="lastactive"'.
 4090:                       $lastactiveon.' value="1"'.$onclick.' />'.&mt('Yes').'</label>'.
 4091:                       '<div id="lastactive_div" style="display:'.$lastactivestyle.';">'.
 4092:                       ':&nbsp;'.&mt('inactive = no activity in last [_1] days',
 4093:                           '<input type="text" size="5" name="lastactivedays" value="'.
 4094:                           $lastactivedays.'" />').
 4095:                       '</span></td>'.
 4096:                       '</tr>';
 4097:         $$rowtotal ++;
 4098:     } elsif ($position eq 'middle') {
 4099:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4100:         my $numinrow = 3;
 4101:         my $locknamesettings;
 4102:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 4103:                                      $dom,$numinrow,$othertitle,
 4104:                                     'lockablenames',$rowtotal);
 4105:         $$rowtotal ++;
 4106:     } else {
 4107:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4108:         my @fields = ('lastname','firstname','middlename','generation',
 4109:                       'permanentemail','id');
 4110:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 4111:         my $numrows = 0;
 4112:         if (ref($types) eq 'ARRAY') {
 4113:             if (@{$types} > 0) {
 4114:                 $datatable = 
 4115:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 4116:                                          \@fields,$types,\$numrows);
 4117:                     $$rowtotal += @{$types}; 
 4118:             }
 4119:         }
 4120:         $datatable .= 
 4121:             &usertype_update_row($settings,{'default' => $othertitle},
 4122:                                  \%fieldtitles,\@fields,['default'],
 4123:                                  \$numrows);
 4124:         $$rowtotal ++;     
 4125:     }
 4126:     return $datatable;
 4127: }
 4128: 
 4129: sub print_autocreate {
 4130:     my ($dom,$settings,$rowtotal) = @_;
 4131:     my (%createon,%createoff,%currhash);
 4132:     my @types = ('xml','req');
 4133:     if (ref($settings) eq 'HASH') {
 4134:         foreach my $item (@types) {
 4135:             $createoff{$item} = ' checked="checked" ';
 4136:             $createon{$item} = ' ';
 4137:             if (exists($settings->{$item})) {
 4138:                 if ($settings->{$item}) {
 4139:                     $createon{$item} = ' checked="checked" ';
 4140:                     $createoff{$item} = ' ';
 4141:                 }
 4142:             }
 4143:         }
 4144:         if ($settings->{'xmldc'} ne '') {
 4145:             $currhash{$settings->{'xmldc'}} = 1;
 4146:         }
 4147:     } else {
 4148:         foreach my $item (@types) {
 4149:             $createoff{$item} = ' checked="checked" ';
 4150:             $createon{$item} = ' ';
 4151:         }
 4152:     }
 4153:     $$rowtotal += 2;
 4154:     my $numinrow = 2;
 4155:     my $datatable='<tr class="LC_odd_row">'.
 4156:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 4157:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4158:                   '<input type="radio" name="autocreate_xml"'.
 4159:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4160:                   '<label><input type="radio" name="autocreate_xml"'.
 4161:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 4162:                   '</td></tr><tr>'.
 4163:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 4164:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4165:                   '<input type="radio" name="autocreate_req"'.
 4166:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4167:                   '<label><input type="radio" name="autocreate_req"'.
 4168:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 4169:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4170:                                                    'autocreate_xmldc',%currhash);
 4171:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 4172:     if ($numdc > 1) {
 4173:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 4174:                       '</td><td class="LC_left_item">';
 4175:     } else {
 4176:         $datatable .= &mt('Course creation processed as:').
 4177:                       '</td><td class="LC_right_item">';
 4178:     }
 4179:     $datatable .= $dctable.'</td></tr>';
 4180:     $$rowtotal += $rows;
 4181:     return $datatable;
 4182: }
 4183: 
 4184: sub print_directorysrch {
 4185:     my ($position,$dom,$settings,$rowtotal) = @_;
 4186:     my $datatable;
 4187:     if ($position eq 'top') {
 4188:         my $instsrchon = ' ';
 4189:         my $instsrchoff = ' checked="checked" ';
 4190:         my ($exacton,$containson,$beginson);
 4191:         my $instlocalon = ' ';
 4192:         my $instlocaloff = ' checked="checked" ';
 4193:         if (ref($settings) eq 'HASH') {
 4194:             if ($settings->{'available'} eq '1') {
 4195:                 $instsrchon = $instsrchoff;
 4196:                 $instsrchoff = ' ';
 4197:             }
 4198:             if ($settings->{'localonly'} eq '1') {
 4199:                 $instlocalon = $instlocaloff;
 4200:                 $instlocaloff = ' ';
 4201:             }
 4202:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 4203:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 4204:                     if ($type eq 'exact') {
 4205:                         $exacton = ' checked="checked" ';
 4206:                     } elsif ($type eq 'contains') {
 4207:                         $containson = ' checked="checked" ';
 4208:                     } elsif ($type eq 'begins') {
 4209:                         $beginson = ' checked="checked" ';
 4210:                     }
 4211:                 }
 4212:             } else {
 4213:                 if ($settings->{'searchtypes'} eq 'exact') {
 4214:                     $exacton = ' checked="checked" ';
 4215:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 4216:                     $containson = ' checked="checked" ';
 4217:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 4218:                     $exacton = ' checked="checked" ';
 4219:                     $containson = ' checked="checked" ';
 4220:                 }
 4221:             }
 4222:         }
 4223:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 4224:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4225: 
 4226:         my $numinrow = 4;
 4227:         my $cansrchrow = 0;
 4228:         $datatable='<tr class="LC_odd_row">'.
 4229:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 4230:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4231:                    '<input type="radio" name="dirsrch_available"'.
 4232:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4233:                    '<label><input type="radio" name="dirsrch_available"'.
 4234:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4235:                    '</tr><tr>'.
 4236:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 4237:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4238:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 4239:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4240:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 4241:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4242:                    '</tr>';
 4243:         $$rowtotal += 2;
 4244:         if (ref($usertypes) eq 'HASH') {
 4245:             if (keys(%{$usertypes}) > 0) {
 4246:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4247:                                              $numinrow,$othertitle,'cansearch',
 4248:                                              $rowtotal);
 4249:                 $cansrchrow = 1;
 4250:             }
 4251:         }
 4252:         if ($cansrchrow) {
 4253:             $$rowtotal ++;
 4254:             $datatable .= '<tr>';
 4255:         } else {
 4256:             $datatable .= '<tr class="LC_odd_row">';
 4257:         }
 4258:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 4259:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 4260:         foreach my $title (@{$titleorder}) {
 4261:             if (defined($searchtitles->{$title})) {
 4262:                 my $check = ' ';
 4263:                 if (ref($settings) eq 'HASH') {
 4264:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 4265:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 4266:                             $check = ' checked="checked" ';
 4267:                         }
 4268:                     }
 4269:                 }
 4270:                 $datatable .= '<td class="LC_left_item">'.
 4271:                               '<span class="LC_nobreak"><label>'.
 4272:                               '<input type="checkbox" name="searchby" '.
 4273:                               'value="'.$title.'"'.$check.'/>'.
 4274:                               $searchtitles->{$title}.'</label></span></td>';
 4275:             }
 4276:         }
 4277:         $datatable .= '</tr></table></td></tr>';
 4278:         $$rowtotal ++;
 4279:         if ($cansrchrow) {
 4280:             $datatable .= '<tr class="LC_odd_row">';
 4281:         } else {
 4282:             $datatable .= '<tr>';
 4283:         }
 4284:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 4285:                       '<td class="LC_left_item" colspan="2">'.
 4286:                       '<span class="LC_nobreak"><label>'.
 4287:                       '<input type="checkbox" name="searchtypes" '.
 4288:                       $exacton.' value="exact" />'.&mt('Exact match').
 4289:                       '</label>&nbsp;'.
 4290:                       '<label><input type="checkbox" name="searchtypes" '.
 4291:                       $beginson.' value="begins" />'.&mt('Begins with').
 4292:                       '</label>&nbsp;'.
 4293:                       '<label><input type="checkbox" name="searchtypes" '.
 4294:                       $containson.' value="contains" />'.&mt('Contains').
 4295:                       '</label></span></td></tr>';
 4296:         $$rowtotal ++;
 4297:     } else {
 4298:         my $domsrchon = ' checked="checked" ';
 4299:         my $domsrchoff = ' ';
 4300:         my $domlocalon = ' ';
 4301:         my $domlocaloff = ' checked="checked" ';
 4302:         if (ref($settings) eq 'HASH') {
 4303:             if ($settings->{'lclocalonly'} eq '1') {
 4304:                 $domlocalon = $domlocaloff;
 4305:                 $domlocaloff = ' ';
 4306:             }
 4307:             if ($settings->{'lcavailable'} eq '0') {
 4308:                 $domsrchoff = $domsrchon;
 4309:                 $domsrchon = ' ';
 4310:             }
 4311:         }
 4312:         $datatable='<tr class="LC_odd_row">'.
 4313:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 4314:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4315:                       '<input type="radio" name="dirsrch_domavailable"'.
 4316:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 4317:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 4318:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 4319:                       '</tr><tr>'.
 4320:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 4321:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 4322:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 4323:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 4324:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 4325:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 4326:                       '</tr>';
 4327:         $$rowtotal += 2;
 4328:     }
 4329:     return $datatable;
 4330: }
 4331: 
 4332: sub print_contacts {
 4333:     my ($position,$dom,$settings,$rowtotal) = @_;
 4334:     my $datatable;
 4335:     my @contacts = ('adminemail','supportemail');
 4336:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 4337:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 4338:     if ($position eq 'top') {
 4339:         if (ref($settings) eq 'HASH') {
 4340:             foreach my $item (@contacts) {
 4341:                 if (exists($settings->{$item})) {
 4342:                     $to{$item} = $settings->{$item};
 4343:                 }
 4344:             }
 4345:         }
 4346:     } elsif ($position eq 'middle') {
 4347:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 4348:                      'updatesmail','idconflictsmail','hostipmail');
 4349:         foreach my $type (@mailings) {
 4350:             $otheremails{$type} = '';
 4351:         }
 4352:     } elsif ($position eq 'lower') {
 4353:         if (ref($settings) eq 'HASH') {
 4354:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 4355:                 %lonstatus = %{$settings->{'lonstatus'}};
 4356:             }
 4357:         }
 4358:     } else {
 4359:         @mailings = ('helpdeskmail','otherdomsmail');
 4360:         foreach my $type (@mailings) {
 4361:             $otheremails{$type} = '';
 4362:         }
 4363:         $bccemails{'helpdeskmail'} = '';
 4364:         $bccemails{'otherdomsmail'} = '';
 4365:         $includestr{'helpdeskmail'} = '';
 4366:         $includestr{'otherdomsmail'} = '';
 4367:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 4368:     }
 4369:     if (ref($settings) eq 'HASH') {
 4370:         unless (($position eq 'top') || ($position eq 'lower')) {
 4371:             foreach my $type (@mailings) {
 4372:                 if (exists($settings->{$type})) {
 4373:                     if (ref($settings->{$type}) eq 'HASH') {
 4374:                         foreach my $item (@contacts) {
 4375:                             if ($settings->{$type}{$item}) {
 4376:                                 $checked{$type}{$item} = ' checked="checked" ';
 4377:                             }
 4378:                         }
 4379:                         $otheremails{$type} = $settings->{$type}{'others'};
 4380:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4381:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 4382:                             if ($settings->{$type}{'include'} ne '') {
 4383:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4384:                                 $includestr{$type} = &unescape($includestr{$type});
 4385:                             }
 4386:                         }
 4387:                     }
 4388:                 } elsif ($type eq 'lonstatusmail') {
 4389:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4390:                 }
 4391:             }
 4392:         }
 4393:         if ($position eq 'bottom') {
 4394:             foreach my $type (@mailings) {
 4395:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 4396:                 if ($settings->{$type}{'include'} ne '') {
 4397:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 4398:                     $includestr{$type} = &unescape($includestr{$type});
 4399:                 }
 4400:             }
 4401:             if (ref($settings->{'helpform'}) eq 'HASH') {
 4402:                 if (ref($fields) eq 'ARRAY') {
 4403:                     foreach my $field (@{$fields}) {
 4404:                         $currfield{$field} = $settings->{'helpform'}{$field};
 4405:                     }
 4406:                 }
 4407:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 4408:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 4409:                 } else {
 4410:                     $maxsize = '1.0';
 4411:                 }
 4412:             } else {
 4413:                 if (ref($fields) eq 'ARRAY') {
 4414:                     foreach my $field (@{$fields}) {
 4415:                         $currfield{$field} = 'yes';
 4416:                     }
 4417:                 }
 4418:                 $maxsize = '1.0';
 4419:             }
 4420:         }
 4421:     } else {
 4422:         if ($position eq 'top') {
 4423:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 4424:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 4425:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 4426:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 4427:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 4428:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 4429:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 4430:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 4431:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 4432:         } elsif ($position eq 'bottom') {
 4433:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 4434:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 4435:             if (ref($fields) eq 'ARRAY') {
 4436:                 foreach my $field (@{$fields}) {
 4437:                     $currfield{$field} = 'yes';
 4438:                 }
 4439:             }
 4440:             $maxsize = '1.0';
 4441:         }
 4442:     }
 4443:     my ($titles,$short_titles) = &contact_titles();
 4444:     my $rownum = 0;
 4445:     my $css_class;
 4446:     if ($position eq 'top') {
 4447:         foreach my $item (@contacts) {
 4448:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4449:             $datatable .= '<tr'.$css_class.'>'. 
 4450:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 4451:                           '</span></td><td class="LC_right_item">'.
 4452:                           '<input type="text" name="'.$item.'" value="'.
 4453:                           $to{$item}.'" /></td></tr>';
 4454:             $rownum ++;
 4455:         }
 4456:     } elsif ($position eq 'bottom') {
 4457:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4458:         $datatable .= '<tr'.$css_class.'>'.
 4459:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 4460:                       &mt('(e-mail, subject, and description always shown)').
 4461:                       '</td><td class="LC_left_item">';
 4462:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 4463:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 4464:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 4465:             foreach my $field (@{$fields}) {
 4466:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 4467:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 4468:                     $datatable .= ' '.&mt('(logged-in users)');
 4469:                 }
 4470:                 $datatable .='</td><td>';
 4471:                 my $clickaction;
 4472:                 if ($field eq 'screenshot') {
 4473:                     $clickaction = ' onclick="screenshotSize(this);"';
 4474:                 }
 4475:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 4476:                     foreach my $option (@{$possoptions->{$field}}) {
 4477:                         my $checked;
 4478:                         if ($currfield{$field} eq $option) {
 4479:                             $checked = ' checked="checked"';
 4480:                         }
 4481:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4482:                                       '<input type="radio" name="helpform_'.$field.'" '.
 4483:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 4484:                                       '</label></span>'.('&nbsp;'x2);
 4485:                     }
 4486:                 }
 4487:                 if ($field eq 'screenshot') {
 4488:                     my $display;
 4489:                     if ($currfield{$field} eq 'no') {
 4490:                         $display = ' style="display:none"';
 4491:                     }
 4492:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 4493:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 4494:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 4495:                 }
 4496:                 $datatable .= '</td></tr>';
 4497:             }
 4498:             $datatable .= '</table>';
 4499:         }
 4500:         $datatable .= '</td></tr>'."\n";
 4501:         $rownum ++;
 4502:     }
 4503:     unless (($position eq 'top') || ($position eq 'lower')) {
 4504:         foreach my $type (@mailings) {
 4505:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4506:             $datatable .= '<tr'.$css_class.'>'.
 4507:                           '<td><span class="LC_nobreak">'.
 4508:                           $titles->{$type}.': </span></td>'.
 4509:                           '<td class="LC_left_item">';
 4510:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4511:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 4512:             }
 4513:             $datatable .= '<span class="LC_nobreak">';
 4514:             foreach my $item (@contacts) {
 4515:                 $datatable .= '<label>'.
 4516:                               '<input type="checkbox" name="'.$type.'"'.
 4517:                               $checked{$type}{$item}.
 4518:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 4519:                               '</label>&nbsp;';
 4520:             }
 4521:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4522:                           '<input type="text" name="'.$type.'_others" '.
 4523:                           'value="'.$otheremails{$type}.'"  />';
 4524:             my %locchecked;
 4525:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 4526:                 foreach my $loc ('s','b') {
 4527:                     if ($includeloc{$type} eq $loc) {
 4528:                         $locchecked{$loc} = ' checked="checked"';
 4529:                         last;
 4530:                     }
 4531:                 }
 4532:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4533:                               '<input type="text" name="'.$type.'_bcc" '.
 4534:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 4535:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4536:                               &mt('Text automatically added to e-mail:').' '.
 4537:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 4538:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4539:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4540:                               ('&nbsp;'x2).
 4541:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4542:                               '</span></fieldset>';
 4543:             }
 4544:             $datatable .= '</td></tr>'."\n";
 4545:             $rownum ++;
 4546:         }
 4547:     }
 4548:     if ($position eq 'middle') {
 4549:         my %choices;
 4550:         my $corelink = &core_link_msu();
 4551:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 4552:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 4553:                                         $corelink);
 4554:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 4555:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 4556:         my %defaultchecked = ('reporterrors'  => 'on',
 4557:                               'reportupdates' => 'on',
 4558:                               'reportstatus'  => 'on');
 4559:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4560:                                                    \%choices,$rownum);
 4561:         $datatable .= $reports;
 4562:     } elsif ($position eq 'lower') {
 4563:         my (%current,%excluded,%weights);
 4564:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 4565:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 4566:             $current{'errorthreshold'} = $lonstatus{'threshold'};
 4567:         } else {
 4568:             $current{'errorthreshold'} = $defaults->{'threshold'};
 4569:         }
 4570:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 4571:             $current{'errorsysmail'} = $lonstatus{'sysmail'};
 4572:         } else {
 4573:             $current{'errorsysmail'} = $defaults->{'sysmail'};
 4574:         }
 4575:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 4576:             foreach my $type ('E','W','N','U') {
 4577:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 4578:                     $weights{$type} = $lonstatus{'weights'}{$type};
 4579:                 } else {
 4580:                     $weights{$type} = $defaults->{$type};
 4581:                 }
 4582:             }
 4583:         } else {
 4584:             foreach my $type ('E','W','N','U') {
 4585:                 $weights{$type} = $defaults->{$type};
 4586:             }
 4587:         }
 4588:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 4589:             if (@{$lonstatus{'excluded'}} > 0) {
 4590:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 4591:             }
 4592:         }
 4593:         foreach my $item ('errorthreshold','errorsysmail') { 
 4594:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 4595:             $datatable .= '<tr'.$css_class.'>'.
 4596:                           '<td class="LC_left_item"><span class="LC_nobreak">'.
 4597:                           $titles->{$item}.
 4598:                           '</span></td><td class="LC_left_item">'.
 4599:                           '<input type="text" name="'.$item.'" value="'.
 4600:                           $current{$item}.'" size="5" /></td></tr>';
 4601:             $rownum ++;
 4602:         }
 4603:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4604:         $datatable .= '<tr'.$css_class.'>'.
 4605:                       '<td class="LC_left_item">'.
 4606:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 4607:                       '</span></td><td class="LC_left_item"><table><tr>';
 4608:         foreach my $type ('E','W','N','U') {
 4609:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 4610:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 4611:                           $weights{$type}.'" size="5" /></td>';
 4612:         }
 4613:         $datatable .= '</tr></table></tr>';
 4614:         $rownum ++;
 4615:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 4616:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 4617:                       $titles->{'errorexcluded'}.'</td>'.
 4618:                       '<td class="LC_left_item"><table>';
 4619:         my $numinrow = 4;
 4620:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 4621:         for (my $i=0; $i<@ids; $i++) {
 4622:             my $rem = $i%($numinrow);
 4623:             if ($rem == 0) {
 4624:                 if ($i > 0) {
 4625:                     $datatable .= '</tr>';
 4626:                 }
 4627:                 $datatable .= '<tr>';
 4628:             }
 4629:             my $check;
 4630:             if ($excluded{$ids[$i]}) {
 4631:                 $check = ' checked="checked" ';
 4632:             }
 4633:             $datatable .= '<td class="LC_left_item">'.
 4634:                           '<span class="LC_nobreak"><label>'.
 4635:                           '<input type="checkbox" name="errorexcluded" '.
 4636:                           'value="'.$ids[$i].'"'.$check.' />'.
 4637:                           $ids[$i].'</label></span></td>';
 4638:         }
 4639:         my $colsleft = $numinrow - @ids%($numinrow);
 4640:         if ($colsleft > 1 ) {
 4641:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4642:                           '&nbsp;</td>';
 4643:         } elsif ($colsleft == 1) {
 4644:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4645:         }
 4646:         $datatable .= '</tr></table></td></tr>';
 4647:         $rownum ++;
 4648:     } elsif ($position eq 'bottom') {
 4649:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4650:         my (@posstypes,%usertypeshash);
 4651:         if (ref($types) eq 'ARRAY') {
 4652:             @posstypes = @{$types};
 4653:         }
 4654:         if (@posstypes) {
 4655:             if (ref($usertypes) eq 'HASH') {
 4656:                 %usertypeshash = %{$usertypes};
 4657:             }
 4658:             my @overridden;
 4659:             my $numinrow = 4;
 4660:             if (ref($settings) eq 'HASH') {
 4661:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 4662:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 4663:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 4664:                             push(@overridden,$key);
 4665:                             foreach my $item (@contacts) {
 4666:                                 if ($settings->{'overrides'}{$key}{$item}) {
 4667:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 4668:                                 }
 4669:                             }
 4670:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 4671:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 4672:                             $includeloc{'override_'.$key} = '';
 4673:                             $includestr{'override_'.$key} = '';
 4674:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 4675:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 4676:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 4677:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 4678:                             }
 4679:                         }
 4680:                     }
 4681:                 }
 4682:             }
 4683:             my $customclass = 'LC_helpdesk_override';
 4684:             my $optionsprefix = 'LC_options_helpdesk_';
 4685: 
 4686:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 4687:  
 4688:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 4689:                                          $numinrow,$othertitle,'overrides',
 4690:                                          \$rownum,$onclicktypes,$customclass);
 4691:             $rownum ++;
 4692:             $usertypeshash{'default'} = $othertitle;
 4693:             foreach my $status (@posstypes) {
 4694:                 my $css_class;
 4695:                 if ($rownum%2) {
 4696:                     $css_class = 'LC_odd_row ';
 4697:                 }
 4698:                 $css_class .= $customclass;
 4699:                 my $rowid = $optionsprefix.$status;
 4700:                 my $hidden = 1;
 4701:                 my $currstyle = 'display:none';
 4702:                 if (grep(/^\Q$status\E$/,@overridden)) {
 4703:                     $currstyle = 'display:table-row';
 4704:                     $hidden = 0;
 4705:                 }
 4706:                 my $key = 'override_'.$status;
 4707:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 4708:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 4709:                                                   $usertypeshash{$status},$css_class,$currstyle,
 4710:                                                   \@contacts,$short_titles);
 4711:                 unless ($hidden) {
 4712:                     $rownum ++;
 4713:                 }
 4714:             }
 4715:         }
 4716:     }
 4717:     $$rowtotal += $rownum;
 4718:     return $datatable;
 4719: }
 4720: 
 4721: sub core_link_msu {
 4722:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 4723:                                           &mt('LON-CAPA core group - MSU'),600,500);
 4724: }
 4725: 
 4726: sub overridden_helpdesk {
 4727:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 4728:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 4729:     my $class = 'LC_left_item';
 4730:     if ($css_class) {
 4731:         $css_class = ' class="'.$css_class.'"';
 4732:     }
 4733:     if ($rowid) {
 4734:         $rowid = ' id="'.$rowid.'"';
 4735:     }
 4736:     if ($rowstyle) {
 4737:         $rowstyle = ' style="'.$rowstyle.'"';
 4738:     }
 4739:     my ($output,$description);
 4740:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 4741:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 4742:               "<td>$description</td>\n".
 4743:               '<td class="'.$class.'" colspan="2">'.
 4744:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 4745:               '<span class="LC_nobreak">';
 4746:     if (ref($contacts) eq 'ARRAY') {
 4747:         foreach my $item (@{$contacts}) {
 4748:             my $check;
 4749:             if (ref($checked) eq 'HASH') {
 4750:                $check = $checked->{$item};
 4751:             }
 4752:             my $title;
 4753:             if (ref($short_titles) eq 'HASH') {
 4754:                 $title = $short_titles->{$item}; 
 4755:             }
 4756:             $output .= '<label>'.
 4757:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 4758:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 4759:         }
 4760:     }
 4761:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 4762:                '<input type="text" name="override_'.$type.'_others" '.
 4763:                'value="'.$otheremails.'"  />';
 4764:     my %locchecked;
 4765:     foreach my $loc ('s','b') {
 4766:         if ($includeloc eq $loc) {
 4767:             $locchecked{$loc} = ' checked="checked"';
 4768:             last;
 4769:         }
 4770:     }
 4771:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 4772:                '<input type="text" name="override_'.$type.'_bcc" '.
 4773:                'value="'.$bccemails.'"  /></fieldset>'.
 4774:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 4775:                &mt('Text automatically added to e-mail:').' '.
 4776:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 4777:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 4778:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 4779:                ('&nbsp;'x2).
 4780:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 4781:                '</span></fieldset>'.
 4782:                '</td></tr>'."\n";
 4783:     return $output;
 4784: }
 4785: 
 4786: sub contacts_javascript {
 4787:     return <<"ENDSCRIPT";
 4788: 
 4789: <script type="text/javascript">
 4790: // <![CDATA[
 4791: 
 4792: function screenshotSize(field) {
 4793:     if (document.getElementById('help_screenshotsize')) {
 4794:         if (field.value == 'no') {
 4795:             document.getElementById('help_screenshotsize').style.display="none";
 4796:         } else {
 4797:             document.getElementById('help_screenshotsize').style.display="";
 4798:         }
 4799:     }
 4800:     return;
 4801: }
 4802: 
 4803: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4804:     if (form.elements[checkbox].length != undefined) {
 4805:         var count = 0;
 4806:         if (docount) {
 4807:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4808:                 if (form.elements[checkbox][i].checked) {
 4809:                     count ++;
 4810:                 }
 4811:             }
 4812:         }
 4813:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4814:             var type = form.elements[checkbox][i].value;
 4815:             if (document.getElementById(prefix+type)) {
 4816:                 if (form.elements[checkbox][i].checked) {
 4817:                     document.getElementById(prefix+type).style.display = 'table-row';
 4818:                     if (count % 2 == 1) {
 4819:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4820:                     } else {
 4821:                         document.getElementById(prefix+type).className = target;
 4822:                     }
 4823:                     count ++;
 4824:                 } else {
 4825:                     document.getElementById(prefix+type).style.display = 'none';
 4826:                 }
 4827:             }
 4828:         }
 4829:     }
 4830:     return;
 4831: }
 4832: 
 4833: 
 4834: // ]]>
 4835: </script>
 4836: 
 4837: ENDSCRIPT
 4838: }
 4839: 
 4840: sub print_helpsettings {
 4841:     my ($position,$dom,$settings,$rowtotal) = @_;
 4842:     my $confname = $dom.'-domainconfig';
 4843:     my $formname = 'display';
 4844:     my ($datatable,$itemcount);
 4845:     if ($position eq 'top') {
 4846:         $itemcount = 1;
 4847:         my (%choices,%defaultchecked,@toggles);
 4848:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4849:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4850:                                      &mt('LON-CAPA bug tracker'),600,500));
 4851:         %defaultchecked = ('submitbugs' => 'on');
 4852:         @toggles = ('submitbugs');
 4853:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4854:                                                      \%choices,$itemcount);
 4855:         $$rowtotal ++;
 4856:     } else {
 4857:         my $css_class;
 4858:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4859:         my (%customroles,%ordered,%current);
 4860:         if (ref($settings) eq 'HASH') {
 4861:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4862:                 %current = %{$settings->{'adhoc'}};
 4863:             }
 4864:         }
 4865:         my $count = 0;
 4866:         foreach my $key (sort(keys(%existing))) {
 4867:             if ($key=~/^rolesdef\_(\w+)$/) {
 4868:                 my $rolename = $1;
 4869:                 my (%privs,$order);
 4870:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4871:                 $customroles{$rolename} = \%privs;
 4872:                 if (ref($current{$rolename}) eq 'HASH') {
 4873:                     $order = $current{$rolename}{'order'};
 4874:                 }
 4875:                 if ($order eq '') {
 4876:                     $order = $count;
 4877:                 }
 4878:                 $ordered{$order} = $rolename;
 4879:                 $count++;
 4880:             }
 4881:         }
 4882:         my $maxnum = scalar(keys(%ordered));
 4883:         my @roles_by_num = ();
 4884:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4885:             push(@roles_by_num,$item);
 4886:         }
 4887:         my $context = 'domprefs';
 4888:         my $crstype = 'Course';
 4889:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4890:         my @accesstypes = ('all','dh','da','none');
 4891:         my ($numstatustypes,@jsarray);
 4892:         if (ref($types) eq 'ARRAY') {
 4893:             if (@{$types} > 0) {
 4894:                 $numstatustypes = scalar(@{$types});
 4895:                 push(@accesstypes,'status');
 4896:                 @jsarray = ('bystatus');
 4897:             }
 4898:         }
 4899:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4900:         if (keys(%domhelpdesk)) {
 4901:             push(@accesstypes,('inc','exc'));
 4902:             push(@jsarray,('notinc','notexc'));
 4903:         }
 4904:         my $hiddenstr = join("','",@jsarray);
 4905:         my $context = 'domprefs';
 4906:         my $crstype = 'Course';
 4907:         my $prefix = 'helproles_';
 4908:         my $add_class = 'LC_hidden';
 4909:         foreach my $num (@roles_by_num) {
 4910:             my $role = $ordered{$num};
 4911:             my ($desc,$access,@statuses);
 4912:             if (ref($current{$role}) eq 'HASH') {
 4913:                 $desc = $current{$role}{'desc'};
 4914:                 $access = $current{$role}{'access'};
 4915:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4916:                     @statuses = @{$current{$role}{'insttypes'}};
 4917:                 }
 4918:             }
 4919:             if ($desc eq '') {
 4920:                 $desc = $role;
 4921:             }
 4922:             my $identifier = 'custhelp'.$num;
 4923:             my %full=();
 4924:             my %levels= (
 4925:                          course => {},
 4926:                          domain => {},
 4927:                          system => {},
 4928:                         );
 4929:             my %levelscurrent=(
 4930:                                course => {},
 4931:                                domain => {},
 4932:                                system => {},
 4933:                               );
 4934:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4935:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4936:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4937:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4938:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4939:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4940:             for (my $k=0; $k<=$maxnum; $k++) {
 4941:                 my $vpos = $k+1;
 4942:                 my $selstr;
 4943:                 if ($k == $num) {
 4944:                     $selstr = ' selected="selected" ';
 4945:                 }
 4946:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4947:             }
 4948:             $datatable .= '</select>'.('&nbsp;'x2).
 4949:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4950:                           '</td>'.
 4951:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4952:                           &mt('Name shown to users:').
 4953:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4954:                           '</fieldset>'.
 4955:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4956:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4957:                           '<fieldset>'.
 4958:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4959:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4960:                                                                    \%levelscurrent,$identifier,
 4961:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4962:                           '</fieldset></td>';
 4963:             $itemcount ++;
 4964:         }
 4965:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4966:         my $newcust = 'custhelp'.$count;
 4967:         my (%privs,%levelscurrent);
 4968:         my %full=();
 4969:         my %levels= (
 4970:                      course => {},
 4971:                      domain => {},
 4972:                      system => {},
 4973:                     );
 4974:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4975:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4976:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4977:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4978:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4979:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4980:         for (my $k=0; $k<$maxnum+1; $k++) {
 4981:             my $vpos = $k+1;
 4982:             my $selstr;
 4983:             if ($k == $maxnum) {
 4984:                 $selstr = ' selected="selected" ';
 4985:             }
 4986:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4987:         }
 4988:         $datatable .= '</select>&nbsp;'."\n".
 4989:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4990:                       '</label></span></td>'.
 4991:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4992:                       '<span class="LC_nobreak">'.
 4993:                       &mt('Internal name:').
 4994:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4995:                       '</span>'.('&nbsp;'x4).
 4996:                       '<span class="LC_nobreak">'.
 4997:                       &mt('Name shown to users:').
 4998:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4999:                       '</span></fieldset>'.
 5000:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 5001:                                              $usertypes,$types,\%domhelpdesk).
 5002:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 5003:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 5004:                                                                 \@templateroles,$newcust).
 5005:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 5006:                                                                \%levelscurrent,$newcust).
 5007:                       '</fieldset>'.
 5008:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 5009:                       '</td></tr>';
 5010:         $count ++;
 5011:         $$rowtotal += $count;
 5012:     }
 5013:     return $datatable;
 5014: }
 5015: 
 5016: sub adhocbutton {
 5017:     my ($prefix,$num,$field,$visibility) = @_;
 5018:     my %lt = &Apache::lonlocal::texthash(
 5019:                                           show => 'Show details',
 5020:                                           hide => 'Hide details',
 5021:                                         );
 5022:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 5023:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 5024:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 5025:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 5026: }
 5027: 
 5028: sub helpsettings_javascript {
 5029:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 5030:     return unless(ref($roles_by_num) eq 'ARRAY');
 5031:     my %html_js_lt = &Apache::lonlocal::texthash(
 5032:                                           show => 'Show details',
 5033:                                           hide => 'Hide details',
 5034:                                         );
 5035:     &html_escape(\%html_js_lt);
 5036:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 5037:     return <<"ENDSCRIPT";
 5038: <script type="text/javascript">
 5039: // <![CDATA[
 5040: 
 5041: function reorderHelpRoles(form,item) {
 5042:     var changedVal;
 5043: $jstext
 5044:     var newpos = 'helproles_${total}_pos';
 5045:     var maxh = 1 + $total;
 5046:     var current = new Array();
 5047:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 5048:     if (item == newpos) {
 5049:         changedVal = newitemVal;
 5050:     } else {
 5051:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 5052:         current[newitemVal] = newpos;
 5053:     }
 5054:     for (var i=0; i<helproles.length; i++) {
 5055:         var elementName = 'helproles_'+helproles[i]+'_pos';
 5056:         if (elementName != item) {
 5057:             if (form.elements[elementName]) {
 5058:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 5059:                 current[currVal] = elementName;
 5060:             }
 5061:         }
 5062:     }
 5063:     var oldVal;
 5064:     for (var j=0; j<maxh; j++) {
 5065:         if (current[j] == undefined) {
 5066:             oldVal = j;
 5067:         }
 5068:     }
 5069:     if (oldVal < changedVal) {
 5070:         for (var k=oldVal+1; k<=changedVal ; k++) {
 5071:            var elementName = current[k];
 5072:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 5073:         }
 5074:     } else {
 5075:         for (var k=changedVal; k<oldVal; k++) {
 5076:             var elementName = current[k];
 5077:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 5078:         }
 5079:     }
 5080:     return;
 5081: }
 5082: 
 5083: function helpdeskAccess(num) {
 5084:     var curraccess = null;
 5085:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 5086:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 5087:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 5088:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 5089:             }
 5090:         }
 5091:     }
 5092:     var shown = Array();
 5093:     var hidden = Array();
 5094:     if (curraccess == 'none') {
 5095:         hidden = Array('$hiddenstr');
 5096:     } else {
 5097:         if (curraccess == 'status') {
 5098:             shown = Array('bystatus');
 5099:             hidden = Array('notinc','notexc');
 5100:         } else {
 5101:             if (curraccess == 'exc') {
 5102:                 shown = Array('notexc');
 5103:                 hidden = Array('notinc','bystatus');
 5104:             }
 5105:             if (curraccess == 'inc') {
 5106:                 shown = Array('notinc');
 5107:                 hidden = Array('notexc','bystatus');
 5108:             }
 5109:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 5110:                 hidden = Array('notinc','notexc','bystatus');
 5111:             }
 5112:         }
 5113:     }
 5114:     if (hidden.length > 0) {
 5115:         for (var i=0; i<hidden.length; i++) {
 5116:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 5117:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 5118:             }
 5119:         }
 5120:     }
 5121:     if (shown.length > 0) {
 5122:         for (var i=0; i<shown.length; i++) {
 5123:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 5124:                 if (shown[i] == 'privs') {
 5125:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 5126:                 } else {
 5127:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 5128:                 }
 5129:             }
 5130:         }
 5131:     }
 5132:     return;
 5133: }
 5134: 
 5135: function toggleHelpdeskItem(num,field) {
 5136:     if (document.getElementById('helproles_'+num+'_'+field)) {
 5137:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 5138:             document.getElementById('helproles_'+num+'_'+field).className =
 5139:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 5140:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5141:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 5142:             }
 5143:         } else {
 5144:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 5145:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 5146:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 5147:             }
 5148:         }
 5149:     }
 5150:     return;
 5151: }
 5152: 
 5153: // ]]>
 5154: </script>
 5155: 
 5156: ENDSCRIPT
 5157: }
 5158: 
 5159: sub helpdeskroles_access {
 5160:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 5161:         $usertypes,$types,$domhelpdesk) = @_;
 5162:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 5163:     my %lt = &Apache::lonlocal::texthash(
 5164:                     'rou'    => 'Role usage',
 5165:                     'whi'    => 'Which helpdesk personnel may use this role?',
 5166:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 5167:                     'dh'     => 'All with domain helpdesk role',
 5168:                     'da'     => 'All with domain helpdesk assistant role',
 5169:                     'none'   => 'None',
 5170:                     'status' => 'Determined based on institutional status',
 5171:                     'inc'    => 'Include all, but exclude specific personnel',
 5172:                     'exc'    => 'Exclude all, but include specific personnel',
 5173:                   );
 5174:     my %usecheck = (
 5175:                      all => ' checked="checked"',
 5176:                    );
 5177:     my %displaydiv = (
 5178:                       status => 'none',
 5179:                       inc    => 'none',
 5180:                       exc    => 'none',
 5181:                       priv   => 'block',
 5182:                      );
 5183:     my $output;
 5184:     if (ref($current) eq 'HASH') {
 5185:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 5186:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 5187:                 $usecheck{$current->{access}} = $usecheck{'all'};
 5188:                 delete($usecheck{'all'});
 5189:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 5190:                     my $access = $1;
 5191:                     $displaydiv{$access} = 'inline';
 5192:                 } elsif ($current->{access} eq 'none') {
 5193:                     $displaydiv{'priv'} = 'none';
 5194:                 }
 5195:             }
 5196:         }
 5197:     }
 5198:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 5199:               '<p>'.$lt{'whi'}.'</p>';
 5200:     foreach my $access (@{$accesstypes}) {
 5201:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 5202:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 5203:                    $lt{$access}.'</label>';
 5204:         if ($access eq 'status') {
 5205:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 5206:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 5207:                                                                  $othertitle,$usertypes,$types).
 5208:                        '</div>';
 5209:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 5210:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 5211:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5212:                        '</div>';
 5213:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 5214:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 5215:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 5216:                        '</div>';
 5217:         }
 5218:         $output .= '</p>';
 5219:     }
 5220:     $output .= '</fieldset>';
 5221:     return $output;
 5222: }
 5223: 
 5224: sub radiobutton_prefs {
 5225:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 5226:         $additional,$align,$firstval) = @_;
 5227:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 5228:                    (ref($choices) eq 'HASH'));
 5229: 
 5230:     my (%checkedon,%checkedoff,$datatable,$css_class);
 5231: 
 5232:     foreach my $item (@{$toggles}) {
 5233:         if ($defaultchecked->{$item} eq 'on') {
 5234:             $checkedon{$item} = ' checked="checked" ';
 5235:             $checkedoff{$item} = ' ';
 5236:         } elsif ($defaultchecked->{$item} eq 'off') {
 5237:             $checkedoff{$item} = ' checked="checked" ';
 5238:             $checkedon{$item} = ' ';
 5239:         }
 5240:     }
 5241:     if (ref($settings) eq 'HASH') {
 5242:         foreach my $item (@{$toggles}) {
 5243:             if ($settings->{$item} eq '1') {
 5244:                 $checkedon{$item} =  ' checked="checked" ';
 5245:                 $checkedoff{$item} = ' ';
 5246:             } elsif ($settings->{$item} eq '0') {
 5247:                 $checkedoff{$item} =  ' checked="checked" ';
 5248:                 $checkedon{$item} = ' ';
 5249:             }
 5250:         }
 5251:     }
 5252:     if ($onclick) {
 5253:         $onclick = ' onclick="'.$onclick.'"';
 5254:     }
 5255:     foreach my $item (@{$toggles}) {
 5256:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5257:         $datatable .=
 5258:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 5259:             '<span class="LC_nobreak">'.$choices->{$item}.
 5260:             '</span></td>';
 5261:         if ($align eq 'left') {
 5262:             $datatable .= '<td class="LC_left_item">';
 5263:         } else {
 5264:             $datatable .= '<td class="LC_right_item">';
 5265:         }
 5266:         $datatable .= '<span class="LC_nobreak">';
 5267:         if ($firstval eq 'no') {
 5268:             $datatable .=
 5269:                 '<label><input type="radio" name="'.
 5270:                 $item.'" '.$checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').
 5271:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5272:                 $checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').'</label>';
 5273:         } else {
 5274:             $datatable .=
 5275:                 '<label><input type="radio" name="'.
 5276:                 $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 5277:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 5278:                 $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>';
 5279:         }
 5280:         $datatable .= '</span>'.$additional.'</td></tr>';
 5281:         $itemcount ++;
 5282:     }
 5283:     return ($datatable,$itemcount);
 5284: }
 5285: 
 5286: sub print_ltitools {
 5287:     my ($position,$dom,$settings,$rowtotal) = @_;
 5288:     my (%rules,%encrypt,%privkeys,%linkprot);
 5289:     if (ref($settings) eq 'HASH') {
 5290:         if ($position eq 'top') {
 5291:             if (exists($settings->{'encrypt'})) {
 5292:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 5293:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 5294:                         $encrypt{'toolsec_'.$key} = $settings->{'encrypt'}{$key};
 5295:                     }
 5296:                 }
 5297:             }
 5298:             if (exists($settings->{'private'})) {
 5299:                 if (ref($settings->{'private'}) eq 'HASH') {
 5300:                     if (ref($settings->{'private'}) eq 'HASH') {
 5301:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 5302:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 5303:                         }
 5304:                     }
 5305:                 }
 5306:             }
 5307:         } elsif ($position eq 'middle') {
 5308:             if (exists($settings->{'rules'})) {
 5309:                 if (ref($settings->{'rules'}) eq 'HASH') {
 5310:                     %rules = %{$settings->{'rules'}};
 5311:                 }
 5312:             }
 5313:         } else {
 5314:             foreach my $key ('encrypt','private','rules') {
 5315:                 if (exists($settings->{$key})) {
 5316:                     delete($settings->{$key});
 5317:                 }
 5318:             }
 5319:         }
 5320:     }
 5321:     my $datatable;
 5322:     my $itemcount = 1;
 5323:     if ($position eq 'top') {
 5324:         $datatable = &secrets_form($dom,'toolsec',\%encrypt,\%privkeys,$rowtotal);
 5325:     } elsif ($position eq 'middle') {
 5326:         $datatable = &password_rules('toolsecrets',\$itemcount,\%rules);
 5327:         $$rowtotal += $itemcount;
 5328:     } else {
 5329:         $datatable = &Apache::courseprefs::print_ltitools($dom,'',$settings,\$rowtotal,'','','domain');
 5330:     }
 5331:     return $datatable;
 5332: }
 5333: 
 5334: sub ltitools_names {
 5335:     my %lt = &Apache::lonlocal::texthash(
 5336:                                           'title'          => 'Title',
 5337:                                           'version'        => 'Version',
 5338:                                           'msgtype'        => 'Message Type',
 5339:                                           'sigmethod'      => 'Signature Method',
 5340:                                           'url'            => 'URL',
 5341:                                           'key'            => 'Key',
 5342:                                           'lifetime'       => 'Nonce lifetime (s)',
 5343:                                           'secret'         => 'Secret',
 5344:                                           'icon'           => 'Icon',   
 5345:                                           'user'           => 'User',
 5346:                                           'fullname'       => 'Full Name',
 5347:                                           'firstname'      => 'First Name',
 5348:                                           'lastname'       => 'Last Name',
 5349:                                           'email'          => 'E-mail',
 5350:                                           'roles'          => 'Role',
 5351:                                           'window'         => 'Window',
 5352:                                           'tab'            => 'Tab',
 5353:                                           'iframe'         => 'iFrame',
 5354:                                           'height'         => 'Height',
 5355:                                           'width'          => 'Width',
 5356:                                           'linktext'       => 'Default Link Text',
 5357:                                           'explanation'    => 'Default Explanation',
 5358:                                           'passback'       => 'Tool can return grades:',
 5359:                                           'roster'         => 'Tool can retrieve roster:',
 5360:                                           'crstarget'      => 'Display target',
 5361:                                           'crslabel'       => 'Course label',
 5362:                                           'crstitle'       => 'Course title', 
 5363:                                           'crslinktext'    => 'Link Text',
 5364:                                           'crsexplanation' => 'Explanation',
 5365:                                           'crsappend'      => 'Provider URL',
 5366:                                         );
 5367:     return %lt;
 5368: }
 5369: 
 5370: sub secrets_form {
 5371:     my ($dom,$context,$encrypt,$privkeys,$rowtotal) = @_;
 5372:     my @ids=&Apache::lonnet::current_machine_ids();
 5373:     my %servers = &Apache::lonnet::get_servers($dom,'library');
 5374:     my $primary = &Apache::lonnet::domain($dom,'primary');
 5375:     my ($css_class,$extra,$numshown,$itemcount,$output);
 5376:     $itemcount = 0;
 5377:     foreach my $hostid (sort(keys(%servers))) {
 5378:         my ($showextra,$divsty,$switch);
 5379:         if ($hostid eq $primary) {
 5380:             if ($context eq 'ltisec') {
 5381:                 if (($encrypt->{'ltisec_consumers'}) || ($encrypt->{'ltisec_domlinkprot'})) {
 5382:                     $showextra = 1;
 5383:                 }
 5384:                 if ($encrypt->{'ltisec_crslinkprot'}) {
 5385:                     $showextra = 1;
 5386:                 }
 5387:             } else {
 5388:                 if (($encrypt->{'toolsec_crs'}) || ($encrypt->{'toolsec_dom'})) {
 5389:                     $showextra = 1;
 5390:                 }
 5391:             }
 5392:             unless (grep(/^\Q$hostid\E$/,@ids)) {
 5393:                 $switch = 1;
 5394:             }
 5395:             if ($showextra) {
 5396:                 $numshown ++;
 5397:                 $divsty = 'display:inline-block';
 5398:             } else {
 5399:                 $divsty = 'display:none';
 5400:             }
 5401:             $extra .= '<fieldset id="'.$context.'_info_'.$hostid.'" style="'.$divsty.'">'.
 5402:                       '<legend>'.$hostid.'</legend>';
 5403:             if ($switch) {
 5404:                 my $switchserver = '<a href="/adm/switchserver?otherserver='.$hostid.'&amp;role='.
 5405:                                    &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 5406:                                    '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 5407:                 if (exists($privkeys->{$hostid})) {
 5408:                     $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" />'.
 5409:                               '<span class="LC_nobreak">'.
 5410:                               &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5411:                               '<span class="LC_nobreak">'.&mt('Change?').
 5412:                               '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5413:                               ('&nbsp;'x2).
 5414:                               '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5415:                               '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5416:                               '<span class="LC_nobreak"> - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5417:                               '</span></div>';
 5418:                 } else {
 5419:                     $extra .= '<span class="LC_nobreak">'.
 5420:                               &mt('Key required').' - '.&mt('submit from server ([_1]): [_2].',$hostid,$switchserver).
 5421:                               '</span>'."\n";
 5422:                 }
 5423:             } elsif (exists($privkeys->{$hostid})) {
 5424:                 $extra .= '<div id="'.$context.'_divcurrprivkey_'.$hostid.'" style="display:inline-block" /><span class="LC_nobreak">'.
 5425:                           &mt('Encryption Key').': ['.&mt('not shown').'] '.('&nbsp;'x2).'</span></div>'.
 5426:                           '<span class="LC_nobreak">'.&mt('Change?').
 5427:                           '<label><input type="radio" value="0" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" checked="checked" />'.&mt('No').'</label>'.
 5428:                           ('&nbsp;'x2).
 5429:                           '<label><input type="radio" value="1" name="'.$context.'_changeprivkey_'.$hostid.'" onclick="javascript:togglePrivKey(this.form,'."'$context','$hostid'".');" />'.&mt('Yes').
 5430:                           '</label>&nbsp;&nbsp;</span><div id="'.$context.'_divchgprivkey_'.$hostid.'" style="display:none" />'.
 5431:                           '<span class="LC_nobreak">'.&mt('New Key').':'.
 5432:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5433:                           '<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>'.
 5434:                           '</span></div>';
 5435:             } else {
 5436:                 $extra .= '<span class="LC_nobreak">'.&mt('Encryption Key').':'.
 5437:                           '<input type="password" size="20" name="'.$context.'_privkey_'.$hostid.'" value="" autocomplete="new-password" />'.
 5438:                           '<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>';
 5439:             }
 5440:             $extra .= '</fieldset>';
 5441:         }
 5442:     }
 5443:     my (%choices,@toggles,%defaultchecked);
 5444:     if ($context eq 'ltisec') {
 5445:         %choices = &Apache::lonlocal::texthash (
 5446:                                                   ltisec_crslinkprot => 'Encrypt stored link protection secrets defined in courses',
 5447:                                                   ltisec_domlinkprot => 'Encrypt stored link protection secrets defined in domain',
 5448:                                                   ltisec_consumers   => 'Encrypt stored consumer secrets defined in domain',
 5449:                                                );
 5450:         @toggles = qw(ltisec_crslinkprot ltisec_domlinkprot ltisec_consumers);
 5451:         %defaultchecked = (
 5452:                            'ltisec_crslinkprot' => 'off',
 5453:                            'ltisec_domlinkprot' => 'off',
 5454:                            'ltisec_consumers'   => 'off',
 5455:                           );
 5456:     } else {
 5457:         %choices = &Apache::lonlocal::texthash (
 5458:                                                   toolsec_crs => 'Encrypt stored external tool secrets defined in courses',
 5459:                                                   toolsec_dom => 'Encrypt stored external tool secrets defined in domain',
 5460:                                                );
 5461:         @toggles = qw(toolsec_crs toolsec_dom);
 5462:         %defaultchecked = (
 5463:                            'toolsec_crs' => 'off',
 5464:                            'toolsec_dom' => 'off',
 5465:                           );
 5466:     }
 5467:     my ($onclick,$itemcount);
 5468:     $onclick = 'javascript:toggleLTIEncKey(this.form,'."'$context'".');';
 5469:     ($output,$itemcount) = &radiobutton_prefs($encrypt,\@toggles,\%defaultchecked,
 5470:                                               \%choices,$itemcount,$onclick,'','left','no');
 5471: 
 5472:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5473:     my $noprivkeysty = 'display:inline-block';
 5474:     if ($numshown) {
 5475:         $noprivkeysty = 'display:none';
 5476:     }
 5477:     $output .= '<tr '.$css_class.'><td><span class="LC_nobreak">'.&mt('Encryption Key(s)').'</td>'.
 5478:                '<td><div id="'.$context.'_noprivkey" style="'.$noprivkeysty.'" >'.
 5479:                '<span class="LC_nobreak">'.&mt('Not in use').'</span></div>'.
 5480:                $extra.
 5481:                '</td></tr>';
 5482:     $itemcount ++;
 5483:     $$rowtotal += $itemcount;
 5484:     return $output;
 5485: }
 5486: 
 5487: sub print_proctoring {
 5488:     my ($dom,$settings,$rowtotal) = @_;
 5489:     my $itemcount = 1;
 5490:     my (%ordered,%providernames,%current,%currentdef);
 5491:     my $confname = $dom.'-domainconfig';
 5492:     my $switchserver = &check_switchserver($dom,$confname);
 5493:     if (ref($settings) eq 'HASH') {
 5494:         foreach my $item (keys(%{$settings})) {
 5495:             if (ref($settings->{$item}) eq 'HASH') {
 5496:                 my $num = $settings->{$item}{'order'};
 5497:                 $ordered{$num} = $item;
 5498:             }
 5499:         }
 5500:     } else {
 5501:         %ordered = (
 5502:                      1 => 'proctorio',
 5503:                      2 => 'examity',
 5504:                    );
 5505:     }
 5506:     %providernames = &proctoring_providernames();
 5507:     my $maxnum = scalar(keys(%ordered));
 5508:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 5509:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 5510:     if (ref($requref) eq 'HASH') {
 5511:         %requserfields = %{$requref};
 5512:     }
 5513:     if (ref($opturef) eq 'HASH') {
 5514:         %optuserfields = %{$opturef};
 5515:     }
 5516:     if (ref($defref) eq 'HASH') {
 5517:         %defaults = %{$defref};
 5518:     }
 5519:     if (ref($extref) eq 'HASH') {
 5520:         %extended = %{$extref};
 5521:     }
 5522:     if (ref($crsref) eq 'HASH') {
 5523:         %crsconf = %{$crsref};
 5524:     }
 5525:     if (ref($rolesref) eq 'ARRAY') {
 5526:         @courseroles = @{$rolesref};
 5527:     }
 5528:     if (ref($ltiref) eq 'ARRAY') {
 5529:         @ltiroles = @{$ltiref};
 5530:     }
 5531:     my $datatable;
 5532:     my $css_class;
 5533:     if (keys(%ordered)) {
 5534:         my @items = sort { $a <=> $b } keys(%ordered);
 5535:         for (my $i=0; $i<@items; $i++) {
 5536:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5537:             my $provider = $ordered{$items[$i]};
 5538:             my $optionsty = 'none';
 5539:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5540:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5541:             if (ref($settings) eq 'HASH') {
 5542:                 if (ref($settings->{$provider}) eq 'HASH') {
 5543:                     %current = %{$settings->{$provider}};
 5544:                     if ($current{'available'}) {
 5545:                         $optionsty = 'block';
 5546:                         $available = 1;
 5547:                     }
 5548:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5549:                         $lifetime = $current{'lifetime'};
 5550:                     }
 5551:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5552:                         $version = $current{'version'};
 5553:                     }
 5554:                     if ($current{'image'} ne '') {
 5555:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5556:                     }
 5557:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5558:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5559:                     }
 5560:                     $userincdom = $current{'incdom'};
 5561:                     if (ref($current{'roles'}) eq 'HASH') {
 5562:                         %rolemaps = %{$current{'roles'}};
 5563:                         $checkedfields{'roles'} = 1;
 5564:                     }
 5565:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5566:                         foreach my $val (@{$current{'defaults'}}) {
 5567:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5568:                                 $inuse{$val} = 1;
 5569:                             } else {
 5570:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5571:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5572:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5573:                                             $inuse{$poss} = $val;
 5574:                                             last;
 5575:                                         }
 5576:                                     }
 5577:                                 }
 5578:                             }
 5579:                         }
 5580:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5581:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5582:                             my $currval = $current{'defaults'}{$key}; 
 5583:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5584:                                 $inuse{$key} = 1;
 5585:                             } else {
 5586:                                 my $match;
 5587:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5588:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5589:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5590:                                             $inuse{$poss} = $key;
 5591:                                             last;
 5592:                                         }
 5593:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5594:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5595:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5596:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5597:                                                     $currentdef{$inner} = $currval;
 5598:                                                     $match = 1;
 5599:                                                     last;
 5600:                                                 }
 5601:                                             } elsif ($inner eq $key) {
 5602:                                                 $currentdef{$key} = $currval;
 5603:                                                 $match = 1;
 5604:                                                 last;
 5605:                                             }
 5606:                                         }
 5607:                                     }
 5608:                                     last if ($match);
 5609:                                 }
 5610:                             }
 5611:                         }
 5612:                     }
 5613:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5614:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5615:                     }
 5616:                 }
 5617:             }
 5618:             my %lt = &proctoring_titles($provider);
 5619:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5620:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5621:             my %checkedavailable = (
 5622:                                    yes => '',
 5623:                                    no  => ' checked="checked"',
 5624:                                  );
 5625:             if ($available) {
 5626:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5627:                 $checkedavailable{'no'} = '';
 5628:             }
 5629:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5630:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5631:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5632:             for (my $k=0; $k<$maxnum; $k++) {
 5633:                 my $vpos = $k+1;
 5634:                 my $selstr;
 5635:                 if ($k == $i) {
 5636:                     $selstr = ' selected="selected" ';
 5637:                 }
 5638:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5639:             }
 5640:             if ($version eq '') {
 5641:                 if ($provider eq 'proctorio') {
 5642:                     $version = '1.0';
 5643:                 } elsif ($provider eq 'examity') {
 5644:                     $version = '1.1';
 5645:                 }
 5646:             }
 5647:             if ($lifetime eq '') {
 5648:                 $lifetime = '300';
 5649:             }
 5650:             $datatable .=
 5651:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5652:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5653:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5654:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5655:                 '</td>'.
 5656:                 '<td colspan="2">'.
 5657:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5658:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5659:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5660:                 ('&nbsp;'x2).
 5661:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5662:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5663:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5664:                 ('&nbsp;'x2).
 5665:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5666:                 '<br />'.
 5667:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5668:                 '<br />'.
 5669:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5670:                 ('&nbsp;'x2).
 5671:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5672:                 '<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";
 5673:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5674:             if ($imgsrc) {
 5675:                 $datatable .= $imgsrc.
 5676:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5677:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5678:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5679:             }
 5680:             $datatable .= '&nbsp;';
 5681:             if ($switchserver) {
 5682:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5683:             } else {
 5684:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5685:             }
 5686:             unless ($imgsrc) {
 5687:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5688:             }
 5689:             $datatable .= '</fieldset>'."\n";
 5690:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5691:                 if (@{$requserfields{$provider}} > 0) {
 5692:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5693:                     foreach my $field (@{$requserfields{$provider}}) {
 5694:                         $datatable .= '<span class="LC_nobreak">'.
 5695:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5696:                                       $lt{$field}.'</label>';
 5697:                         if ($field eq 'user') {
 5698:                             my $seluserdom = '';
 5699:                             my $unseluserdom = ' selected="selected"';
 5700:                             if ($userincdom) {
 5701:                                 $seluserdom = $unseluserdom;
 5702:                                 $unseluserdom = '';
 5703:                             }
 5704:                             $datatable .= ':&nbsp;'.
 5705:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5706:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5707:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5708:                                 '</select>&nbsp;';
 5709:                         } else {
 5710:                             $datatable .= '&nbsp;';
 5711:                             if ($field eq 'roles') {
 5712:                                 $showroles = 1; 
 5713:                             } 
 5714:                         }
 5715:                         $datatable .= '</span> ';
 5716:                     }
 5717:                 }
 5718:                 $datatable .= '</fieldset>'."\n";
 5719:             }
 5720:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5721:                 if (@{$optuserfields{$provider}} > 0) {
 5722:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5723:                     foreach my $field (@{$optuserfields{$provider}}) {
 5724:                         my $checked;
 5725:                         if ($checkedfields{$field}) {
 5726:                             $checked = ' checked="checked"';
 5727:                         }
 5728:                         $datatable .= '<span class="LC_nobreak">'.
 5729:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5730:                     }
 5731:                     $datatable .= '</fieldset>'."\n";
 5732:                 }
 5733:             }
 5734:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5735:                 if (@{$defaults{$provider}}) {
 5736:                     my (%options,@selectboxes);
 5737:                     if (ref($extended{$provider}) eq 'HASH') {
 5738:                         %options = %{$extended{$provider}};
 5739:                     }
 5740:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5741:                     my ($rem,$numinrow,$dropdowns);
 5742:                     if ($provider eq 'proctorio') { 
 5743:                         $datatable .= '<table>';
 5744:                         $numinrow = 4;
 5745:                     }
 5746:                     my $i = 0;
 5747:                     foreach my $field (@{$defaults{$provider}}) {
 5748:                         my $checked;
 5749:                         if ($inuse{$field}) {
 5750:                             $checked = ' checked="checked"';
 5751:                         }
 5752:                         if ($provider eq 'examity') {
 5753:                             if ($field eq 'display') {
 5754:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5755:                                 foreach my $option ('iframe','tab','window') {
 5756:                                     my $checkdisp;
 5757:                                     if ($currentdef{'target'} eq $option) {
 5758:                                         $checkdisp = ' checked="checked"';
 5759:                                     }
 5760:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5761:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5762:                                 }
 5763:                                 $datatable .= ('&nbsp;'x4);
 5764:                                 foreach my $dimen ('width','height') {
 5765:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5766:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5767:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5768:                                                   ('&nbsp;'x2);
 5769:                                 }
 5770:                                 $datatable .= '</span><br />'.
 5771:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5772:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5773:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5774:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5775:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5776:                                               $currentdef{'explanation'}.
 5777:                                               '</textarea></div><div style=""></div><br />';
 5778:                             }
 5779:                         } else {
 5780:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5781:                                 my ($output,$selnone);
 5782:                                 unless ($checked) {
 5783:                                     $selnone = ' selected="selected"';
 5784:                                 }
 5785:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5786:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5787:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5788:                                 foreach my $option (@{$options{$field}}) {
 5789:                                     my $sel; 
 5790:                                     if ($inuse{$field} eq $option) {
 5791:                                         $sel = ' selected="selected"';
 5792:                                     }
 5793:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5794:                                 }
 5795:                                 $output .= '</select></span>';
 5796:                                 push(@selectboxes,$output);
 5797:                             } else {
 5798:                                 $rem = $i%($numinrow);
 5799:                                 if ($rem == 0) {
 5800:                                     if ($i > 0) {
 5801:                                         $datatable .= '</tr>';
 5802:                                     }
 5803:                                     $datatable .= '<tr>';
 5804:                                 }
 5805:                                 $datatable .= '<td class="LC_left_item">'.
 5806:                                               '<span class="LC_nobreak">'.
 5807:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5808:                                               $fieldtitles{$field}.'</label></span></td>';
 5809:                                 $i++;
 5810:                             }
 5811:                         }
 5812:                     }
 5813:                     if ($provider eq 'proctorio') {
 5814:                         if ($numinrow) {
 5815:                             $rem = $i%$numinrow;
 5816:                         }
 5817:                         my $colsleft = $numinrow - $rem;
 5818:                         if ($colsleft > 1) {
 5819:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5820:                         } else {
 5821:                             $datatable .= '<td class="LC_left_item">';
 5822:                         }
 5823:                         $datatable .= '&nbsp;'.
 5824:                                       '</td></tr></table>';
 5825:                         if (@selectboxes) {
 5826:                             $datatable .= '<hr /><table>';
 5827:                             $numinrow = 2;
 5828:                             for (my $i=0; $i<@selectboxes; $i++) {
 5829:                                 $rem = $i%($numinrow);
 5830:                                 if ($rem == 0) {
 5831:                                     if ($i > 0) {
 5832:                                         $datatable .= '</tr>';
 5833:                                     }
 5834:                                     $datatable .= '<tr>';
 5835:                                 }
 5836:                                 $datatable .= '<td class="LC_left_item">'.
 5837:                                               $selectboxes[$i].'</td>';
 5838:                             }
 5839:                             if ($numinrow) {
 5840:                                 $rem = $i%$numinrow;
 5841:                             }
 5842:                             $colsleft = $numinrow - $rem;
 5843:                             if ($colsleft > 1) {
 5844:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5845:                             } else {
 5846:                                 $datatable .= '<td class="LC_left_item">';
 5847:                             }
 5848:                             $datatable .= '&nbsp;'.
 5849:                                           '</td></tr></table>';
 5850:                         }
 5851:                     }
 5852:                     $datatable .= '</fieldset>';
 5853:                 }
 5854:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5855:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5856:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5857:                     my ($rem,$numinrow);
 5858:                     if ($provider eq 'proctorio') {
 5859:                         $datatable .= '<table>';
 5860:                         $numinrow = 4;
 5861:                     }
 5862:                     my $i = 0;
 5863:                     foreach my $item (@{$crsconf{$provider}}) {
 5864:                         my $name;
 5865:                         if ($provider eq 'examity') {
 5866:                             $name = $lt{'crs'.$item};
 5867:                         } elsif ($provider eq 'proctorio') {
 5868:                             $name = $fieldtitles{$item};
 5869:                             $rem = $i%($numinrow);
 5870:                             if ($rem == 0) {
 5871:                                 if ($i > 0) {
 5872:                                     $datatable .= '</tr>';
 5873:                                 }
 5874:                                 $datatable .= '<tr>';
 5875:                             }
 5876:                             $datatable .= '<td class="LC_left_item>';
 5877:                         }
 5878:                         my $checked;
 5879:                         if ($crsconfig{$item}) {
 5880:                             $checked = ' checked="checked"';
 5881:                         }
 5882:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5883:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5884:                                       $name.'</label></span>';
 5885:                         if ($provider eq 'examity') {
 5886:                             $datatable .= '&nbsp; ';
 5887:                         }
 5888:                         $datatable .= "\n";
 5889:                         $i++;
 5890:                     }
 5891:                     if ($provider eq 'proctorio') {
 5892:                         if ($numinrow) {
 5893:                             $rem = $i%$numinrow;
 5894:                         }
 5895:                         my $colsleft = $numinrow - $rem;
 5896:                         if ($colsleft > 1) {
 5897:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5898:                         } else {
 5899:                             $datatable .= '<td class="LC_left_item">';
 5900:                         }
 5901:                         $datatable .= '&nbsp;'.
 5902:                                       '</td></tr></table>';
 5903:                     }
 5904:                     $datatable .= '</fieldset>';
 5905:                 }
 5906:                 if ($showroles) {
 5907:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5908:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5909:                     foreach my $role (@courseroles) {
 5910:                         my ($selected,$selectnone);
 5911:                         if (!$rolemaps{$role}) {
 5912:                             $selectnone = ' selected="selected"';
 5913:                         }
 5914:                         $datatable .= '<td style="text-align: center">'.
 5915:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5916:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5917:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5918:                         foreach my $ltirole (@ltiroles) {
 5919:                             unless ($selectnone) {
 5920:                                 if ($rolemaps{$role} eq $ltirole) {
 5921:                                     $selected = ' selected="selected"';
 5922:                                 } else {
 5923:                                     $selected = '';
 5924:                                 }
 5925:                             }
 5926:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5927:                         }
 5928:                         $datatable .= '</select></td>';
 5929:                     }
 5930:                     $datatable .= '</tr></table></fieldset>'.
 5931:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5932:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5933:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5934:                                   '<tr><td></td><td>lms</td>'.
 5935:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5936:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5937:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5938:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5939:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5940:                         if (keys(%custom) > 0) {
 5941:                             foreach my $key (sort(keys(%custom))) {
 5942:                                 next if ($key eq 'lms');
 5943:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5944:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5945:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5946:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5947:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5948:                             }
 5949:                         }
 5950:                     }
 5951:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5952:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5953:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5954:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5955:                                   '</table></fieldset></td></tr>'."\n";
 5956:                 }
 5957:                 $datatable .= '</td></tr>';
 5958:             }
 5959:             $itemcount ++;
 5960:         }
 5961:     }
 5962:     return $datatable;
 5963: }
 5964: 
 5965: sub proctoring_data {
 5966:     my $requserfields = {
 5967:                       proctorio => ['user'],
 5968:                       examity   => ['roles','user'],
 5969:                      };
 5970:     my $optuserfields = {
 5971:                         proctorio => ['fullname'],
 5972:                         examity   => ['fullname','firstname','lastname','email'],
 5973:                      };
 5974:     my $defaults = {
 5975:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5976:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5977:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5978:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 5979:                                 'reentry','calculator','whiteboard'],
 5980:                   examity   => ['display'],
 5981:                 };
 5982:     my $extended = { 
 5983:                   proctorio => {
 5984:                                  verifyid => ['verifyidauto','verifyidlive'],
 5985:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 5986:                                  tabslinks => ['notabs','linksonly'],
 5987:                                  reentry => ['noreentry','agentreentry'],
 5988:                                  calculator => ['calculatorbasic','calculatorsci'],
 5989:                                },
 5990:                   examity => {
 5991:                                display => {
 5992:                                            target      => ['iframe','tab','window'],
 5993:                                            width       => '',
 5994:                                            height      => '',
 5995:                                            linktext    => '',
 5996:                                            explanation => '',
 5997:                                           },
 5998:                              },
 5999:                 };
 6000:     my $crsconf = {
 6001:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 6002:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 6003:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 6004:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 6005:                                'reentry','calculator','whiteboard'],
 6006:                  examity => ['label','title','target','linktext','explanation','append'],
 6007:                };
 6008:     my $courseroles = ['cc','in','ta','ep','st'];
 6009:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 6010:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 6011: }
 6012: 
 6013: sub proctoring_titles {
 6014:     my ($item) = @_;
 6015:     my (%common_lt,%custom_lt);
 6016:     %common_lt = &Apache::lonlocal::texthash (
 6017:                      'avai'      => 'Available?',
 6018:                      'base'      => 'Basic Settings',
 6019:                      'requ'      => 'User data required to be sent on launch',
 6020:                      'optu'      => 'User data optionally sent on launch',
 6021:                      'udsl'      => 'User data sent on launch',
 6022:                      'defa'      => 'Defaults for items configurable in course',
 6023:                      'sigmethod' => 'Signature Method',
 6024:                      'key'       => 'Key',
 6025:                      'lifetime'  => 'Nonce lifetime (s)',
 6026:                      'secret'    => 'Secret',
 6027:                      'icon'      => 'Icon',
 6028:                      'fullname'  => 'Full Name',
 6029:                      'visible'   => 'Visible input',
 6030:                      'username'  => 'username',
 6031:                      'user'      => 'User',
 6032:                  );
 6033:     if ($item eq 'proctorio') {
 6034:         %custom_lt = &Apache::lonlocal::texthash (
 6035:                          'version'        => 'OAuth version',
 6036:                          'url'            => 'API URL',
 6037:                          'uname:dom'      => 'username-domain',
 6038:         );
 6039:     } elsif ($item eq 'examity') {
 6040:         %custom_lt = &Apache::lonlocal::texthash (
 6041:                          'version'        => 'LTI Version',
 6042:                          'url'            => 'URL',
 6043:                          'uname:dom'      => 'username:domain',
 6044:                          'msgtype'        => 'Message Type',
 6045:                          'firstname'      => 'First Name',
 6046:                          'lastname'       => 'Last Name',
 6047:                          'email'          => 'E-mail',
 6048:                          'roles'          => 'Role',
 6049:                          'crstarget'      => 'Display target',
 6050:                          'crslabel'       => 'Course label',
 6051:                          'crstitle'       => 'Course title', 
 6052:                          'crslinktext'    => 'Link Text',
 6053:                          'crsexplanation' => 'Explanation',
 6054:                          'crsappend'      => 'Provider URL',
 6055:         );
 6056:     }
 6057:     my %lt = (%common_lt,%custom_lt);
 6058:     return %lt;
 6059: }
 6060: 
 6061: sub proctoring_fieldtitles {
 6062:     my ($item) = @_;
 6063:     if ($item eq 'proctorio') {
 6064:         return &Apache::lonlocal::texthash (
 6065:                   'recordvideo' => 'Record video',
 6066:                   'recordaudio' => 'Record audio',
 6067:                   'recordscreen' => 'Record screen',
 6068:                   'recordwebtraffic' => 'Record web traffic',
 6069:                   'recordroomstart' => 'Record room scan',
 6070:                   'verifyvideo' => 'Verify webcam',
 6071:                   'verifyaudio' => 'Verify microphone',
 6072:                   'verifydesktop' => 'Verify desktop recording',
 6073:                   'verifyid' => 'Photo ID verification',
 6074:                   'verifysignature' => 'Require signature',
 6075:                   'fullscreen' => 'Fullscreen',
 6076:                   'clipboard' => 'Disable copy/paste',
 6077:                   'tabslinks' => 'New tabs/windows',
 6078:                   'closetabs' => 'Close other tabs',
 6079:                   'onescreen' => 'Limit to single screen',
 6080:                   'print' => 'Disable Printing',
 6081:                   'downloads' => 'Disable Downloads',
 6082:                   'cache' => 'Empty cache after exam',
 6083:                   'rightclick' => 'Disable right click',
 6084:                   'reentry' => 'Re-entry to exam',
 6085:                   'calculator' => 'Onscreen calculator',
 6086:                   'whiteboard' => 'Onscreen whiteboard',
 6087:                   'verifyidauto' => 'Automated verification',
 6088:                   'verifyidlive' => 'Live agent verification',
 6089:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 6090:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 6091:                   'fullscreensever' => 'Forced, navigation away ends exam',
 6092:                   'notabs' => 'Disaallowed',
 6093:                   'linksonly' => 'Allowed from links in exam',
 6094:                   'noreentry' => 'Disallowed',
 6095:                   'agentreentry' => 'Agent required for re-entry',
 6096:                   'calculatorbasic' => 'Basic',
 6097:                   'calculatorsci' => 'Scientific',
 6098:         );
 6099:     } elsif ($item eq 'examity') {
 6100:         return &Apache::lonlocal::texthash (
 6101:                 'target'         => 'Display target',   
 6102:                 'window'         => 'Window',
 6103:                 'tab'            => 'Tab',
 6104:                 'iframe'         => 'iFrame',
 6105:                 'height'         => 'Height (pixels)',
 6106:                 'width'          => 'Width (pixels)',
 6107:                 'linktext'       => 'Default Link Text',
 6108:                 'explanation'    => 'Default Explanation',
 6109:                 'append'         => 'Provider URL',
 6110:         );
 6111:     }
 6112: }
 6113: 
 6114: sub proctoring_providernames {
 6115:     return (
 6116:              proctorio => 'Proctorio',
 6117:              examity   => 'Examity',
 6118:            );
 6119: }
 6120: 
 6121: sub print_lti {
 6122:     my ($position,$dom,$settings,$rowtotal) = @_;
 6123:     my $itemcount = 1;
 6124:     my ($datatable,$css_class);
 6125:     my (%rules,%encrypt,%privkeys,%linkprot);
 6126:     if (ref($settings) eq 'HASH') {
 6127:         if ($position eq 'top') {
 6128:             if (exists($settings->{'encrypt'})) {
 6129:                 if (ref($settings->{'encrypt'}) eq 'HASH') {
 6130:                     foreach my $key (keys(%{$settings->{'encrypt'}})) {
 6131:                         if ($key eq 'consumers') {
 6132:                             $encrypt{'ltisec_'.$key} = $settings->{'encrypt'}{$key};
 6133:                         } else {
 6134:                             $encrypt{'ltisec_'.$key.'linkprot'} = $settings->{'encrypt'}{$key};
 6135:                         }
 6136:                     }
 6137:                 }
 6138:             }
 6139:             if (exists($settings->{'private'})) {
 6140:                 if (ref($settings->{'private'}) eq 'HASH') {
 6141:                     if (ref($settings->{'private'}) eq 'HASH') {
 6142:                         if (ref($settings->{'private'}{'keys'}) eq 'ARRAY') {
 6143:                             map { $privkeys{$_} = 1; } (@{$settings->{'private'}{'keys'}});
 6144:                         }
 6145:                     }
 6146:                 }
 6147:             }
 6148:         } elsif ($position eq 'middle') {
 6149:             if (exists($settings->{'rules'})) {
 6150:                 if (ref($settings->{'rules'}) eq 'HASH') {
 6151:                     %rules = %{$settings->{'rules'}};
 6152:                 }
 6153:             }
 6154:         } elsif ($position eq 'lower') {
 6155:             if (exists($settings->{'linkprot'})) {
 6156:                 if (ref($settings->{'linkprot'}) eq 'HASH') {
 6157:                     %linkprot = %{$settings->{'linkprot'}};
 6158:                     if ($linkprot{'lock'}) {
 6159:                         delete($linkprot{'lock'});
 6160:                     }
 6161:                 }
 6162:             }
 6163:         } else {
 6164:             foreach my $key ('encrypt','private','rules','linkprot') {
 6165:                 if (exists($settings->{$key})) {
 6166:                     delete($settings->{$key});
 6167:                 }
 6168:             }
 6169:         }
 6170:     }
 6171:     if ($position eq 'top') {
 6172:         $datatable = &secrets_form($dom,'ltisec',\%encrypt,\%privkeys,$rowtotal);
 6173:     } elsif ($position eq 'middle') {
 6174:         $datatable = &password_rules('ltisecrets',\$itemcount,\%rules);
 6175:         $$rowtotal += $itemcount;
 6176:     } elsif ($position eq 'lower') {
 6177:          $datatable .= &Apache::courseprefs::print_linkprotection($dom,'',$settings,$rowtotal,'','','domain');
 6178:     } else {
 6179:         my $maxnum = 0;
 6180:         my %ordered;
 6181:         if (ref($settings) eq 'HASH') {
 6182:             foreach my $item (keys(%{$settings})) {
 6183:                 if (ref($settings->{$item}) eq 'HASH') {
 6184:                     my $num = $settings->{$item}{'order'};
 6185:                     if ($num eq '') {
 6186:                         $num = scalar(keys(%{$settings}));
 6187:                     }
 6188:                     $ordered{$num} = $item;
 6189:                 }
 6190:             }
 6191:         }
 6192:         $maxnum = scalar(keys(%ordered));
 6193:         my %lt = &lti_names();
 6194:         if (keys(%ordered)) {
 6195:             my @items = sort { $a <=> $b } keys(%ordered);
 6196:             for (my $i=0; $i<@items; $i++) {
 6197:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6198:                 my $item = $ordered{$items[$i]};
 6199:                 my ($key,$secret,$lifetime,$consumer,$requser,$crsinc,$current);
 6200:                 if (ref($settings->{$item}) eq 'HASH') {
 6201:                     $key = $settings->{$item}->{'key'};
 6202:                     $secret = $settings->{$item}->{'secret'};
 6203:                     $lifetime = $settings->{$item}->{'lifetime'};
 6204:                     $consumer = $settings->{$item}->{'consumer'};
 6205:                     $requser = $settings->{$item}->{'requser'};
 6206:                     $crsinc = $settings->{$item}->{'crsinc'};
 6207:                     $current = $settings->{$item};
 6208:                 }
 6209:                 my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 6210:                 my %checkedrequser = (
 6211:                                        yes => ' checked="checked"',
 6212:                                        no  => '',
 6213:                                      );
 6214:                 if (!$requser) {
 6215:                     $checkedrequser{'no'} = $checkedrequser{'yes'};
 6216:                     $checkedrequser{'yes'} = '';
 6217:                 }
 6218:                 my $onclickcrsinc = ' onclick="toggleLTI(this.form,'."'crsinc','$i'".');"';
 6219:                 my %checkedcrsinc = (
 6220:                                       yes => ' checked="checked"',
 6221:                                       no  => '',
 6222:                                     );
 6223:                 if (!$crsinc) {
 6224:                     $checkedcrsinc{'no'} = $checkedcrsinc{'yes'};
 6225:                     $checkedcrsinc{'yes'} = '';
 6226:                 }
 6227:                 my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 6228:                 $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6229:                              .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 6230:                 for (my $k=0; $k<=$maxnum; $k++) {
 6231:                     my $vpos = $k+1;
 6232:                     my $selstr;
 6233:                     if ($k == $i) {
 6234:                         $selstr = ' selected="selected" ';
 6235:                     }
 6236:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6237:                 }
 6238:                 $datatable .= '</select>'.('&nbsp;'x2).
 6239:                     '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 6240:                     &mt('Delete?').'</label></span></td>'.
 6241:                     '<td colspan="2">'.
 6242:                     '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6243:                     '<span class="LC_nobreak">'.$lt{'consumer'}.
 6244:                     ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 6245:                     ('&nbsp;'x2).
 6246:                     '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 6247:                     '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 6248:                     ('&nbsp;'x2).
 6249:                     '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 6250:                     'value="'.$lifetime.'" size="3" /></span>'.
 6251:                     ('&nbsp;'x2).
 6252:                     '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6253:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6254:                     '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 6255:                     '<br /><br />'.
 6256:                     '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6257:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="1"'.$onclickcrsinc.$checkedcrsinc{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 6258:                     '<label><input type="radio" name="lti_crsinc_'.$i.'" value="0"'.$onclickcrsinc.$checkedcrsinc{no}.' />'.&mt('No').'</label></span>'."\n".
 6259:                     ('&nbsp;'x4).
 6260:                     '<span class="LC_nobreak">'.$lt{'key'}.
 6261:                     ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 6262:                     ('&nbsp;'x2).
 6263:                     '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 6264:                     '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 6265:                     '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.lti_secret_'.$i.'.type='."'text'".' } else { this.form.lti_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 6266:                     '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 6267:                     '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 6268:                 $itemcount ++;
 6269:             }
 6270:         }
 6271:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6272:         my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 6273:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 6274:                       '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 6275:                       '<select name="lti_pos_add"'.$chgstr.'>';
 6276:         for (my $k=0; $k<$maxnum+1; $k++) {
 6277:             my $vpos = $k+1;
 6278:             my $selstr;
 6279:             if ($k == $maxnum) {
 6280:                 $selstr = ' selected="selected" ';
 6281:             }
 6282:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6283:         }
 6284:         $datatable .= '</select>&nbsp;'."\n".
 6285:                       '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 6286:                       '<td colspan="2">'.
 6287:                       '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 6288:                       '<span class="LC_nobreak">'.$lt{'consumer'}.
 6289:                       ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 6290:                       ('&nbsp;'x2).
 6291:                       '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 6292:                       '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 6293:                       ('&nbsp;'x2).
 6294:                       '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 6295:                       ('&nbsp;'x2).
 6296:                       '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 6297:                       '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6298:                       '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 6299:                       '<br /><br />'.
 6300:                       '<span class="LC_nobreak">'.$lt{'crsinc'}.':'.
 6301:                       '<label><input type="radio" name="lti_crsinc_add" value="1" onclick="toggleLTI(this.form,'."'crsinc','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 6302:                       '<label><input type="radio" name="lti_crsinc_add" value="0" onclick="toggleLTI(this.form,'."'crsinc','add'".');" />'.&mt('No').'</label></span>'."\n".
 6303:                       ('&nbsp;'x4).
 6304:                       '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 6305:                       ('&nbsp;'x2).
 6306:                       '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 6307:                       '<label><input type="checkbox" name="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".
 6308:                       '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 6309:                       '</td>'."\n".
 6310:                       '</tr>'."\n";
 6311:         $itemcount ++;
 6312:     }
 6313:     $$rowtotal += $itemcount;
 6314:     return $datatable;
 6315: }
 6316: 
 6317: sub lti_names {
 6318:     my %lt = &Apache::lonlocal::texthash(
 6319:                                           'version'   => 'LTI Version',
 6320:                                           'url'       => 'URL',
 6321:                                           'key'       => 'Key',
 6322:                                           'lifetime'  => 'Nonce lifetime (s)',
 6323:                                           'consumer'  => 'Consumer',
 6324:                                           'secret'    => 'Secret',
 6325:                                           'requser'   => "User's identity sent",
 6326:                                           'crsinc'    => "Course's identity sent",
 6327:                                           'email'     => 'Email address',
 6328:                                           'sourcedid' => 'User ID',
 6329:                                           'other'     => 'Other',
 6330:                                           'passback'  => 'Can return grades to Consumer:',
 6331:                                           'roster'    => 'Can retrieve roster from Consumer:',
 6332:                                           'topmenu'   => 'Display LON-CAPA page header',
 6333:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 6334:                                         );
 6335:     return %lt;
 6336: }
 6337: 
 6338: sub lti_options {
 6339:     my ($num,$current,$itemcount,%lt) = @_;
 6340:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 6341:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 6342:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 6343:     $checked{'storecrs'}{'Y'} = ' checked="checked"';
 6344:     $checked{'makecrs'}{'N'} = ' checked="checked"';
 6345:     $checked{'mapcrstype'} = {};
 6346:     $checked{'makeuser'} = {};
 6347:     $checked{'selfenroll'} = {};
 6348:     $checked{'crssec'} = {};
 6349:     $checked{'crssecsrc'} = {};
 6350:     $checked{'lcauth'} = {};
 6351:     $checked{'menuitem'} = {};
 6352:     if ($num eq 'add') {
 6353:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 6354:     }
 6355:     my $userfieldsty = 'none';
 6356:     my $crsfieldsty = 'none';
 6357:     my $crssecfieldsty = 'none';
 6358:     my $secsrcfieldsty = 'none';
 6359:     my $callbacksty = 'none';
 6360:     my $passbacksty = 'none';
 6361:     my $optionsty = 'block';
 6362:     my $crssty = 'block';
 6363:     my $lcauthparm;
 6364:     my $lcauthparmstyle = 'display:none';
 6365:     my $lcauthparmtext;
 6366:     my $menusty;
 6367:     my $numinrow = 4;
 6368:     my %menutitles = &ltimenu_titles();
 6369: 
 6370:     if (ref($current) eq 'HASH') {
 6371:         if (!$current->{'requser'}) {
 6372:             $optionsty = 'none';
 6373:             $crssty = 'none';
 6374:         } elsif (!$current->{'crsinc'}) {
 6375:             $crssty = 'none';
 6376:         }
 6377:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 6378:             $checked{'mapuser'}{'sourcedid'} = '';
 6379:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 6380:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 6381:             } else {
 6382:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 6383:                 $userfield = $current->{'mapuser'};
 6384:                 $userfieldsty = 'inline-block';
 6385:             }
 6386:         }
 6387:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 6388:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 6389:             if ($current->{'mapcrs'} eq 'context_id') {
 6390:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 6391:             } else {
 6392:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 6393:                 $cidfield = $current->{'mapcrs'};
 6394:                 $crsfieldsty = 'inline-block';
 6395:             }
 6396:         }
 6397:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 6398:             foreach my $type (@{$current->{'mapcrstype'}}) {
 6399:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 6400:             }
 6401:         }
 6402:         if (!$current->{'storecrs'}) {
 6403:             $checked{'storecrs'}{'N'} = $checked{'storecrs'}{'Y'};
 6404:             $checked{'storecrs'}{'Y'} = '';
 6405:         }
 6406:         if ($current->{'makecrs'}) {
 6407:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 6408:         }
 6409:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 6410:             foreach my $role (@{$current->{'makeuser'}}) {
 6411:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 6412:             }
 6413:         }
 6414:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 6415:             $checked{'lcauth'}{$1} = ' checked="checked"';
 6416:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 6417:                 $lcauthparm = $current->{'lcauthparm'};
 6418:                 $lcauthparmstyle = 'display:table-row'; 
 6419:                 if ($current->{'lcauth'} eq 'localauth') {
 6420:                     $lcauthparmtext = &mt('Local auth argument');
 6421:                 } else {
 6422:                     $lcauthparmtext = &mt('Kerberos domain');
 6423:                 }
 6424:             }
 6425:         }
 6426:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 6427:             foreach my $role (@{$current->{'selfenroll'}}) {
 6428:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 6429:             }
 6430:         }
 6431:         if (ref($current->{'maproles'}) eq 'HASH') {
 6432:             %rolemaps = %{$current->{'maproles'}};
 6433:         }
 6434:         if ($current->{'section'} ne '') {
 6435:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 6436:             $crssecfieldsty = 'inline-block';
 6437:             if ($current->{'section'} eq 'course_section_sourcedid') {
 6438:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 6439:             } else {
 6440:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 6441:                 $crssecsrc = $current->{'section'};
 6442:                 $secsrcfieldsty = 'inline-block';
 6443:             }
 6444:         } else {
 6445:             $checked{'crssec'}{'N'} = ' checked="checked"';
 6446:         }
 6447:         if ($current->{'callback'} ne '') {
 6448:             $callback = $current->{'callback'};
 6449:             $checked{'callback'}{'Y'} = ' checked="checked"';
 6450:             $callbacksty = 'inline-block';
 6451:         } else {
 6452:             $checked{'callback'}{'N'} = ' checked="checked"';
 6453:         }
 6454:         if ($current->{'topmenu'}) {
 6455:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 6456:         } else {
 6457:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 6458:         }
 6459:         if ($current->{'inlinemenu'}) {
 6460:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 6461:         } else {
 6462:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 6463:         }
 6464:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 6465:             $menusty = 'inline-block';
 6466:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 6467:                 foreach my $item (@{$current->{'lcmenu'}}) {
 6468:                     if (exists($menutitles{$item})) {
 6469:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 6470:                     }
 6471:                 }
 6472:             }
 6473:         } else {
 6474:             $menusty = 'none';
 6475:         }
 6476:     } else {
 6477:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 6478:         $checked{'crssec'}{'N'} = ' checked="checked"';
 6479:         $checked{'callback'}{'N'} = ' checked="checked"';
 6480:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 6481:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 6482:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 6483:         $menusty = 'inline-block'; 
 6484:     }
 6485:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 6486:     my %coursetypetitles = &Apache::lonlocal::texthash (
 6487:                                official   => 'Official',
 6488:                                unofficial => 'Unofficial',
 6489:                                community  => 'Community',
 6490:                                textbook   => 'Textbook',
 6491:                                placement  => 'Placement Test',
 6492:                                lti        => 'LTI Provider',
 6493:     );
 6494:     my @authtypes = ('internal','krb4','krb5','localauth');
 6495:     my %shortauth = (
 6496:                      internal => 'int',
 6497:                      krb4 => 'krb4',
 6498:                      krb5 => 'krb5',
 6499:                      localauth  => 'loc'
 6500:                     );
 6501:     my %authnames = &authtype_names();
 6502:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 6503:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 6504:     my @courseroles = ('cc','in','ta','ep','st');
 6505:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 6506:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 6507:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 6508:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 6509:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 6510:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 6511:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 6512:     my $output = '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Logout options').'</legend>'.
 6513:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback to logout LON-CAPA on log out from Consumer').':&nbsp;'.
 6514:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6515:                  $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6516:                  '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6517:                  $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6518:                  '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6519:                  '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6520:                  '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6521:                  '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6522:                  '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 6523:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 6524:     foreach my $option ('sourcedid','email','other') {
 6525:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 6526:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 6527:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6528:     }
 6529:     $output .= '</span></div>'.
 6530:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 6531:                '<input type="text" name="lti_customuser_'.$num.'" '.
 6532:                'value="'.$userfield.'" /></div></fieldset>'.
 6533:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 6534:     foreach my $ltirole (@ltiroles) {
 6535:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 6536:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 6537:     }
 6538:     $output .= '</fieldset>'.
 6539:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 6540:                '<table>'.
 6541:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 6542:                '</table>'.
 6543:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 6544:                '<td class="LC_left_item">';
 6545:     foreach my $auth ('lti',@authtypes) {
 6546:         my $authtext;
 6547:         if ($auth eq 'lti') {
 6548:             $authtext = &mt('None');
 6549:         } else {
 6550:             $authtext = $authnames{$shortauth{$auth}};
 6551:         }
 6552:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 6553:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 6554:                    $authtext.'</label></span> &nbsp;';
 6555:     }
 6556:     $output .= '</td></tr>'.
 6557:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 6558:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 6559:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 6560:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 6561:                '</table></fieldset>'.
 6562:                '<fieldset class="ltioption_usr_'.$num.'" style="display:'.$optionsty.'"><legend>'.
 6563:                &mt('LON-CAPA menu items (Course Coordinator can override)').'</legend>'.
 6564:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6565:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6566:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6567:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6568:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6569:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6570:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6571:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6572:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6573:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6574:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6575:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6576:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6577:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6578:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6579:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6580:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6581:                    ('&nbsp;'x2);
 6582:     }
 6583:     $output .= '</span></div></fieldset>'.
 6584:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 6585:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 6586:                &mt('Unique course identifier').':&nbsp;';
 6587:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 6588:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 6589:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 6590:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 6591:     }
 6592:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 6593:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 6594:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6595:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 6596:     foreach my $type (@coursetypes) {
 6597:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 6598:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 6599:                    ('&nbsp;'x2);
 6600:     }
 6601:     $output .= '</span><br /><br />'.
 6602:                '<span class="LC_nobreak">'.&mt('Store mapping of course identifier to LON-CAPA CourseID').':&nbsp;'.
 6603:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="0"'.
 6604:                $checked{'storecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6605:                '<label><input type="radio" name="lti_storecrs_'.$num.'" value="1"'.
 6606:                $checked{'storecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6607:                '</fieldset>'.
 6608:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 6609:     foreach my $ltirole (@lticourseroles) {
 6610:         my ($selected,$selectnone);
 6611:         if ($rolemaps{$ltirole} eq '') {
 6612:             $selectnone = ' selected="selected"';
 6613:         }
 6614:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 6615:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 6616:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 6617:         foreach my $role (@courseroles) {
 6618:             unless ($selectnone) {
 6619:                 if ($rolemaps{$ltirole} eq $role) {
 6620:                     $selected = ' selected="selected"';
 6621:                 } else {
 6622:                     $selected = '';
 6623:                 }
 6624:             }
 6625:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 6626:                        &Apache::lonnet::plaintext($role,'Course').
 6627:                        '</option>';
 6628:         }
 6629:         $output .= '</select></td>';
 6630:     }
 6631:     $output .= '</tr></table></fieldset>'.
 6632:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Creating courses').'</legend>'.
 6633:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 6634:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 6635:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6636:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 6637:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 6638:                '</fieldset>'.
 6639:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 6640:     foreach my $lticrsrole (@lticourseroles) {
 6641:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 6642:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 6643:     }
 6644:     $output .= '</fieldset>'.
 6645:                '<fieldset class="ltioption_crs_'.$num.'" style="display:'.$crssty.'"><legend>'.&mt('Course options').'</legend>'.
 6646:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 6647:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 6648:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6649:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 6650:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 6651:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 6652:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 6653:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 6654:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 6655:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 6656:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 6657:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 6658:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 6659:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 6660:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 6661:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 6662:     foreach my $extra ('roster','passback') {
 6663:         my $checkedon = '';
 6664:         my $checkedoff = ' checked="checked"';
 6665:         if ($extra eq 'passback') {
 6666:             $pb1p1chk = ' checked="checked"';
 6667:             $pb1p0chk = '';
 6668:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6669:         } else {
 6670:             $onclickpb = ''; 
 6671:         }
 6672:         if (ref($current) eq 'HASH') {
 6673:             if (($current->{$extra})) {
 6674:                 $checkedon = $checkedoff;
 6675:                 $checkedoff = '';
 6676:                 if ($extra eq 'passback') {
 6677:                     $passbacksty = 'inline-block';
 6678:                 }
 6679:                 if ($current->{'passbackformat'} eq '1.0') {
 6680:                     $pb1p0chk =  ' checked="checked"';
 6681:                     $pb1p1chk = '';
 6682:                 }
 6683:             }
 6684:         }
 6685:         $output .= $lt{$extra}.'&nbsp;'.
 6686:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6687:                    &mt('No').'</label>'.('&nbsp;'x2).
 6688:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6689:                    &mt('Yes').'</label><br />';
 6690:     }
 6691:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6692:                '<span class="LC_nobreak">'.&mt('Grade format').
 6693:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6694:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6695:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6696:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6697:                '<div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>';
 6698:     $output .= '</span></div></fieldset>';
 6699: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6700: #
 6701: #    $output .= '</fieldset>'.
 6702: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6703:     return $output;
 6704: }
 6705: 
 6706: sub ltimenu_titles {
 6707:     return &Apache::lonlocal::texthash(
 6708:                                         fullname    => 'Full name',
 6709:                                         coursetitle => 'Course title',
 6710:                                         role        => 'Role',
 6711:                                         logout      => 'Logout',
 6712:                                         grades      => 'Grades',
 6713:     );
 6714: }
 6715: 
 6716: sub check_switchserver {
 6717:     my ($home) = @_;
 6718:     my $switchserver;
 6719:     if ($home ne '') {
 6720:         my $allowed;
 6721:         my @ids=&Apache::lonnet::current_machine_ids();
 6722:         foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 6723:         if (!$allowed) {
 6724:             $switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role='.
 6725:                           &HTML::Entities::encode($env{'request.role'},'\'<>"&').
 6726:                           '&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 6727:         }
 6728:     }
 6729:     return $switchserver;
 6730: }
 6731: 
 6732: sub print_coursedefaults {
 6733:     my ($position,$dom,$settings,$rowtotal) = @_;
 6734:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6735:     my $itemcount = 1;
 6736:     my %choices =  &Apache::lonlocal::texthash (
 6737:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6738:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6739:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6740:         coursecredits        => 'Credits can be specified for courses',
 6741:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6742:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6743:         inline_chem          => 'Use inline previewer for chemical reaction response in place of pop-up',
 6744:         texengine            => 'Default method to display mathematics',
 6745:         postsubmit           => 'Disable submit button/keypress following student submission',
 6746:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6747:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6748:         ltiauth              => 'Student username in LTI launch of deep-linked URL can be accepted without re-authentication',
 6749:         domexttool           => 'External Tools defined in the domain may be used in courses/communities (by type)',
 6750:         exttool              => 'External Tools can be defined and configured in courses/communities (by type)',
 6751:     );
 6752:     my %staticdefaults = (
 6753:                            anonsurvey_threshold => 10,
 6754:                            uploadquota          => 500,
 6755:                            postsubmit           => 60,
 6756:                            mysqltables          => 172800,
 6757:                            domexttool           => 1,
 6758:                            exttool              => 0,
 6759:                          );
 6760:     if ($position eq 'top') {
 6761:         %defaultchecked = (
 6762:                             'canuse_pdfforms' => 'off',
 6763:                             'uselcmath'       => 'on',
 6764:                             'usejsme'         => 'on',
 6765:                             'inline_chem'     => 'on',
 6766:                             'canclone'        => 'none',
 6767:                           );
 6768:         @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem');
 6769:         my $deftex = $Apache::lonnet::deftex;
 6770:         if (ref($settings) eq 'HASH') {
 6771:             if ($settings->{'texengine'}) {
 6772:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6773:                     $deftex = $settings->{'texengine'};
 6774:                 }
 6775:             }
 6776:         }
 6777:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6778:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6779:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6780:                        '</span></td><td class="LC_right_item">'.
 6781:                        '<select name="texengine">'."\n";
 6782:         my %texoptions = (
 6783:                             MathJax  => 'MathJax',
 6784:                             mimetex  => &mt('Convert to Images'),
 6785:                             tth      => &mt('TeX to HTML'),
 6786:                          );
 6787:         foreach my $renderer ('MathJax','mimetex','tth') {
 6788:             my $selected = '';
 6789:             if ($renderer eq $deftex) {
 6790:                 $selected = ' selected="selected"';
 6791:             }
 6792:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6793:         }
 6794:         $mathdisp .= '</select></td></tr>'."\n";
 6795:         $itemcount ++;
 6796:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6797:                                                      \%choices,$itemcount);
 6798:         $datatable = $mathdisp.$datatable;
 6799:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6800:         $datatable .=
 6801:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6802:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6803:             '</span></td><td class="LC_left_item">';
 6804:         my $currcanclone = 'none';
 6805:         my $onclick;
 6806:         my @cloneoptions = ('none','domain');
 6807:         my %clonetitles = &Apache::lonlocal::texthash (
 6808:                              none     => 'No additional course requesters',
 6809:                              domain   => "Any course requester in course's domain",
 6810:                              instcode => 'Course requests for official courses ...',
 6811:                           );
 6812:         my (%codedefaults,@code_order,@posscodes);
 6813:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6814:                                                     \@code_order) eq 'ok') {
 6815:             if (@code_order > 0) {
 6816:                 push(@cloneoptions,'instcode');
 6817:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6818:             }
 6819:         }
 6820:         if (ref($settings) eq 'HASH') {
 6821:             if ($settings->{'canclone'}) {
 6822:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6823:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6824:                         if (@code_order > 0) {
 6825:                             $currcanclone = 'instcode';
 6826:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6827:                         }
 6828:                     }
 6829:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6830:                     $currcanclone = $settings->{'canclone'};
 6831:                 }
 6832:             }
 6833:         }
 6834:         foreach my $option (@cloneoptions) {
 6835:             my ($checked,$additional);
 6836:             if ($currcanclone eq $option) {
 6837:                 $checked = ' checked="checked"';
 6838:             }
 6839:             if ($option eq 'instcode') {
 6840:                 if (@code_order) {
 6841:                     my $show = 'none';
 6842:                     if ($checked) {
 6843:                         $show = 'block';
 6844:                     }
 6845:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6846:                                   &mt('Institutional codes for new and cloned course have identical:').
 6847:                                   '<br />';
 6848:                     foreach my $item (@code_order) {
 6849:                         my $codechk;
 6850:                         if ($checked) {
 6851:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6852:                                 $codechk = ' checked="checked"';
 6853:                             }
 6854:                         }
 6855:                         $additional .= '<label>'.
 6856:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6857:                                        $item.'</label>';
 6858:                     }
 6859:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6860:                 }
 6861:             }
 6862:             $datatable .=
 6863:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6864:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6865:                 '</label>&nbsp;'.$additional.'</span><br />';
 6866:         }
 6867:         $datatable .= '</td>'.
 6868:                       '</tr>';
 6869:         $itemcount ++;
 6870:     } else {
 6871:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6872:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6873:         my $currusecredits = 0;
 6874:         my $postsubmitclient = 1;
 6875:         my $ltiauth = 0;
 6876:         my %domexttool;
 6877:         my %exttool;
 6878:         my @types = ('official','unofficial','community','textbook','placement');
 6879:         if (ref($settings) eq 'HASH') {
 6880:             if ($settings->{'ltiauth'}) {
 6881:                 $ltiauth = 1;
 6882:             }
 6883:             if (ref($settings->{'domexttool'}) eq 'HASH') {
 6884:                 foreach my $type (@types) {
 6885:                     if ($settings->{'domexttool'}->{$type}) {
 6886:                         $domexttool{$type} = ' checked="checked"';
 6887:                     }
 6888:                 }
 6889:             } else {
 6890:                 foreach my $type (@types) {
 6891:                     if ($staticdefaults{'domexttool'}) {
 6892:                         $domexttool{$type} = ' checked="checked"';
 6893:                     }
 6894:                 }
 6895:             }
 6896:             if (ref($settings->{'exttool'}) eq 'HASH') {
 6897:                 foreach my $type (@types) {
 6898:                     if ($settings->{'exttool'}->{$type}) {
 6899:                         $exttool{$type} = ' checked="checked"';
 6900:                     }
 6901:                 }
 6902:             }
 6903:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6904:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6905:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6906:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6907:                 }
 6908:             }
 6909:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6910:                 foreach my $type (@types) {
 6911:                     next if ($type eq 'community');
 6912:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6913:                     if ($defcredits{$type} ne '') {
 6914:                         $currusecredits = 1;
 6915:                     }
 6916:                 }
 6917:             }
 6918:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6919:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6920:                     $postsubmitclient = 0;
 6921:                     foreach my $type (@types) {
 6922:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6923:                     }
 6924:                 } else {
 6925:                     foreach my $type (@types) {
 6926:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6927:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6928:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6929:                             } else {
 6930:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6931:                             }
 6932:                         } else {
 6933:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6934:                         }
 6935:                     }
 6936:                 }
 6937:             } else {
 6938:                 foreach my $type (@types) {
 6939:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6940:                 }
 6941:             }
 6942:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6943:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6944:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6945:                 }
 6946:             } else {
 6947:                 foreach my $type (@types) {
 6948:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6949:                 }
 6950:             }
 6951:         } else {
 6952:             foreach my $type (@types) {
 6953:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6954:                 if ($staticdefaults{'domexttool'}) {
 6955:                     $domexttool{$type} = ' checked="checked"';
 6956:                 }
 6957:             }
 6958:         }
 6959:         if (!$currdefresponder) {
 6960:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6961:         } elsif ($currdefresponder < 1) {
 6962:             $currdefresponder = 1;
 6963:         }
 6964:         foreach my $type (@types) {
 6965:             if ($curruploadquota{$type} eq '') {
 6966:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6967:             }
 6968:         }
 6969:         $datatable .=
 6970:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6971:                 $choices{'anonsurvey_threshold'}.
 6972:                 '</span></td>'.
 6973:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6974:                 '<input type="text" name="anonsurvey_threshold"'.
 6975:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6976:                 '</td></tr>'."\n";
 6977:         $itemcount ++;
 6978:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6979:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6980:                       $choices{'uploadquota'}.
 6981:                       '</span></td>'.
 6982:                       '<td style="text-align: right" class="LC_right_item">'.
 6983:                       '<table><tr>';
 6984:         foreach my $type (@types) {
 6985:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6986:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6987:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6988:         }
 6989:         $datatable .= '</tr></table></td></tr>'."\n";
 6990:         $itemcount ++;
 6991:         my $onclick = "toggleDisplay(this.form,'credits');";
 6992:         my $display = 'none';
 6993:         if ($currusecredits) {
 6994:             $display = 'block';
 6995:         }
 6996:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6997:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6998:         foreach my $type (@types) {
 6999:             next if ($type eq 'community');
 7000:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7001:                            '<input type="text" name="'.$type.'_credits"'.
 7002:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 7003:         }
 7004:         $additional .= '</tr></table></div>'."\n";
 7005:         %defaultchecked = ('coursecredits' => 'off');
 7006:         @toggles = ('coursecredits');
 7007:         my $current = {
 7008:                         'coursecredits' => $currusecredits,
 7009:                       };
 7010:         (my $table,$itemcount) =
 7011:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7012:                                \%choices,$itemcount,$onclick,$additional,'left');
 7013:         $datatable .= $table;
 7014:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 7015:         my $display = 'none';
 7016:         if ($postsubmitclient) {
 7017:             $display = 'block';
 7018:         }
 7019:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 7020:                       &mt('Number of seconds submit is disabled').'<br />'.
 7021:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 7022:                       '<table><tr>';
 7023:         foreach my $type (@types) {
 7024:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7025:                            '<input type="text" name="'.$type.'_timeout" value="'.
 7026:                            $deftimeout{$type}.'" size="5" /></td>';
 7027:         }
 7028:         $additional .= '</tr></table></div>'."\n";
 7029:         %defaultchecked = ('postsubmit' => 'on');
 7030:         @toggles = ('postsubmit');
 7031:         $current = {
 7032:                        'postsubmit' => $postsubmitclient,
 7033:                    };
 7034:         ($table,$itemcount) =
 7035:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7036:                                \%choices,$itemcount,$onclick,$additional,'left');
 7037:         $datatable .= $table;
 7038:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7039:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7040:                       $choices{'mysqltables'}.
 7041:                       '</span></td>'.
 7042:                       '<td style="text-align: right" class="LC_right_item">'.
 7043:                       '<table><tr>';
 7044:         foreach my $type (@types) {
 7045:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 7046:                            '<input type="text" name="mysqltables_'.$type.'"'.
 7047:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 7048:         }
 7049:         $datatable .= '</tr></table></td></tr>'."\n";
 7050:         $itemcount ++;
 7051:         %defaultchecked = ('ltiauth' => 'off');
 7052:         @toggles = ('ltiauth');
 7053:         $current = {
 7054:                        'ltiauth' => $ltiauth,
 7055:                    };
 7056:         ($table,$itemcount) =
 7057:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 7058:                                \%choices,$itemcount,undef,undef,'left');
 7059:         $datatable .= $table;
 7060:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7061:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7062:                       $choices{'domexttool'}.
 7063:                       '</span></td>'.
 7064:                       '<td style="text-align: right" class="LC_right_item">'.
 7065:                       '<table><tr>';
 7066:         foreach my $type (@types) {
 7067:             $datatable .= '<td style="text-align: left">'.
 7068:                           '<span class="LC_nobreak">'.
 7069:                           '<input type="checkbox" name="domexttool"'.
 7070:                           ' value="'.$type.'"'.$domexttool{$type}.' />'.
 7071:                           &mt($type).'</span></td>'."\n";
 7072:         }
 7073:         $datatable .= '</tr></table></td></tr>'."\n";
 7074:         $itemcount ++;
 7075:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7076:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7077:                       $choices{'exttool'}.
 7078:                       '</span></td>'.
 7079:                       '<td style="text-align: right" class="LC_right_item">'.
 7080:                       '<table><tr>';
 7081:         foreach my $type (@types) {
 7082:             $datatable .= '<td style="text-align: left">'.
 7083:                           '<span class="LC_nobreak">'.
 7084:                           '<input type="checkbox" name="exttool"'.
 7085:                           ' value="'.$type.'"'.$exttool{$type}.' />'.
 7086:                           &mt($type).'</span></td>'."\n";
 7087:         }
 7088:         $datatable .= '</tr></table></td></tr>'."\n";
 7089:     }
 7090:     $$rowtotal += $itemcount;
 7091:     return $datatable;
 7092: }
 7093: 
 7094: sub print_selfenrollment {
 7095:     my ($position,$dom,$settings,$rowtotal) = @_;
 7096:     my ($css_class,$datatable);
 7097:     my $itemcount = 1;
 7098:     my @types = ('official','unofficial','community','textbook','placement');
 7099:     if (($position eq 'top') || ($position eq 'middle')) {
 7100:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 7101:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 7102:         my @rows;
 7103:         my $key;
 7104:         if ($position eq 'top') {
 7105:             $key = 'admin'; 
 7106:             if (ref($rowsref) eq 'ARRAY') {
 7107:                 @rows = @{$rowsref};
 7108:             }
 7109:         } elsif ($position eq 'middle') {
 7110:             $key = 'default';
 7111:             @rows = ('types','registered','approval','limit');
 7112:         }
 7113:         foreach my $row (@rows) {
 7114:             if (defined($titlesref->{$row})) {
 7115:                 $itemcount ++;
 7116:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7117:                 $datatable .= '<tr'.$css_class.'>'.
 7118:                               '<td>'.$titlesref->{$row}.'</td>'.
 7119:                               '<td class="LC_left_item">'.
 7120:                               '<table><tr>';
 7121:                 my (%current,%currentcap);
 7122:                 if (ref($settings) eq 'HASH') {
 7123:                     if (ref($settings->{$key}) eq 'HASH') {
 7124:                         foreach my $type (@types) {
 7125:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7126:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 7127:                             }
 7128:                             if (($row eq 'limit') && ($key eq 'default')) {
 7129:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 7130:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 7131:                                 }
 7132:                             }
 7133:                         }
 7134:                     }
 7135:                 }
 7136:                 my %roles = (
 7137:                              '0' => &Apache::lonnet::plaintext('dc'),
 7138:                             ); 
 7139:             
 7140:                 foreach my $type (@types) {
 7141:                     unless (($row eq 'registered') && ($key eq 'default')) {
 7142:                         $datatable .= '<th>'.&mt($type).'</th>';
 7143:                     }
 7144:                 }
 7145:                 unless (($row eq 'registered') && ($key eq 'default')) {
 7146:                     $datatable .= '</tr><tr>';
 7147:                 }
 7148:                 foreach my $type (@types) {
 7149:                     if ($type eq 'community') {
 7150:                         $roles{'1'} = &mt('Community personnel');
 7151:                     } else {
 7152:                         $roles{'1'} = &mt('Course personnel');
 7153:                     }
 7154:                     $datatable .= '<td style="vertical-align: top">';
 7155:                     if ($position eq 'top') {
 7156:                         my %checked;
 7157:                         if ($current{$type} eq '0') {
 7158:                             $checked{'0'} = ' checked="checked"';
 7159:                         } else {
 7160:                             $checked{'1'} = ' checked="checked"';
 7161:                         }
 7162:                         foreach my $role ('1','0') {
 7163:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7164:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 7165:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 7166:                                           $roles{$role}.'</label></span> ';
 7167:                         }
 7168:                     } else {
 7169:                         if ($row eq 'types') {
 7170:                             my %checked;
 7171:                             if ($current{$type} =~ /^(all|dom)$/) {
 7172:                                 $checked{$1} = ' checked="checked"';
 7173:                             } else {
 7174:                                 $checked{''} = ' checked="checked"';
 7175:                             }
 7176:                             foreach my $val ('','dom','all') {
 7177:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7178:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7179:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7180:                             }
 7181:                         } elsif ($row eq 'registered') {
 7182:                             my %checked;
 7183:                             if ($current{$type} eq '1') {
 7184:                                 $checked{'1'} = ' checked="checked"';
 7185:                             } else {
 7186:                                 $checked{'0'} = ' checked="checked"';
 7187:                             }
 7188:                             foreach my $val ('0','1') {
 7189:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7190:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7191:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7192:                             }
 7193:                         } elsif ($row eq 'approval') {
 7194:                             my %checked;
 7195:                             if ($current{$type} =~ /^([12])$/) {
 7196:                                 $checked{$1} = ' checked="checked"';
 7197:                             } else {
 7198:                                 $checked{'0'} = ' checked="checked"';
 7199:                             }
 7200:                             for my $val (0..2) {
 7201:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7202:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7203:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7204:                             }
 7205:                         } elsif ($row eq 'limit') {
 7206:                             my %checked;
 7207:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 7208:                                 $checked{$1} = ' checked="checked"';
 7209:                             } else {
 7210:                                 $checked{'none'} = ' checked="checked"';
 7211:                             }
 7212:                             my $cap;
 7213:                             if ($currentcap{$type} =~ /^\d+$/) {
 7214:                                 $cap = $currentcap{$type};
 7215:                             }
 7216:                             foreach my $val ('none','allstudents','selfenrolled') {
 7217:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 7218:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 7219:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 7220:                             }
 7221:                             $datatable .= '<br />'.
 7222:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 7223:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 7224:                                           '</span>'; 
 7225:                         }
 7226:                     }
 7227:                     $datatable .= '</td>';
 7228:                 }
 7229:                 $datatable .= '</tr>';
 7230:             }
 7231:             $datatable .= '</table></td></tr>';
 7232:         }
 7233:     } elsif ($position eq 'bottom') {
 7234:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 7235:     }
 7236:     $$rowtotal += $itemcount;
 7237:     return $datatable;
 7238: }
 7239: 
 7240: sub print_validation_rows {
 7241:     my ($caller,$dom,$settings,$rowtotal) = @_;
 7242:     my ($itemsref,$namesref,$fieldsref);
 7243:     if ($caller eq 'selfenroll') { 
 7244:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 7245:     } elsif ($caller eq 'requestcourses') {
 7246:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 7247:     }
 7248:     my %currvalidation;
 7249:     if (ref($settings) eq 'HASH') {
 7250:         if (ref($settings->{'validation'}) eq 'HASH') {
 7251:             %currvalidation = %{$settings->{'validation'}};
 7252:         }
 7253:     }
 7254:     my $datatable;
 7255:     my $itemcount = 0;
 7256:     foreach my $item (@{$itemsref}) {
 7257:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7258:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 7259:                       $namesref->{$item}.
 7260:                       '</span></td>'.
 7261:                       '<td class="LC_left_item">';
 7262:         if (($item eq 'url') || ($item eq 'button')) {
 7263:             $datatable .= '<span class="LC_nobreak">'.
 7264:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 7265:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 7266:         } elsif ($item eq 'fields') {
 7267:             my @currfields;
 7268:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 7269:                 @currfields = @{$currvalidation{$item}};
 7270:             }
 7271:             foreach my $field (@{$fieldsref}) {
 7272:                 my $check = '';
 7273:                 if (grep(/^\Q$field\E$/,@currfields)) {
 7274:                     $check = ' checked="checked"';
 7275:                 }
 7276:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7277:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 7278:                               ' value="'.$field.'"'.$check.' />'.$field.
 7279:                               '</label></span> ';
 7280:             }
 7281:         } elsif ($item eq 'markup') {
 7282:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 7283:                            $currvalidation{$item}.
 7284:                               '</textarea>';
 7285:         }
 7286:         $datatable .= '</td></tr>'."\n";
 7287:         if (ref($rowtotal)) {
 7288:             $itemcount ++;
 7289:         }
 7290:     }
 7291:     if ($caller eq 'requestcourses') {
 7292:         my %currhash;
 7293:         if (ref($settings) eq 'HASH') {
 7294:             if (ref($settings->{'validation'}) eq 'HASH') {
 7295:                 if ($settings->{'validation'}{'dc'} ne '') {
 7296:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 7297:                 }
 7298:             }
 7299:         }
 7300:         my $numinrow = 2;
 7301:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 7302:                                                        'validationdc',%currhash);
 7303:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7304:         $datatable .= '<tr'.$css_class.'><td>';
 7305:         if ($numdc > 1) {
 7306:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 7307:         } else {
 7308:             $datatable .=  &mt('Course creation processed as: ');
 7309:         }
 7310:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 7311:         $itemcount ++;
 7312:     }
 7313:     if (ref($rowtotal)) {
 7314:         $$rowtotal += $itemcount;
 7315:     }
 7316:     return $datatable;
 7317: }
 7318: 
 7319: sub print_privacy {
 7320:     my ($position,$dom,$settings,$rowtotal) = @_;
 7321:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 7322:     my $itemcount = 0;
 7323:     if ($position eq 'top') {
 7324:         $numinrow = 2;
 7325:     } else {
 7326:         @items = ('domain','author','course','community');
 7327:         %names = &Apache::lonlocal::texthash (
 7328:                      domain => 'Assigned domain role(s)',
 7329:                      author => 'Assigned co-author role(s)',
 7330:                      course => 'Assigned course role(s)',
 7331:                      community => 'Assigned community role(s)',
 7332:                  );
 7333:         $numinrow = 4;
 7334:         ($othertitle,$usertypes,$types) =
 7335:             &Apache::loncommon::sorted_inst_types($dom);
 7336:     }
 7337:     if (($position eq 'top') || ($position eq 'middle')) {
 7338:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7339:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7340:         if ($position eq 'top') {
 7341:             my %curr;
 7342:             my @options = ('none','user','domain','auto');
 7343:             my %titles = &Apache::lonlocal::texthash (
 7344:                 none   => 'Not allowed',
 7345:                 user   => 'User authorizes',
 7346:                 domain => 'DC authorizes',
 7347:                 auto   => 'Unrestricted',
 7348:                 instdom => 'Other domain shares institution/provider',
 7349:                 extdom => 'Other domain has different institution/provider',
 7350:                 notify => 'Notify when role needs authorization',
 7351:             );
 7352:             my %names = &Apache::lonlocal::texthash (
 7353:                 domain => 'Domain role',
 7354:                 author => 'Co-author role',
 7355:                 course => 'Course role',
 7356:                 community => 'Community role',
 7357:             );
 7358:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7359:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7360:             foreach my $domtype ('instdom','extdom') {
 7361:                 my (%checked,$skip);
 7362:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7363:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 7364:                               '<td class="LC_left_item">';
 7365:                 if ($domtype eq 'instdom') {
 7366:                     unless (@instdoms > 1) {
 7367:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 7368:                         $skip = 1;
 7369:                     }
 7370:                 } elsif ($domtype eq 'extdom') {
 7371:                     if (keys(%by_location) == 0) {
 7372:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 7373:                         $skip = 1;
 7374:                     }
 7375:                 }
 7376:                 unless ($skip) {
 7377:                     foreach my $roletype ('domain','author','course','community') {
 7378:                         $checked{'auto'} = ' checked="checked"';
 7379:                         if (ref($settings) eq 'HASH') {
 7380:                             if (ref($settings->{approval}) eq 'HASH') {
 7381:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 7382:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 7383:                                         $checked{$1} = ' checked="checked"';
 7384:                                         $checked{'auto'} = '';
 7385:                                     }
 7386:                                 }
 7387:                             }
 7388:                         }
 7389:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 7390:                         foreach my $option (@options) {
 7391:                             $datatable .= '<span class="LC_nobreak"><label>'.
 7392:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 7393:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7394:                                           '</label></span>&nbsp; ';
 7395:                         }
 7396:                         $datatable .= '</fieldset>';
 7397:                     }
 7398:                 }
 7399:                 $datatable .= '</td></tr>';
 7400:                 $itemcount ++;
 7401:             }
 7402:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7403:             $datatable .= '<tr'.$css_class.'><td>'.$titles{'notify'}.'</td>'.
 7404:                           '<td class="LC_left_item">';
 7405:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7406:                 my %curr;
 7407:                 if (ref($settings) eq 'HASH') {
 7408:                     if ($settings->{'notify'} ne '') {
 7409:                         map {$curr{$_}=1;} split(/,/,$settings->{'notify'});
 7410:                     }
 7411:                 }
 7412:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7413:                 my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 7414:                                                              'privacy_notify',%curr);
 7415:                 if ($numdc > 0) {
 7416:                     $datatable .= $table;
 7417:                 } else {
 7418:                     $datatable .= &mt('There are no active Domain Coordinators');
 7419:                 }
 7420:             } else {
 7421:                 $datatable .= &mt('Nothing to set here, as there are no other domains');
 7422:             }
 7423:             $datatable .='</td></tr>';
 7424:         } elsif ($position eq 'middle') {
 7425:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 7426:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7427:                     foreach my $item (@{$types}) {
 7428:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 7429:                                                                $numinrow,$itemcount,'','','','','',
 7430:                                                                '',$usertypes->{$item});
 7431:                         $itemcount ++;
 7432:                     }
 7433:                 }
 7434:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 7435:                                                        $numinrow,$itemcount,'','','','','',
 7436:                                                        '',$othertitle);
 7437:                 $itemcount ++;
 7438:             } else {
 7439:                 my (@insttypes,%insttitles);
 7440:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7441:                     @insttypes = @{$types};
 7442:                     %insttitles = %{$usertypes};
 7443:                 }
 7444:                 foreach my $item (@insttypes,'default') {
 7445:                     my $title;
 7446:                     if ($item eq 'default') {
 7447:                         $title = $othertitle;
 7448:                     } else {
 7449:                         $title = $insttitles{$item};
 7450:                     }
 7451:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7452:                     $datatable .= '<tr'.$css_class.'>'.
 7453:                                   '<td class="LC_left_item">'.$title.'</td>'.
 7454:                                   '<td class="LC_left_item">'.
 7455:                                   &mt('Nothing to set here, as there are no other domains').
 7456:                                   '</td></tr>';
 7457:                     $itemcount ++;
 7458:                 }
 7459:             }
 7460:         }
 7461:     } else {
 7462:         my $prefix;
 7463:         if ($position eq 'lower') {
 7464:             $prefix = 'priv';
 7465:         } else {
 7466:             $prefix = 'unpriv';
 7467:         }
 7468:         foreach my $item (@items) {
 7469:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 7470:                                                    $numinrow,$itemcount,'','','','','',
 7471:                                                    '',$names{$item});
 7472:             $itemcount ++;
 7473:         }
 7474:     }
 7475:     if (ref($rowtotal)) {
 7476:         $$rowtotal += $itemcount;
 7477:     }
 7478:     return $datatable;
 7479: }
 7480: 
 7481: sub print_passwords {
 7482:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 7483:     my ($datatable,$css_class);
 7484:     my $itemcount = 0;
 7485:     my %titles = &Apache::lonlocal::texthash (
 7486:         captcha        => '"Forgot Password" CAPTCHA validation',
 7487:         link           => 'Reset link expiration (hours)',
 7488:         case           => 'Case-sensitive usernames/e-mail',
 7489:         prelink        => 'Information required (form 1)',
 7490:         postlink       => 'Information required (form 2)',
 7491:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 7492:         customtext     => 'Domain specific text (HTML)',
 7493:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 7494:         intauth_check  => 'Check bcrypt cost if authenticated',
 7495:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 7496:         permanent      => 'Permanent e-mail address',
 7497:         critical       => 'Critical notification address',
 7498:         notify         => 'Notification address',
 7499:         min            => 'Minimum password length',
 7500:         max            => 'Maximum password length',
 7501:         chars          => 'Required characters',
 7502:         expire         => 'Password expiration (days)',
 7503:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 7504:     );
 7505:     if ($position eq 'top') {
 7506:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7507:         my $shownlinklife = 2;
 7508:         my $prelink = 'both';
 7509:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 7510:         if (ref($settings) eq 'HASH') {
 7511:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 7512:                 $shownlinklife = $settings->{resetlink};
 7513:             }
 7514:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 7515:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 7516:             }
 7517:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 7518:                 $prelink = $settings->{resetprelink};
 7519:             }
 7520:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 7521:                 %postlink = %{$settings->{resetpostlink}};
 7522:             }
 7523:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 7524:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 7525:             }
 7526:             if ($settings->{resetremove}) {
 7527:                 $nostdtext = 1;
 7528:             }
 7529:             if ($settings->{resetcustom}) {
 7530:                 $customurl = $settings->{resetcustom};
 7531:             }
 7532:         } else {
 7533:             if (ref($types) eq 'ARRAY') {
 7534:                 foreach my $item (@{$types}) {
 7535:                     $casesens{$item} = 1;
 7536:                     $postlink{$item} = ['username','email'];
 7537:                 }
 7538:             }
 7539:             $casesens{'default'} = 1;
 7540:             $postlink{'default'} = ['username','email'];
 7541:             $prelink = 'both';
 7542:             %emailsrc = (
 7543:                           permanent => 1,
 7544:                           critical  => 1,
 7545:                           notify    => 1,
 7546:             );
 7547:         }
 7548:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 7549:         $itemcount ++;
 7550:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7551:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 7552:                       '<td class="LC_left_item">'.
 7553:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 7554:                       'name="passwords_link" size="3" /></td></tr>';
 7555:         $itemcount ++;
 7556:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7557:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 7558:                       '<td class="LC_left_item">';
 7559:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7560:             foreach my $item (@{$types}) {
 7561:                 my $checkedcase;
 7562:                 if ($casesens{$item}) {
 7563:                     $checkedcase = ' checked="checked"';
 7564:                 }
 7565:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7566:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 7567:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 7568:                               '</span>&nbsp;&nbsp; ';
 7569:             }
 7570:         }
 7571:         my $checkedcase;
 7572:         if ($casesens{'default'}) {
 7573:             $checkedcase = ' checked="checked"';
 7574:         }
 7575:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7576:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 7577:                       $othertitle.'</label></span></td>';
 7578:         $itemcount ++;
 7579:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7580:         my %checkedpre = (
 7581:                              both => ' checked="checked"',
 7582:                              either => '',
 7583:                          );
 7584:         if ($prelink eq 'either') {
 7585:             $checkedpre{either} = ' checked="checked"';
 7586:             $checkedpre{both} = '';
 7587:         }
 7588:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 7589:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7590:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 7591:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 7592:                       '<span class="LC_nobreak"><label>'.
 7593:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 7594:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 7595:         $itemcount ++;
 7596:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7597:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 7598:                       '<td class="LC_left_item">';
 7599:         my %postlinked;
 7600:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7601:             foreach my $item (@{$types}) {
 7602:                 undef(%postlinked);
 7603:                 $datatable .= '<fieldset style="display: inline-block;">'.
 7604:                               '<legend>'.$usertypes->{$item}.'</legend>';
 7605:                 if (ref($postlink{$item}) eq 'ARRAY') {
 7606:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 7607:                 }
 7608:                 foreach my $field ('email','username') {
 7609:                     my $checked;
 7610:                     if ($postlinked{$field}) {
 7611:                         $checked = ' checked="checked"';
 7612:                     }
 7613:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7614:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 7615:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 7616:                                   '<span>&nbsp;&nbsp; ';
 7617:                 }
 7618:                 $datatable .= '</fieldset>';
 7619:             }
 7620:         }
 7621:         if (ref($postlink{'default'}) eq 'ARRAY') {
 7622:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 7623:         }
 7624:         $datatable .= '<fieldset style="display: inline-block;">'.
 7625:                       '<legend>'.$othertitle.'</legend>';
 7626:         foreach my $field ('email','username') {
 7627:             my $checked;
 7628:             if ($postlinked{$field}) {
 7629:                 $checked = ' checked="checked"';
 7630:             }
 7631:             $datatable .= '<span class="LC_nobreak"><label>'.
 7632:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 7633:                           $field.'"'.$checked.' />'.$field.'</label>'.
 7634:                           '<span>&nbsp;&nbsp; ';
 7635:         }
 7636:         $datatable .= '</fieldset></td></tr>';
 7637:         $itemcount ++;
 7638:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7639:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 7640:                       '<td class="LC_left_item">';
 7641:         foreach my $type ('permanent','critical','notify') {
 7642:             my $checkedemail;
 7643:             if ($emailsrc{$type}) {
 7644:                 $checkedemail = ' checked="checked"';
 7645:             }
 7646:             $datatable .= '<span class="LC_nobreak"><label>'.
 7647:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 7648:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 7649:                           '<span>&nbsp;&nbsp; ';
 7650:         }
 7651:         $datatable .= '</td></tr>';
 7652:         $itemcount ++;
 7653:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7654:         my $switchserver = &check_switchserver($dom,$confname);
 7655:         my ($showstd,$noshowstd);
 7656:         if ($nostdtext) {
 7657:             $noshowstd = ' checked="checked"';
 7658:         } else {
 7659:             $showstd = ' checked="checked"';
 7660:         }
 7661:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 7662:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7663:                       &mt('Retain standard text:').
 7664:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 7665:                       &mt('Yes').'</label>'.'&nbsp;'.
 7666:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 7667:                       &mt('No').'</label></span><br />'.
 7668:                       '<span class="LC_fontsize_small">'.
 7669:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 7670:                       &mt('Include custom text:');
 7671:         if ($customurl) {
 7672:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 7673:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7674:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 7675:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 7676:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 7677:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 7678:         }
 7679:         if ($switchserver) {
 7680:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 7681:         } else {
 7682:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7683:                          '<input type="file" name="passwords_customfile" /></span>';
 7684:         }
 7685:         $datatable .= '</td></tr>';
 7686:     } elsif ($position eq 'middle') {
 7687:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 7688:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 7689:         my %defaults;
 7690:         if (ref($domconf{'defaults'}) eq 'HASH') {
 7691:             %defaults = %{$domconf{'defaults'}};
 7692:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 7693:                 $defaults{'intauth_cost'} = 10;
 7694:             }
 7695:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 7696:                 $defaults{'intauth_check'} = 0;
 7697:             }
 7698:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 7699:                 $defaults{'intauth_switch'} = 0;
 7700:             }
 7701:         } else {
 7702:             %defaults = (
 7703:                           'intauth_cost'   => 10,
 7704:                           'intauth_check'  => 0,
 7705:                           'intauth_switch' => 0,
 7706:                         );
 7707:         }
 7708:         foreach my $item (@items) {
 7709:             if ($itemcount%2) {
 7710:                 $css_class = '';
 7711:             } else {
 7712:                 $css_class = ' class="LC_odd_row" ';
 7713:             }
 7714:             $datatable .= '<tr'.$css_class.'>'.
 7715:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 7716:                           '</span></td><td class="LC_left_item" colspan="3">';
 7717:             if ($item eq 'intauth_switch') {
 7718:                 my @options = (0,1,2);
 7719:                 my %optiondesc = &Apache::lonlocal::texthash (
 7720:                                    0 => 'No',
 7721:                                    1 => 'Yes',
 7722:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 7723:                                  );
 7724:                 $datatable .= '<table width="100%">';
 7725:                 foreach my $option (@options) {
 7726:                     my $checked = ' ';
 7727:                     if ($defaults{$item} eq $option) {
 7728:                         $checked = ' checked="checked"';
 7729:                     }
 7730:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7731:                                   '<label><input type="radio" name="'.$item.
 7732:                                   '" value="'.$option.'"'.$checked.' />'.
 7733:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7734:                 }
 7735:                 $datatable .= '</table>';
 7736:             } elsif ($item eq 'intauth_check') {
 7737:                 my @options = (0,1,2);
 7738:                 my %optiondesc = &Apache::lonlocal::texthash (
 7739:                                    0 => 'No',
 7740:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 7741:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 7742:                                  );
 7743:                 $datatable .= '<table width="100%">';
 7744:                 foreach my $option (@options) {
 7745:                     my $checked = ' ';
 7746:                     my $onclick;
 7747:                     if ($defaults{$item} eq $option) {
 7748:                         $checked = ' checked="checked"';
 7749:                     }
 7750:                     if ($option == 2) {
 7751:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7752:                     }
 7753:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7754:                                   '<label><input type="radio" name="'.$item.
 7755:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7756:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7757:                 }
 7758:                 $datatable .= '</table>';
 7759:             } else {
 7760:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7761:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7762:             }
 7763:             $datatable .= '</td></tr>';
 7764:             $itemcount ++;
 7765:         }
 7766:     } elsif ($position eq 'lower') {
 7767:         $datatable .= &password_rules('passwords',\$itemcount,$settings);
 7768:     } else {
 7769:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7770:         my %ownerchg = (
 7771:                           by  => {},
 7772:                           for => {},
 7773:                        );
 7774:         my %ownertitles = &Apache::lonlocal::texthash (
 7775:                             by  => 'Course owner status(es) allowed',
 7776:                             for => 'Student status(es) allowed',
 7777:                           );
 7778:         if (ref($settings) eq 'HASH') {
 7779:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7780:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7781:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7782:                 }
 7783:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7784:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7785:                 }
 7786:             }
 7787:         }
 7788:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7789:         $datatable .= '<tr '.$css_class.'>'.
 7790:                       '<td>'.
 7791:                       &mt('Requirements').'<ul>'.
 7792:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7793:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7794:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7795:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7796:                       '</ul>'.
 7797:                       '</td>'.
 7798:                       '<td class="LC_left_item">';
 7799:         foreach my $item ('by','for') {
 7800:             $datatable .= '<fieldset style="display: inline-block;">'.
 7801:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7802:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7803:                 foreach my $type (@{$types}) {
 7804:                     my $checked;
 7805:                     if ($ownerchg{$item}{$type}) {
 7806:                         $checked = ' checked="checked"';
 7807:                     }
 7808:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7809:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7810:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7811:                                   '</span>&nbsp;&nbsp; ';
 7812:                 }
 7813:             }
 7814:             my $checked;
 7815:             if ($ownerchg{$item}{'default'}) {
 7816:                 $checked = ' checked="checked"';
 7817:             }
 7818:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7819:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7820:                           $othertitle.'</label></span></fieldset>';
 7821:         }
 7822:         $datatable .= '</td></tr>';
 7823:     }
 7824:     return $datatable;
 7825: }
 7826: 
 7827: sub password_rules {
 7828:     my ($prefix,$itemcountref,$settings) = @_;
 7829:     my ($min,$max,%chars,$expire,$numsaved,$numinrow);
 7830:     my %titles;
 7831:     if ($prefix eq 'passwords') {
 7832:         %titles = &Apache::lonlocal::texthash (
 7833:             min            => 'Minimum password length',
 7834:             max            => 'Maximum password length',
 7835:             chars          => 'Required characters',
 7836:         );
 7837:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
 7838:         %titles = &Apache::lonlocal::texthash (
 7839:             min            => 'Minimum secret length',
 7840:             max            => 'Maximum secret length',
 7841:             chars          => 'Required characters',
 7842:         );
 7843:     }
 7844:     $min = $Apache::lonnet::passwdmin;
 7845:     my $datatable;
 7846:     my $itemcount;
 7847:     if (ref($itemcountref)) {
 7848:         $itemcount = $$itemcountref;
 7849:     }
 7850:     if (ref($settings) eq 'HASH') {
 7851:         if ($settings->{min}) {
 7852:             $min = $settings->{min};
 7853:         }
 7854:         if ($settings->{max}) {
 7855:             $max = $settings->{max};
 7856:         }
 7857:         if (ref($settings->{chars}) eq 'ARRAY') {
 7858:             map { $chars{$_} = 1; } (@{$settings->{chars}});
 7859:         }
 7860:         if ($prefix eq 'passwords') { 
 7861:             if ($settings->{expire}) {
 7862:                 $expire = $settings->{expire};
 7863:             }
 7864:             if ($settings->{numsaved}) {
 7865:                 $numsaved = $settings->{numsaved};
 7866:             }
 7867:         }
 7868:     }
 7869:     my %rulenames = &Apache::lonlocal::texthash(
 7870:                                                  uc => 'At least one upper case letter',
 7871:                                                  lc => 'At least one lower case letter',
 7872:                                                  num => 'At least one number',
 7873:                                                  spec => 'At least one non-alphanumeric',
 7874:                                                );
 7875:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7876:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7877:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7878:                   '<input type="text" name="'.$prefix.'_min" value="'.$min.'" size="3" '.
 7879:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7880:                   '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7881:                   '</span></td></tr>';
 7882:     $itemcount ++;
 7883:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7884:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7885:                   '<td class="LC_left_item"><span class="LC_nobreak">'.
 7886:                   '<input type="text" name="'.$prefix.'_max" value="'.$max.'" size="3" '.
 7887:                   'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7888:                   '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7889:                   '</span></td></tr>';
 7890:     $itemcount ++;
 7891:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7892:     $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7893:                   '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7894:                   '</span></td>';
 7895:     my $numinrow = 2;
 7896:     my @possrules = ('uc','lc','num','spec');
 7897:     $datatable .= '<td class="LC_left_item"><table>';
 7898:     for (my $i=0; $i<@possrules; $i++) {
 7899:         my ($rem,$checked);
 7900:         if ($chars{$possrules[$i]}) {
 7901:             $checked = ' checked="checked"';
 7902:         }
 7903:         $rem = $i%($numinrow);
 7904:         if ($rem == 0) {
 7905:             if ($i > 0) {
 7906:                 $datatable .= '</tr>';
 7907:             }
 7908:             $datatable .= '<tr>';
 7909:         }
 7910:         $datatable .= '<td><span class="LC_nobreak"><label>'.
 7911:                       '<input type="checkbox" name="'.$prefix.'_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7912:                       $rulenames{$possrules[$i]}.'</label></span></td>';
 7913:     }
 7914:     my $rem = @possrules%($numinrow);
 7915:     my $colsleft = $numinrow - $rem;
 7916:     if ($colsleft > 1 ) {
 7917:         $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7918:                       '&nbsp;</td>';
 7919:     } elsif ($colsleft == 1) {
 7920:         $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7921:     }
 7922:     $datatable .='</table></td></tr>';
 7923:     $itemcount ++;
 7924:     if ($prefix eq 'passwords') {
 7925:         $titles{'expire'} = &mt('Password expiration (days)');
 7926:         $titles{'numsaved'} = &mt('Number of previous passwords to save and disallow reuse');
 7927:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7928:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7929:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7930:                       '<input type="text" name="'.$prefix.'_expire" value="'.$expire.'" size="4" '.
 7931:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7932:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7933:                       '</span></td></tr>';
 7934:         $itemcount ++;
 7935:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7936:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7937:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7938:                       '<input type="text" name="'.$prefix.'_numsaved" value="'.$numsaved.'" size="3" '.
 7939:                       'onblur="javascript:warnInt'.$prefix.'(this);" />'.
 7940:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7941:                       '</span></td></tr>';
 7942:         $itemcount ++;
 7943:     }
 7944:     if (ref($itemcountref)) {
 7945:         $$itemcountref += $itemcount;
 7946:     }
 7947:     return $datatable;
 7948: }
 7949: 
 7950: sub print_wafproxy {
 7951:     my ($position,$dom,$settings,$rowtotal) = @_;
 7952:     my $css_class;
 7953:     my $itemcount = 0;
 7954:     my $datatable;
 7955:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7956:     my (%othercontrol,%otherdoms,%aliases,%saml,%values,$setdom,$showdom);
 7957:     my %lt = &wafproxy_titles();
 7958:     foreach my $server (sort(keys(%servers))) {
 7959:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
 7960:         next if ($serverhome eq '');
 7961:         my $serverdom;
 7962:         if ($serverhome ne $server) {
 7963:             $serverdom = &Apache::lonnet::host_domain($serverhome);
 7964:             if (($serverdom ne '') && (&Apache::lonnet::domain($serverdom) ne '')) {
 7965:                 $othercontrol{$server} = $serverdom;
 7966:             }
 7967:         } else {
 7968:             $serverdom = &Apache::lonnet::host_domain($server);
 7969:             next if (($serverdom eq '') || (&Apache::lonnet::domain($serverdom) eq ''));
 7970:             if ($serverdom ne $dom) {
 7971:                 $othercontrol{$server} = $serverdom;
 7972:             } else {
 7973:                 $setdom = 1;
 7974:                 if (ref($settings) eq 'HASH') {
 7975:                     if (ref($settings->{'alias'}) eq 'HASH') {
 7976:                         $aliases{$dom} = $settings->{'alias'};
 7977:                         if ($aliases{$dom} ne '') {
 7978:                             $showdom = 1;
 7979:                         }
 7980:                     }
 7981:                     if (ref($settings->{'saml'}) eq 'HASH') {
 7982:                         $saml{$dom} = $settings->{'saml'};
 7983:                     }
 7984:                 }
 7985:             }
 7986:         }
 7987:     }
 7988:     if ($setdom) {
 7989:         %{$values{$dom}} = ();
 7990:         if (ref($settings) eq 'HASH') {
 7991:             foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 7992:                 $values{$dom}{$item} = $settings->{$item};
 7993:             }
 7994:         }
 7995:     }
 7996:     if (keys(%othercontrol)) {
 7997:         %otherdoms = reverse(%othercontrol);
 7998:         foreach my $domain (keys(%otherdoms)) {
 7999:             %{$values{$domain}} = ();
 8000:             my %config = &Apache::lonnet::get_dom('configuration',['wafproxy'],$domain);
 8001:             if (ref($config{'wafproxy'}) eq 'HASH') {
 8002:                 $aliases{$domain} = $config{'wafproxy'}{'alias'};
 8003:                 if (exists($config{'wafproxy'}{'saml'})) { 
 8004:                     $saml{$domain} = $config{'wafproxy'}{'saml'};
 8005:                 }
 8006:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext') {
 8007:                     $values{$domain}{$item} = $config{'wafproxy'}{$item};
 8008:                 }
 8009:             }
 8010:         }
 8011:     }
 8012:     if ($position eq 'top') {
 8013:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8014:         my %aliasinfo;
 8015:         foreach my $server (sort(keys(%servers))) {
 8016:             $itemcount ++;
 8017:             my $dom_in_effect;
 8018:             my $aliasrows = '<tr>'.
 8019:                             '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8020:                             &mt('Hostname').':&nbsp;'.
 8021:                             '<i>'.&Apache::lonnet::hostname($server).'</i></td><td>&nbsp;</td>';
 8022:             if ($othercontrol{$server}) {
 8023:                 $dom_in_effect = $othercontrol{$server};
 8024:                 my ($current,$forsaml);
 8025:                 if (ref($aliases{$dom_in_effect}) eq 'HASH') {
 8026:                     $current = $aliases{$dom_in_effect}{$server};
 8027:                 }
 8028:                 if (ref($saml{$dom_in_effect}) eq 'HASH') {
 8029:                     if ($saml{$dom_in_effect}{$server}) {
 8030:                         $forsaml = 1;
 8031:                     }
 8032:                 }
 8033:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8034:                               &mt('Alias').':&nbsp';
 8035:                 if ($current) {
 8036:                     $aliasrows .= $current;
 8037:                     if ($forsaml) {
 8038:                          $aliasrows .= '&nbsp;('.&mt('also for SSO Auth').')';
 8039:                     }
 8040:                 } else {
 8041:                     $aliasrows .= &mt('None');
 8042:                 }
 8043:                 $aliasrows .= '&nbsp;<span class="LC_small">('.
 8044:                               &mt('controlled by domain: [_1]',
 8045:                                   '<b>'.$dom_in_effect.'</b>').')</span></td>';
 8046:             } else {
 8047:                 $dom_in_effect = $dom;
 8048:                 my ($current,$samlon,$samloff);
 8049:                 $samloff = ' checked="checked"';
 8050:                 if (ref($aliases{$dom}) eq 'HASH') {
 8051:                     if ($aliases{$dom}{$server}) {
 8052:                         $current = $aliases{$dom}{$server};
 8053:                     }
 8054:                 }
 8055:                 if (ref($saml{$dom}) eq 'HASH') {
 8056:                     if ($saml{$dom}{$server}) {
 8057:                         $samlon = $samloff;
 8058:                         undef($samloff);
 8059:                     }
 8060:                 }
 8061:                 $aliasrows .= '<td class="LC_left_item" style="vertical-align: baseline;">'.
 8062:                               &mt('Alias').':&nbsp;'.
 8063:                               '<input type="text" name="wafproxy_alias_'.$server.'" '.
 8064:                               'value="'.$current.'" size="30" />'.
 8065:                               ('&nbsp;'x2).'<span class="LC_nobreak">'.
 8066:                               &mt('Alias used for SSO Auth').':&nbsp;<label>'.
 8067:                               '<input type="radio" value="0"'.$samloff.' name="wafproxy_alias_saml_'.$server.'" />'.
 8068:                               &mt('No').'</label>&nbsp;<label>'. 
 8069:                               '<input type="radio" value="1"'.$samlon.' name="wafproxy_alias_saml_'.$server.'" />'.
 8070:                               &mt('Yes').'</label></span>'.
 8071:                               '</td>'; 
 8072:             }
 8073:             $aliasrows .= '</tr>';
 8074:             $aliasinfo{$dom_in_effect} .= $aliasrows;
 8075:         }
 8076:         if ($aliasinfo{$dom}) {
 8077:             my ($onclick,$wafon,$wafoff,$showtable);
 8078:             $onclick = ' onclick="javascript:toggleWAF();"';
 8079:             $wafoff = ' checked="checked"';
 8080:             $showtable = ' style="display:none";';
 8081:             if ($showdom) {
 8082:                 $wafon = $wafoff;
 8083:                 $wafoff = '';
 8084:                 $showtable = ' style="display:inline;"';
 8085:             }
 8086:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8087:             $datatable = '<tr'.$css_class.'>'.
 8088:                          '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br />'.
 8089:                          '<span class="LC_nobreak">'.&mt('WAF in use?').'&nbsp;<label>'.
 8090:                          '<input type="radio" name="wafproxy_'.$dom.'" value="1"'.$wafon.$onclick.' />'.
 8091:                          &mt('Yes').'</label>'.('&nbsp;'x2).'<label>'.
 8092:                          '<input type="radio" name="wafproxy_'.$dom.'" value="0"'.$wafoff.$onclick.' />'.
 8093:                          &mt('No').'</label></span></td>'.
 8094:                          '<td class="LC_left_item">'.
 8095:                          '<table id="wafproxy_table"'.$showtable.'>'.$aliasinfo{$dom}.
 8096:                          '</table></td></tr>';
 8097:             $itemcount++;
 8098:         }
 8099:         if (keys(%otherdoms)) {
 8100:             foreach my $key (sort(keys(%otherdoms))) {
 8101:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8102:                 $datatable .= '<tr'.$css_class.'>'.
 8103:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$key.'</b>').'</td>'.
 8104:                               '<td class="LC_left_item"><table>'.$aliasinfo{$key}.
 8105:                               '</table></td></tr>';
 8106:                 $itemcount++;
 8107:             }
 8108:         }
 8109:     } else {
 8110:         my %ip_methods = &remoteip_methods();
 8111:         if ($setdom) {
 8112:             $itemcount ++;
 8113:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8114:             my ($nowafstyle,$wafstyle,$curr_remotip,$currwafdisplay,$vpndircheck,$vpnaliascheck,
 8115:                 $currwafvpn,$wafrangestyle,$alltossl,$ssltossl);
 8116:             $wafstyle = ' style="display:none;"';
 8117:             $nowafstyle = ' style="display:table-row;"';
 8118:             $currwafdisplay = ' style="display: none"';
 8119:             $wafrangestyle = ' style="display: none"';
 8120:             $curr_remotip = 'n';
 8121:             $ssltossl = ' checked="checked"';
 8122:             if ($showdom) {
 8123:                 $wafstyle = ' style="display:table-row;"';
 8124:                 $nowafstyle =  ' style="display:none;"';
 8125:                 if (keys(%{$values{$dom}})) {
 8126:                     if ($values{$dom}{remoteip} =~ /^[nmh]$/) {
 8127:                         $curr_remotip = $values{$dom}{remoteip};
 8128:                     }
 8129:                     if ($curr_remotip eq 'h') {
 8130:                         $currwafdisplay = ' style="display:table-row"';
 8131:                         $wafrangestyle = ' style="display:inline-block;"';
 8132:                     }
 8133:                     if ($values{$dom}{'sslopt'}) {
 8134:                         $alltossl = ' checked="checked"';
 8135:                         $ssltossl = '';
 8136:                     }
 8137:                 }
 8138:                 if (($values{$dom}{'vpnint'} ne '') || ($values{$dom}{'vpnext'} ne '')) {
 8139:                     $vpndircheck = ' checked="checked"';
 8140:                     $currwafvpn = ' style="display:table-row;"';
 8141:                     $wafrangestyle = ' style="display:inline-block;"';
 8142:                 } else {
 8143:                     $vpnaliascheck = ' checked="checked"';
 8144:                     $currwafvpn = ' style="display:none;"';
 8145:                 }
 8146:             }
 8147:             $datatable .= '<tr'.$css_class.' id="nowafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8148:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'</td>'.
 8149:                           '<td class="LC_right_item">'.&mt('WAF not in use, nothing to set').'</td>'.
 8150:                           '</tr>'.
 8151:                           '<tr'.$css_class.' id="wafproxyrow_'.$dom.'"'.$wafstyle.'>'.
 8152:                           '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$dom.'</b>').'<br /><br />'.
 8153:                           '<div id="wafproxyranges_'.$dom.'">'.&mt('Format for comma separated IP ranges').':<br />'.
 8154:                           &mt('A.B.C.D/N or A.B.C.D-E.F.G.H').'<br />'.
 8155:                           &mt('Range(s) stored in CIDR notation').'</div></td>'.
 8156:                           '<td class="LC_left_item"><table>'.
 8157:                           '<tr>'.
 8158:                           '<td valign="top">'.$lt{'remoteip'}.':&nbsp;'.
 8159:                           '<select name="wafproxy_remoteip" id="wafproxy_remoteip" onchange="javascript:updateWAF();">';
 8160:             foreach my $option ('m','h','n') {
 8161:                 my $sel;
 8162:                 if ($option eq $curr_remotip) {
 8163:                    $sel = ' selected="selected"';
 8164:                 }
 8165:                 $datatable .= '<option value="'.$option.'"'.$sel.'>'.
 8166:                               $ip_methods{$option}.'</option>';
 8167:             }
 8168:             $datatable .= '</select></td></tr>'."\n".
 8169:                           '<tr id="wafproxy_header"'.$currwafdisplay.'><td>'.
 8170:                           $lt{'ipheader'}.':&nbsp;'.
 8171:                           '<input type="text" value="'.$values{$dom}{'ipheader'}.'" '.
 8172:                           'name="wafproxy_ipheader" />'.
 8173:                           '</td></tr>'."\n".
 8174:                           '<tr id="wafproxy_trust"'.$currwafdisplay.'><td>'.
 8175:                           $lt{'trusted'}.':<br />'.
 8176:                           '<textarea name="wafproxy_trusted" rows="3" cols="80">'.
 8177:                           $values{$dom}{'trusted'}.'</textarea>'.
 8178:                           '</td></tr>'."\n".
 8179:                           '<tr><td><hr /></td></tr>'."\n".
 8180:                           '<tr>'.
 8181:                           '<td valign="top">'.$lt{'vpnaccess'}.':<br /><span class="LC_nobreak">'.
 8182:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpndircheck.' value="1" onclick="javascript:checkWAF();" />'.
 8183:                           $lt{'vpndirect'}.'</label>'.('&nbsp;'x2).
 8184:                           '<label><input type="radio" name="wafproxy_vpnaccess"'.$vpnaliascheck.' value="0" onclick="javascript:checkWAF();" />'.
 8185:                           $lt{'vpnaliased'}.'</label></span></td></tr>';
 8186:             foreach my $item ('vpnint','vpnext') {
 8187:                 $datatable .= '<tr id="wafproxy_show_'.$item.'"'.$currwafvpn.'>'.
 8188:                               '<td valign="top">'.$lt{$item}.':<br />'.
 8189:                               '<textarea name="wafproxy_'.$item.'" rows="3" cols="80">'.
 8190:                               $values{$dom}{$item}.'</textarea>'.
 8191:                               '</td></tr>'."\n";
 8192:             }
 8193:             $datatable .= '<tr><td><hr /></td></tr>'."\n".
 8194:                           '<tr>'.
 8195:                           '<td valign="top">'.$lt{'sslopt'}.':<br /><span class="LC_nobreak">'.
 8196:                           '<label><input type="radio" name="wafproxy_sslopt"'.$alltossl.' value="1" />'.
 8197:                           $lt{'alltossl'}.'</label>'.('&nbsp;'x2).
 8198:                           '<label><input type="radio" name="wafproxy_sslopt"'.$ssltossl.' value="0" />'.
 8199:                           $lt{'ssltossl'}.'</label></span></td></tr>'."\n".
 8200:                           '</table></td></tr>';
 8201:         }
 8202:         if (keys(%otherdoms)) {
 8203:             foreach my $domain (sort(keys(%otherdoms))) {
 8204:                 $itemcount ++;
 8205:                 $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8206:                 $datatable .= '<tr'.$css_class.'>'.
 8207:                               '<td class="LC_left_item">'.&mt('Domain: [_1]','<b>'.$domain.'</b>').'</td>'.
 8208:                               '<td class="LC_left_item"><table>';
 8209:                 foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
 8210:                     my $showval = &mt('None');
 8211:                     if ($item eq 'ssl') {
 8212:                         $showval = $lt{'ssltossl'};
 8213:                     }
 8214:                     if ($values{$domain}{$item}) {
 8215:                         $showval = $values{$domain}{$item};
 8216:                         if ($item eq 'ssl') {
 8217:                             $showval = $lt{'alltossl'};
 8218:                         } elsif ($item eq 'remoteip') {
 8219:                             $showval = $ip_methods{$values{$domain}{$item}};
 8220:                         }
 8221:                     }
 8222:                     $datatable .= '<tr>'.
 8223:                                   '<td>'.$lt{$item}.':&nbsp;'.$showval.'</td></tr>';
 8224:                 }
 8225:                 $datatable .= '</table></td></tr>';
 8226:             }
 8227:         }
 8228:     }
 8229:     $$rowtotal += $itemcount;
 8230:     return $datatable;
 8231: }
 8232: 
 8233: sub wafproxy_titles {
 8234:     return &Apache::lonlocal::texthash(
 8235:                remoteip   => "Method for determining user's IP",
 8236:                ipheader   => 'Request header containing remote IP',
 8237:                trusted    => 'Trusted IP range(s)',
 8238:                vpnaccess  => 'Access from institutional VPN',
 8239:                vpndirect  => 'via regular hostname (no WAF)',
 8240:                vpnaliased => 'via aliased hostname (WAF)',
 8241:                vpnint     => 'Internal IP Range(s) for VPN sessions',
 8242:                vpnext     => 'IP Range(s) for backend WAF connections',
 8243:                sslopt     => 'Forwarding http/https',
 8244:                alltossl   => 'WAF forwards both http and https requests to https',
 8245:                ssltossl   => 'WAF forwards http requests to http and https to https',
 8246:            );
 8247: }
 8248: 
 8249: sub remoteip_methods {
 8250:     return &Apache::lonlocal::texthash(
 8251:               m => 'Use Apache mod_remoteip',
 8252:               h => 'Use headers parsed by LON-CAPA',
 8253:               n => 'Not in use',
 8254:            );
 8255: }
 8256: 
 8257: sub print_usersessions {
 8258:     my ($position,$dom,$settings,$rowtotal) = @_;
 8259:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 8260:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8261:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8262: 
 8263:     my @alldoms = &Apache::lonnet::all_domains();
 8264:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8265:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8266:     my %altids = &id_for_thisdom(%servers);
 8267:     if ($position eq 'top') {
 8268:         if (keys(%serverhomes) > 1) {
 8269:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 8270:             my ($curroffloadnow,$curroffloadoth);
 8271:             if (ref($settings) eq 'HASH') {
 8272:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 8273:                     $curroffloadnow = $settings->{'offloadnow'};
 8274:                 }
 8275:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 8276:                     $curroffloadoth = $settings->{'offloadoth'};
 8277:                 }
 8278:             }
 8279:             my $other_insts = scalar(keys(%by_location));
 8280:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 8281:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 8282:         } else {
 8283:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 8284:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 8285:                           '</td></tr>';
 8286:         }
 8287:     } else {
 8288:         my %titles = &usersession_titles();
 8289:         my ($prefix,@types);
 8290:         if ($position eq 'bottom') {
 8291:             $prefix = 'remote';
 8292:             @types = ('version','excludedomain','includedomain');
 8293:         } else {
 8294:             $prefix = 'hosted';
 8295:             @types = ('excludedomain','includedomain');
 8296:         }
 8297:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8298:     }
 8299:     $$rowtotal += $itemcount;
 8300:     return $datatable;
 8301: }
 8302: 
 8303: sub rules_by_location {
 8304:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 8305:     my ($datatable,$itemcount,$css_class);
 8306:     if (keys(%{$by_location}) == 0) {
 8307:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8308:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 8309:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 8310:                      '</td></tr>';
 8311:         $itemcount = 1;
 8312:     } else {
 8313:         $itemcount = 0;
 8314:         my $numinrow = 5;
 8315:         my (%current,%checkedon,%checkedoff);
 8316:         my @locations = sort(keys(%{$by_location}));
 8317:         foreach my $type (@{$types}) {
 8318:             $checkedon{$type} = '';
 8319:             $checkedoff{$type} = ' checked="checked"';
 8320:         }
 8321:         if (ref($settings) eq 'HASH') {
 8322:             if (ref($settings->{$prefix}) eq 'HASH') {
 8323:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 8324:                     $current{$key} = $settings->{$prefix}{$key};
 8325:                     if ($key eq 'version') {
 8326:                         if ($current{$key} ne '') {
 8327:                             $checkedon{$key} = ' checked="checked"';
 8328:                             $checkedoff{$key} = '';
 8329:                         }
 8330:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 8331:                         $checkedon{$key} = ' checked="checked"';
 8332:                         $checkedoff{$key} = '';
 8333:                     }
 8334:                 }
 8335:             }
 8336:         }
 8337:         foreach my $type (@{$types}) {
 8338:             next if ($type ne 'version' && !@locations);
 8339:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8340:             $datatable .= '<tr'.$css_class.'>
 8341:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 8342:                            <span class="LC_nobreak">&nbsp;
 8343:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 8344:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 8345:             if ($type eq 'version') {
 8346:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 8347:                 my $selector = '<select name="'.$prefix.'_version">';
 8348:                 foreach my $version (@lcversions) {
 8349:                     my $selected = '';
 8350:                     if ($current{'version'} eq $version) {
 8351:                         $selected = ' selected="selected"';
 8352:                     }
 8353:                     $selector .= ' <option value="'.$version.'"'.
 8354:                                  $selected.'>'.$version.'</option>';
 8355:                 }
 8356:                 $selector .= '</select> ';
 8357:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 8358:             } else {
 8359:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 8360:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 8361:                              ' />'.('&nbsp;'x2).
 8362:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 8363:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 8364:                              "\n".
 8365:                              '</div><div><table>';
 8366:                 my $rem;
 8367:                 for (my $i=0; $i<@locations; $i++) {
 8368:                     my ($showloc,$value,$checkedtype);
 8369:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 8370:                         my $ip = $by_location->{$locations[$i]}->[0];
 8371:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8372:                             $value = join(':',@{$by_ip->{$ip}});
 8373:                             $showloc = join(', ',@{$by_ip->{$ip}});
 8374:                             if (ref($current{$type}) eq 'ARRAY') {
 8375:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 8376:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 8377:                                         $checkedtype = ' checked="checked"';
 8378:                                         last;
 8379:                                     }
 8380:                                 }
 8381:                             }
 8382:                         }
 8383:                     }
 8384:                     $rem = $i%($numinrow);
 8385:                     if ($rem == 0) {
 8386:                         if ($i > 0) {
 8387:                             $datatable .= '</tr>';
 8388:                         }
 8389:                         $datatable .= '<tr>';
 8390:                     }
 8391:                     $datatable .= '<td class="LC_left_item">'.
 8392:                                   '<span class="LC_nobreak"><label>'.
 8393:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 8394:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 8395:                                   '</label></span></td>';
 8396:                 }
 8397:                 $rem = @locations%($numinrow);
 8398:                 my $colsleft = $numinrow - $rem;
 8399:                 if ($colsleft > 1 ) {
 8400:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8401:                                   '&nbsp;</td>';
 8402:                 } elsif ($colsleft == 1) {
 8403:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8404:                 }
 8405:                 $datatable .= '</tr></table>';
 8406:             }
 8407:             $datatable .= '</td></tr>';
 8408:             $itemcount ++;
 8409:         }
 8410:     }
 8411:     return ($datatable,$itemcount);
 8412: }
 8413: 
 8414: sub print_ssl {
 8415:     my ($position,$dom,$settings,$rowtotal) = @_;
 8416:     my ($css_class,$datatable);
 8417:     my $itemcount = 1;
 8418:     if ($position eq 'top') {
 8419:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8420:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8421:         my $same_institution;
 8422:         if ($intdom ne '') {
 8423:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 8424:             if (ref($internet_names) eq 'ARRAY') {
 8425:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 8426:                     $same_institution = 1;
 8427:                 }
 8428:             }
 8429:         }
 8430:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8431:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 8432:         if ($same_institution) {
 8433:             my %domservers = &Apache::lonnet::get_servers($dom);
 8434:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 8435:         } else {
 8436:             $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.");
 8437:         }
 8438:         $datatable .= '</td></tr>';
 8439:         $itemcount ++;
 8440:     } else {
 8441:         my %titles = &ssl_titles();
 8442:         my (%by_ip,%by_location,@intdoms,@instdoms);
 8443:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8444:         my @alldoms = &Apache::lonnet::all_domains();
 8445:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 8446:         my @domservers = &Apache::lonnet::get_servers($dom);
 8447:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8448:         my %altids = &id_for_thisdom(%servers);
 8449:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 8450:             my $legacy;
 8451:             unless (ref($settings) eq 'HASH') {
 8452:                 my $name;
 8453:                 if ($position eq 'connto') {
 8454:                     $name = 'loncAllowInsecure';
 8455:                 } else {
 8456:                     $name = 'londAllowInsecure';
 8457:                 }
 8458:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 8459:                 my @ids=&Apache::lonnet::current_machine_ids();
 8460:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 8461:                     my %what = (
 8462:                                    $name => 1,
 8463:                                );
 8464:                     my ($result,$returnhash) =
 8465:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 8466:                     if ($result eq 'ok') {
 8467:                         if (ref($returnhash) eq 'HASH') {
 8468:                             $legacy = $returnhash->{$name};
 8469:                         }
 8470:                     }
 8471:                 } else {
 8472:                     $legacy = $Apache::lonnet::perlvar{$name};
 8473:                 }
 8474:             }
 8475:             foreach my $type ('dom','intdom','other') {
 8476:                 my %checked;
 8477:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 8478:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 8479:                               '<td class="LC_right_item">';
 8480:                 my $skip; 
 8481:                 if ($type eq 'dom') {
 8482:                     unless (keys(%servers) > 1) {
 8483:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 8484:                         $skip = 1;
 8485:                     }
 8486:                 }
 8487:                 if ($type eq 'intdom') {
 8488:                     unless (@instdoms > 1) {
 8489:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 8490:                         $skip = 1;
 8491:                     } 
 8492:                 } elsif ($type eq 'other') {
 8493:                     if (keys(%by_location) == 0) {
 8494:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 8495:                         $skip = 1;
 8496:                     }
 8497:                 }
 8498:                 unless ($skip) {
 8499:                     $checked{'yes'} = ' checked="checked"'; 
 8500:                     if (ref($settings) eq 'HASH') {
 8501:                         if (ref($settings->{$position}) eq 'HASH') {
 8502:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 8503:                                 $checked{$1} = $checked{'yes'};
 8504:                                 delete($checked{'yes'}); 
 8505:                             }
 8506:                         }
 8507:                     } else {
 8508:                         if ($legacy == 0) {
 8509:                             $checked{'req'} = $checked{'yes'};
 8510:                             delete($checked{'yes'});    
 8511:                         }
 8512:                     }
 8513:                     foreach my $option ('no','yes','req') {
 8514:                         $datatable .= '<span class="LC_nobreak"><label>'.
 8515:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 8516:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 8517:                                       '</label></span>'.('&nbsp;'x2);
 8518:                     }
 8519:                 }
 8520:                 $datatable .= '</td></tr>';
 8521:                 $itemcount ++; 
 8522:             }
 8523:         } else {
 8524:             my $prefix = 'replication';
 8525:             my @types = ('certreq','nocertreq');
 8526:             if (keys(%by_location) == 0) {
 8527:                 $datatable .= '<tr'.$css_class.'><td>'.
 8528:                               &mt('Nothing to set here, as there are no other institutions').
 8529:                               '</td></tr>';
 8530:                 $itemcount ++;
 8531:             } else {
 8532:                 ($datatable,$itemcount) = 
 8533:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8534:             }
 8535:         }
 8536:     }
 8537:     $$rowtotal += $itemcount;
 8538:     return $datatable;
 8539: }
 8540: 
 8541: sub ssl_titles {
 8542:     return &Apache::lonlocal::texthash (
 8543:                dom           => 'LON-CAPA servers/VMs from same domain',
 8544:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 8545:                other         => 'External LON-CAPA servers/VMs',
 8546:                connto        => 'Connections to other servers',
 8547:                connfrom      => 'Connections from other servers',
 8548:                replication   => 'Replicating content to other institutions',
 8549:                certreq       => 'Client certificate required, but specific domains exempt',
 8550:                nocertreq     => 'No client certificate required, except for specific domains',
 8551:                no            => 'SSL not used',
 8552:                yes           => 'SSL Optional (used if available)',
 8553:                req           => 'SSL Required',
 8554:     );
 8555: }
 8556: 
 8557: sub print_trust {
 8558:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 8559:     my ($css_class,$datatable,%checked,%choices);
 8560:     my (%by_ip,%by_location,@intdoms,@instdoms);
 8561:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 8562:     my $itemcount = 1;
 8563:     my %titles = &trust_titles();
 8564:     my @types = ('exc','inc');
 8565:     if ($prefix eq 'top') {
 8566:         $prefix = 'content';
 8567:     } elsif ($prefix eq 'bottom') {
 8568:         $prefix = 'msg';
 8569:     }
 8570:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 8571:     $$rowtotal += $itemcount;
 8572:     return $datatable;
 8573: }
 8574: 
 8575: sub trust_titles {
 8576:     return &Apache::lonlocal::texthash(
 8577:                content  => "Access to this domain's content by others",
 8578:                shared   => "Access to other domain's content by this domain",
 8579:                enroll   => "Enrollment in this domain's courses by others", 
 8580:                othcoau  => "Co-author roles in this domain for others",
 8581:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 8582:                domroles => "Domain roles in this domain assignable to others",
 8583:                catalog  => "Course Catalog for this domain displayed elsewhere",
 8584:                reqcrs   => "Requests for creation of courses in this domain by others",
 8585:                msg      => "Users in other domains can send messages to this domain",
 8586:                exc      => "Allow all, but exclude specific domains",
 8587:                inc      => "Deny all, but include specific domains",
 8588:            );
 8589: } 
 8590: 
 8591: sub build_location_hashes {
 8592:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 8593:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 8594:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 8595:     my %iphost = &Apache::lonnet::get_iphost();
 8596:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 8597:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 8598:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 8599:         foreach my $id (@{$iphost{$primary_ip}}) {
 8600:             my $intdom = &Apache::lonnet::internet_dom($id);
 8601:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 8602:                 push(@{$intdoms},$intdom);
 8603:             }
 8604:         }
 8605:     }
 8606:     foreach my $ip (keys(%iphost)) {
 8607:         if (ref($iphost{$ip}) eq 'ARRAY') {
 8608:             foreach my $id (@{$iphost{$ip}}) {
 8609:                 my $location = &Apache::lonnet::internet_dom($id);
 8610:                 if ($location) {
 8611:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 8612:                         my $dom = &Apache::lonnet::host_domain($id);
 8613:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 8614:                             push(@{$instdoms},$dom);
 8615:                         }
 8616:                         next;
 8617:                     }
 8618:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8619:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 8620:                             push(@{$by_ip->{$ip}},$location);
 8621:                         }
 8622:                     } else {
 8623:                         $by_ip->{$ip} = [$location];
 8624:                     }
 8625:                 }
 8626:             }
 8627:         }
 8628:     }
 8629:     foreach my $ip (sort(keys(%{$by_ip}))) {
 8630:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 8631:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 8632:             my $first = $by_ip->{$ip}->[0];
 8633:             if (ref($by_location->{$first}) eq 'ARRAY') {
 8634:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 8635:                     push(@{$by_location->{$first}},$ip);
 8636:                 }
 8637:             } else {
 8638:                 $by_location->{$first} = [$ip];
 8639:             }
 8640:         }
 8641:     }
 8642:     return;
 8643: }
 8644: 
 8645: sub current_offloads_to {
 8646:     my ($dom,$settings,$servers) = @_;
 8647:     my (%spareid,%otherdomconfigs);
 8648:     if (ref($servers) eq 'HASH') {
 8649:         foreach my $lonhost (sort(keys(%{$servers}))) {
 8650:             my $gotspares;
 8651:             if (ref($settings) eq 'HASH') {
 8652:                 if (ref($settings->{'spares'}) eq 'HASH') {
 8653:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 8654:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 8655:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 8656:                         $gotspares = 1;
 8657:                     }
 8658:                 }
 8659:             }
 8660:             unless ($gotspares) {
 8661:                 my $gotspares;
 8662:                 my $serverhomeID =
 8663:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 8664:                 my $serverhomedom =
 8665:                     &Apache::lonnet::host_domain($serverhomeID);
 8666:                 if ($serverhomedom ne $dom) {
 8667:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 8668:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8669:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8670:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8671:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8672:                                 $gotspares = 1;
 8673:                             }
 8674:                         }
 8675:                     } else {
 8676:                         $otherdomconfigs{$serverhomedom} =
 8677:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 8678:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 8679:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 8680:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 8681:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 8682:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 8683:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 8684:                                         $gotspares = 1;
 8685:                                     }
 8686:                                 }
 8687:                             }
 8688:                         }
 8689:                     }
 8690:                 }
 8691:             }
 8692:             unless ($gotspares) {
 8693:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8694:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8695:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8696:                } else {
 8697:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 8698:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 8699:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 8700:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 8701:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 8702:                     } else {
 8703:                         my %what = (
 8704:                              spareid => 1,
 8705:                         );
 8706:                         my ($result,$returnhash) = 
 8707:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 8708:                         if ($result eq 'ok') { 
 8709:                             if (ref($returnhash) eq 'HASH') {
 8710:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 8711:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 8712:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 8713:                                 }
 8714:                             }
 8715:                         }
 8716:                     }
 8717:                 }
 8718:             }
 8719:         }
 8720:     }
 8721:     return %spareid;
 8722: }
 8723: 
 8724: sub spares_row {
 8725:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 8726:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 8727:     my $css_class;
 8728:     my $numinrow = 4;
 8729:     my $itemcount = 1;
 8730:     my $datatable;
 8731:     my %typetitles = &sparestype_titles();
 8732:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8733:         foreach my $server (sort(keys(%{$servers}))) {
 8734:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 8735:             my ($othercontrol,$serverdom);
 8736:             if ($serverhome ne $server) {
 8737:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 8738:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8739:             } else {
 8740:                 $serverdom = &Apache::lonnet::host_domain($server);
 8741:                 if ($serverdom ne $dom) {
 8742:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 8743:                 }
 8744:             }
 8745:             next unless (ref($spareid->{$server}) eq 'HASH');
 8746:             my ($checkednow,$checkedoth);
 8747:             if (ref($curroffloadnow) eq 'HASH') {
 8748:                 if ($curroffloadnow->{$server}) {
 8749:                     $checkednow = ' checked="checked"';
 8750:                 }
 8751:             }
 8752:             if (ref($curroffloadoth) eq 'HASH') {
 8753:                 if ($curroffloadoth->{$server}) {
 8754:                     $checkedoth = ' checked="checked"';
 8755:                 }
 8756:             }
 8757:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 8758:             $datatable .= '<tr'.$css_class.'>
 8759:                            <td rowspan="2">
 8760:                             <span class="LC_nobreak">'.
 8761:                           &mt('[_1] when busy, offloads to:'
 8762:                               ,'<b>'.$server.'</b>').'</span><br />'.
 8763:                           '<span class="LC_nobreak">'."\n".
 8764:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 8765:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 8766:                           "\n";
 8767:             if ($other_insts) {
 8768:                 $datatable .= '<br />'.
 8769:                               '<span class="LC_nobreak">'."\n".
 8770:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 8771:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 8772:                           "\n";
 8773:             }
 8774:             my (%current,%canselect);
 8775:             my @choices = 
 8776:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 8777:             foreach my $type ('primary','default') {
 8778:                 if (ref($spareid->{$server}) eq 'HASH') {
 8779:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 8780:                         my @spares = @{$spareid->{$server}{$type}};
 8781:                         if (@spares > 0) {
 8782:                             if ($othercontrol) {
 8783:                                 $current{$type} = join(', ',@spares);
 8784:                             } else {
 8785:                                 $current{$type} .= '<table>';
 8786:                                 my $numspares = scalar(@spares);
 8787:                                 for (my $i=0;  $i<@spares; $i++) {
 8788:                                     my $rem = $i%($numinrow);
 8789:                                     if ($rem == 0) {
 8790:                                         if ($i > 0) {
 8791:                                             $current{$type} .= '</tr>';
 8792:                                         }
 8793:                                         $current{$type} .= '<tr>';
 8794:                                     }
 8795:                                     $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;'.
 8796:                                                        $spareid->{$server}{$type}[$i].
 8797:                                                        '</label></td>'."\n";
 8798:                                 }
 8799:                                 my $rem = @spares%($numinrow);
 8800:                                 my $colsleft = $numinrow - $rem;
 8801:                                 if ($colsleft > 1 ) {
 8802:                                     $current{$type} .= '<td colspan="'.$colsleft.
 8803:                                                        '" class="LC_left_item">'.
 8804:                                                        '&nbsp;</td>';
 8805:                                 } elsif ($colsleft == 1) {
 8806:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 8807:                                 }
 8808:                                 $current{$type} .= '</tr></table>';
 8809:                             }
 8810:                         }
 8811:                     }
 8812:                     if ($current{$type} eq '') {
 8813:                         $current{$type} = &mt('None specified');
 8814:                     }
 8815:                     if ($othercontrol) {
 8816:                         if ($type eq 'primary') {
 8817:                             $canselect{$type} = $othercontrol;
 8818:                         }
 8819:                     } else {
 8820:                         $canselect{$type} = 
 8821:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 8822:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 8823:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 8824:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 8825:                         if (@choices > 0) {
 8826:                             foreach my $lonhost (@choices) {
 8827:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 8828:                             }
 8829:                         }
 8830:                         $canselect{$type} .= '</select>'."\n";
 8831:                     }
 8832:                 } else {
 8833:                     $current{$type} = &mt('Could not be determined');
 8834:                     if ($type eq 'primary') {
 8835:                         $canselect{$type} =  $othercontrol;
 8836:                     }
 8837:                 }
 8838:                 if ($type eq 'default') {
 8839:                     $datatable .= '<tr'.$css_class.'>';
 8840:                 }
 8841:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 8842:                               '<td>'.$current{$type}.'</td>'."\n".
 8843:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 8844:             }
 8845:             $itemcount ++;
 8846:         }
 8847:     }
 8848:     $$rowtotal += $itemcount;
 8849:     return $datatable;
 8850: }
 8851: 
 8852: sub possible_newspares {
 8853:     my ($server,$currspares,$serverhomes,$altids) = @_;
 8854:     my $serverhostname = &Apache::lonnet::hostname($server);
 8855:     my %excluded;
 8856:     if ($serverhostname ne '') {
 8857:         %excluded = (
 8858:                        $serverhostname => 1,
 8859:                     );
 8860:     }
 8861:     if (ref($currspares) eq 'HASH') {
 8862:         foreach my $type (keys(%{$currspares})) {
 8863:             if (ref($currspares->{$type}) eq 'ARRAY') {
 8864:                 if (@{$currspares->{$type}} > 0) {
 8865:                     foreach my $curr (@{$currspares->{$type}}) {
 8866:                         my $hostname = &Apache::lonnet::hostname($curr);
 8867:                         $excluded{$hostname} = 1;
 8868:                     }
 8869:                 }
 8870:             }
 8871:         }
 8872:     }
 8873:     my @choices;
 8874:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 8875:         if (keys(%{$serverhomes}) > 1) {
 8876:             foreach my $name (sort(keys(%{$serverhomes}))) {
 8877:                 unless ($excluded{$name}) {
 8878:                     if (exists($altids->{$serverhomes->{$name}})) {
 8879:                         push(@choices,$altids->{$serverhomes->{$name}});
 8880:                     } else {
 8881:                         push(@choices,$serverhomes->{$name});
 8882:                     }
 8883:                 }
 8884:             }
 8885:         }
 8886:     }
 8887:     return sort(@choices);
 8888: }
 8889: 
 8890: sub print_loadbalancing {
 8891:     my ($dom,$settings,$rowtotal) = @_;
 8892:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8893:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8894:     my $numinrow = 1;
 8895:     my $datatable;
 8896:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8897:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 8898:     if (ref($settings) eq 'HASH') {
 8899:         %existing = %{$settings};
 8900:     }
 8901:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 8902:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 8903:                                   \%currtargets,\%currrules,\%currcookies);
 8904:     } else {
 8905:         return;
 8906:     }
 8907:     my ($othertitle,$usertypes,$types) =
 8908:         &Apache::loncommon::sorted_inst_types($dom);
 8909:     my $rownum = 8;
 8910:     if (ref($types) eq 'ARRAY') {
 8911:         $rownum += scalar(@{$types});
 8912:     }
 8913:     my @css_class = ('LC_odd_row','LC_even_row');
 8914:     my $balnum = 0;
 8915:     my $islast;
 8916:     my (@toshow,$disabledtext);
 8917:     if (keys(%currbalancer) > 0) {
 8918:         @toshow = sort(keys(%currbalancer));
 8919:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 8920:             push(@toshow,'');
 8921:         }
 8922:     } else {
 8923:         @toshow = ('');
 8924:         $disabledtext = &mt('No existing load balancer');
 8925:     }
 8926:     foreach my $lonhost (@toshow) {
 8927:         if ($balnum == scalar(@toshow)-1) {
 8928:             $islast = 1;
 8929:         } else {
 8930:             $islast = 0;
 8931:         }
 8932:         my $cssidx = $balnum%2;
 8933:         my $targets_div_style = 'display: none';
 8934:         my $disabled_div_style = 'display: block';
 8935:         my $homedom_div_style = 'display: none';
 8936:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 8937:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8938:                       '<p>';
 8939:         if ($lonhost eq '') {
 8940:             $datatable .= '<span class="LC_nobreak">';
 8941:             if (keys(%currbalancer) > 0) {
 8942:                 $datatable .= &mt('Add balancer:');
 8943:             } else {
 8944:                 $datatable .= &mt('Enable balancer:');
 8945:             }
 8946:             $datatable .= '&nbsp;'.
 8947:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 8948:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 8949:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 8950:                           '<option value="" selected="selected">'.&mt('None').
 8951:                           '</option>'."\n";
 8952:             foreach my $server (sort(keys(%servers))) {
 8953:                 next if ($currbalancer{$server});
 8954:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 8955:             }
 8956:             $datatable .=
 8957:                 '</select>'."\n".
 8958:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 8959:         } else {
 8960:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 8961:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 8962:                            &mt('Stop balancing').'</label>'.
 8963:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 8964:             $targets_div_style = 'display: block';
 8965:             $disabled_div_style = 'display: none';
 8966:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 8967:                 $homedom_div_style = 'display: block';
 8968:             }
 8969:         }
 8970:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 8971:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 8972:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 8973:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 8974:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 8975:         my @sparestypes = ('primary','default');
 8976:         my %typetitles = &sparestype_titles();
 8977:         my %hostherechecked = (
 8978:                                   no => ' checked="checked"',
 8979:                               );
 8980:         my %balcookiechecked = (
 8981:                                   no => ' checked="checked"', 
 8982:                                );
 8983:         foreach my $sparetype (@sparestypes) {
 8984:             my $targettable;
 8985:             for (my $i=0; $i<$numspares; $i++) {
 8986:                 my $checked;
 8987:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 8988:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 8989:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 8990:                             $checked = ' checked="checked"';
 8991:                         }
 8992:                     }
 8993:                 }
 8994:                 my ($chkboxval,$disabled);
 8995:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 8996:                     $chkboxval = $spares[$i];
 8997:                 }
 8998:                 if (exists($currbalancer{$spares[$i]})) {
 8999:                     $disabled = ' disabled="disabled"';
 9000:                 }
 9001:                 $targettable .=
 9002:                     '<td><span class="LC_nobreak"><label>'.
 9003:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 9004:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 9005:                     '</span></label></span></td>';
 9006:                 my $rem = $i%($numinrow);
 9007:                 if ($rem == 0) {
 9008:                     if (($i > 0) && ($i < $numspares-1)) {
 9009:                         $targettable .= '</tr>';
 9010:                     }
 9011:                     if ($i < $numspares-1) {
 9012:                         $targettable .= '<tr>';
 9013:                     }
 9014:                 }
 9015:             }
 9016:             if ($targettable ne '') {
 9017:                 my $rem = $numspares%($numinrow);
 9018:                 my $colsleft = $numinrow - $rem;
 9019:                 if ($colsleft > 1 ) {
 9020:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9021:                                     '&nbsp;</td>';
 9022:                 } elsif ($colsleft == 1) {
 9023:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 9024:                 }
 9025:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 9026:                                '<table><tr>'.$targettable.'</tr></table><br />';
 9027:             }
 9028:             $hostherechecked{$sparetype} = '';
 9029:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 9030:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 9031:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 9032:                         $hostherechecked{$sparetype} = ' checked="checked"';
 9033:                         $hostherechecked{'no'} = '';
 9034:                     }
 9035:                 }
 9036:             }
 9037:         }
 9038:         if ($currcookies{$lonhost}) {
 9039:             %balcookiechecked = (
 9040:                                     yes => ' checked="checked"',
 9041:                                 );
 9042:         }
 9043:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 9044:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 9045:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 9046:         foreach my $sparetype (@sparestypes) {
 9047:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 9048:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 9049:                           '</i></label><br />';
 9050:         }
 9051:         $datatable .= &mt('Use balancer cookie').'<br />'.
 9052:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 9053:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 9054:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 9055:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 9056:                       '</div></td></tr>'.
 9057:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 9058:                                            $othertitle,$usertypes,$types,\%servers,
 9059:                                            \%currbalancer,$lonhost,
 9060:                                            $targets_div_style,$homedom_div_style,
 9061:                                            $css_class[$cssidx],$balnum,$islast);
 9062:         $$rowtotal += $rownum;
 9063:         $balnum ++;
 9064:     }
 9065:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 9066:     return $datatable;
 9067: }
 9068: 
 9069: sub get_loadbalancers_config {
 9070:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 9071:     return unless ((ref($servers) eq 'HASH') &&
 9072:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 9073:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 9074:                    (ref($currcookies) eq 'HASH'));
 9075:     if (keys(%{$existing}) > 0) {
 9076:         my $oldlonhost;
 9077:         foreach my $key (sort(keys(%{$existing}))) {
 9078:             if ($key eq 'lonhost') {
 9079:                 $oldlonhost = $existing->{'lonhost'};
 9080:                 $currbalancer->{$oldlonhost} = 1;
 9081:             } elsif ($key eq 'targets') {
 9082:                 if ($oldlonhost) {
 9083:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 9084:                 }
 9085:             } elsif ($key eq 'rules') {
 9086:                 if ($oldlonhost) {
 9087:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 9088:                 }
 9089:             } elsif (ref($existing->{$key}) eq 'HASH') {
 9090:                 $currbalancer->{$key} = 1;
 9091:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 9092:                 $currrules->{$key} = $existing->{$key}{'rules'};
 9093:                 if ($existing->{$key}{'cookie'}) {
 9094:                     $currcookies->{$key} = 1;
 9095:                 }
 9096:             }
 9097:         }
 9098:     } else {
 9099:         my ($balancerref,$targetsref) =
 9100:                 &Apache::lonnet::get_lonbalancer_config($servers);
 9101:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 9102:             foreach my $server (sort(keys(%{$balancerref}))) {
 9103:                 $currbalancer->{$server} = 1;
 9104:                 $currtargets->{$server} = $targetsref->{$server};
 9105:             }
 9106:         }
 9107:     }
 9108:     return;
 9109: }
 9110: 
 9111: sub loadbalancing_rules {
 9112:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 9113:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 9114:         $css_class,$balnum,$islast) = @_;
 9115:     my $output;
 9116:     my $num = 0;
 9117:     my ($alltypes,$othertypes,$titles) =
 9118:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 9119:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 9120:         foreach my $type (@{$alltypes}) {
 9121:             $num ++;
 9122:             my $current;
 9123:             if (ref($currrules) eq 'HASH') {
 9124:                 $current = $currrules->{$type};
 9125:             }
 9126:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9127:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 9128:                     $current = '';
 9129:                 }
 9130:             }
 9131:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 9132:                                              $servers,$currbalancer,$lonhost,$dom,
 9133:                                              $targets_div_style,$homedom_div_style,
 9134:                                              $css_class,$balnum,$num,$islast);
 9135:         }
 9136:     }
 9137:     return $output;
 9138: }
 9139: 
 9140: sub loadbalancing_titles {
 9141:     my ($dom,$intdom,$usertypes,$types) = @_;
 9142:     my %othertypes = (
 9143:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 9144:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 9145:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 9146:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 9147:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 9148:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 9149:                      );
 9150:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 9151:     my @available;
 9152:     if (ref($types) eq 'ARRAY') {
 9153:         @available = @{$types};
 9154:     }
 9155:     unless (grep(/^default$/,@available)) {
 9156:         push(@available,'default');
 9157:     }
 9158:     unshift(@alltypes,@available);
 9159:     my %titles;
 9160:     foreach my $type (@alltypes) {
 9161:         if ($type =~ /^_LC_/) {
 9162:             $titles{$type} = $othertypes{$type};
 9163:         } elsif ($type eq 'default') {
 9164:             $titles{$type} = &mt('All users from [_1]',$dom);
 9165:             if (ref($types) eq 'ARRAY') {
 9166:                 if (@{$types} > 0) {
 9167:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 9168:                 }
 9169:             }
 9170:         } elsif (ref($usertypes) eq 'HASH') {
 9171:             $titles{$type} = $usertypes->{$type};
 9172:         }
 9173:     }
 9174:     return (\@alltypes,\%othertypes,\%titles);
 9175: }
 9176: 
 9177: sub loadbalance_rule_row {
 9178:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 9179:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 9180:     my @rulenames;
 9181:     my %ruletitles = &offloadtype_text();
 9182:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 9183:         @rulenames = ('balancer','offloadedto','specific');
 9184:     } else {
 9185:         @rulenames = ('default','homeserver');
 9186:         if ($type eq '_LC_external') {
 9187:             push(@rulenames,'externalbalancer');
 9188:         } else {
 9189:             push(@rulenames,'specific');
 9190:         }
 9191:         push(@rulenames,'none');
 9192:     }
 9193:     my $style = $targets_div_style;
 9194:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 9195:         $style = $homedom_div_style;
 9196:     }
 9197:     my $space;
 9198:     if ($islast && $num == 1) {
 9199:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 9200:     }
 9201:     my $output =
 9202:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 9203:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 9204:         '<td valaign="top">'.$space.
 9205:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 9206:     for (my $i=0; $i<@rulenames; $i++) {
 9207:         my $rule = $rulenames[$i];
 9208:         my ($checked,$extra);
 9209:         if ($rulenames[$i] eq 'default') {
 9210:             $rule = '';
 9211:         }
 9212:         if ($rulenames[$i] eq 'specific') {
 9213:             if (ref($servers) eq 'HASH') {
 9214:                 my $default;
 9215:                 if (($current ne '') && (exists($servers->{$current}))) {
 9216:                     $checked = ' checked="checked"';
 9217:                 }
 9218:                 unless ($checked) {
 9219:                     $default = ' selected="selected"';
 9220:                 }
 9221:                 $extra =
 9222:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9223:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 9224:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 9225:                     '<option value=""'.$default.'></option>'."\n";
 9226:                 foreach my $server (sort(keys(%{$servers}))) {
 9227:                     if (ref($currbalancer) eq 'HASH') {
 9228:                         next if (exists($currbalancer->{$server}));
 9229:                     }
 9230:                     my $selected;
 9231:                     if ($server eq $current) {
 9232:                         $selected = ' selected="selected"';
 9233:                     }
 9234:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 9235:                 }
 9236:                 $extra .= '</select>';
 9237:             }
 9238:         } elsif ($rule eq $current) {
 9239:             $checked = ' checked="checked"';
 9240:         }
 9241:         $output .= '<span class="LC_nobreak"><label>'.
 9242:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 9243:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 9244:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 9245:                    ')"'.$checked.' />&nbsp;';
 9246:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 9247:             $output .= $ruletitles{'particular'};
 9248:         } else {
 9249:             $output .= $ruletitles{$rulenames[$i]};
 9250:         }
 9251:         $output .= '</label>'.$extra.'</span><br />'."\n";
 9252:     }
 9253:     $output .= '</div></td></tr>'."\n";
 9254:     return $output;
 9255: }
 9256: 
 9257: sub offloadtype_text {
 9258:     my %ruletitles = &Apache::lonlocal::texthash (
 9259:            'default'          => 'Offloads to default destinations',
 9260:            'homeserver'       => "Offloads to user's home server",
 9261:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 9262:            'specific'         => 'Offloads to specific server',
 9263:            'none'             => 'No offload',
 9264:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 9265:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 9266:            'particular'       => 'Session hosted (after re-auth) on server:',
 9267:     );
 9268:     return %ruletitles;
 9269: }
 9270: 
 9271: sub sparestype_titles {
 9272:     my %typestitles = &Apache::lonlocal::texthash (
 9273:                           'primary' => 'primary',
 9274:                           'default' => 'default',
 9275:                       );
 9276:     return %typestitles;
 9277: }
 9278: 
 9279: sub contact_titles {
 9280:     my %titles = &Apache::lonlocal::texthash (
 9281:                    'supportemail'    => 'Support E-mail address',
 9282:                    'adminemail'      => 'Default Server Admin E-mail address',
 9283:                    'errormail'       => 'Error reports to be e-mailed to',
 9284:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 9285:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 9286:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 9287:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 9288:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 9289:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 9290:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 9291:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 9292:                    'errorthreshold'  => 'Error count threshold for status e-mail to admin(s)',
 9293:                    'errorsysmail'    => 'Error count threshold for e-mail to developer group',
 9294:                    'errorweights'    => 'Weights used to compute error count',
 9295:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 9296:                  );
 9297:     my %short_titles = &Apache::lonlocal::texthash (
 9298:                            adminemail   => 'Admin E-mail address',
 9299:                            supportemail => 'Support E-mail',
 9300:                        );   
 9301:     return (\%titles,\%short_titles);
 9302: }
 9303: 
 9304: sub helpform_fields {
 9305:     my %titles =  &Apache::lonlocal::texthash (
 9306:                        'username'   => 'Name',
 9307:                        'user'       => 'Username/domain',
 9308:                        'phone'      => 'Phone',
 9309:                        'cc'         => 'Cc e-mail',
 9310:                        'course'     => 'Course Details',
 9311:                        'section'    => 'Sections',
 9312:                        'screenshot' => 'File upload',
 9313:     );
 9314:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 9315:     my %possoptions = (
 9316:                         username     => ['yes','no','req'],
 9317:                         phone        => ['yes','no','req'],
 9318:                         user         => ['yes','no'],
 9319:                         cc           => ['yes','no'],
 9320:                         course       => ['yes','no'],
 9321:                         section      => ['yes','no'],
 9322:                         screenshot   => ['yes','no'],
 9323:                       );
 9324:     my %fieldoptions = &Apache::lonlocal::texthash (
 9325:                          'yes'  => 'Optional',
 9326:                          'req'  => 'Required',
 9327:                          'no'   => "Not shown",
 9328:     );
 9329:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 9330: }
 9331: 
 9332: sub tool_titles {
 9333:     my %titles = &Apache::lonlocal::texthash (
 9334:                      aboutme    => 'Personal web page',
 9335:                      blog       => 'Blog',
 9336:                      webdav     => 'WebDAV',
 9337:                      portfolio  => 'Portfolio',
 9338:                      timezone   => 'Can set time zone',
 9339:                      official   => 'Official courses (with institutional codes)',
 9340:                      unofficial => 'Unofficial courses',
 9341:                      community  => 'Communities',
 9342:                      textbook   => 'Textbook courses',
 9343:                      placement  => 'Placement tests',
 9344:                  );
 9345:     return %titles;
 9346: }
 9347: 
 9348: sub courserequest_titles {
 9349:     my %titles = &Apache::lonlocal::texthash (
 9350:                                    official   => 'Official',
 9351:                                    unofficial => 'Unofficial',
 9352:                                    community  => 'Communities',
 9353:                                    textbook   => 'Textbook',
 9354:                                    placement  => 'Placement tests',
 9355:                                    lti        => 'LTI Provider',
 9356:                                    norequest  => 'Not allowed',
 9357:                                    approval   => 'Approval by DC',
 9358:                                    validate   => 'With validation',
 9359:                                    autolimit  => 'Numerical limit',
 9360:                                    unlimited  => '(blank for unlimited)',
 9361:                  );
 9362:     return %titles;
 9363: }
 9364: 
 9365: sub authorrequest_titles {
 9366:     my %titles = &Apache::lonlocal::texthash (
 9367:                                    norequest  => 'Not allowed',
 9368:                                    approval   => 'Approval by Dom. Coord.',
 9369:                                    automatic  => 'Automatic approval',
 9370:                  );
 9371:     return %titles;
 9372: }
 9373: 
 9374: sub courserequest_conditions {
 9375:     my %conditions = &Apache::lonlocal::texthash (
 9376:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 9377:        validate   => '(Processing of request subject to institutional validation).',
 9378:                  );
 9379:     return %conditions;
 9380: }
 9381: 
 9382: 
 9383: sub print_usercreation {
 9384:     my ($position,$dom,$settings,$rowtotal) = @_;
 9385:     my $numinrow = 4;
 9386:     my $datatable;
 9387:     if ($position eq 'top') {
 9388:         $$rowtotal ++;
 9389:         my $rowcount = 0;
 9390:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 9391:         if (ref($rules) eq 'HASH') {
 9392:             if (keys(%{$rules}) > 0) {
 9393:                 $datatable .= &user_formats_row('username',$settings,$rules,
 9394:                                                 $ruleorder,$numinrow,$rowcount);
 9395:                 $$rowtotal ++;
 9396:                 $rowcount ++;
 9397:             }
 9398:         }
 9399:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 9400:         if (ref($idrules) eq 'HASH') {
 9401:             if (keys(%{$idrules}) > 0) {
 9402:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 9403:                                                 $idruleorder,$numinrow,$rowcount);
 9404:                 $$rowtotal ++;
 9405:                 $rowcount ++;
 9406:             }
 9407:         }
 9408:         if ($rowcount == 0) {
 9409:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 9410:             $$rowtotal ++;
 9411:             $rowcount ++;
 9412:         }
 9413:     } elsif ($position eq 'middle') {
 9414:         my @creators = ('author','course','requestcrs');
 9415:         my ($rules,$ruleorder) =
 9416:             &Apache::lonnet::inst_userrules($dom,'username');
 9417:         my %lt = &usercreation_types();
 9418:         my %checked;
 9419:         if (ref($settings) eq 'HASH') {
 9420:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 9421:                 foreach my $item (@creators) {
 9422:                     $checked{$item} = $settings->{'cancreate'}{$item};
 9423:                 }
 9424:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 9425:                 foreach my $item (@creators) {
 9426:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 9427:                         $checked{$item} = 'none';
 9428:                     }
 9429:                 }
 9430:             }
 9431:         }
 9432:         my $rownum = 0;
 9433:         foreach my $item (@creators) {
 9434:             $rownum ++;
 9435:             if ($checked{$item} eq '') {
 9436:                 $checked{$item} = 'any';
 9437:             }
 9438:             my $css_class;
 9439:             if ($rownum%2) {
 9440:                 $css_class = '';
 9441:             } else {
 9442:                 $css_class = ' class="LC_odd_row" ';
 9443:             }
 9444:             $datatable .= '<tr'.$css_class.'>'.
 9445:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 9446:                          '</span></td><td style="text-align: right">';
 9447:             my @options = ('any');
 9448:             if (ref($rules) eq 'HASH') {
 9449:                 if (keys(%{$rules}) > 0) {
 9450:                     push(@options,('official','unofficial'));
 9451:                 }
 9452:             }
 9453:             push(@options,'none');
 9454:             foreach my $option (@options) {
 9455:                 my $type = 'radio';
 9456:                 my $check = ' ';
 9457:                 if ($checked{$item} eq $option) {
 9458:                     $check = ' checked="checked" ';
 9459:                 } 
 9460:                 $datatable .= '<span class="LC_nobreak"><label>'.
 9461:                               '<input type="'.$type.'" name="can_createuser_'.
 9462:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 9463:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 9464:             }
 9465:             $datatable .= '</td></tr>';
 9466:         }
 9467:     } else {
 9468:         my @contexts = ('author','course','domain');
 9469:         my @authtypes = ('int','krb4','krb5','loc','lti');
 9470:         my %checked;
 9471:         if (ref($settings) eq 'HASH') {
 9472:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 9473:                 foreach my $item (@contexts) {
 9474:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 9475:                         foreach my $auth (@authtypes) {
 9476:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 9477:                                 $checked{$item}{$auth} = ' checked="checked" ';
 9478:                             }
 9479:                         }
 9480:                     }
 9481:                 }
 9482:             }
 9483:         } else {
 9484:             foreach my $item (@contexts) {
 9485:                 foreach my $auth (@authtypes) {
 9486:                     $checked{$item}{$auth} = ' checked="checked" ';
 9487:                 }
 9488:             }
 9489:         }
 9490:         my %title = &context_names();
 9491:         my %authname = &authtype_names();
 9492:         my $rownum = 0;
 9493:         my $css_class; 
 9494:         foreach my $item (@contexts) {
 9495:             if ($rownum%2) {
 9496:                 $css_class = '';
 9497:             } else {
 9498:                 $css_class = ' class="LC_odd_row" ';
 9499:             }
 9500:             $datatable .=   '<tr'.$css_class.'>'.
 9501:                             '<td>'.$title{$item}.
 9502:                             '</td><td class="LC_left_item">'.
 9503:                             '<span class="LC_nobreak">';
 9504:             foreach my $auth (@authtypes) {
 9505:                 $datatable .= '<label>'. 
 9506:                               '<input type="checkbox" name="'.$item.'_auth" '.
 9507:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 9508:                               $authname{$auth}.'</label>&nbsp;';
 9509:             }
 9510:             $datatable .= '</span></td></tr>';
 9511:             $rownum ++;
 9512:         }
 9513:         $$rowtotal += $rownum;
 9514:     }
 9515:     return $datatable;
 9516: }
 9517: 
 9518: sub print_selfcreation {
 9519:     my ($position,$dom,$settings,$rowtotal) = @_;
 9520:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 9521:         $emaildomain,$datatable);
 9522:     if (ref($settings) eq 'HASH') {
 9523:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 9524:             $createsettings = $settings->{'cancreate'};
 9525:             if (ref($createsettings) eq 'HASH') {
 9526:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 9527:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 9528:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 9529:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 9530:                         @selfcreate = ('email','login','sso');
 9531:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 9532:                         @selfcreate = ($createsettings->{'selfcreate'});
 9533:                     }
 9534:                 }
 9535:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 9536:                     $processing = $createsettings->{'selfcreateprocessing'};
 9537:                 }
 9538:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 9539:                     $emailoptions = $createsettings->{'emailoptions'};
 9540:                 }
 9541:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 9542:                     $emailverified = $createsettings->{'emailverified'};
 9543:                 }
 9544:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 9545:                     $emaildomain = $createsettings->{'emaildomain'};
 9546:                 }
 9547:             }
 9548:         }
 9549:     }
 9550:     my %radiohash;
 9551:     my $numinrow = 4;
 9552:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 9553:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9554:     if ($position eq 'top') {
 9555:         my %choices = &Apache::lonlocal::texthash (
 9556:                                                       cancreate_login      => 'Institutional Login',
 9557:                                                       cancreate_sso        => 'Institutional Single Sign On',
 9558:                                                   );
 9559:         my @toggles = sort(keys(%choices));
 9560:         my %defaultchecked = (
 9561:                                'cancreate_login' => 'off',
 9562:                                'cancreate_sso'   => 'off',
 9563:                              );
 9564:         my ($onclick,$itemcount);
 9565:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9566:                                                      \%choices,$itemcount,$onclick);
 9567:         $$rowtotal += $itemcount;
 9568:         
 9569:         if (ref($usertypes) eq 'HASH') {
 9570:             if (keys(%{$usertypes}) > 0) {
 9571:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 9572:                                              $dom,$numinrow,$othertitle,
 9573:                                              'statustocreate',$rowtotal);
 9574:                 $$rowtotal ++;
 9575:             }
 9576:         }
 9577:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 9578:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 9579:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 9580:         my $rem;
 9581:         my $numperrow = 2;
 9582:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 9583:         $datatable .= '<tr'.$css_class.'>'.
 9584:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 9585:                      '<td class="LC_left_item">'."\n".
 9586:                      '<table>'."\n";
 9587:         for (my $i=0; $i<@fields; $i++) {
 9588:             $rem = $i%($numperrow);
 9589:             if ($rem == 0) {
 9590:                 if ($i > 0) {
 9591:                     $datatable .= '</tr>';
 9592:                 }
 9593:                 $datatable .= '<tr>';
 9594:             }
 9595:             my $currval;
 9596:             if (ref($createsettings) eq 'HASH') {
 9597:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 9598:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 9599:                 }
 9600:             }
 9601:             $datatable .= '<td class="LC_left_item">'.
 9602:                           '<span class="LC_nobreak">'.
 9603:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 9604:                           'value="'.$currval.'" size="10" />&nbsp;'.
 9605:                           $fieldtitles{$fields[$i]}.'</span></td>';
 9606:         }
 9607:         my $colsleft = $numperrow - $rem;
 9608:         if ($colsleft > 1 ) {
 9609:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9610:                          '&nbsp;</td>';
 9611:         } elsif ($colsleft == 1) {
 9612:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 9613:         }
 9614:         $datatable .= '</tr></table></td></tr>';
 9615:         $$rowtotal ++;
 9616:     } elsif ($position eq 'middle') {
 9617:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 9618:         my @posstypes;
 9619:         if (ref($types) eq 'ARRAY') {
 9620:             @posstypes = @{$types};
 9621:         }
 9622:         unless (grep(/^default$/,@posstypes)) {
 9623:             push(@posstypes,'default');
 9624:         }
 9625:         my %usertypeshash;
 9626:         if (ref($usertypes) eq 'HASH') {
 9627:             %usertypeshash = %{$usertypes};
 9628:         }
 9629:         $usertypeshash{'default'} = $othertitle;
 9630:         foreach my $status (@posstypes) {
 9631:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 9632:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 9633:             $$rowtotal ++;
 9634:         }
 9635:     } else {
 9636:         my %choices = &Apache::lonlocal::texthash (
 9637:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 9638:                                                   );
 9639:         my @toggles = sort(keys(%choices));
 9640:         my %defaultchecked = (
 9641:                                'cancreate_email' => 'off',
 9642:                              );
 9643:         my $customclass = 'LC_selfcreate_email';
 9644:         my $classprefix = 'LC_canmodify_emailusername_';
 9645:         my $optionsprefix = 'LC_options_emailusername_';
 9646:         my $display = 'none';
 9647:         my $rowstyle = 'display:none';
 9648:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 9649:             $display = 'block';
 9650:             $rowstyle = 'display:table-row';
 9651:         }
 9652:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 9653:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 9654:                                                      \%choices,$$rowtotal,$onclick);
 9655:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 9656:                                          $rowstyle);
 9657:         $$rowtotal ++;
 9658:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 9659:                                       $rowstyle);
 9660:         $$rowtotal ++;
 9661:         my (@ordered,@posstypes,%usertypeshash);
 9662:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 9663:         my ($emailrules,$emailruleorder) =
 9664:             &Apache::lonnet::inst_userrules($dom,'email');
 9665:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 9666:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 9667:         if (ref($types) eq 'ARRAY') {
 9668:             @posstypes = @{$types};
 9669:         }
 9670:         if (@posstypes) {
 9671:             unless (grep(/^default$/,@posstypes)) {
 9672:                 push(@posstypes,'default');
 9673:             }
 9674:             if (ref($usertypes) eq 'HASH') {
 9675:                 %usertypeshash = %{$usertypes};
 9676:             }
 9677:             my $currassign;
 9678:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 9679:                 $currassign = {
 9680:                                   selfassign => $domdefaults{'inststatusguest'},
 9681:                               };
 9682:                 @ordered = @{$domdefaults{'inststatusguest'}};
 9683:             } else {
 9684:                 $currassign = { selfassign => [] };
 9685:             }
 9686:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 9687:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 9688:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 9689:                                          $numinrow,$othertitle,'selfassign',
 9690:                                          $rowtotal,$onclicktypes,$customclass,
 9691:                                          $rowstyle);
 9692:             $$rowtotal ++;
 9693:             $usertypeshash{'default'} = $othertitle;
 9694:             foreach my $status (@posstypes) {
 9695:                 my $css_class;
 9696:                 if ($$rowtotal%2) {
 9697:                     $css_class = 'LC_odd_row ';
 9698:                 }
 9699:                 $css_class .= $customclass;
 9700:                 my $rowid = $optionsprefix.$status;
 9701:                 my $hidden = 1;
 9702:                 my $currstyle = 'display:none';
 9703:                 if (grep(/^\Q$status\E$/,@ordered)) {
 9704:                     $currstyle = $rowstyle;
 9705:                     $hidden = 0; 
 9706:                 }
 9707:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9708:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 9709:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 9710:                 unless ($hidden) {
 9711:                     $$rowtotal ++;
 9712:                 }
 9713:             }
 9714:         } else {
 9715:             my $css_class;
 9716:             if ($$rowtotal%2) {
 9717:                 $css_class = 'LC_odd_row ';
 9718:             }
 9719:             $css_class .= $customclass;
 9720:             $usertypeshash{'default'} = $othertitle;
 9721:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 9722:                                          $emailrules,$emailruleorder,$settings,'default','',
 9723:                                          $othertitle,$css_class,$rowstyle,$intdom);
 9724:             $$rowtotal ++;
 9725:         }
 9726:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 9727:         $numinrow = 1;
 9728:         if (@posstypes) {
 9729:             foreach my $status (@posstypes) {
 9730:                 my $rowid = $classprefix.$status;
 9731:                 my $datarowstyle = 'display:none';
 9732:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 9733:                     $datarowstyle = $rowstyle; 
 9734:                 }
 9735:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 9736:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9737:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 9738:                 unless ($datarowstyle eq 'display:none') {
 9739:                     $$rowtotal ++;
 9740:                 }
 9741:             }
 9742:         } else {
 9743:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 9744:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 9745:                                                    $infotitles,'',$customclass,$rowstyle);
 9746:         }
 9747:     }
 9748:     return $datatable;
 9749: }
 9750: 
 9751: sub selfcreate_javascript {
 9752:     return <<"ENDSCRIPT";
 9753: 
 9754: <script type="text/javascript">
 9755: // <![CDATA[
 9756: 
 9757: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 9758:     var x = document.getElementsByClassName(target);
 9759:     var insttypes = 0;
 9760:     var insttypeRegExp = new RegExp(prefix);
 9761:     if ((x.length != undefined) && (x.length > 0)) {
 9762:         if (form.elements[radio].length != undefined) {
 9763:             for (var i=0; i<form.elements[radio].length; i++) {
 9764:                 if (form.elements[radio][i].checked) {
 9765:                     if (form.elements[radio][i].value == 1) {
 9766:                         for (var j=0; j<x.length; j++) {
 9767:                             if (x[j].id == 'undefined') {
 9768:                                 x[j].style.display = 'table-row';
 9769:                             } else if (insttypeRegExp.test(x[j].id)) {
 9770:                                 insttypes ++;
 9771:                             } else {
 9772:                                 x[j].style.display = 'table-row';
 9773:                             }
 9774:                         }
 9775:                     } else {
 9776:                         for (var j=0; j<x.length; j++) {
 9777:                             x[j].style.display = 'none';
 9778:                         }
 9779:                     }
 9780:                     break;
 9781:                 }
 9782:             }
 9783:             if (insttypes > 0) {
 9784:                 toggleDataRow(form,checkbox,target,altprefix);
 9785:                 toggleDataRow(form,checkbox,target,prefix,1);
 9786:             }
 9787:         }
 9788:     }
 9789:     return;
 9790: }
 9791: 
 9792: function toggleDataRow(form,checkbox,target,prefix,docount) {
 9793:     if (form.elements[checkbox].length != undefined) {
 9794:         var count = 0;
 9795:         if (docount) {
 9796:             for (var i=0; i<form.elements[checkbox].length; i++) {
 9797:                 if (form.elements[checkbox][i].checked) {
 9798:                     count ++;
 9799:                 }
 9800:             }
 9801:         }
 9802:         for (var i=0; i<form.elements[checkbox].length; i++) {
 9803:             var type = form.elements[checkbox][i].value;
 9804:             if (document.getElementById(prefix+type)) {
 9805:                 if (form.elements[checkbox][i].checked) {
 9806:                     document.getElementById(prefix+type).style.display = 'table-row';
 9807:                     if (count % 2 == 1) {
 9808:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 9809:                     } else {
 9810:                         document.getElementById(prefix+type).className = target;
 9811:                     }
 9812:                     count ++;
 9813:                 } else {
 9814:                     document.getElementById(prefix+type).style.display = 'none';
 9815:                 }
 9816:             }
 9817:         }
 9818:     }
 9819:     return;
 9820: }
 9821: 
 9822: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 9823:     var caller = radio+'_'+status;
 9824:     if (form.elements[caller].length != undefined) {
 9825:         for (var i=0; i<form.elements[caller].length; i++) {
 9826:             if (form.elements[caller][i].checked) {
 9827:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 9828:                     var curr = form.elements[caller][i].value;
 9829:                     if (prefix) {
 9830:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 9831:                     }
 9832:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 9833:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 9834:                     if (curr == 'custom') {
 9835:                         if (prefix) { 
 9836:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 9837:                         }
 9838:                     } else if (curr == 'inst') {
 9839:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 9840:                     } else if (curr == 'noninst') {
 9841:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 9842:                     }
 9843:                     break;
 9844:                 }
 9845:             }
 9846:         }
 9847:     }
 9848: }
 9849: 
 9850: // ]]>
 9851: </script>
 9852: 
 9853: ENDSCRIPT
 9854: }
 9855: 
 9856: sub noninst_users {
 9857:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 9858:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 9859:     my $class = 'LC_left_item';
 9860:     if ($css_class) {
 9861:         $css_class = ' class="'.$css_class.'"'; 
 9862:     }
 9863:     if ($rowid) {
 9864:         $rowid = ' id="'.$rowid.'"';
 9865:     }
 9866:     if ($rowstyle) {
 9867:         $rowstyle = ' style="'.$rowstyle.'"';
 9868:     }
 9869:     my ($output,$description);
 9870:     if ($type eq 'default') {
 9871:         $description = &mt('Requests for: [_1]',$typetitle);
 9872:     } else {
 9873:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 9874:     }
 9875:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 9876:               "<td>$description</td>\n".           
 9877:               '<td class="'.$class.'" colspan="2">'.
 9878:               '<table><tr>';
 9879:     my %headers = &Apache::lonlocal::texthash( 
 9880:               approve  => 'Processing',
 9881:               email    => 'E-mail',
 9882:               username => 'Username',
 9883:     );
 9884:     foreach my $item ('approve','email','username') {
 9885:         $output .= '<th>'.$headers{$item}.'</th>';
 9886:     }
 9887:     $output .= '</tr><tr>';
 9888:     foreach my $item ('approve','email','username') {
 9889:         $output .= '<td style="vertical-align: top">';
 9890:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 9891:         if ($item eq 'approve') {
 9892:             %choices = &Apache::lonlocal::texthash (
 9893:                                                      automatic => 'Automatically approved',
 9894:                                                      approval  => 'Queued for approval',
 9895:                                                    );
 9896:             @options = ('automatic','approval');
 9897:             $hashref = $processing;
 9898:             $defoption = 'automatic';
 9899:             $name = 'cancreate_emailprocess_'.$type;
 9900:         } elsif ($item eq 'email') {
 9901:             %choices = &Apache::lonlocal::texthash (
 9902:                                                      any     => 'Any e-mail',
 9903:                                                      inst    => 'Institutional only',
 9904:                                                      noninst => 'Non-institutional only',
 9905:                                                      custom  => 'Custom restrictions',
 9906:                                                    );
 9907:             @options = ('any','inst','noninst');
 9908:             my $showcustom;
 9909:             if (ref($emailrules) eq 'HASH') {
 9910:                 if (keys(%{$emailrules}) > 0) {
 9911:                     push(@options,'custom');
 9912:                     $showcustom = 'cancreate_emailrule';
 9913:                     if (ref($settings) eq 'HASH') {
 9914:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 9915:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 9916:                                 if (exists($emailrules->{$rule})) {
 9917:                                     $hascustom ++;
 9918:                                 }
 9919:                             }
 9920:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 9921:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 9922:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 9923:                                     if (exists($emailrules->{$rule})) {
 9924:                                         $hascustom ++;
 9925:                                     }
 9926:                                 }
 9927:                             }
 9928:                         }
 9929:                     }
 9930:                 }
 9931:             }
 9932:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 9933:                                                      "'cancreate_emaildomain','$type'".');"';
 9934:             $hashref = $emailoptions;
 9935:             $defoption = 'any';
 9936:             $name = 'cancreate_emailoptions_'.$type;
 9937:         } elsif ($item eq 'username') {
 9938:             %choices = &Apache::lonlocal::texthash (
 9939:                                                      all    => 'Same as e-mail',
 9940:                                                      first  => 'Omit @domain',
 9941:                                                      free   => 'Free to choose',
 9942:                                                    );
 9943:             @options = ('all','first','free');
 9944:             $hashref = $emailverified;
 9945:             $defoption = 'all';
 9946:             $name = 'cancreate_usernameoptions_'.$type;
 9947:         }
 9948:         foreach my $option (@options) {
 9949:             my $checked;
 9950:             if (ref($hashref) eq 'HASH') {
 9951:                 if ($type eq '') {
 9952:                     if (!exists($hashref->{'default'})) {
 9953:                         if ($option eq $defoption) {
 9954:                             $checked = ' checked="checked"';
 9955:                         }
 9956:                     } else {
 9957:                         if ($hashref->{'default'} eq $option) {
 9958:                             $checked = ' checked="checked"';
 9959:                         }
 9960:                     }
 9961:                 } else {
 9962:                     if (!exists($hashref->{$type})) {
 9963:                         if ($option eq $defoption) {
 9964:                             $checked = ' checked="checked"';
 9965:                         }
 9966:                     } else {
 9967:                         if ($hashref->{$type} eq $option) {
 9968:                             $checked = ' checked="checked"';
 9969:                         }
 9970:                     }
 9971:                 }
 9972:             } elsif (($item eq 'email') && ($hascustom)) {
 9973:                 if ($option eq 'custom') {
 9974:                     $checked = ' checked="checked"';
 9975:                 }
 9976:             } elsif ($option eq $defoption) {
 9977:                 $checked = ' checked="checked"';
 9978:             }
 9979:             $output .= '<span class="LC_nobreak"><label>'.
 9980:                        '<input type="radio" name="'.$name.'"'.
 9981:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 9982:                        $choices{$option}.'</label></span><br />';
 9983:             if ($item eq 'email') {
 9984:                 if ($option eq 'custom') {
 9985:                     my $id = 'cancreate_emailrule_'.$type;
 9986:                     my $display = 'none';
 9987:                     if ($checked) {
 9988:                         $display = 'inline';
 9989:                     }
 9990:                     my $numinrow = 2;
 9991:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 9992:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 9993:                                &user_formats_row('email',$settings,$emailrules,
 9994:                                                  $emailruleorder,$numinrow,'',$type);
 9995:                               '</table></fieldset>';
 9996:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 9997:                     my %text = &Apache::lonlocal::texthash (
 9998:                                                              inst    => 'must end:',
 9999:                                                              noninst => 'cannot end:',
10000:                                                            );
10001:                     my $value;
10002:                     if (ref($emaildomain) eq 'HASH') {
10003:                         if (ref($emaildomain->{$type}) eq 'HASH') {
10004:                             $value = $emaildomain->{$type}->{$option}; 
10005:                         }
10006:                     }
10007:                     if ($value eq '') {
10008:                         $value = '@'.$intdom;
10009:                     }
10010:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
10011:                     my $display = 'none';
10012:                     if ($checked) {
10013:                         $display = 'inline';
10014:                     }
10015:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
10016:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
10017:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
10018:                                '</div>';
10019:                 }
10020:             }
10021:         }
10022:         $output .= '</td>'."\n";
10023:     }
10024:     $output .= "</tr></table></td></tr>\n";
10025:     return $output;
10026: }
10027: 
10028: sub captcha_choice {
10029:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
10030:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
10031:         $vertext,$currver);
10032:     my %lt = &captcha_phrases();
10033:     $keyentry = 'hidden';
10034:     my $colspan=2;
10035:     if ($context eq 'cancreate') {
10036:         $rowname = &mt('CAPTCHA validation');
10037:     } elsif ($context eq 'login') {
10038:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
10039:     } elsif ($context eq 'passwords') {
10040:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
10041:         $colspan=1;
10042:     }
10043:     if (ref($settings) eq 'HASH') {
10044:         if ($settings->{'captcha'}) {
10045:             $checked{$settings->{'captcha'}} = ' checked="checked"';
10046:         } else {
10047:             $checked{'original'} = ' checked="checked"';
10048:         }
10049:         if ($settings->{'captcha'} eq 'recaptcha') {
10050:             $pubtext = $lt{'pub'};
10051:             $privtext = $lt{'priv'};
10052:             $keyentry = 'text';
10053:             $vertext = $lt{'ver'};
10054:             $currver = $settings->{'recaptchaversion'};
10055:             if ($currver ne '2') {
10056:                 $currver = 1;
10057:             }
10058:         }
10059:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
10060:             $currpub = $settings->{'recaptchakeys'}{'public'};
10061:             $currpriv = $settings->{'recaptchakeys'}{'private'};
10062:         }
10063:     } else {
10064:         $checked{'original'} = ' checked="checked"';
10065:     }
10066:     my $css_class;
10067:     if ($itemcount%2) {
10068:         $css_class = 'LC_odd_row';
10069:     }
10070:     if ($customcss) {
10071:         $css_class .= " $customcss";
10072:     }
10073:     $css_class =~ s/^\s+//;
10074:     if ($css_class) {
10075:         $css_class = ' class="'.$css_class.'"';
10076:     }
10077:     if ($rowstyle) {
10078:         $css_class .= ' style="'.$rowstyle.'"';
10079:     }
10080:     my $output = '<tr'.$css_class.'>'.
10081:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
10082:                  '<table><tr><td>'."\n";
10083:     foreach my $option ('original','recaptcha','notused') {
10084:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
10085:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
10086:                    $lt{$option}.'</label></span>';
10087:         unless ($option eq 'notused') {
10088:             $output .= ('&nbsp;'x2)."\n";
10089:         }
10090:     }
10091: #
10092: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
10093: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
10094: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
10095: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
10096: #
10097:     $output .= '</td></tr>'."\n".
10098:                '<tr><td class="LC_zero_height">'."\n".
10099:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
10100:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
10101:                $currpub.'" size="40" /></span><br />'."\n".
10102:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
10103:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
10104:                $currpriv.'" size="40" /></span><br />'.
10105:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
10106:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
10107:                $currver.'" size="3" /></span><br />'.
10108:                '</td></tr></table>'."\n".
10109:                '</td></tr>';
10110:     return $output;
10111: }
10112: 
10113: sub user_formats_row {
10114:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
10115:     my $output;
10116:     my %text = (
10117:                    'username' => 'new usernames',
10118:                    'id'       => 'IDs',
10119:                );
10120:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10121:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
10122:         $output = '<tr '.$css_class.'>'.
10123:                   '<td><span class="LC_nobreak">'.
10124:                   &mt("Format rules to check for $text{$type}: ").
10125:                   '</td><td class="LC_left_item" colspan="2"><table>';
10126:     }
10127:     my $rem;
10128:     if (ref($ruleorder) eq 'ARRAY') {
10129:         for (my $i=0; $i<@{$ruleorder}; $i++) {
10130:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
10131:                 my $rem = $i%($numinrow);
10132:                 if ($rem == 0) {
10133:                     if ($i > 0) {
10134:                         $output .= '</tr>';
10135:                     }
10136:                     $output .= '<tr>';
10137:                 }
10138:                 my $check = ' ';
10139:                 if (ref($settings) eq 'HASH') {
10140:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
10141:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
10142:                             $check = ' checked="checked" ';
10143:                         }
10144:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
10145:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
10146:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
10147:                                 $check = ' checked="checked" ';
10148:                             }
10149:                         }
10150:                     }
10151:                 }
10152:                 my $name = $type.'_rule';
10153:                 if ($type eq 'email') {
10154:                     $name .= '_'.$status;
10155:                 }
10156:                 $output .= '<td class="LC_left_item">'.
10157:                            '<span class="LC_nobreak"><label>'.
10158:                            '<input type="checkbox" name="'.$name.'" '.
10159:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
10160:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
10161:             }
10162:         }
10163:         $rem = @{$ruleorder}%($numinrow);
10164:     }
10165:     my $colsleft;
10166:     if ($rem) {
10167:         $colsleft = $numinrow - $rem;
10168:     }
10169:     if ($colsleft > 1 ) {
10170:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10171:                    '&nbsp;</td>';
10172:     } elsif ($colsleft == 1) {
10173:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10174:     }
10175:     $output .= '</tr>';
10176:     unless (($type eq 'email') || ($type eq 'unamemap')) {
10177:         $output .= '</table></td></tr>';
10178:     }
10179:     return $output;
10180: }
10181: 
10182: sub usercreation_types {
10183:     my %lt = &Apache::lonlocal::texthash (
10184:                     author     => 'When adding a co-author',
10185:                     course     => 'When adding a user to a course',
10186:                     requestcrs => 'When requesting a course',
10187:                     any        => 'Any',
10188:                     official   => 'Institutional only ',
10189:                     unofficial => 'Non-institutional only',
10190:                     none       => 'None',
10191:     );
10192:     return %lt;
10193: }
10194: 
10195: sub selfcreation_types {
10196:     my %lt = &Apache::lonlocal::texthash (
10197:                     selfcreate => 'User creates own account',
10198:                     any        => 'Any',
10199:                     official   => 'Institutional only ',
10200:                     unofficial => 'Non-institutional only',
10201:                     email      => 'E-mail address',
10202:                     login      => 'Institutional Login',
10203:                     sso        => 'SSO',
10204:              );
10205: }
10206: 
10207: sub authtype_names {
10208:     my %lt = &Apache::lonlocal::texthash(
10209:                       int    => 'Internal',
10210:                       krb4   => 'Kerberos 4',
10211:                       krb5   => 'Kerberos 5',
10212:                       loc    => 'Local',
10213:                       lti    => 'LTI',
10214:                   );
10215:     return %lt;
10216: }
10217: 
10218: sub context_names {
10219:     my %context_title = &Apache::lonlocal::texthash(
10220:        author => 'Creating users when an Author',
10221:        course => 'Creating users when in a course',
10222:        domain => 'Creating users when a Domain Coordinator',
10223:     );
10224:     return %context_title;
10225: }
10226: 
10227: sub print_usermodification {
10228:     my ($position,$dom,$settings,$rowtotal) = @_;
10229:     my $numinrow = 4;
10230:     my ($context,$datatable,$rowcount);
10231:     if ($position eq 'top') {
10232:         $rowcount = 0;
10233:         $context = 'author'; 
10234:         foreach my $role ('ca','aa') {
10235:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10236:                                                    $numinrow,$rowcount);
10237:             $$rowtotal ++;
10238:             $rowcount ++;
10239:         }
10240:     } elsif ($position eq 'bottom') {
10241:         $context = 'course';
10242:         $rowcount = 0;
10243:         foreach my $role ('st','ep','ta','in','cr') {
10244:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
10245:                                                    $numinrow,$rowcount);
10246:             $$rowtotal ++;
10247:             $rowcount ++;
10248:         }
10249:     }
10250:     return $datatable;
10251: }
10252: 
10253: sub print_defaults {
10254:     my ($position,$dom,$settings,$rowtotal) = @_;
10255:     my $rownum = 0;
10256:     my ($datatable,$css_class,$titles);
10257:     unless ($position eq 'bottom') {
10258:         $titles = &defaults_titles($dom);
10259:     }
10260:     if ($position eq 'top') {
10261:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
10262:                      'datelocale_def','portal_def');
10263:         my %defaults;
10264:         if (ref($settings) eq 'HASH') {
10265:             %defaults = %{$settings};
10266:         } else {
10267:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10268:             foreach my $item (@items) {
10269:                 $defaults{$item} = $domdefaults{$item};
10270:             }
10271:         }
10272:         foreach my $item (@items) {
10273:             if ($rownum%2) {
10274:                 $css_class = '';
10275:             } else {
10276:                 $css_class = ' class="LC_odd_row" ';
10277:             }
10278:             $datatable .= '<tr'.$css_class.'>'.
10279:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
10280:                           '</span></td><td class="LC_right_item" colspan="3">';
10281:             if ($item eq 'auth_def') {
10282:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
10283:                 my %shortauth = (
10284:                                  internal => 'int',
10285:                                  krb4 => 'krb4',
10286:                                  krb5 => 'krb5',
10287:                                  localauth  => 'loc',
10288:                                  lti => 'lti',
10289:                                 );
10290:                 my %authnames = &authtype_names();
10291:                 foreach my $auth (@authtypes) {
10292:                     my $checked = ' ';
10293:                     if ($defaults{$item} eq $auth) {
10294:                         $checked = ' checked="checked" ';
10295:                     }
10296:                     $datatable .= '<label><input type="radio" name="'.$item.
10297:                                   '" value="'.$auth.'"'.$checked.'/>'.
10298:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
10299:                 }
10300:             } elsif ($item eq 'timezone_def') {
10301:                 my $includeempty = 1;
10302:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
10303:             } elsif ($item eq 'datelocale_def') {
10304:                 my $includeempty = 1;
10305:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
10306:             } elsif ($item eq 'lang_def') {
10307:                 my $includeempty = 1;
10308:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
10309:             } elsif ($item eq 'portal_def') {
10310:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
10311:                               $defaults{$item}.'" size="25" onkeyup="portalExtras(this);" />';
10312:                 my $portalsty = 'none';
10313:                 if ($defaults{$item}) {
10314:                     $portalsty = 'block';
10315:                 }
10316:                 foreach my $field ('email','web') {
10317:                     my $checkedoff = ' checked="checked"';
10318:                     my $checkedon;
10319:                     if ($defaults{$item.'_'.$field}) {
10320:                         $checkedon = $checkedoff;
10321:                         $checkedoff = '';
10322:                     } 
10323:                     $datatable .= '<div id="'.$item.'_'.$field.'_div" style="display:'.$portalsty.'">'.
10324:                               '<span class="LC_nobreak">'.$titles->{$field}.'&nbsp;'.
10325:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="1"'.$checkedon.'/>'.&mt('Yes').'</label>'.
10326:                               ('&nbsp;'x2).
10327:                               '<label><input type="radio" name="'.$item.'_'.$field.'" value="0"'.$checkedoff.'/>'.&mt('No').'</label>'.
10328:                               '</div>';
10329:                 }
10330:             } else {
10331:                 $datatable .= '<input type="text" name="'.$item.'" value="'.$defaults{$item}.'" />';
10332:             }
10333:             $datatable .= '</td></tr>';
10334:             $rownum ++;
10335:         }
10336:     } elsif ($position eq 'middle') {
10337:         my %defaults;
10338:         if (ref($settings) eq 'HASH') {
10339:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10340:                 my $maxnum = @{$settings->{'inststatusorder'}};
10341:                 for (my $i=0; $i<$maxnum; $i++) {
10342:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
10343:                     my $item = $settings->{'inststatusorder'}->[$i];
10344:                     my $title = $settings->{'inststatustypes'}->{$item};
10345:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
10346:                     $datatable .= '<tr'.$css_class.'>'.
10347:                                   '<td><span class="LC_nobreak">'.
10348:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
10349:                     for (my $k=0; $k<=$maxnum; $k++) {
10350:                         my $vpos = $k+1;
10351:                         my $selstr;
10352:                         if ($k == $i) {
10353:                             $selstr = ' selected="selected" ';
10354:                         }
10355:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10356:                     }
10357:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
10358:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
10359:                                   &mt('delete').'</span></td>'.
10360:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed').':'.
10361:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
10362:                                   '</span></td></tr>';
10363:                 }
10364:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
10365:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
10366:                 $datatable .= '<tr '.$css_class.'>'.
10367:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
10368:                 for (my $k=0; $k<=$maxnum; $k++) {
10369:                     my $vpos = $k+1;
10370:                     my $selstr;
10371:                     if ($k == $maxnum) {
10372:                         $selstr = ' selected="selected" ';
10373:                     }
10374:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10375:                 }
10376:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
10377:                               '<input type="text" size="10" name="addinststatus" value="" />'.
10378:                               '&nbsp;'.&mt('(new)').
10379:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
10380:                               &mt('Name displayed').':'.
10381:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
10382:                               '</tr>'."\n";
10383:                 $rownum ++;
10384:             }
10385:         }
10386:     } else {
10387:         my ($unamemaprules,$ruleorder) =
10388:             &Apache::lonnet::inst_userrules($dom,'unamemap');
10389:         $css_class = $rownum%2?' class="LC_odd_row"':'';
10390:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
10391:             my $numinrow = 2;
10392:             $datatable .= '<tr'.$css_class.'><td>'.&mt('Available conversions').'</td><td><table>'.
10393:                           &user_formats_row('unamemap',$settings,$unamemaprules,
10394:                                             $ruleorder,$numinrow).
10395:                           '</table></td></tr>';
10396:         }
10397:         if ($datatable eq '') {
10398:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
10399:                           &mt('No rules set for domain in customized localenroll.pm').
10400:                           '</td></tr>';
10401:         }
10402:     }
10403:     $$rowtotal += $rownum;
10404:     return $datatable;
10405: }
10406: 
10407: sub get_languages_hash {
10408:     my %langchoices;
10409:     foreach my $id (&Apache::loncommon::languageids()) {
10410:         my $code = &Apache::loncommon::supportedlanguagecode($id);
10411:         if ($code ne '') {
10412:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
10413:         }
10414:     }
10415:     return %langchoices;
10416: }
10417: 
10418: sub defaults_titles {
10419:     my ($dom) = @_;
10420:     my %titles = &Apache::lonlocal::texthash (
10421:                    'auth_def'      => 'Default authentication type',
10422:                    'auth_arg_def'  => 'Default authentication argument',
10423:                    'lang_def'      => 'Default language',
10424:                    'timezone_def'  => 'Default timezone',
10425:                    'datelocale_def' => 'Default locale for dates',
10426:                    'portal_def'     => 'Portal/Default URL',
10427:                    'email'          => 'Email links use portal URL',
10428:                    'web'            => 'Public web links use portal URL',
10429:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
10430:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
10431:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
10432:                  );
10433:     if ($dom) {
10434:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
10435:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
10436:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
10437:         $protocol = 'http' if ($protocol ne 'https');
10438:         if ($uint_dom) {
10439:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
10440:                                          $uint_dom);
10441:         }
10442:     }
10443:     return (\%titles);
10444: }
10445: 
10446: sub print_scantron {
10447:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
10448:     if ($position eq 'top') {
10449:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
10450:     } else {
10451:         return &print_scantronconfig($dom,$settings,\$rowtotal);
10452:     }
10453: }
10454: 
10455: sub scantron_javascript {
10456:     return <<"ENDSCRIPT";
10457: 
10458: <script type="text/javascript">
10459: // <![CDATA[
10460: 
10461: function toggleScantron(form) {
10462:     var csvfieldset = new Array();
10463:     if (document.getElementById('scantroncsv_cols')) {
10464:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
10465:     }
10466:     if (document.getElementById('scantroncsv_options')) {
10467:         csvfieldset.push(document.getElementById('scantroncsv_options'));
10468:     }
10469:     if (csvfieldset.length) {
10470:         if (document.getElementById('scantronconfcsv')) {
10471:             var scantroncsv = document.getElementById('scantronconfcsv');
10472:             if (scantroncsv.checked) {
10473:                 for (var i=0; i<csvfieldset.length; i++) {
10474:                     csvfieldset[i].style.display = 'block';
10475:                 }
10476:             } else {
10477:                 for (var i=0; i<csvfieldset.length; i++) {
10478:                     csvfieldset[i].style.display = 'none';
10479:                 }
10480:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
10481:                 if (csvselects.length) {
10482:                     for (var j=0; j<csvselects.length; j++) {
10483:                         csvselects[j].selectedIndex = 0;
10484:                     }
10485:                 }
10486:             }
10487:         }
10488:     }
10489:     return;
10490: }
10491: // ]]>
10492: </script>
10493: 
10494: ENDSCRIPT
10495: 
10496: }
10497: 
10498: sub print_scantronformat {
10499:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
10500:     my $itemcount = 1;
10501:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
10502:         %confhash);
10503:     my $switchserver = &check_switchserver($dom,$confname);
10504:     my %lt = &Apache::lonlocal::texthash (
10505:                 default => 'Default bubblesheet format file error',
10506:                 custom  => 'Custom bubblesheet format file error',
10507:              );
10508:     my %scantronfiles = (
10509:         default => 'default.tab',
10510:         custom => 'custom.tab',
10511:     );
10512:     foreach my $key (keys(%scantronfiles)) {
10513:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
10514:                               .$scantronfiles{$key};
10515:     }
10516:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
10517:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
10518:         if (!$switchserver) {
10519:             my $servadm = $r->dir_config('lonAdmEMail');
10520:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
10521:             if ($configuserok eq 'ok') {
10522:                 if ($author_ok eq 'ok') {
10523:                     my %legacyfile = (
10524:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
10525:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
10526:                     );
10527:                     my %md5chk;
10528:                     foreach my $type (keys(%legacyfile)) {
10529:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
10530:                         chomp($md5chk{$type});
10531:                     }
10532:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
10533:                         foreach my $type (keys(%legacyfile)) {
10534:                             ($scantronurls{$type},my $error) =
10535:                                 &legacy_scantronformat($r,$dom,$confname,
10536:                                                  $type,$legacyfile{$type},
10537:                                                  $scantronurls{$type},
10538:                                                  $scantronfiles{$type});
10539:                             if ($error ne '') {
10540:                                 $error{$type} = $error;
10541:                             }
10542:                         }
10543:                         if (keys(%error) == 0) {
10544:                             $is_custom = 1;
10545:                             $confhash{'scantron'}{'scantronformat'} =
10546:                                 $scantronurls{'custom'};
10547:                             my $putresult =
10548:                                 &Apache::lonnet::put_dom('configuration',
10549:                                                          \%confhash,$dom);
10550:                             if ($putresult ne 'ok') {
10551:                                 $error{'custom'} =
10552:                                     '<span class="LC_error">'.
10553:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10554:                             }
10555:                         }
10556:                     } else {
10557:                         ($scantronurls{'default'},my $error) =
10558:                             &legacy_scantronformat($r,$dom,$confname,
10559:                                           'default',$legacyfile{'default'},
10560:                                           $scantronurls{'default'},
10561:                                           $scantronfiles{'default'});
10562:                         if ($error eq '') {
10563:                             $confhash{'scantron'}{'scantronformat'} = ''; 
10564:                             my $putresult =
10565:                                 &Apache::lonnet::put_dom('configuration',
10566:                                                          \%confhash,$dom);
10567:                             if ($putresult ne 'ok') {
10568:                                 $error{'default'} =
10569:                                     '<span class="LC_error">'.
10570:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
10571:                             }
10572:                         } else {
10573:                             $error{'default'} = $error;
10574:                         }
10575:                     }
10576:                 }
10577:             }
10578:         } else {
10579:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
10580:         }
10581:     }
10582:     if (ref($settings) eq 'HASH') {
10583:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
10584:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
10585:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
10586:                 $scantronurl = '';
10587:             } else {
10588:                 $scantronurl = $settings->{'scantronformat'};
10589:             }
10590:             $is_custom = 1;
10591:         } else {
10592:             $scantronurl = $scantronurls{'default'};
10593:         }
10594:     } else {
10595:         if ($is_custom) {
10596:             $scantronurl = $scantronurls{'custom'};
10597:         } else {
10598:             $scantronurl = $scantronurls{'default'};
10599:         }
10600:     }
10601:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10602:     $datatable .= '<tr'.$css_class.'>';
10603:     if (!$is_custom) {
10604:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
10605:                       '<span class="LC_nobreak">';
10606:         if ($scantronurl) {
10607:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
10608:                                                          undef,undef,undef,undef,'background-color:#ffffff');
10609:         } else {
10610:             $datatable = &mt('File unavailable for display');
10611:         }
10612:         $datatable .= '</span></td>';
10613:         if (keys(%error) == 0) { 
10614:             $datatable .= '<td style="vertical-align: bottom">';
10615:             if (!$switchserver) {
10616:                 $datatable .= &mt('Upload:').'<br />';
10617:             }
10618:         } else {
10619:             my $errorstr;
10620:             foreach my $key (sort(keys(%error))) {
10621:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10622:             }
10623:             $datatable .= '<td>'.$errorstr;
10624:         }
10625:     } else {
10626:         if (keys(%error) > 0) {
10627:             my $errorstr;
10628:             foreach my $key (sort(keys(%error))) {
10629:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
10630:             } 
10631:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
10632:         } elsif ($scantronurl) {
10633:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
10634:                                                        undef,undef,undef,undef,'background-color:#ffffff');
10635:             $datatable .= '<td><span class="LC_nobreak">'.
10636:                           $link.
10637:                           '<label><input type="checkbox" name="scantronformat_del"'.
10638:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
10639:                           '<td><span class="LC_nobreak">&nbsp;'.
10640:                           &mt('Replace:').'</span><br />';
10641:         }
10642:     }
10643:     if (keys(%error) == 0) {
10644:         if ($switchserver) {
10645:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
10646:         } else {
10647:             $datatable .='<span class="LC_nobreak">&nbsp;'.
10648:                          '<input type="file" name="scantronformat" /></span>';
10649:         }
10650:     }
10651:     $datatable .= '</td></tr>';
10652:     $$rowtotal ++;
10653:     return $datatable;
10654: }
10655: 
10656: sub legacy_scantronformat {
10657:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
10658:     my ($url,$error);
10659:     my @statinfo = &Apache::lonnet::stat_file($newurl);
10660:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
10661:         my $modified = [];
10662:         (my $result,$url) =
10663:             &Apache::lonconfigsettings::publishlogo($r,'copy',$legacyfile,$dom,$confname,
10664:                                                     'scantron','','',$newfile,$modified);
10665:         if ($result eq 'ok') {
10666:             &update_modify_urls($r,$modified);
10667:         } else {
10668:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
10669:         }
10670:     }
10671:     return ($url,$error);
10672: }
10673: 
10674: sub print_scantronconfig {
10675:     my ($dom,$settings,$rowtotal) = @_;
10676:     my $itemcount = 2;
10677:     my $is_checked = ' checked="checked"';
10678:     my %optionson = (
10679:                      hdr => ' checked="checked"',
10680:                      pad => ' checked="checked"',
10681:                      rem => ' checked="checked"',
10682:                     );
10683:     my %optionsoff = (
10684:                       hdr => '',
10685:                       pad => '',
10686:                       rem => '',
10687:                      );
10688:     my $currcsvsty = 'none';
10689:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
10690:     my @fields = &scantroncsv_fields();
10691:     my %titles = &scantronconfig_titles();
10692:     if (ref($settings) eq 'HASH') {
10693:         if (ref($settings->{config}) eq 'HASH') {
10694:             if ($settings->{config}->{dat}) {
10695:                 $checked{'dat'} = $is_checked;
10696:             }
10697:             if (ref($settings->{config}->{csv}) eq 'HASH') {
10698:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
10699:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
10700:                     if (keys(%csvfields) > 0) {
10701:                         $checked{'csv'} = $is_checked;
10702:                         $currcsvsty = 'block';
10703:                     }
10704:                 }
10705:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
10706:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
10707:                     foreach my $option (keys(%optionson)) {
10708:                         unless ($csvoptions{$option}) {
10709:                             $optionsoff{$option} = $optionson{$option};
10710:                             $optionson{$option} = '';
10711:                         }
10712:                     }
10713:                 }
10714:             }
10715:         } else {
10716:             $checked{'dat'} = $is_checked;
10717:         }
10718:     } else {
10719:         $checked{'dat'} = $is_checked;
10720:     }
10721:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
10722:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10723:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
10724:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
10725:     foreach my $item ('dat','csv') {
10726:         my $id;
10727:         if ($item eq 'csv') {
10728:             $id = 'id="scantronconfcsv" ';
10729:         }
10730:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
10731:                       $titles{$item}.'</label>'.('&nbsp;'x3);
10732:         if ($item eq 'csv') {
10733:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
10734:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
10735:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
10736:             foreach my $col (@fields) {
10737:                 my $selnone;
10738:                 if ($csvfields{$col} eq '') {
10739:                     $selnone = ' selected="selected"';
10740:                 }
10741:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
10742:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
10743:                               '<option value=""'.$selnone.'></option>';
10744:                 for (my $i=0; $i<20; $i++) {
10745:                     my $shown = $i+1;
10746:                     my $sel;
10747:                     unless ($selnone) {
10748:                         if (exists($csvfields{$col})) {
10749:                             if ($csvfields{$col} == $i) {
10750:                                 $sel = ' selected="selected"';
10751:                             }
10752:                         }
10753:                     }
10754:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
10755:                 }
10756:                 $datatable .= '</select></td></tr>';
10757:            }
10758:            $datatable .= '</table></fieldset>'.
10759:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
10760:                          '<legend>'.&mt('CSV Options').'</legend>';
10761:            foreach my $option ('hdr','pad','rem') {
10762:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
10763:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
10764:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
10765:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
10766:            }
10767:            $datatable .= '</fieldset>';
10768:            $itemcount ++;
10769:         }
10770:     }
10771:     $datatable .= '</td></tr>';
10772:     $$rowtotal ++;
10773:     return $datatable;
10774: }
10775: 
10776: sub scantronconfig_titles {
10777:     return &Apache::lonlocal::texthash(
10778:                                           dat => 'Standard format (.dat)',
10779:                                           csv => 'Comma separated values (.csv)',
10780:                                           hdr => 'Remove first line in file (contains column titles)',
10781:                                           pad => 'Prepend 0s to PaperID',
10782:                                           rem => 'Remove leading spaces (except Question Response columns)',
10783:                                           CODE => 'CODE',
10784:                                           ID   => 'Student ID',
10785:                                           PaperID => 'Paper ID',
10786:                                           FirstName => 'First Name',
10787:                                           LastName => 'Last Name',
10788:                                           FirstQuestion => 'First Question Response',
10789:                                           Section => 'Section',
10790:     );
10791: }
10792: 
10793: sub scantroncsv_fields {
10794:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
10795: }
10796: 
10797: sub print_coursecategories {
10798:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
10799:     my $datatable;
10800:     if ($position eq 'top') {
10801:         my (%checked);
10802:         my @catitems = ('unauth','auth');
10803:         my @cattypes = ('std','domonly','codesrch','none');
10804:         $checked{'unauth'} = 'std';
10805:         $checked{'auth'} = 'std';
10806:         if (ref($settings) eq 'HASH') {
10807:             foreach my $type (@cattypes) {
10808:                 if ($type eq $settings->{'unauth'}) {
10809:                     $checked{'unauth'} = $type;
10810:                 }
10811:                 if ($type eq $settings->{'auth'}) {
10812:                     $checked{'auth'} = $type;
10813:                 }
10814:             }
10815:         }
10816:         my %lt = &Apache::lonlocal::texthash (
10817:                                                unauth   => 'Catalog type for unauthenticated users',
10818:                                                auth     => 'Catalog type for authenticated users',
10819:                                                none     => 'No catalog',
10820:                                                std      => 'Standard catalog',
10821:                                                domonly  => 'Domain-only catalog',
10822:                                                codesrch => "Code search form",
10823:                                              );
10824:        my $itemcount = 0;
10825:        foreach my $item (@catitems) {
10826:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
10827:            $datatable .= '<tr '.$css_class.'>'.
10828:                          '<td>'.$lt{$item}.'</td>'.
10829:                          '<td class="LC_right_item"><span class="LC_nobreak">';
10830:            foreach my $type (@cattypes) {
10831:                my $ischecked;
10832:                if ($checked{$item} eq $type) {
10833:                    $ischecked=' checked="checked"';
10834:                }
10835:                $datatable .= '<label>'.
10836:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
10837:                              ' />'.$lt{$type}.'</label>&nbsp;';
10838:            }
10839:            $datatable .= '</span></td></tr>';
10840:            $itemcount ++;
10841:         }
10842:         $$rowtotal += $itemcount;
10843:     } elsif ($position eq 'middle') {
10844:         my $toggle_cats_crs = ' ';
10845:         my $toggle_cats_dom = ' checked="checked" ';
10846:         my $can_cat_crs = ' ';
10847:         my $can_cat_dom = ' checked="checked" ';
10848:         my $toggle_catscomm_comm = ' ';
10849:         my $toggle_catscomm_dom = ' checked="checked" ';
10850:         my $can_catcomm_comm = ' ';
10851:         my $can_catcomm_dom = ' checked="checked" ';
10852:         my $toggle_catsplace_place = ' ';
10853:         my $toggle_catsplace_dom = ' checked="checked" ';
10854:         my $can_catplace_place = ' ';
10855:         my $can_catplace_dom = ' checked="checked" ';
10856: 
10857:         if (ref($settings) eq 'HASH') {
10858:             if ($settings->{'togglecats'} eq 'crs') {
10859:                 $toggle_cats_crs = $toggle_cats_dom;
10860:                 $toggle_cats_dom = ' ';
10861:             }
10862:             if ($settings->{'categorize'} eq 'crs') {
10863:                 $can_cat_crs = $can_cat_dom;
10864:                 $can_cat_dom = ' ';
10865:             }
10866:             if ($settings->{'togglecatscomm'} eq 'comm') {
10867:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
10868:                 $toggle_catscomm_dom = ' ';
10869:             }
10870:             if ($settings->{'categorizecomm'} eq 'comm') {
10871:                 $can_catcomm_comm = $can_catcomm_dom;
10872:                 $can_catcomm_dom = ' ';
10873:             }
10874:             if ($settings->{'togglecatsplace'} eq 'place') {
10875:                 $toggle_catsplace_place = $toggle_catsplace_dom;
10876:                 $toggle_catsplace_dom = ' ';
10877:             }
10878:             if ($settings->{'categorizeplace'} eq 'place') {
10879:                 $can_catplace_place = $can_catplace_dom;
10880:                 $can_catplace_dom = ' ';
10881:             }
10882:         }
10883:         my %title = &Apache::lonlocal::texthash (
10884:                      togglecats      => 'Show/Hide a course in catalog',
10885:                      togglecatscomm  => 'Show/Hide a community in catalog',
10886:                      togglecatsplace => 'Show/Hide a placement test in catalog',
10887:                      categorize      => 'Assign a category to a course',
10888:                      categorizecomm  => 'Assign a category to a community',
10889:                      categorizeplace => 'Assign a category to a placement test',
10890:                     );
10891:         my %level = &Apache::lonlocal::texthash (
10892:                      dom   => 'Set in Domain',
10893:                      crs   => 'Set in Course',
10894:                      comm  => 'Set in Community',
10895:                      place => 'Set in Placement Test',
10896:                     );
10897:         $datatable = '<tr class="LC_odd_row">'.
10898:                   '<td>'.$title{'togglecats'}.'</td>'.
10899:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10900:                   '<input type="radio" name="togglecats"'.
10901:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10902:                   '<label><input type="radio" name="togglecats"'.
10903:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
10904:                   '</tr><tr>'.
10905:                   '<td>'.$title{'categorize'}.'</td>'.
10906:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10907:                   '<label><input type="radio" name="categorize"'.
10908:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10909:                   '<label><input type="radio" name="categorize"'.
10910:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
10911:                   '</tr><tr class="LC_odd_row">'.
10912:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
10913:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10914:                   '<input type="radio" name="togglecatscomm"'.
10915:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10916:                   '<label><input type="radio" name="togglecatscomm"'.
10917:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
10918:                   '</tr><tr>'.
10919:                   '<td>'.$title{'categorizecomm'}.'</td>'.
10920:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10921:                   '<label><input type="radio" name="categorizecomm"'.
10922:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10923:                   '<label><input type="radio" name="categorizecomm"'.
10924:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
10925:                   '</tr><tr class="LC_odd_row">'.
10926:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
10927:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
10928:                   '<input type="radio" name="togglecatsplace"'.
10929:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10930:                   '<label><input type="radio" name="togglecatscomm"'.
10931:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
10932:                   '</tr><tr>'.
10933:                   '<td>'.$title{'categorizeplace'}.'</td>'.
10934:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
10935:                   '<label><input type="radio" name="categorizeplace"'.
10936:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
10937:                   '<label><input type="radio" name="categorizeplace"'.
10938:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
10939:                   '</tr>';
10940:         $$rowtotal += 6;
10941:     } else {
10942:         my $css_class;
10943:         my $itemcount = 1;
10944:         my $cathash; 
10945:         if (ref($settings) eq 'HASH') {
10946:             $cathash = $settings->{'cats'};
10947:         }
10948:         if (ref($cathash) eq 'HASH') {
10949:             my (@cats,@trails,%allitems,%idx,@jsarray);
10950:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
10951:                                                    \%allitems,\%idx,\@jsarray);
10952:             my $maxdepth = scalar(@cats);
10953:             my $colattrib = '';
10954:             if ($maxdepth > 2) {
10955:                 $colattrib = ' colspan="2" ';
10956:             }
10957:             my @path;
10958:             if (@cats > 0) {
10959:                 if (ref($cats[0]) eq 'ARRAY') {
10960:                     my $numtop = @{$cats[0]};
10961:                     my $maxnum = $numtop;
10962:                     my %default_names = (
10963:                           instcode    => &mt('Official courses'),
10964:                           communities => &mt('Communities'),
10965:                           placement   => &mt('Placement Tests'),
10966:                     );
10967: 
10968:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
10969:                         ($cathash->{'instcode::0'} eq '') ||
10970:                         (!grep(/^communities$/,@{$cats[0]})) || 
10971:                         ($cathash->{'communities::0'} eq '') ||
10972:                         (!grep(/^placement$/,@{$cats[0]})) ||
10973:                         ($cathash->{'placement::0'} eq '')) {
10974:                         $maxnum ++;
10975:                     }
10976:                     my $lastidx;
10977:                     for (my $i=0; $i<$numtop; $i++) {
10978:                         my $parent = $cats[0][$i];
10979:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10980:                         my $item = &escape($parent).'::0';
10981:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
10982:                         $lastidx = $idx{$item};
10983:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10984:                                       .'<select name="'.$item.'"'.$chgstr.'>';
10985:                         for (my $k=0; $k<=$maxnum; $k++) {
10986:                             my $vpos = $k+1;
10987:                             my $selstr;
10988:                             if ($k == $i) {
10989:                                 $selstr = ' selected="selected" ';
10990:                             }
10991:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
10992:                         }
10993:                         $datatable .= '</select></span></td><td>';
10994:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
10995:                             $datatable .=  '<span class="LC_nobreak">'
10996:                                            .$default_names{$parent}.'</span>';
10997:                             if ($parent eq 'instcode') {
10998:                                 $datatable .= '<br /><span class="LC_nobreak">('
10999:                                               .&mt('with institutional codes')
11000:                                               .')</span></td><td'.$colattrib.'>';
11001:                             } else {
11002:                                 $datatable .= '<table><tr><td>';
11003:                             }
11004:                             $datatable .= '<span class="LC_nobreak">'
11005:                                           .'<label><input type="radio" name="'
11006:                                           .$parent.'" value="1" checked="checked" />'
11007:                                           .&mt('Display').'</label>';
11008:                             if ($parent eq 'instcode') {
11009:                                 $datatable .= '&nbsp;';
11010:                             } else {
11011:                                 $datatable .= '</span></td></tr><tr><td>'
11012:                                               .'<span class="LC_nobreak">';
11013:                             }
11014:                             $datatable .= '<label><input type="radio" name="'
11015:                                           .$parent.'" value="0" />'
11016:                                           .&mt('Do not display').'</label></span>';
11017:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
11018:                                 $datatable .= '</td></tr></table>';
11019:                             }
11020:                             $datatable .= '</td>';
11021:                         } else {
11022:                             $datatable .= $parent
11023:                                           .'&nbsp;<span class="LC_nobreak"><label>'
11024:                                           .'<input type="checkbox" name="deletecategory" '
11025:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
11026:                         }
11027:                         my $depth = 1;
11028:                         push(@path,$parent);
11029:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
11030:                         pop(@path);
11031:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
11032:                         $itemcount ++;
11033:                     }
11034:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11035:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
11036:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
11037:                     for (my $k=0; $k<=$maxnum; $k++) {
11038:                         my $vpos = $k+1;
11039:                         my $selstr;
11040:                         if ($k == $numtop) {
11041:                             $selstr = ' selected="selected" ';
11042:                         }
11043:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11044:                     }
11045:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
11046:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
11047:                                   .'</tr>'."\n";
11048:                     $itemcount ++;
11049:                     foreach my $default ('instcode','communities','placement') {
11050:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
11051:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
11052:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
11053:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
11054:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
11055:                             for (my $k=0; $k<=$maxnum; $k++) {
11056:                                 my $vpos = $k+1;
11057:                                 my $selstr;
11058:                                 if ($k == $maxnum) {
11059:                                     $selstr = ' selected="selected" ';
11060:                                 }
11061:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
11062:                             }
11063:                             $datatable .= '</select></span></td>'.
11064:                                           '<td><span class="LC_nobreak">'.
11065:                                           $default_names{$default}.'</span>';
11066:                             if ($default eq 'instcode') {
11067:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
11068:                                               .&mt('with institutional codes').')</span>';
11069:                             }
11070:                             $datatable .= '</td>'
11071:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
11072:                                           .&mt('Display').'</label>&nbsp;'
11073:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
11074:                                           .&mt('Do not display').'</label></span></td></tr>';
11075:                         }
11076:                     }
11077:                 }
11078:             } else {
11079:                 $datatable .= &initialize_categories($itemcount);
11080:             }
11081:         } else {
11082:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
11083:                           .&initialize_categories($itemcount);
11084:         }
11085:         $$rowtotal += $itemcount;
11086:     }
11087:     return $datatable;
11088: }
11089: 
11090: sub print_serverstatuses {
11091:     my ($dom,$settings,$rowtotal) = @_;
11092:     my $datatable;
11093:     my @pages = &serverstatus_pages();
11094:     my (%namedaccess,%machineaccess);
11095:     foreach my $type (@pages) {
11096:         $namedaccess{$type} = '';
11097:         $machineaccess{$type}= '';
11098:     }
11099:     if (ref($settings) eq 'HASH') {
11100:         foreach my $type (@pages) {
11101:             if (exists($settings->{$type})) {
11102:                 if (ref($settings->{$type}) eq 'HASH') {
11103:                     foreach my $key (keys(%{$settings->{$type}})) {
11104:                         if ($key eq 'namedusers') {
11105:                             $namedaccess{$type} = $settings->{$type}->{$key};
11106:                         } elsif ($key eq 'machines') {
11107:                             $machineaccess{$type} = $settings->{$type}->{$key};
11108:                         }
11109:                     }
11110:                 }
11111:             }
11112:         }
11113:     }
11114:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
11115:     my $rownum = 0;
11116:     my $css_class;
11117:     foreach my $type (@pages) {
11118:         $rownum ++;
11119:         $css_class = $rownum%2?' class="LC_odd_row"':'';
11120:         $datatable .= '<tr'.$css_class.'>'.
11121:                       '<td><span class="LC_nobreak">'.
11122:                       $titles->{$type}.'</span></td>'.
11123:                       '<td class="LC_left_item">'.
11124:                       '<input type="text" name="'.$type.'_namedusers" '.
11125:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
11126:                       '<td class="LC_right_item">'.
11127:                       '<span class="LC_nobreak">'.
11128:                       '<input type="text" name="'.$type.'_machines" '.
11129:                       'value="'.$machineaccess{$type}.'" size="10" />'.
11130:                       '</span></td></tr>'."\n";
11131:     }
11132:     $$rowtotal += $rownum;
11133:     return $datatable;
11134: }
11135: 
11136: sub serverstatus_pages {
11137:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
11138:             'checksums','clusterstatus','certstatus','metadata_keywords',
11139:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
11140:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
11141: }
11142: 
11143: sub defaults_javascript {
11144:     my ($settings) = @_;
11145:     return unless (ref($settings) eq 'HASH');
11146:     my $portal_js = <<"ENDPORTAL";
11147: 
11148: function portalExtras(caller) {
11149:     var x = caller.value;
11150:     var y = new Array('email','web');
11151:     for (var i=0; i<y.length; i++) { 
11152:         if (document.getElementById('portal_def_'+y[i]+'_div')) {
11153:             var z = document.getElementById('portal_def_'+y[i]+'_div');
11154:             if (x.length > 0) {
11155:                 z.style.display = 'block';
11156:             } else {
11157:                 z.style.display = 'none';
11158:             }
11159:         }
11160:     }
11161: }
11162: ENDPORTAL
11163:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
11164:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
11165:         if ($maxnum eq '') {
11166:             $maxnum = 0;
11167:         }
11168:         $maxnum ++;
11169:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
11170:         return <<"ENDSCRIPT";
11171: <script type="text/javascript">
11172: // <![CDATA[
11173: function reorderTypes(form,caller) {
11174:     var changedVal;
11175: $jstext 
11176:     var newpos = 'addinststatus_pos';
11177:     var current = new Array;
11178:     var maxh = $maxnum;
11179:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11180:     var oldVal;
11181:     if (caller == newpos) {
11182:         changedVal = newitemVal;
11183:     } else {
11184:         var curritem = 'inststatus_pos_'+caller;
11185:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
11186:         current[newitemVal] = newpos;
11187:     }
11188:     for (var i=0; i<inststatuses.length; i++) {
11189:         if (inststatuses[i] != caller) {
11190:             var elementName = 'inststatus_pos_'+inststatuses[i];
11191:             if (form.elements[elementName]) {
11192:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11193:                 current[currVal] = elementName;
11194:             }
11195:         }
11196:     }
11197:     for (var j=0; j<maxh; j++) {
11198:         if (current[j] == undefined) {
11199:             oldVal = j;
11200:         }
11201:     }
11202:     if (oldVal < changedVal) {
11203:         for (var k=oldVal+1; k<=changedVal ; k++) {
11204:            var elementName = current[k];
11205:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11206:         }
11207:     } else {
11208:         for (var k=changedVal; k<oldVal; k++) {
11209:             var elementName = current[k];
11210:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11211:         }
11212:     }
11213:     return;
11214: }
11215: 
11216: $portal_js
11217: 
11218: // ]]>
11219: </script>
11220: 
11221: ENDSCRIPT
11222:     } else {
11223: return <<"ENDSCRIPT";
11224: <script type="text/javascript">
11225: // <![CDATA[
11226: $portal_js
11227: // ]]>
11228: </script>
11229: 
11230: ENDSCRIPT
11231:     }
11232:     return;
11233: }
11234: 
11235: sub passwords_javascript {
11236:     my ($prefix) = @_;
11237:     my %intalert;
11238:     if ($prefix eq 'passwords') {
11239:         %intalert = &Apache::lonlocal::texthash (
11240:             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.',
11241:             authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
11242:             passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
11243:             passmax => 'Warning: maximum password length must be a positive integer (or blank).',
11244:             passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
11245:             passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
11246:         );
11247:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
11248:         %intalert = &Apache::lonlocal::texthash (
11249:             passmin => 'Warning: minimum secret length must be a positive integer greater than 6.',
11250:             passmax => 'Warning: maximum secret length must be a positive integer (or blank).',
11251:         );
11252:     }
11253:     &js_escape(\%intalert);
11254:     my $defmin = $Apache::lonnet::passwdmin;
11255:     my $intauthjs; 
11256:     if ($prefix eq 'passwords') { $intauthjs = <<"ENDSCRIPT";
11257: 
11258: function warnIntAuth(field) {
11259:     if (field.name == 'intauth_check') {
11260:         if (field.value == '2') {
11261:             alert('$intalert{authcheck}');
11262:         }
11263:     }
11264:     if (field.name == 'intauth_cost') {
11265:         field.value.replace(/\s/g,'');
11266:         if (field.value != '') {
11267:             var regexdigit=/^\\d+\$/;
11268:             if (!regexdigit.test(field.value)) {
11269:                 alert('$intalert{authcost}');
11270:             }
11271:         }
11272:     }
11273:     return;
11274: }
11275: 
11276: ENDSCRIPT
11277: 
11278:      }
11279: 
11280:      $intauthjs .= <<"ENDSCRIPT";
11281: 
11282: function warnInt$prefix(field) {
11283:     field.value.replace(/^\s+/,'');
11284:     field.value.replace(/\s+\$/,'');
11285:     var regexdigit=/^\\d+\$/;
11286:     if (field.name == '${prefix}_min') {
11287:         if (field.value == '') {
11288:             alert('$intalert{passmin}');
11289:             field.value = '$defmin';
11290:         } else {
11291:             if (!regexdigit.test(field.value)) {
11292:                 alert('$intalert{passmin}');
11293:                 field.value = '$defmin';
11294:             }
11295:             var minval = parseInt(field.value,10);
11296:             if (minval < $defmin) {
11297:                 alert('$intalert{passmin}');
11298:                 field.value = '$defmin';
11299:             }
11300:         }
11301:     } else {
11302:         if (field.value == '0') {
11303:             field.value = '';
11304:         }
11305:         if (field.value != '') {
11306:             if (field.name == '${prefix}_expire') {
11307:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
11308:                 if (!regexpposnum.test(field.value)) {
11309:                     alert('$intalert{passexp}');
11310:                     field.value = '';
11311:                 } else {
11312:                     var expval = parseFloat(field.value);
11313:                     if (expval == 0) {
11314:                         alert('$intalert{passexp}');
11315:                         field.value = '';
11316:                     }
11317:                 }
11318:             } else {
11319:                 if (!regexdigit.test(field.value)) {
11320:                     if (field.name == '${prefix}_max') {
11321:                         alert('$intalert{passmax}');
11322:                     } else {
11323:                         if (field.name == '${prefix}_numsaved') {
11324:                             alert('$intalert{passnum}');
11325:                         }
11326:                     }
11327:                     field.value = '';
11328:                 }
11329:             }
11330:         }
11331:     }
11332:     return;
11333: }
11334: 
11335: ENDSCRIPT
11336:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
11337: }
11338: 
11339: sub coursecategories_javascript {
11340:     my ($settings) = @_;
11341:     my ($output,$jstext,$cathash);
11342:     if (ref($settings) eq 'HASH') {
11343:         $cathash = $settings->{'cats'};
11344:     }
11345:     if (ref($cathash) eq 'HASH') {
11346:         my (@cats,@jsarray,%idx);
11347:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
11348:         if (@jsarray > 0) {
11349:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
11350:             for (my $i=0; $i<@jsarray; $i++) {
11351:                 if (ref($jsarray[$i]) eq 'ARRAY') {
11352:                     my $catstr = join('","',@{$jsarray[$i]});
11353:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
11354:                 }
11355:             }
11356:         }
11357:     } else {
11358:         $jstext  = '    var categories = Array(1);'."\n".
11359:                    '    categories[0] = Array("instcode_pos");'."\n"; 
11360:     }
11361:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
11362:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
11363:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
11364:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
11365:     &js_escape(\$instcode_reserved);
11366:     &js_escape(\$communities_reserved);
11367:     &js_escape(\$placement_reserved);
11368:     &js_escape(\$choose_again);
11369:     $output = <<"ENDSCRIPT";
11370: <script type="text/javascript">
11371: // <![CDATA[
11372: function reorderCats(form,parent,item,idx) {
11373:     var changedVal;
11374: $jstext
11375:     var newpos = 'addcategory_pos';
11376:     if (parent == '') {
11377:         var has_instcode = 0;
11378:         var maxtop = categories[idx].length;
11379:         for (var j=0; j<maxtop; j++) {
11380:             if (categories[idx][j] == 'instcode::0') {
11381:                 has_instcode == 1;
11382:             }
11383:         }
11384:         if (has_instcode == 0) {
11385:             categories[idx][maxtop] = 'instcode_pos';
11386:         }
11387:     } else {
11388:         newpos += '_'+parent;
11389:     }
11390:     var maxh = 1 + categories[idx].length;
11391:     var current = new Array;
11392:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
11393:     if (item == newpos) {
11394:         changedVal = newitemVal;
11395:     } else {
11396:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
11397:         current[newitemVal] = newpos;
11398:     }
11399:     for (var i=0; i<categories[idx].length; i++) {
11400:         var elementName = categories[idx][i];
11401:         if (elementName != item) {
11402:             if (form.elements[elementName]) {
11403:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
11404:                 current[currVal] = elementName;
11405:             }
11406:         }
11407:     }
11408:     var oldVal;
11409:     for (var j=0; j<maxh; j++) {
11410:         if (current[j] == undefined) {
11411:             oldVal = j;
11412:         }
11413:     }
11414:     if (oldVal < changedVal) {
11415:         for (var k=oldVal+1; k<=changedVal ; k++) {
11416:            var elementName = current[k];
11417:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
11418:         }
11419:     } else {
11420:         for (var k=changedVal; k<oldVal; k++) {
11421:             var elementName = current[k];
11422:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
11423:         }
11424:     }
11425:     return;
11426: }
11427: 
11428: function categoryCheck(form) {
11429:     if (form.elements['addcategory_name'].value == 'instcode') {
11430:         alert('$instcode_reserved\\n$choose_again');
11431:         return false;
11432:     }
11433:     if (form.elements['addcategory_name'].value == 'communities') {
11434:         alert('$communities_reserved\\n$choose_again');
11435:         return false;
11436:     }
11437:     if (form.elements['addcategory_name'].value == 'placement') {
11438:         alert('$placement_reserved\\n$choose_again');
11439:         return false;
11440:     }
11441:     return true;
11442: }
11443: 
11444: // ]]>
11445: </script>
11446: 
11447: ENDSCRIPT
11448:     return $output;
11449: }
11450: 
11451: sub initialize_categories {
11452:     my ($itemcount) = @_;
11453:     my ($datatable,$css_class,$chgstr);
11454:     my %default_names = &Apache::lonlocal::texthash (
11455:                       instcode    => 'Official courses (with institutional codes)',
11456:                       communities => 'Communities',
11457:                       placement   => 'Placement Tests',
11458:                         );
11459:     my %selnum = (
11460:                    instcode    => '0',
11461:                    communities => '1',
11462:                    placement   => '2',
11463:                  );
11464:     my %selected;
11465:     foreach my $default ('instcode','communities','placement') {
11466:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
11467:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
11468:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
11469:         $selected{$selnum{$default}} = ' selected="selected"';
11470:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11471:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
11472:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
11473:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
11474:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
11475:                      .'<option value="3">4</option></select>&nbsp;'
11476:                      .$default_names{$default}
11477:                      .'</span></td><td><span class="LC_nobreak">'
11478:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
11479:                      .&mt('Display').'</label>&nbsp;<label>'
11480:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
11481:                  .'</label></span></td></tr>';
11482:         $itemcount ++;
11483:     }
11484:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
11485:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
11486:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
11487:                   .'<select name="addcategory_pos"'.$chgstr.'>'
11488:                   .'<option value="0">1</option>'
11489:                   .'<option value="1">2</option>'
11490:                   .'<option value="2">3</option>'
11491:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
11492:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
11493:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
11494:                   .'</td></tr>';
11495:     return $datatable;
11496: }
11497: 
11498: sub build_category_rows {
11499:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
11500:     my ($text,$name,$item,$chgstr);
11501:     if (ref($cats) eq 'ARRAY') {
11502:         my $maxdepth = scalar(@{$cats});
11503:         if (ref($cats->[$depth]) eq 'HASH') {
11504:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
11505:                 my $numchildren = @{$cats->[$depth]{$parent}};
11506:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
11507:                 $text .= '<td><table class="LC_data_table">';
11508:                 my ($idxnum,$parent_name,$parent_item);
11509:                 my $higher = $depth - 1;
11510:                 if ($higher == 0) {
11511:                     $parent_name = &escape($parent).'::'.$higher;
11512:                 } else {
11513:                     if (ref($path) eq 'ARRAY') {
11514:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11515:                     }
11516:                 }
11517:                 $parent_item = 'addcategory_pos_'.$parent_name;
11518:                 for (my $j=0; $j<=$numchildren; $j++) {
11519:                     if ($j < $numchildren) {
11520:                         $name = $cats->[$depth]{$parent}[$j];
11521:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
11522:                         $idxnum = $idx->{$item};
11523:                     } else {
11524:                         $name = $parent_name;
11525:                         $item = $parent_item;
11526:                     }
11527:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
11528:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
11529:                     for (my $i=0; $i<=$numchildren; $i++) {
11530:                         my $vpos = $i+1;
11531:                         my $selstr;
11532:                         if ($j == $i) {
11533:                             $selstr = ' selected="selected" ';
11534:                         }
11535:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
11536:                     }
11537:                     $text .= '</select>&nbsp;';
11538:                     if ($j < $numchildren) {
11539:                         my $deeper = $depth+1;
11540:                         $text .= $name.'&nbsp;'
11541:                                  .'<label><input type="checkbox" name="deletecategory" value="'
11542:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
11543:                         if(ref($path) eq 'ARRAY') {
11544:                             push(@{$path},$name);
11545:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
11546:                             pop(@{$path});
11547:                         }
11548:                     } else {
11549:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
11550:                         if ($j == $numchildren) {
11551:                             $text .= $name;
11552:                         } else {
11553:                             $text .= $item;
11554:                         }
11555:                         $text .= '" value="" />';
11556:                     }
11557:                     $text .= '</td></tr>';
11558:                 }
11559:                 $text .= '</table></td>';
11560:             } else {
11561:                 my $higher = $depth-1;
11562:                 if ($higher == 0) {
11563:                     $name = &escape($parent).'::'.$higher;
11564:                 } else {
11565:                     if (ref($path) eq 'ARRAY') {
11566:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
11567:                     }
11568:                 }
11569:                 my $colspan;
11570:                 if ($parent ne 'instcode') {
11571:                     $colspan = $maxdepth - $depth - 1;
11572:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
11573:                 }
11574:             }
11575:         }
11576:     }
11577:     return $text;
11578: }
11579: 
11580: sub modifiable_userdata_row {
11581:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
11582:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
11583:     my ($role,$rolename,$statustype);
11584:     $role = $item;
11585:     if ($context eq 'cancreate') {
11586:         if ($item =~ /^(emailusername)_(.+)$/) {
11587:             $role = $1;
11588:             $statustype = $2;
11589:             if (ref($usertypes) eq 'HASH') {
11590:                 if ($usertypes->{$statustype}) {
11591:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
11592:                 } else {
11593:                     $rolename = &mt('Data provided by user');
11594:                 }
11595:             }
11596:         }
11597:     } elsif ($context eq 'selfcreate') {
11598:         if (ref($usertypes) eq 'HASH') {
11599:             $rolename = $usertypes->{$role};
11600:         } else {
11601:             $rolename = $role;
11602:         }
11603:     } elsif ($context eq 'lti') {
11604:         $rolename = &mt('Institutional data used (if available)');
11605:     } elsif ($context eq 'privacy') {
11606:         $rolename = $itemdesc;
11607:     } else {
11608:         if ($role eq 'cr') {
11609:             $rolename = &mt('Custom role');
11610:         } else {
11611:             $rolename = &Apache::lonnet::plaintext($role);
11612:         }
11613:     }
11614:     my (@fields,%fieldtitles);
11615:     if (ref($fieldsref) eq 'ARRAY') {
11616:         @fields = @{$fieldsref};
11617:     } else {
11618:         @fields = ('lastname','firstname','middlename','generation',
11619:                    'permanentemail','id');
11620:     }
11621:     if ((ref($titlesref) eq 'HASH')) {
11622:         %fieldtitles = %{$titlesref};
11623:     } else {
11624:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11625:     }
11626:     my $output;
11627:     my $css_class;
11628:     if ($rowcount%2) {
11629:         $css_class = 'LC_odd_row';
11630:     }
11631:     if ($customcss) {
11632:         $css_class .= " $customcss";
11633:     }
11634:     $css_class =~ s/^\s+//;
11635:     if ($css_class) {
11636:         $css_class = ' class="'.$css_class.'"';
11637:     }
11638:     if ($rowstyle) {
11639:         $css_class .= ' style="'.$rowstyle.'"';
11640:     }
11641:     if ($rowid) {
11642:         $rowid = ' id="'.$rowid.'"';
11643:     }
11644:     $output = '<tr '.$css_class.$rowid.'>'.
11645:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
11646:               '<td class="LC_left_item" colspan="2"><table>';
11647:     my $rem;
11648:     my %checks;
11649:     my %current;
11650:     if (ref($settings) eq 'HASH') {
11651:         my $hashref;
11652:         if ($context eq 'lti') {
11653:             if (ref($settings) eq 'HASH') {
11654:                 $hashref = $settings->{'instdata'};
11655:             }
11656:         } elsif ($context eq 'privacy') {
11657:             my ($key,$inner) = split(/_/,$role);
11658:             if (ref($settings) eq 'HASH') {
11659:                 if (ref($settings->{$key}) eq 'HASH') {
11660:                     $hashref = $settings->{$key}->{$inner};
11661:                 }
11662:             }
11663:         } elsif (ref($settings->{$context}) eq 'HASH') {
11664:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
11665:                 $hashref = $settings->{'lti_instdata'};
11666:             }
11667:             if ($role eq 'emailusername') {
11668:                 if ($statustype) {
11669:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
11670:                         $hashref = $settings->{$context}->{$role}->{$statustype};
11671:                     }
11672:                 }
11673:             }
11674:         }
11675:         if (ref($hashref) eq 'HASH') { 
11676:             foreach my $field (@fields) {
11677:                 if ($hashref->{$field}) {
11678:                     if ($role eq 'emailusername') {
11679:                         $checks{$field} = $hashref->{$field};
11680:                     } else {
11681:                         $checks{$field} = ' checked="checked" ';
11682:                     }
11683:                 }
11684:             }
11685:         }
11686:     }
11687:  
11688:     my $total = scalar(@fields);
11689:     for (my $i=0; $i<$total; $i++) {
11690:         $rem = $i%($numinrow);
11691:         if ($rem == 0) {
11692:             if ($i > 0) {
11693:                 $output .= '</tr>';
11694:             }
11695:             $output .= '<tr>';
11696:         }
11697:         my $check = ' ';
11698:         unless ($role eq 'emailusername') {
11699:             if (exists($checks{$fields[$i]})) {
11700:                 $check = $checks{$fields[$i]};
11701:             } elsif ($context eq 'privacy') {
11702:                 if ($role =~ /^priv_(domain|course)$/) {
11703:                     if (ref($settings) ne 'HASH') {
11704:                         $check = ' checked="checked" ';
11705:                     }
11706:                 } elsif ($role =~ /^priv_(author|community)$/) {
11707:                     if (ref($settings) ne 'HASH') {
11708:                         unless ($fields[$i] eq 'id') {
11709:                             $check = ' checked="checked" ';
11710:                         }
11711:                     }
11712:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
11713:                     if (ref($settings) ne 'HASH') {
11714:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
11715:                             $check = ' checked="checked" ';
11716:                         }
11717:                     }
11718:                 }
11719:             } elsif ($context ne 'lti') {
11720:                 if ($role eq 'st') {
11721:                     if (ref($settings) ne 'HASH') {
11722:                         $check = ' checked="checked" '; 
11723:                     }
11724:                 }
11725:             }
11726:         }
11727:         $output .= '<td class="LC_left_item">'.
11728:                    '<span class="LC_nobreak">';
11729:         my $prefix = 'canmodify';
11730:         if ($role eq 'emailusername') {
11731:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
11732:                 $checks{$fields[$i]} = 'omit';
11733:             }
11734:             foreach my $option ('required','optional','omit') {
11735:                 my $checked='';
11736:                 if ($checks{$fields[$i]} eq $option) {
11737:                     $checked='checked="checked" ';
11738:                 }
11739:                 $output .= '<label>'.
11740:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
11741:                            &mt($option).'</label>'.('&nbsp;' x2);
11742:             }
11743:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
11744:         } else {
11745:             if ($context eq 'lti') {
11746:                 $prefix = 'lti';
11747:             } elsif ($context eq 'privacy') {
11748:                 $prefix = 'privacy';
11749:             }
11750:             $output .= '<label>'.
11751:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
11752:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
11753:                        '</label>';
11754:         }
11755:         $output .= '</span></td>';
11756:     }
11757:     $rem = $total%$numinrow;
11758:     my $colsleft;
11759:     if ($rem) {
11760:         $colsleft = $numinrow - $rem;
11761:     }
11762:     if ($colsleft > 1) {
11763:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
11764:                    '&nbsp;</td>';
11765:     } elsif ($colsleft == 1) {
11766:         $output .= '<td class="LC_left_item">&nbsp;</td>';
11767:     }
11768:     $output .= '</tr></table></td></tr>';
11769:     return $output;
11770: }
11771: 
11772: sub insttypes_row {
11773:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
11774:         $customcss,$rowstyle) = @_;
11775:     my %lt = &Apache::lonlocal::texthash (
11776:                       cansearch => 'Users allowed to search',
11777:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
11778:                       lockablenames => 'User preference to lock name',
11779:                       selfassign    => 'Self-reportable affiliations',
11780:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
11781:              );
11782:     my $showdom;
11783:     if ($context eq 'cansearch') {
11784:         $showdom = ' ('.$dom.')';
11785:     }
11786:     my $class = 'LC_left_item';
11787:     if ($context eq 'statustocreate') {
11788:         $class = 'LC_right_item';
11789:     }
11790:     my $css_class;
11791:     if ($$rowtotal%2) {
11792:         $css_class = 'LC_odd_row';
11793:     }
11794:     if ($customcss) {
11795:         $css_class .= ' '.$customcss;
11796:     }
11797:     $css_class =~ s/^\s+//;
11798:     if ($css_class) {
11799:         $css_class = ' class="'.$css_class.'"';
11800:     }
11801:     if ($rowstyle) {
11802:         $css_class .= ' style="'.$rowstyle.'"';
11803:     }
11804:     if ($onclick) {
11805:         $onclick = 'onclick="'.$onclick.'" ';
11806:     }
11807:     my $output = '<tr'.$css_class.'>'.
11808:                  '<td>'.$lt{$context}.$showdom.
11809:                  '</td><td class="'.$class.'" colspan="2"><table>';
11810:     my $rem;
11811:     if (ref($types) eq 'ARRAY') {
11812:         for (my $i=0; $i<@{$types}; $i++) {
11813:             if (defined($usertypes->{$types->[$i]})) {
11814:                 my $rem = $i%($numinrow);
11815:                 if ($rem == 0) {
11816:                     if ($i > 0) {
11817:                         $output .= '</tr>';
11818:                     }
11819:                     $output .= '<tr>';
11820:                 }
11821:                 my $check = ' ';
11822:                 if (ref($settings) eq 'HASH') {
11823:                     if (ref($settings->{$context}) eq 'ARRAY') {
11824:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
11825:                             $check = ' checked="checked" ';
11826:                         }
11827:                     } elsif (ref($settings->{$context}) eq 'HASH') {
11828:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
11829:                             $check = ' checked="checked" ';
11830:                         }
11831:                     } elsif ($context eq 'statustocreate') {
11832:                         $check = ' checked="checked" ';
11833:                     }
11834:                 }
11835:                 $output .= '<td class="LC_left_item">'.
11836:                            '<span class="LC_nobreak"><label>'.
11837:                            '<input type="checkbox" name="'.$context.'" '.
11838:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
11839:                            $usertypes->{$types->[$i]}.'</label></span></td>';
11840:             }
11841:         }
11842:         $rem = @{$types}%($numinrow);
11843:     }
11844:     my $colsleft = $numinrow - $rem;
11845:     if ($context eq 'overrides') {
11846:         if ($colsleft > 1) {
11847:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11848:         } else {
11849:             $output .= '<td class="LC_left_item">';
11850:         }
11851:         $output .= '&nbsp;';  
11852:     } else {
11853:         if ($rem == 0) {
11854:             $output .= '<tr>';
11855:         }
11856:         if ($colsleft > 1) {
11857:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
11858:         } else {
11859:             $output .= '<td class="LC_left_item">';
11860:         }
11861:         my $defcheck = ' ';
11862:         if (ref($settings) eq 'HASH') {  
11863:             if (ref($settings->{$context}) eq 'ARRAY') {
11864:                 if (grep(/^default$/,@{$settings->{$context}})) {
11865:                     $defcheck = ' checked="checked" ';
11866:                 }
11867:             } elsif ($context eq 'statustocreate') {
11868:                 $defcheck = ' checked="checked" ';
11869:             }
11870:         }
11871:         $output .= '<span class="LC_nobreak"><label>'.
11872:                    '<input type="checkbox" name="'.$context.'" '.
11873:                    'value="default"'.$defcheck.$onclick.' />'.
11874:                    $othertitle.'</label></span>';
11875:     }
11876:     $output .= '</td></tr></table></td></tr>';
11877:     return $output;
11878: }
11879: 
11880: sub sorted_searchtitles {
11881:     my %searchtitles = &Apache::lonlocal::texthash(
11882:                          'uname' => 'username',
11883:                          'lastname' => 'last name',
11884:                          'lastfirst' => 'last name, first name',
11885:                      );
11886:     my @titleorder = ('uname','lastname','lastfirst');
11887:     return (\%searchtitles,\@titleorder);
11888: }
11889: 
11890: sub sorted_searchtypes {
11891:     my %srchtypes_desc = (
11892:                            exact    => 'is exact match',
11893:                            contains => 'contains ..',
11894:                            begins   => 'begins with ..',
11895:                          );
11896:     my @srchtypeorder = ('exact','begins','contains');
11897:     return (\%srchtypes_desc,\@srchtypeorder);
11898: }
11899: 
11900: sub usertype_update_row {
11901:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
11902:     my $datatable;
11903:     my $numinrow = 4;
11904:     foreach my $type (@{$types}) {
11905:         if (defined($usertypes->{$type})) {
11906:             $$rownums ++;
11907:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
11908:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
11909:                           '</td><td class="LC_left_item"><table>';
11910:             for (my $i=0; $i<@{$fields}; $i++) {
11911:                 my $rem = $i%($numinrow);
11912:                 if ($rem == 0) {
11913:                     if ($i > 0) {
11914:                         $datatable .= '</tr>';
11915:                     }
11916:                     $datatable .= '<tr>';
11917:                 }
11918:                 my $check = ' ';
11919:                 if (ref($settings) eq 'HASH') {
11920:                     if (ref($settings->{'fields'}) eq 'HASH') {
11921:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
11922:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
11923:                                 $check = ' checked="checked" ';
11924:                             }
11925:                         }
11926:                     }
11927:                 }
11928: 
11929:                 if ($i == @{$fields}-1) {
11930:                     my $colsleft = $numinrow - $rem;
11931:                     if ($colsleft > 1) {
11932:                         $datatable .= '<td colspan="'.$colsleft.'">';
11933:                     } else {
11934:                         $datatable .= '<td>';
11935:                     }
11936:                 } else {
11937:                     $datatable .= '<td>';
11938:                 }
11939:                 $datatable .= '<span class="LC_nobreak"><label>'.
11940:                               '<input type="checkbox" name="updateable_'.$type.
11941:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
11942:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
11943:             }
11944:             $datatable .= '</tr></table></td></tr>';
11945:         }
11946:     }
11947:     return $datatable;
11948: }
11949: 
11950: sub modify_login {
11951:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
11952:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
11953:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon,
11954:         %currsaml,%saml,%samltext,%samlimg,%samlalt,%samlurl,%samltitle,%samlwindow,%samlnotsso);
11955:     %title = ( coursecatalog => 'Display course catalog',
11956:                adminmail => 'Display administrator E-mail address',
11957:                helpdesk  => 'Display "Contact Helpdesk" link',
11958:                newuser => 'Link for visitors to create a user account',
11959:                loginheader => 'Log-in box header',
11960:                saml => 'Dual SSO and non-SSO login');
11961:     @offon = ('off','on');
11962:     if (ref($domconfig{login}) eq 'HASH') {
11963:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
11964:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
11965:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
11966:             }
11967:         }
11968:         if (ref($domconfig{login}{'saml'}) eq 'HASH') {
11969:             foreach my $lonhost (keys(%{$domconfig{login}{'saml'}})) {
11970:                 if (ref($domconfig{login}{'saml'}{$lonhost}) eq 'HASH') {
11971:                     $currsaml{$lonhost} = $domconfig{login}{'saml'}{$lonhost};
11972:                     $saml{$lonhost} = 1;
11973:                     $samltext{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'text'};
11974:                     $samlurl{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'url'};
11975:                     $samlalt{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'alt'};
11976:                     $samlimg{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'img'};
11977:                     $samltitle{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'title'};
11978:                     $samlwindow{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'window'};
11979:                     $samlnotsso{$lonhost} = $domconfig{login}{'saml'}{$lonhost}{'notsso'};
11980:                 }
11981:             }
11982:         }
11983:     }
11984:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
11985:                                            \%domconfig,\%loginhash);
11986:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11987:     foreach my $item (@toggles) {
11988:         $loginhash{login}{$item} = $env{'form.'.$item};
11989:     }
11990:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
11991:     if (ref($colchanges{'login'}) eq 'HASH') {  
11992:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
11993:                                          \%loginhash);
11994:     }
11995: 
11996:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
11997:     my %domservers = &Apache::lonnet::get_servers($dom);
11998:     my @loginvia_attribs = ('serverpath','custompath','exempt');
11999:     if (keys(%servers) > 1) {
12000:         foreach my $lonhost (keys(%servers)) {
12001:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
12002:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
12003:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
12004:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
12005:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
12006:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12007:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12008:                         $changes{'loginvia'}{$lonhost} = 1;
12009:                     } else {
12010:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
12011:                         $changes{'loginvia'}{$lonhost} = 1;
12012:                     }
12013:                 } else {
12014:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12015:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12016:                         $changes{'loginvia'}{$lonhost} = 1;
12017:                     }
12018:                 }
12019:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
12020:                     foreach my $item (@loginvia_attribs) {
12021:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
12022:                     }
12023:                 } else {
12024:                     foreach my $item (@loginvia_attribs) {
12025:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12026:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12027:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
12028:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12029:                                 $new = '/';
12030:                             }
12031:                         }
12032:                         if (($item eq 'custompath') && 
12033:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12034:                             $new = '';
12035:                         }
12036:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
12037:                             $changes{'loginvia'}{$lonhost} = 1;
12038:                         }
12039:                         if ($item eq 'exempt') {
12040:                             $new = &check_exempt_addresses($new);
12041:                         }
12042:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12043:                     }
12044:                 }
12045:             } else {
12046:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
12047:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
12048:                     $changes{'loginvia'}{$lonhost} = 1;
12049:                     foreach my $item (@loginvia_attribs) {
12050:                         my $new = $env{'form.'.$lonhost.'_'.$item};
12051:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
12052:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
12053:                                 $new = '/';
12054:                             }
12055:                         }
12056:                         if (($item eq 'custompath') && 
12057:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
12058:                             $new = '';
12059:                         }
12060:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
12061:                     }
12062:                 }
12063:             }
12064:         }
12065:     }
12066: 
12067:     my $servadm = $r->dir_config('lonAdmEMail');
12068:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
12069:     if (ref($domconfig{'login'}) eq 'HASH') {
12070:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
12071:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
12072:                 if ($lang eq 'nolang') {
12073:                     push(@currlangs,$lang);
12074:                 } elsif (defined($langchoices{$lang})) {
12075:                     push(@currlangs,$lang);
12076:                 } else {
12077:                     next;
12078:                 }
12079:             }
12080:         }
12081:     }
12082:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
12083:     if (@currlangs > 0) {
12084:         foreach my $lang (@currlangs) {
12085:             if (grep(/^\Q$lang\E$/,@delurls)) {
12086:                 $changes{'helpurl'}{$lang} = 1;
12087:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
12088:                 $changes{'helpurl'}{$lang} = 1;
12089:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
12090:                 push(@newlangs,$lang);
12091:             } else {
12092:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12093:             }
12094:         }
12095:     }
12096:     unless (grep(/^nolang$/,@currlangs)) {
12097:         if ($env{'form.loginhelpurl_nolang.filename'}) {
12098:             $changes{'helpurl'}{'nolang'} = 1;
12099:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
12100:             push(@newlangs,'nolang');
12101:         }
12102:     }
12103:     if ($env{'form.loginhelpurl_add_lang'}) {
12104:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
12105:             ($env{'form.loginhelpurl_add_file.filename'})) {
12106:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
12107:             $addedfile = $env{'form.loginhelpurl_add_lang'};
12108:         }
12109:     }
12110:     if ((@newlangs > 0) || ($addedfile)) {
12111:         my $error;
12112:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12113:         if ($configuserok eq 'ok') {
12114:             if ($switchserver) {
12115:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
12116:             } elsif ($author_ok eq 'ok') {
12117:                 my @allnew = @newlangs;
12118:                 if ($addedfile ne '') {
12119:                     push(@allnew,$addedfile);
12120:                 }
12121:                 my $modified = [];
12122:                 foreach my $lang (@allnew) {
12123:                     my $formelem = 'loginhelpurl_'.$lang;
12124:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
12125:                         $formelem = 'loginhelpurl_add_file';
12126:                     }
12127:                     (my $result,$newurl{$lang}) = 
12128:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12129:                                                                 "help/$lang",'','',$newfile{$lang},
12130:                                                                 $modified);
12131:                     if ($result eq 'ok') {
12132:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
12133:                         $changes{'helpurl'}{$lang} = 1;
12134:                     } else {
12135:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
12136:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12137:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
12138:                             (!grep(/^\Q$lang\E$/,@delurls))) {
12139:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
12140:                         }
12141:                     }
12142:                 }
12143:                 &update_modify_urls($r,$modified);
12144:             } else {
12145:                 $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);
12146:             }
12147:         } else {
12148:             $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);
12149:         }
12150:         if ($error) {
12151:             &Apache::lonnet::logthis($error);
12152:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12153:         }
12154:     }
12155: 
12156:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
12157:     if (ref($domconfig{'login'}) eq 'HASH') {
12158:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
12159:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
12160:                 if ($domservers{$lonhost}) {
12161:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12162:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
12163:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
12164:                     }
12165:                 }
12166:             }
12167:         }
12168:     }
12169:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
12170:     foreach my $lonhost (sort(keys(%domservers))) {
12171:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12172:             $changes{'headtag'}{$lonhost} = 1;
12173:         } else {
12174:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
12175:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
12176:             }
12177:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
12178:                 push(@newhosts,$lonhost);
12179:             } elsif ($currheadtagurls{$lonhost}) {
12180:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
12181:                 if ($currexempt{$lonhost}) {
12182:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
12183:                         $changes{'headtag'}{$lonhost} = 1;
12184:                     }
12185:                 } elsif ($possexempt{$lonhost}) {
12186:                     $changes{'headtag'}{$lonhost} = 1;
12187:                 }
12188:                 if ($possexempt{$lonhost}) {
12189:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12190:                 }
12191:             }
12192:         }
12193:     }
12194:     if (@newhosts) {
12195:         my $error;
12196:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12197:         if ($configuserok eq 'ok') {
12198:             if ($switchserver) {
12199:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
12200:             } elsif ($author_ok eq 'ok') {
12201:                 my $modified = [];
12202:                 foreach my $lonhost (@newhosts) {
12203:                     my $formelem = 'loginheadtag_'.$lonhost;
12204:                     (my $result,$newheadtagurls{$lonhost}) = 
12205:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12206:                                                                 "login/headtag/$lonhost",'','',
12207:                                                                 $env{'form.loginheadtag_'.$lonhost.'.filename'},
12208:                                                                 $modified);
12209:                     if ($result eq 'ok') {
12210:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
12211:                         $changes{'headtag'}{$lonhost} = 1;
12212:                         if ($possexempt{$lonhost}) {
12213:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
12214:                         }
12215:                     } else {
12216:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
12217:                                            $newheadtagurls{$lonhost},$result);
12218:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12219:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
12220:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
12221:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
12222:                         }
12223:                     }
12224:                 }
12225:                 &update_modify_urls($r,$modified);
12226:             } else {
12227:                 $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);
12228:             }
12229:         } else {
12230:             $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);
12231:         }
12232:         if ($error) {
12233:             &Apache::lonnet::logthis($error);
12234:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12235:         }
12236:     }
12237:     my @delsamlimg = &Apache::loncommon::get_env_multiple('form.saml_img_del');
12238:     my @newsamlimgs;
12239:     foreach my $lonhost (keys(%domservers)) {
12240:         if ($env{'form.saml_'.$lonhost}) {
12241:             if ($env{'form.saml_img_'.$lonhost.'.filename'}) {
12242:                 push(@newsamlimgs,$lonhost);
12243:             }
12244:             foreach my $item ('text','alt','url','title','window','notsso') {
12245:                 $env{'form.saml_'.$item.'_'.$lonhost} =~ s/^\s+|\s+$//g;
12246:             }
12247:             if ($saml{$lonhost}) {
12248:                 if ($env{'form.saml_window_'.$lonhost} ne '1') {
12249:                     $env{'form.saml_window_'.$lonhost} = '';
12250:                 }
12251:                 if (grep(/^\Q$lonhost\E$/,@delsamlimg)) {
12252: #FIXME Need to obsolete published image
12253:                     delete($currsaml{$lonhost}{'img'});
12254:                     $changes{'saml'}{$lonhost} = 1;
12255:                 }
12256:                 if ($env{'form.saml_alt_'.$lonhost} ne $samlalt{$lonhost}) {
12257:                     $changes{'saml'}{$lonhost} = 1;
12258:                 }
12259:                 if ($env{'form.saml_text_'.$lonhost} ne $samltext{$lonhost}) {
12260:                     $changes{'saml'}{$lonhost} = 1;
12261:                 }
12262:                 if ($env{'form.saml_url_'.$lonhost} ne $samlurl{$lonhost}) {
12263:                     $changes{'saml'}{$lonhost} = 1;
12264:                 }
12265:                 if ($env{'form.saml_title_'.$lonhost} ne $samltitle{$lonhost}) {
12266:                     $changes{'saml'}{$lonhost} = 1;
12267:                 }
12268:                 if ($env{'form.saml_window_'.$lonhost} ne $samlwindow{$lonhost}) {
12269:                     $changes{'saml'}{$lonhost} = 1;
12270:                 }
12271:                 if ($env{'form.saml_notsso_'.$lonhost} ne $samlnotsso{$lonhost}) {
12272:                     $changes{'saml'}{$lonhost} = 1;
12273:                 }
12274:             } else {
12275:                 $changes{'saml'}{$lonhost} = 1;
12276:             }
12277:             foreach my $item ('text','alt','url','title','window','notsso') {
12278:                 $currsaml{$lonhost}{$item} = $env{'form.saml_'.$item.'_'.$lonhost};
12279:             }
12280:         } else {
12281:             if ($saml{$lonhost}) {  
12282:                 $changes{'saml'}{$lonhost} = 1;
12283:                 delete($currsaml{$lonhost});
12284:             }
12285:         }
12286:     }
12287:     foreach my $posshost (keys(%currsaml)) {
12288:         unless (exists($domservers{$posshost})) { 
12289:             delete($currsaml{$posshost}); 
12290:         }
12291:     }
12292:     %{$loginhash{'login'}{'saml'}} = %currsaml;
12293:     if (@newsamlimgs) {
12294:         my $error;
12295:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12296:         if ($configuserok eq 'ok') {
12297:             if ($switchserver) {
12298:                 $error = &mt("Upload of SSO Button Image is not permitted to this server: [_1].",$switchserver);
12299:             } elsif ($author_ok eq 'ok') {
12300:                 my $modified = [];
12301:                 foreach my $lonhost (@newsamlimgs) {
12302:                     my $formelem = 'saml_img_'.$lonhost;
12303:                     my ($result,$imgurl) = 
12304:                         &Apache::lonconfigsettings::publishlogo($r,'upload',$formelem,$dom,$confname,
12305:                                                                 "login/saml/$lonhost",'','',
12306:                                                                 $env{'form.saml_img_'.$lonhost.'.filename'},
12307:                                                                 $modified);
12308:                     if ($result eq 'ok') {
12309:                         $currsaml{$lonhost}{'img'} = $imgurl;
12310:                         $loginhash{'login'}{'saml'}{$lonhost}{'img'} = $imgurl;
12311:                         $changes{'saml'}{$lonhost} = 1;
12312:                     } else {
12313:                         my $puberror = &mt("Upload of SSO button image failed for [_1] because an error occurred publishing the file in RES space. Error was: [_2].",
12314:                                            $lonhost,$result);
12315:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
12316:                     }
12317:                 }
12318:                 &update_modify_urls($r,$modified);
12319:             } else {
12320:                 $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);
12321:             }
12322:         } else {
12323:             $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);
12324:         }
12325:         if ($error) {
12326:             &Apache::lonnet::logthis($error);
12327:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12328:         }
12329:     }
12330:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
12331: 
12332:     my $defaulthelpfile = '/adm/loginproblems.html';
12333:     my $defaulttext = &mt('Default in use');
12334: 
12335:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
12336:                                              $dom);
12337:     if ($putresult eq 'ok') {
12338:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
12339:         my %defaultchecked = (
12340:                     'coursecatalog' => 'on',
12341:                     'helpdesk'      => 'on',
12342:                     'adminmail'     => 'off',
12343:                     'newuser'       => 'off',
12344:         );
12345:         if (ref($domconfig{'login'}) eq 'HASH') {
12346:             foreach my $item (@toggles) {
12347:                 if ($defaultchecked{$item} eq 'on') { 
12348:                     if (($domconfig{'login'}{$item} eq '0') &&
12349:                         ($env{'form.'.$item} eq '1')) {
12350:                         $changes{$item} = 1;
12351:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12352:                               $domconfig{'login'}{$item} eq '1') &&
12353:                              ($env{'form.'.$item} eq '0')) {
12354:                         $changes{$item} = 1;
12355:                     }
12356:                 } elsif ($defaultchecked{$item} eq 'off') {
12357:                     if (($domconfig{'login'}{$item} eq '1') &&
12358:                         ($env{'form.'.$item} eq '0')) {
12359:                         $changes{$item} = 1;
12360:                     } elsif (($domconfig{'login'}{$item} eq '' ||
12361:                               $domconfig{'login'}{$item} eq '0') &&
12362:                              ($env{'form.'.$item} eq '1')) {
12363:                         $changes{$item} = 1;
12364:                     }
12365:                 }
12366:             }
12367:         }
12368:         if (keys(%changes) > 0 || $colchgtext) {
12369:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12370:             if (exists($changes{'saml'})) {
12371:                 my $hostid_in_use;
12372:                 my @hosts = &Apache::lonnet::current_machine_ids();
12373:                 if (@hosts > 1) {
12374:                     foreach my $hostid (@hosts) {
12375:                         if (&Apache::lonnet::host_domain($hostid) eq $dom) {
12376:                             $hostid_in_use = $hostid;
12377:                             last;
12378:                         }
12379:                     }
12380:                 } else {
12381:                     $hostid_in_use = $r->dir_config('lonHostID');
12382:                 }
12383:                 if (($hostid_in_use) &&
12384:                     (&Apache::lonnet::host_domain($hostid_in_use) eq $dom)) {
12385:                     &Apache::lonnet::devalidate_cache_new('samllanding',$hostid_in_use);
12386:                 }
12387:                 if (ref($lastactref) eq 'HASH') {
12388:                     if (ref($changes{'saml'}) eq 'HASH') {
12389:                         my %updates;
12390:                         map { $updates{$_} = 1; } keys(%{$changes{'saml'}});
12391:                         $lastactref->{'samllanding'} = \%updates;
12392:                     }
12393:                 }
12394:             }
12395:             if (ref($lastactref) eq 'HASH') {
12396:                 $lastactref->{'domainconfig'} = 1;
12397:             }
12398:             $resulttext = &mt('Changes made:').'<ul>';
12399:             foreach my $item (sort(keys(%changes))) {
12400:                 if ($item eq 'loginvia') {
12401:                     if (ref($changes{$item}) eq 'HASH') {
12402:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
12403:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12404:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
12405:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
12406:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
12407:                                     $protocol = 'http' if ($protocol ne 'https');
12408:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
12409: 
12410:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
12411:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
12412:                                     } else {
12413:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
12414:                                     }
12415:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
12416:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
12417:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
12418:                                     }
12419:                                     $resulttext .= '</li>';
12420:                                 } else {
12421:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
12422:                                 }
12423:                             } else {
12424:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
12425:                             }
12426:                         }
12427:                         $resulttext .= '</ul></li>';
12428:                     }
12429:                 } elsif ($item eq 'helpurl') {
12430:                     if (ref($changes{$item}) eq 'HASH') {
12431:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
12432:                             if (grep(/^\Q$lang\E$/,@delurls)) {
12433:                                 my ($chg,$link);
12434:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
12435:                                 if ($lang eq 'nolang') {
12436:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
12437:                                 } else {
12438:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
12439:                                 }
12440:                                 $resulttext .= '<li>'.$chg.'</li>';
12441:                             } else {
12442:                                 my $chg;
12443:                                 if ($lang eq 'nolang') {
12444:                                     $chg = &mt('custom log-in help file for no preferred language');
12445:                                 } else {
12446:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
12447:                                 }
12448:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
12449:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
12450:                                                       '?inhibitmenu=yes',$chg,600,500).
12451:                                                '</li>';
12452:                             }
12453:                         }
12454:                     }
12455:                 } elsif ($item eq 'headtag') {
12456:                     if (ref($changes{$item}) eq 'HASH') {
12457:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12458:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
12459:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
12460:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
12461:                                 $resulttext .= '<li><a href="'.
12462:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
12463:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
12464:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
12465:                                 if ($possexempt{$lonhost}) {
12466:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
12467:                                 } else {
12468:                                     $resulttext .= &mt('included for any client IP');
12469:                                 }
12470:                                 $resulttext .= '</li>';
12471:                             }
12472:                         }
12473:                     }
12474:                 } elsif ($item eq 'saml') {
12475:                     if (ref($changes{$item}) eq 'HASH') {
12476:                         my %notlt = (
12477:                                        text   => 'Text for log-in by SSO',
12478:                                        img    => 'SSO button image',
12479:                                        alt    => 'Alt text for button image',
12480:                                        url    => 'SSO URL',
12481:                                        title  => 'Tooltip for SSO link',
12482:                                        window => 'Pop-up window if iframe',
12483:                                        notsso => 'Text for non-SSO log-in',
12484:                                     );
12485:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
12486:                             if (ref($currsaml{$lonhost}) eq 'HASH') {
12487:                                 $resulttext .= '<li>'.&mt("$title{$item} in use for [_1]","<b>$lonhost</b>").
12488:                                                '<ul>';
12489:                                 foreach my $key ('text','img','alt','url','title','window','notsso') {
12490:                                     if ($currsaml{$lonhost}{$key} eq '') {
12491:                                         $resulttext .= '<li>'.&mt("$notlt{$key} not in use").'</li>';
12492:                                     } else {
12493:                                         my $value = "'$currsaml{$lonhost}{$key}'";
12494:                                         if ($key eq 'img') {
12495:                                             $value = '<img src="'.$currsaml{$lonhost}{$key}.'" />';
12496:                                         } elsif ($key eq 'window') {
12497:                                             $value = 'On';
12498:                                         }
12499:                                         $resulttext .= '<li>'.&mt("$notlt{$key} set to: [_1]",
12500:                                                                   $value).'</li>';
12501:                                     }
12502:                                 }
12503:                                 $resulttext .= '</ul></li>';
12504:                             } else {
12505:                                 $resulttext .= '<li>'.&mt("$title{$item} not in use for [_1]",$lonhost).'</li>';
12506:                             }
12507:                         }
12508:                     }
12509:                 } elsif ($item eq 'captcha') {
12510:                     if (ref($loginhash{'login'}) eq 'HASH') {
12511:                         my $chgtxt;
12512:                         if ($loginhash{'login'}{$item} eq 'notused') {
12513:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
12514:                         } else {
12515:                             my %captchas = &captcha_phrases();
12516:                             if ($captchas{$loginhash{'login'}{$item}}) {
12517:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
12518:                             } else {
12519:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
12520:                             }
12521:                         }
12522:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12523:                     }
12524:                 } elsif ($item eq 'recaptchakeys') {
12525:                     if (ref($loginhash{'login'}) eq 'HASH') {
12526:                         my ($privkey,$pubkey);
12527:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
12528:                             $pubkey = $loginhash{'login'}{$item}{'public'};
12529:                             $privkey = $loginhash{'login'}{$item}{'private'};
12530:                         }
12531:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
12532:                         if (!$pubkey) {
12533:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
12534:                         } else {
12535:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12536:                         }
12537:                         if (!$privkey) {
12538:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
12539:                         } else {
12540:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
12541:                         }
12542:                         $chgtxt .= '</ul>';
12543:                         $resulttext .= '<li>'.$chgtxt.'</li>';
12544:                     }
12545:                 } elsif ($item eq 'recaptchaversion') {
12546:                     if (ref($loginhash{'login'}) eq 'HASH') {
12547:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
12548:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
12549:                                            '</li>';
12550:                         }
12551:                     }
12552:                 } else {
12553:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
12554:                 }
12555:             }
12556:             $resulttext .= $colchgtext.'</ul>';
12557:         } else {
12558:             $resulttext = &mt('No changes made to log-in page settings');
12559:         }
12560:     } else {
12561:         $resulttext = '<span class="LC_error">'.
12562: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12563:     }
12564:     if ($errors) {
12565:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
12566:                        $errors.'</ul>';
12567:     }
12568:     return $resulttext;
12569: }
12570: 
12571: sub check_exempt_addresses {
12572:     my ($iplist) = @_;
12573:     $iplist =~ s/^\s+//;
12574:     $iplist =~ s/\s+$//;
12575:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
12576:     my (@okips,$new);
12577:     foreach my $ip (@poss_ips) {
12578:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
12579:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
12580:                 push(@okips,$ip);
12581:             }
12582:         }
12583:     }
12584:     if (@okips > 0) {
12585:         $new = join(',',@okips);
12586:     } else {
12587:         $new = '';
12588:     }
12589:     return $new;
12590: }
12591: 
12592: sub color_font_choices {
12593:     my %choices =
12594:         &Apache::lonlocal::texthash (
12595:             img => "Header",
12596:             bgs => "Background colors",
12597:             links => "Link colors",
12598:             images => "Images",
12599:             font => "Font color",
12600:             fontmenu => "Font menu",
12601:             pgbg => "Page",
12602:             tabbg => "Header",
12603:             sidebg => "Border",
12604:             link => "Link",
12605:             alink => "Active link",
12606:             vlink => "Visited link",
12607:         );
12608:     return %choices;
12609: }
12610: 
12611: sub modify_ipaccess {
12612:     my ($dom,$lastactref,%domconfig) = @_;
12613:     my (@allpos,%changes,%confhash,$errors,$resulttext);
12614:     my (@items,%deletions,%itemids,@warnings);
12615:     my ($typeorder,$types) = &commblocktype_text();
12616:     if ($env{'form.ipaccess_add'}) {
12617:         my $name = $env{'form.ipaccess_name_add'};
12618:         my ($newid,$error) = &get_ipaccess_id($dom,$name);
12619:         if ($newid) {
12620:             $itemids{'add'} = $newid;
12621:             push(@items,'add');
12622:             $changes{$newid} = 1;
12623:         } else {
12624:             $error = &mt('Failed to acquire unique ID for new IP access control item');
12625:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12626:         }
12627:     }
12628:     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12629:         my @todelete = &Apache::loncommon::get_env_multiple('form.ipaccess_del');
12630:         if (@todelete) {
12631:             map { $deletions{$_} = 1; } @todelete;
12632:         }
12633:         my $maxnum = $env{'form.ipaccess_maxnum'};
12634:         for (my $i=0; $i<$maxnum; $i++) {
12635:             my $itemid = $env{'form.ipaccess_id_'.$i};
12636:             $itemid =~ s/\D+//g;
12637:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12638:                 if ($deletions{$itemid}) {
12639:                     $changes{$itemid} = $domconfig{'ipaccess'}{$itemid}{'name'};
12640:                 } else {
12641:                     push(@items,$i);
12642:                     $itemids{$i} = $itemid;
12643:                 }
12644:             }
12645:         }
12646:     }
12647:     foreach my $idx (@items) {
12648:         my $itemid = $itemids{$idx};
12649:         next unless ($itemid);
12650:         my %current;
12651:         unless ($idx eq 'add') {
12652:             if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12653:                 %current = %{$domconfig{'ipaccess'}{$itemid}};
12654:             }
12655:         }
12656:         my $position = $env{'form.ipaccess_pos_'.$itemid};
12657:         $position =~ s/\D+//g;
12658:         if ($position ne '') {
12659:             $allpos[$position] = $itemid;
12660:         }
12661:         my $name = $env{'form.ipaccess_name_'.$idx};
12662:         $name =~ s/^\s+|\s+$//g;
12663:         $confhash{$itemid}{'name'} = $name;
12664:         my $possrange = $env{'form.ipaccess_range_'.$idx};
12665:         $possrange =~ s/^\s+|\s+$//g;
12666:         unless ($possrange eq '') {
12667:             $possrange =~ s/[\r\n]+/\s/g;
12668:             $possrange =~ s/\s*-\s*/-/g;
12669:             $possrange =~ s/\s+/,/g;
12670:             $possrange =~ s/,+/,/g;
12671:             if ($possrange ne '') {
12672:                 my (@ok,$count);
12673:                 $count = 0; 
12674:                 foreach my $poss (split(/\,/,$possrange)) {
12675:                     $count ++;
12676:                     $poss = &validate_ip_pattern($poss);
12677:                     if ($poss ne '') {
12678:                         push(@ok,$poss);
12679:                     }
12680:                 }
12681:                 my $diff = $count - scalar(@ok);
12682:                 if ($diff) {
12683:                     $errors .= '<li><span class="LC_error">'.
12684:                                &mt('[quant,_1,IP] invalid and excluded from saved value for IP range(s) for [_2]',
12685:                                    $diff,$name).
12686:                                '</span></li>';
12687:                 }
12688:                 if (@ok) {
12689:                     my @cidr_list;
12690:                     foreach my $item (@ok) {
12691:                         @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
12692:                     }
12693:                     $confhash{$itemid}{'ip'} = join(',',@cidr_list);
12694:                 }
12695:             }
12696:         }
12697:         foreach my $field ('name','ip') {
12698:             unless (($idx eq 'add') || ($changes{$itemid})) {
12699:                 if ($current{$field} ne $confhash{$itemid}{$field}) {
12700:                     $changes{$itemid} = 1;
12701:                     last;
12702:                 }
12703:             }
12704:         }
12705:         $confhash{$itemid}{'commblocks'} = {};
12706:          
12707:         my %commblocks;
12708:         map { $commblocks{$_} = 1; } &Apache::loncommon::get_env_multiple('form.ipaccess_block_'.$idx);  
12709:         foreach my $type (@{$typeorder}) {
12710:             if ($commblocks{$type}) {
12711:                 $confhash{$itemid}{'commblocks'}{$type} = 'on';
12712:             }
12713:             unless (($idx eq 'add') || ($changes{$itemid})) {
12714:                 if (ref($current{'commblocks'}) eq 'HASH') {
12715:                     if ($confhash{$itemid}{'commblocks'}{$type} ne $current{'commblocks'}{$type}) {
12716:                         $changes{$itemid} = 1;
12717:                     }
12718:                 } elsif ($confhash{$itemid}{'commblocks'}{$type}) {
12719:                     $changes{$itemid} = 1;
12720:                 }
12721:             }
12722:         }
12723:         $confhash{$itemid}{'courses'} = {};
12724:         my %crsdeletions;
12725:         my @delcrs = &Apache::loncommon::get_env_multiple('form.ipaccess_course_delete_'.$idx);
12726:         if (@delcrs) {
12727:             map { $crsdeletions{$_} = 1; } @delcrs;
12728:         }
12729:         if (ref($current{'courses'}) eq 'HASH') {
12730:             foreach my $cid (sort(keys(%{$current{'courses'}}))) {
12731:                 if ($crsdeletions{$cid}) {
12732:                     $changes{$itemid} = 1;
12733:                 } else {
12734:                     $confhash{$itemid}{'courses'}{$cid} = 1;
12735:                 }
12736:             }
12737:         }
12738:         $env{'form.ipaccess_cnum_'.$idx} =~ s/^\s+|\s+$//g;
12739:         $env{'form.ipaccess_cdom_'.$idx} =~ s/^\s+|\s+$//g;
12740:         if (($env{'form.ipaccess_cnum_'.$idx} =~ /^$match_courseid$/) && 
12741:             ($env{'form.ipaccess_cdom_'.$idx} =~ /^$match_domain$/)) {
12742:             if (&Apache::lonnet::homeserver($env{'form.ipaccess_cnum_'.$idx},
12743:                                             $env{'form.ipaccess_cdom_'.$idx}) eq 'no_host') {
12744:                 $errors .= '<li><span class="LC_error">'.
12745:                            &mt('Invalid courseID [_1] omitted from list of allowed courses',
12746:                                $env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}).
12747:                            '</span></li>';
12748:             } else {
12749:                 $confhash{$itemid}{'courses'}{$env{'form.ipaccess_cdom_'.$idx}.'_'.$env{'form.ipaccess_cnum_'.$idx}} = 1;
12750:                 $changes{$itemid} = 1;
12751:             }
12752:         }
12753:     }
12754:     if (@allpos > 0) {
12755:         my $idx = 0;
12756:         foreach my $itemid (@allpos) {
12757:             if ($itemid ne '') {
12758:                 $confhash{$itemid}{'order'} = $idx;
12759:                 unless ($changes{$itemid}) {
12760:                     if (ref($domconfig{'ipaccess'}) eq 'HASH') {
12761:                         if (ref($domconfig{'ipaccess'}{$itemid}) eq 'HASH') {
12762:                             if ($domconfig{'ipaccess'}{$itemid}{'order'} ne $idx) {
12763:                                 $changes{$itemid} = 1;
12764:                             }
12765:                         }
12766:                     }
12767:                 }
12768:                 $idx ++;
12769:             }
12770:         }
12771:     }
12772:     if (keys(%changes)) {
12773:         my %defaultshash = (
12774:                               ipaccess => \%confhash,
12775:                            );
12776:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
12777:                                                  $dom);
12778:         if ($putresult eq 'ok') {
12779:             my $cachetime = 1800;
12780:             &Apache::lonnet::do_cache_new('ipaccess',$dom,\%confhash,$cachetime);
12781:             if (ref($lastactref) eq 'HASH') {
12782:                 $lastactref->{'ipaccess'} = 1;
12783:             }
12784:             $resulttext = &mt('Changes made:').'<ul>';
12785:             my %bynum;
12786:             foreach my $itemid (sort(keys(%changes))) {
12787:                 if (ref($confhash{$itemid}) eq 'HASH') {
12788:                     my $position = $confhash{$itemid}{'order'};
12789:                     if ($position =~ /^\d+$/) {
12790:                         $bynum{$position} = $itemid;
12791:                     }
12792:                 }
12793:             }
12794:             if (keys(%deletions)) {
12795:                 foreach my $itemid (sort { $a <=> $b } keys(%deletions)) {
12796:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
12797:                 }
12798:             }
12799:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
12800:                 my $itemid = $bynum{$pos};
12801:                 if (ref($confhash{$itemid}) eq 'HASH') {
12802:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'name'}.'</b><ul>';
12803:                     my $position = $pos + 1;
12804:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
12805:                     if ($confhash{$itemid}{'ip'} eq '') {
12806:                         $resulttext .= '<li>'.&mt('No IP Range(s) set').'</li>';
12807:                     } else {
12808:                         $resulttext .= '<li>'.&mt('IP Range(s): [_1]',$confhash{$itemid}{'ip'}).'</li>';
12809:                     }
12810:                     if (keys(%{$confhash{$itemid}{'commblocks'}})) {
12811:                         $resulttext .= '<li>'.&mt('Functionality Blocked: [_1]',
12812:                                                   join(', ', map { $types->{$_}; } sort(keys(%{$confhash{$itemid}{'commblocks'}})))).
12813:                                        '</li>';
12814:                     } else {
12815:                         $resulttext .= '<li>'.&mt('No functionality blocked').'</li>';
12816:                     }
12817:                     if (keys(%{$confhash{$itemid}{'courses'}})) {
12818:                         my @courses;
12819:                         foreach my $cid (sort(keys(%{$confhash{$itemid}{'courses'}}))) {
12820:                             my %courseinfo = &Apache::lonnet::coursedescription($cid,{'one_time' => 1}); 
12821:                             push(@courses,$courseinfo{'description'}.' ('.$cid.')');
12822:                         }
12823:                         $resulttext .= '<li>'.&mt('Courses/Communities allowed').':<ul><li>'.
12824:                                              join('</li><li>',@courses).'</li></ul>';
12825:                     } else {
12826:                         $resulttext .= '<li>'.&mt('No courses allowed').'</li>';
12827:                     }
12828:                     $resulttext .= '</ul></li>';
12829:                 }
12830:             }
12831:             $resulttext .= '</ul>';
12832:         } else {
12833:             $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
12834:         }
12835:     } else {
12836:         $resulttext = &mt('No changes made');
12837:     }
12838:     if ($errors) {
12839:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
12840:                        $errors.'</ul></p>';
12841:     }
12842:     return $resulttext;
12843: }
12844: 
12845: sub get_ipaccess_id {
12846:     my ($domain,$location) = @_;
12847:     # get lock on ipaccess db
12848:     my $lockhash = {
12849:                       lock => $env{'user.name'}.
12850:                               ':'.$env{'user.domain'},
12851:                    };
12852:     my $tries = 0;
12853:     my $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12854:     my ($id,$error);
12855: 
12856:     while (($gotlock ne 'ok') && ($tries<10)) {
12857:         $tries ++;
12858:         sleep (0.1);
12859:         $gotlock = &Apache::lonnet::newput_dom('ipaccess',$lockhash,$domain);
12860:     }
12861:     if ($gotlock eq 'ok') {
12862:         my %currids = &Apache::lonnet::dump_dom('ipaccess',$domain);
12863:         if ($currids{'lock'}) {
12864:             delete($currids{'lock'});
12865:             if (keys(%currids)) {
12866:                 my @curr = sort { $a <=> $b } keys(%currids);
12867:                 if ($curr[-1] =~ /^\d+$/) {
12868:                     $id = 1 + $curr[-1];
12869:                 }
12870:             } else {
12871:                 $id = 1;
12872:             }
12873:             if ($id) {
12874:                 unless (&Apache::lonnet::newput_dom('ipaccess',{ $id => $location },$domain) eq 'ok') {
12875:                     $error = 'nostore';
12876:                 }
12877:             } else {
12878:                 $error = 'nonumber';
12879:             }
12880:         }
12881:         my $dellockoutcome = &Apache::lonnet::del_dom('ipaccess',['lock'],$domain);
12882:     } else {
12883:         $error = 'nolock';
12884:     }
12885:     return ($id,$error);
12886: }
12887: 
12888: sub modify_rolecolors {
12889:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
12890:     my ($resulttext,%rolehash);
12891:     $rolehash{'rolecolors'} = {};
12892:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
12893:         if ($domconfig{'rolecolors'} eq '') {
12894:             $domconfig{'rolecolors'} = {};
12895:         }
12896:     }
12897:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
12898:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
12899:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
12900:                                              $dom);
12901:     if ($putresult eq 'ok') {
12902:         if (keys(%changes) > 0) {
12903:             &Apache::loncommon::devalidate_domconfig_cache($dom);
12904:             if (ref($lastactref) eq 'HASH') {
12905:                 $lastactref->{'domainconfig'} = 1;
12906:             }
12907:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
12908:                                              $rolehash{'rolecolors'});
12909:         } else {
12910:             $resulttext = &mt('No changes made to default color schemes');
12911:         }
12912:     } else {
12913:         $resulttext = '<span class="LC_error">'.
12914: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12915:     }
12916:     if ($errors) {
12917:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12918:                        $errors.'</ul>';
12919:     }
12920:     return $resulttext;
12921: }
12922: 
12923: sub modify_colors {
12924:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
12925:     my (%changes,%choices);
12926:     my @bgs;
12927:     my @links = ('link','alink','vlink');
12928:     my @logintext;
12929:     my @images;
12930:     my $servadm = $r->dir_config('lonAdmEMail');
12931:     my $errors;
12932:     my %defaults;
12933:     foreach my $role (@{$roles}) {
12934:         if ($role eq 'login') {
12935:             %choices = &login_choices();
12936:             @logintext = ('textcol','bgcol');
12937:         } else {
12938:             %choices = &color_font_choices();
12939:         }
12940:         if ($role eq 'login') {
12941:             @images = ('img','logo','domlogo','login');
12942:             @bgs = ('pgbg','mainbg','sidebg');
12943:         } else {
12944:             @images = ('img');
12945:             @bgs = ('pgbg','tabbg','sidebg');
12946:         }
12947:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
12948:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
12949:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
12950:         }
12951:         if ($role eq 'login') {
12952:             foreach my $item (@logintext) {
12953:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12954:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12955:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12956:                 }
12957:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
12958:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12959:                 }
12960:             }
12961:         } else {
12962:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
12963:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
12964:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
12965:             }
12966:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
12967:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
12968:             }
12969:         }
12970:         foreach my $item (@bgs) {
12971:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12972:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12973:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12974:             }
12975:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
12976:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12977:             }
12978:         }
12979:         foreach my $item (@links) {
12980:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
12981:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
12982:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
12983:             }
12984:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
12985:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
12986:             }
12987:         }
12988:         my ($configuserok,$author_ok,$switchserver) = 
12989:             &config_check($dom,$confname,$servadm);
12990:         my ($width,$height) = &thumb_dimensions();
12991:         if (ref($domconfig->{$role}) ne 'HASH') {
12992:             $domconfig->{$role} = {};
12993:         }
12994:         foreach my $img (@images) {
12995:             if ($role eq 'login') {
12996:                 if (($img eq 'img') || ($img eq 'logo')) {  
12997:                     if (defined($env{'form.login_showlogo_'.$img})) {
12998:                         $confhash->{$role}{'showlogo'}{$img} = 1;
12999:                     } else { 
13000:                         $confhash->{$role}{'showlogo'}{$img} = 0;
13001:                     }
13002:                 }
13003:                 if ($env{'form.login_alt_'.$img} ne '') {
13004:                     $confhash->{$role}{'alttext'}{$img} = $env{'form.login_alt_'.$img};
13005:                 }
13006:             }
13007: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
13008: 		 && !defined($domconfig->{$role}{$img})
13009: 		 && !$env{'form.'.$role.'_del_'.$img}
13010: 		 && $env{'form.'.$role.'_import_'.$img}) {
13011: 		# import the old configured image from the .tab setting
13012: 		# if they haven't provided a new one 
13013: 		$domconfig->{$role}{$img} = 
13014: 		    $env{'form.'.$role.'_import_'.$img};
13015: 	    }
13016:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
13017:                 my $error;
13018:                 if ($configuserok eq 'ok') {
13019:                     if ($switchserver) {
13020:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
13021:                     } else {
13022:                         if ($author_ok eq 'ok') {
13023:                             my $modified = [];
13024:                             my ($result,$logourl) = 
13025:                                 &Apache::lonconfigsettings::publishlogo($r,'upload',$role.'_'.$img,
13026:                                                                         $dom,$confname,$img,$width,$height,
13027:                                                                         '',$modified);
13028:                             if ($result eq 'ok') {
13029:                                 $confhash->{$role}{$img} = $logourl;
13030:                                 $changes{$role}{'images'}{$img} = 1;
13031:                                 &update_modify_urls($r,$modified);
13032:                             } else {
13033:                                 $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);
13034:                             }
13035:                         } else {
13036:                             $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);
13037:                         }
13038:                     }
13039:                 } else {
13040:                     $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);
13041:                 }
13042:                 if ($error) {
13043:                     &Apache::lonnet::logthis($error);
13044:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13045:                 }
13046:             } elsif ($domconfig->{$role}{$img} ne '') {
13047:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
13048:                     my $error;
13049:                     if ($configuserok eq 'ok') {
13050: # is confname an author?
13051:                         if ($switchserver eq '') {
13052:                             if ($author_ok eq 'ok') {
13053:                                 my $modified = [];
13054:                                 my ($result,$logourl) = 
13055:                                     &Apache::lonconfigsettings::publishlogo($r,'copy',$domconfig->{$role}{$img},
13056:                                                                             $dom,$confname,$img,$width,$height,
13057:                                                                             '',$modified);
13058:                                 if ($result eq 'ok') {
13059:                                     $confhash->{$role}{$img} = $logourl;
13060: 				    $changes{$role}{'images'}{$img} = 1;
13061:                                     &update_modify_urls($r,$modified);
13062:                                 }
13063:                             }
13064:                         }
13065:                     }
13066:                 }
13067:             }
13068:         }
13069:         if (ref($domconfig) eq 'HASH') {
13070:             if (ref($domconfig->{$role}) eq 'HASH') {
13071:                 foreach my $img (@images) {
13072:                     if ($domconfig->{$role}{$img} ne '') {
13073:                         if ($env{'form.'.$role.'_del_'.$img}) {
13074:                             $confhash->{$role}{$img} = '';
13075:                             $changes{$role}{'images'}{$img} = 1;
13076:                         } else {
13077:                             if ($confhash->{$role}{$img} eq '') {
13078:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
13079:                             }
13080:                         }
13081:                     } else {
13082:                         if ($env{'form.'.$role.'_del_'.$img}) {
13083:                             $confhash->{$role}{$img} = '';
13084:                             $changes{$role}{'images'}{$img} = 1;
13085:                         } 
13086:                     }
13087:                     if ($role eq 'login') {
13088:                         if (($img eq 'logo') || ($img eq 'img')) {
13089:                             if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
13090:                                 if ($confhash->{$role}{'showlogo'}{$img} ne 
13091:                                     $domconfig->{$role}{'showlogo'}{$img}) {
13092:                                     $changes{$role}{'showlogo'}{$img} = 1; 
13093:                                 }
13094:                             } else {
13095:                                 if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13096:                                     $changes{$role}{'showlogo'}{$img} = 1;
13097:                                 }
13098:                             }
13099:                         }
13100:                         if ($img ne 'login') {
13101:                             if (ref($domconfig->{$role}{'alttext'}) eq 'HASH') {
13102:                                 if ($confhash->{$role}{'alttext'}{$img} ne
13103:                                     $domconfig->{$role}{'alttext'}{$img}) {
13104:                                     $changes{$role}{'alttext'}{$img} = 1;
13105:                                 }
13106:                             } else {
13107:                                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13108:                                     $changes{$role}{'alttext'}{$img} = 1;
13109:                                 }
13110:                             }
13111:                         }
13112:                     }
13113:                 }
13114:                 if ($domconfig->{$role}{'font'} ne '') {
13115:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
13116:                         $changes{$role}{'font'} = 1;
13117:                     }
13118:                 } else {
13119:                     if ($confhash->{$role}{'font'}) {
13120:                         $changes{$role}{'font'} = 1;
13121:                     }
13122:                 }
13123:                 if ($role ne 'login') {
13124:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
13125:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
13126:                             $changes{$role}{'fontmenu'} = 1;
13127:                         }
13128:                     } else {
13129:                         if ($confhash->{$role}{'fontmenu'}) {
13130:                             $changes{$role}{'fontmenu'} = 1;
13131:                         }
13132:                     }
13133:                 }
13134:                 foreach my $item (@bgs) {
13135:                     if ($domconfig->{$role}{$item} ne '') {
13136:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13137:                             $changes{$role}{'bgs'}{$item} = 1;
13138:                         } 
13139:                     } else {
13140:                         if ($confhash->{$role}{$item}) {
13141:                             $changes{$role}{'bgs'}{$item} = 1;
13142:                         }
13143:                     }
13144:                 }
13145:                 foreach my $item (@links) {
13146:                     if ($domconfig->{$role}{$item} ne '') {
13147:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13148:                             $changes{$role}{'links'}{$item} = 1;
13149:                         }
13150:                     } else {
13151:                         if ($confhash->{$role}{$item}) {
13152:                             $changes{$role}{'links'}{$item} = 1;
13153:                         }
13154:                     }
13155:                 }
13156:                 foreach my $item (@logintext) {
13157:                     if ($domconfig->{$role}{$item} ne '') {
13158:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
13159:                             $changes{$role}{'logintext'}{$item} = 1;
13160:                         }
13161:                     } else {
13162:                         if ($confhash->{$role}{$item}) {
13163:                             $changes{$role}{'logintext'}{$item} = 1;
13164:                         }
13165:                     }
13166:                 }
13167:             } else {
13168:                 &default_change_checker($role,\@images,\@links,\@bgs,
13169:                                         \@logintext,$confhash,\%changes); 
13170:             }
13171:         } else {
13172:             &default_change_checker($role,\@images,\@links,\@bgs,
13173:                                     \@logintext,$confhash,\%changes); 
13174:         }
13175:     }
13176:     return ($errors,%changes);
13177: }
13178: 
13179: sub config_check {
13180:     my ($dom,$confname,$servadm) = @_;
13181:     my ($configuserok,$author_ok,$switchserver,%currroles);
13182:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
13183:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
13184:                                                    $confname,$servadm);
13185:     if ($configuserok eq 'ok') {
13186:         $switchserver = &check_switchserver($dom,$confname);
13187:         if ($switchserver eq '') {
13188:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
13189:         }
13190:     }
13191:     return ($configuserok,$author_ok,$switchserver);
13192: }
13193: 
13194: sub default_change_checker {
13195:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
13196:     foreach my $item (@{$links}) {
13197:         if ($confhash->{$role}{$item}) {
13198:             $changes->{$role}{'links'}{$item} = 1;
13199:         }
13200:     }
13201:     foreach my $item (@{$bgs}) {
13202:         if ($confhash->{$role}{$item}) {
13203:             $changes->{$role}{'bgs'}{$item} = 1;
13204:         }
13205:     }
13206:     foreach my $item (@{$logintext}) {
13207:         if ($confhash->{$role}{$item}) {
13208:             $changes->{$role}{'logintext'}{$item} = 1;
13209:         }
13210:     }
13211:     foreach my $img (@{$images}) {
13212:         if ($env{'form.'.$role.'_del_'.$img}) {
13213:             $confhash->{$role}{$img} = '';
13214:             $changes->{$role}{'images'}{$img} = 1;
13215:         }
13216:         if ($role eq 'login') {
13217:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
13218:                 $changes->{$role}{'showlogo'}{$img} = 1;
13219:             }
13220:             if (ref($confhash->{$role}{'alttext'}) eq 'HASH') {
13221:                 if ($confhash->{$role}{'alttext'}{$img} ne '') {
13222:                     $changes->{$role}{'alttext'}{$img} = 1;
13223:                 }
13224:             }
13225:         }
13226:     }
13227:     if ($confhash->{$role}{'font'}) {
13228:         $changes->{$role}{'font'} = 1;
13229:     }
13230: }
13231: 
13232: sub display_colorchgs {
13233:     my ($dom,$changes,$roles,$confhash) = @_;
13234:     my (%choices,$resulttext);
13235:     if (!grep(/^login$/,@{$roles})) {
13236:         $resulttext = &mt('Changes made:').'<br />';
13237:     }
13238:     foreach my $role (@{$roles}) {
13239:         if ($role eq 'login') {
13240:             %choices = &login_choices();
13241:         } else {
13242:             %choices = &color_font_choices();
13243:         }
13244:         if (ref($changes->{$role}) eq 'HASH') {
13245:             if ($role ne 'login') {
13246:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
13247:             }
13248:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
13249:                 if ($role ne 'login') {
13250:                     $resulttext .= '<ul>';
13251:                 }
13252:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
13253:                     if ($role ne 'login') {
13254:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
13255:                     }
13256:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
13257:                         if (($role eq 'login') && ($key eq 'showlogo')) {
13258:                             if ($confhash->{$role}{$key}{$item}) {
13259:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
13260:                             } else {
13261:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
13262:                             }
13263:                         } elsif (($role eq 'login') && ($key eq 'alttext')) {
13264:                             if ($confhash->{$role}{$key}{$item} ne '') {
13265:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} set to [_1].",
13266:                                                $confhash->{$role}{$key}{$item}).'</li>';
13267:                             } else {
13268:                                 $resulttext .= '<li>'.&mt("$choices{$key} for $choices{$item} deleted.").'</li>';
13269:                             }
13270:                         } elsif ($confhash->{$role}{$item} eq '') {
13271:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
13272:                         } else {
13273:                             my $newitem = $confhash->{$role}{$item};
13274:                             if ($key eq 'images') {
13275:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
13276:                             }
13277:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
13278:                         }
13279:                     }
13280:                     if ($role ne 'login') {
13281:                         $resulttext .= '</ul></li>';
13282:                     }
13283:                 } else {
13284:                     if ($confhash->{$role}{$key} eq '') {
13285:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
13286:                     } else {
13287:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
13288:                     }
13289:                 }
13290:                 if ($role ne 'login') {
13291:                     $resulttext .= '</ul>';
13292:                 }
13293:             }
13294:         }
13295:     }
13296:     return $resulttext;
13297: }
13298: 
13299: sub thumb_dimensions {
13300:     return ('200','50');
13301: }
13302: 
13303: sub check_dimensions {
13304:     my ($inputfile) = @_;
13305:     my ($fullwidth,$fullheight);
13306:     if ($inputfile =~ m|^[/\w.\-]+$|) {
13307:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
13308:             my $imageinfo = <PIPE>;
13309:             if (!close(PIPE)) {
13310:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
13311:             }
13312:             chomp($imageinfo);
13313:             my ($fullsize) = 
13314:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
13315:             if ($fullsize) {
13316:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
13317:             }
13318:         }
13319:     }
13320:     return ($fullwidth,$fullheight);
13321: }
13322: 
13323: sub check_configuser {
13324:     my ($uhome,$dom,$confname,$servadm) = @_;
13325:     my ($configuserok,%currroles);
13326:     if ($uhome eq 'no_host') {
13327:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
13328:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
13329:         $configuserok = 
13330:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
13331:                              $configpass,'','','','','',undef,$servadm);
13332:     } else {
13333:         $configuserok = 'ok';
13334:         %currroles = 
13335:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
13336:     }
13337:     return ($configuserok,%currroles);
13338: }
13339: 
13340: sub check_authorstatus {
13341:     my ($dom,$confname,%currroles) = @_;
13342:     my $author_ok;
13343:     if (!$currroles{':'.$dom.':au'}) {
13344:         my $start = time;
13345:         my $end = 0;
13346:         $author_ok = 
13347:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
13348:                                         'au',$end,$start,'','','domconfig');
13349:     } else {
13350:         $author_ok = 'ok';
13351:     }
13352:     return $author_ok;
13353: }
13354: 
13355: sub update_modify_urls {
13356:     my ($r,$modified) = @_;
13357:     if ((ref($modified) eq 'ARRAY') && (@{$modified})) {
13358:         push(@{$modified_urls},$modified);
13359:         unless ($registered_cleanup) {
13360:             my $handlers = $r->get_handlers('PerlCleanupHandler');
13361:             $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
13362:             $registered_cleanup=1;
13363:         }
13364:     }
13365: }
13366: 
13367: sub notifysubscribed {
13368:     foreach my $targetsource (@{$modified_urls}){
13369:         next unless (ref($targetsource) eq 'ARRAY');
13370:         my ($target,$source)=@{$targetsource};
13371:         if ($source ne '') {
13372:             if (open(my $logfh,">>",$source.'.log')) {
13373:                 print $logfh "\nCleanup phase: Notifications\n";
13374:                 my @subscribed=&subscribed_hosts($target);
13375:                 foreach my $subhost (@subscribed) {
13376:                     print $logfh "\nNotifying host ".$subhost.':';
13377:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
13378:                     print $logfh $reply;
13379:                 }
13380:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
13381:                 foreach my $subhost (@subscribedmeta) {
13382:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
13383:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
13384:                                                         $subhost);
13385:                     print $logfh $reply;
13386:                 }
13387:                 print $logfh "\n============ Done ============\n";
13388:                 close($logfh);
13389:             }
13390:         }
13391:     }
13392:     return OK;
13393: }
13394: 
13395: sub subscribed_hosts {
13396:     my ($target) = @_;
13397:     my @subscribed;
13398:     if (open(my $fh,"<","$target.subscription")) {
13399:         while (my $subline=<$fh>) {
13400:             if ($subline =~ /^($match_lonid):/) {
13401:                 my $host = $1;
13402:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
13403:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
13404:                         push(@subscribed,$host);
13405:                     }
13406:                 }
13407:             }
13408:         }
13409:     }
13410:     return @subscribed;
13411: }
13412: 
13413: sub check_switchserver {
13414:     my ($dom,$confname) = @_;
13415:     my ($allowed,$switchserver);
13416:     my $home = &Apache::lonnet::homeserver($confname,$dom);
13417:     if ($home eq 'no_host') {
13418:         $home = &Apache::lonnet::domain($dom,'primary');
13419:     }
13420:     my @ids=&Apache::lonnet::current_machine_ids();
13421:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
13422:     if (!$allowed) {
13423: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
13424:     }
13425:     return $switchserver;
13426: }
13427: 
13428: sub modify_quotas {
13429:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13430:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
13431:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
13432:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
13433:         $validationfieldsref);
13434:     if ($action eq 'quotas') {
13435:         $context = 'tools'; 
13436:     } else {
13437:         $context = $action;
13438:     }
13439:     if ($context eq 'requestcourses') {
13440:         @usertools = ('official','unofficial','community','textbook','placement','lti');
13441:         @options =('norequest','approval','validate','autolimit');
13442:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
13443:         %titles = &courserequest_titles();
13444:         $toolregexp = join('|',@usertools);
13445:         %conditions = &courserequest_conditions();
13446:         $confname = $dom.'-domainconfig';
13447:         my $servadm = $r->dir_config('lonAdmEMail');
13448:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13449:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
13450:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
13451:     } elsif ($context eq 'requestauthor') {
13452:         @usertools = ('author');
13453:         %titles = &authorrequest_titles();
13454:     } else {
13455:         @usertools = ('aboutme','blog','webdav','portfolio','timezone');
13456:         %titles = &tool_titles();
13457:     }
13458:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13459:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13460:     foreach my $key (keys(%env)) {
13461:         if ($context eq 'requestcourses') {
13462:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
13463:                 my $item = $1;
13464:                 my $type = $2;
13465:                 if ($type =~ /^limit_(.+)/) {
13466:                     $limithash{$item}{$1} = $env{$key};
13467:                 } else {
13468:                     $confhash{$item}{$type} = $env{$key};
13469:                 }
13470:             }
13471:         } elsif ($context eq 'requestauthor') {
13472:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
13473:                 $confhash{$1} = $env{$key};
13474:             }
13475:         } else {
13476:             if ($key =~ /^form\.quota_(.+)$/) {
13477:                 $confhash{'defaultquota'}{$1} = $env{$key};
13478:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
13479:                 $confhash{'authorquota'}{$1} = $env{$key};
13480:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
13481:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
13482:             }
13483:         }
13484:     }
13485:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13486:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
13487:         @approvalnotify = sort(@approvalnotify);
13488:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
13489:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
13490:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
13491:         foreach my $type (@hasuniquecode) {
13492:             if (grep(/^\Q$type\E$/,@crstypes)) {
13493:                 $confhash{'uniquecode'}{$type} = 1;
13494:             }
13495:         }
13496:         my (%newbook,%allpos);
13497:         if ($context eq 'requestcourses') {
13498:             foreach my $type ('textbooks','templates') {
13499:                 @{$allpos{$type}} = (); 
13500:                 my $invalid;
13501:                 if ($type eq 'textbooks') {
13502:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
13503:                 } else {
13504:                     $invalid = &mt('Invalid LON-CAPA course for template');
13505:                 }
13506:                 if ($env{'form.'.$type.'_addbook'}) {
13507:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
13508:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
13509:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
13510:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
13511:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13512:                         } else {
13513:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
13514:                             my $position = $env{'form.'.$type.'_addbook_pos'};
13515:                             $position =~ s/\D+//g;
13516:                             if ($position ne '') {
13517:                                 $allpos{$type}[$position] = $newbook{$type};
13518:                             }
13519:                         }
13520:                     } else {
13521:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
13522:                     }
13523:                 }
13524:             } 
13525:         }
13526:         if (ref($domconfig{$action}) eq 'HASH') {
13527:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
13528:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
13529:                     $changes{'notify'}{'approval'} = 1;
13530:                 }
13531:             } else {
13532:                 if ($confhash{'notify'}{'approval'}) {
13533:                     $changes{'notify'}{'approval'} = 1;
13534:                 }
13535:             }
13536:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
13537:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
13538:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
13539:                         unless ($confhash{'uniquecode'}{$crstype}) {
13540:                             $changes{'uniquecode'} = 1;
13541:                         }
13542:                     }
13543:                     unless ($changes{'uniquecode'}) {
13544:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
13545:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
13546:                                 $changes{'uniquecode'} = 1;
13547:                             }
13548:                         }
13549:                     }
13550:                } else {
13551:                    $changes{'uniquecode'} = 1;
13552:                }
13553:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
13554:                 $changes{'uniquecode'} = 1;
13555:             }
13556:             if ($context eq 'requestcourses') {
13557:                 foreach my $type ('textbooks','templates') {
13558:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
13559:                         my %deletions;
13560:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
13561:                         if (@todelete) {
13562:                             map { $deletions{$_} = 1; } @todelete;
13563:                         }
13564:                         my %imgdeletions;
13565:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
13566:                         if (@todeleteimages) {
13567:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
13568:                         }
13569:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
13570:                         for (my $i=0; $i<=$maxnum; $i++) {
13571:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
13572:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
13573:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
13574:                                 if ($deletions{$key}) {
13575:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
13576:                                         #FIXME need to obsolete item in RES space
13577:                                     }
13578:                                     next;
13579:                                 } else {
13580:                                     my $newpos = $env{'form.'.$itemid};
13581:                                     $newpos =~ s/\D+//g;
13582:                                     foreach my $item ('subject','title','publisher','author') {
13583:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13584:                                                  ($type eq 'templates'));
13585:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
13586:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
13587:                                             $changes{$type}{$key} = 1;
13588:                                         }
13589:                                     }
13590:                                     $allpos{$type}[$newpos] = $key;
13591:                                 }
13592:                                 if ($imgdeletions{$key}) {
13593:                                     $changes{$type}{$key} = 1;
13594:                                     #FIXME need to obsolete item in RES space
13595:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
13596:                                     my ($cdom,$cnum) = split(/_/,$key);
13597:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13598:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13599:                                     } else {
13600:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
13601:                                                                                       $cdom,$cnum,$type,$configuserok,
13602:                                                                                       $switchserver,$author_ok);
13603:                                         if ($imgurl) {
13604:                                             $confhash{$type}{$key}{'image'} = $imgurl;
13605:                                             $changes{$type}{$key} = 1; 
13606:                                         }
13607:                                         if ($error) {
13608:                                             &Apache::lonnet::logthis($error);
13609:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13610:                                         }
13611:                                     }
13612:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
13613:                                     $confhash{$type}{$key}{'image'} = 
13614:                                         $domconfig{$action}{$type}{$key}{'image'};
13615:                                 }
13616:                             }
13617:                         }
13618:                     }
13619:                 }
13620:             }
13621:         } else {
13622:             if ($confhash{'notify'}{'approval'}) {
13623:                 $changes{'notify'}{'approval'} = 1;
13624:             }
13625:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
13626:                 $changes{'uniquecode'} = 1;
13627:             }
13628:         }
13629:         if ($context eq 'requestcourses') {
13630:             foreach my $type ('textbooks','templates') {
13631:                 if ($newbook{$type}) {
13632:                     $changes{$type}{$newbook{$type}} = 1;
13633:                     foreach my $item ('subject','title','publisher','author') {
13634:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
13635:                                  ($type eq 'template'));
13636:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
13637:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
13638:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
13639:                         }
13640:                     }
13641:                     if ($type eq 'textbooks') {
13642:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
13643:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
13644:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
13645:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
13646:                             } else {
13647:                                 my ($imageurl,$error) =
13648:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
13649:                                                             $configuserok,$switchserver,$author_ok);
13650:                                 if ($imageurl) {
13651:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
13652:                                 }
13653:                                 if ($error) {
13654:                                     &Apache::lonnet::logthis($error);
13655:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13656:                                 }
13657:                             }
13658:                         }
13659:                     }
13660:                 }
13661:                 if (@{$allpos{$type}} > 0) {
13662:                     my $idx = 0;
13663:                     foreach my $item (@{$allpos{$type}}) {
13664:                         if ($item ne '') {
13665:                             $confhash{$type}{$item}{'order'} = $idx;
13666:                             if (ref($domconfig{$action}) eq 'HASH') {
13667:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
13668:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
13669:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
13670:                                             $changes{$type}{$item} = 1;
13671:                                         }
13672:                                     }
13673:                                 }
13674:                             }
13675:                             $idx ++;
13676:                         }
13677:                     }
13678:                 }
13679:             }
13680:             if (ref($validationitemsref) eq 'ARRAY') {
13681:                 foreach my $item (@{$validationitemsref}) {
13682:                     if ($item eq 'fields') {
13683:                         my @changed;
13684:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
13685:                         if (@{$confhash{'validation'}{$item}} > 0) {
13686:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
13687:                         }
13688:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13689:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13690:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
13691:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
13692:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
13693:                                 } else {
13694:                                     @changed = @{$confhash{'validation'}{$item}};
13695:                                 }
13696:                             } else {
13697:                                 @changed = @{$confhash{'validation'}{$item}};
13698:                             }
13699:                         } else {
13700:                             @changed = @{$confhash{'validation'}{$item}};
13701:                         }
13702:                         if (@changed) {
13703:                             if ($confhash{'validation'}{$item}) {
13704:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
13705:                             } else {
13706:                                 $changes{'validation'}{$item} = &mt('None');
13707:                             }
13708:                         }
13709:                     } else {
13710:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
13711:                         if ($item eq 'markup') {
13712:                             if ($env{'form.requestcourses_validation_'.$item}) {
13713:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13714:                             }
13715:                         }
13716:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13717:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13718:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
13719:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13720:                                 }
13721:                             } else {
13722:                                 if ($confhash{'validation'}{$item} ne '') {
13723:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13724:                                 }
13725:                             }
13726:                         } else {
13727:                             if ($confhash{'validation'}{$item} ne '') {
13728:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
13729:                             }
13730:                         }
13731:                     }
13732:                 }
13733:             }
13734:             if ($env{'form.validationdc'}) {
13735:                 my $newval = $env{'form.validationdc'};
13736:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
13737:                 if (exists($domcoords{$newval})) {
13738:                     $confhash{'validation'}{'dc'} = $newval;
13739:                 }
13740:             }
13741:             if (ref($confhash{'validation'}) eq 'HASH') {
13742:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13743:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13744:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13745:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
13746:                                 if ($confhash{'validation'}{'dc'} eq '') {
13747:                                     $changes{'validation'}{'dc'} = &mt('None');
13748:                                 } else {
13749:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13750:                                 }
13751:                             }
13752:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
13753:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13754:                         }
13755:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
13756:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13757:                     }
13758:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
13759:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
13760:                 }
13761:             } else {
13762:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
13763:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
13764:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
13765:                             $changes{'validation'}{'dc'} = &mt('None');
13766:                         }
13767:                     }
13768:                 }
13769:             }
13770:         }
13771:     } else {
13772:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
13773:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
13774:     }
13775:     foreach my $item (@usertools) {
13776:         foreach my $type (@{$types},'default','_LC_adv') {
13777:             my $unset; 
13778:             if ($context eq 'requestcourses') {
13779:                 $unset = '0';
13780:                 if ($type eq '_LC_adv') {
13781:                     $unset = '';
13782:                 }
13783:                 if ($confhash{$item}{$type} eq 'autolimit') {
13784:                     $confhash{$item}{$type} .= '=';
13785:                     unless ($limithash{$item}{$type} =~ /\D/) {
13786:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
13787:                     }
13788:                 }
13789:             } elsif ($context eq 'requestauthor') {
13790:                 $unset = '0';
13791:                 if ($type eq '_LC_adv') {
13792:                     $unset = '';
13793:                 }
13794:             } else {
13795:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
13796:                     $confhash{$item}{$type} = 1;
13797:                 } else {
13798:                     $confhash{$item}{$type} = 0;
13799:                 }
13800:             }
13801:             if (ref($domconfig{$action}) eq 'HASH') {
13802:                 if ($action eq 'requestauthor') {
13803:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
13804:                         $changes{$type} = 1;
13805:                     }
13806:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
13807:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
13808:                         $changes{$item}{$type} = 1;
13809:                     }
13810:                 } else {
13811:                     if ($context eq 'requestcourses') {
13812:                         if ($confhash{$item}{$type} ne $unset) {
13813:                             $changes{$item}{$type} = 1;
13814:                         }
13815:                     } else {
13816:                         if (!$confhash{$item}{$type}) {
13817:                             $changes{$item}{$type} = 1;
13818:                         }
13819:                     }
13820:                 }
13821:             } else {
13822:                 if ($context eq 'requestcourses') {
13823:                     if ($confhash{$item}{$type} ne $unset) {
13824:                         $changes{$item}{$type} = 1;
13825:                     }
13826:                 } elsif ($context eq 'requestauthor') {
13827:                     if ($confhash{$type} ne $unset) {
13828:                         $changes{$type} = 1;
13829:                     }
13830:                 } else {
13831:                     if (!$confhash{$item}{$type}) {
13832:                         $changes{$item}{$type} = 1;
13833:                     }
13834:                 }
13835:             }
13836:         }
13837:     }
13838:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
13839:         if (ref($domconfig{'quotas'}) eq 'HASH') {
13840:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13841:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
13842:                     if (exists($confhash{'defaultquota'}{$key})) {
13843:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
13844:                             $changes{'defaultquota'}{$key} = 1;
13845:                         }
13846:                     } else {
13847:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
13848:                     }
13849:                 }
13850:             } else {
13851:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
13852:                     if (exists($confhash{'defaultquota'}{$key})) {
13853:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
13854:                             $changes{'defaultquota'}{$key} = 1;
13855:                         }
13856:                     } else {
13857:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
13858:                     }
13859:                 }
13860:             }
13861:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13862:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
13863:                     if (exists($confhash{'authorquota'}{$key})) {
13864:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
13865:                             $changes{'authorquota'}{$key} = 1;
13866:                         }
13867:                     } else {
13868:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
13869:                     }
13870:                 }
13871:             }
13872:         }
13873:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
13874:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
13875:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13876:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
13877:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
13878:                             $changes{'defaultquota'}{$key} = 1;
13879:                         }
13880:                     } else {
13881:                         if (!exists($domconfig{'quotas'}{$key})) {
13882:                             $changes{'defaultquota'}{$key} = 1;
13883:                         }
13884:                     }
13885:                 } else {
13886:                     $changes{'defaultquota'}{$key} = 1;
13887:                 }
13888:             }
13889:         }
13890:         if (ref($confhash{'authorquota'}) eq 'HASH') {
13891:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
13892:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
13893:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
13894:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
13895:                             $changes{'authorquota'}{$key} = 1;
13896:                         }
13897:                     } else {
13898:                         $changes{'authorquota'}{$key} = 1;
13899:                     }
13900:                 } else {
13901:                     $changes{'authorquota'}{$key} = 1;
13902:                 }
13903:             }
13904:         }
13905:     }
13906: 
13907:     if ($context eq 'requestauthor') {
13908:         $domdefaults{'requestauthor'} = \%confhash;
13909:     } else {
13910:         foreach my $key (keys(%confhash)) {
13911:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
13912:                 $domdefaults{$key} = $confhash{$key};
13913:             }
13914:         }
13915:     }
13916: 
13917:     my %quotahash = (
13918:                       $action => { %confhash }
13919:                     );
13920:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
13921:                                              $dom);
13922:     if ($putresult eq 'ok') {
13923:         if (keys(%changes) > 0) {
13924:             my $cachetime = 24*60*60;
13925:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13926:             if (ref($lastactref) eq 'HASH') {
13927:                 $lastactref->{'domdefaults'} = 1;
13928:             }
13929:             $resulttext = &mt('Changes made:').'<ul>';
13930:             unless (($context eq 'requestcourses') ||
13931:                     ($context eq 'requestauthor')) {
13932:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
13933:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
13934:                     foreach my $type (@{$types},'default') {
13935:                         if (defined($changes{'defaultquota'}{$type})) {
13936:                             my $typetitle = $usertypes->{$type};
13937:                             if ($type eq 'default') {
13938:                                 $typetitle = $othertitle;
13939:                             }
13940:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
13941:                         }
13942:                     }
13943:                     $resulttext .= '</ul></li>';
13944:                 }
13945:                 if (ref($changes{'authorquota'}) eq 'HASH') {
13946:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
13947:                     foreach my $type (@{$types},'default') {
13948:                         if (defined($changes{'authorquota'}{$type})) {
13949:                             my $typetitle = $usertypes->{$type};
13950:                             if ($type eq 'default') {
13951:                                 $typetitle = $othertitle;
13952:                             }
13953:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
13954:                         }
13955:                     }
13956:                     $resulttext .= '</ul></li>';
13957:                 }
13958:             }
13959:             my %newenv;
13960:             foreach my $item (@usertools) {
13961:                 my (%haschgs,%inconf);
13962:                 if ($context eq 'requestauthor') {
13963:                     %haschgs = %changes;
13964:                     %inconf = %confhash;
13965:                 } else {
13966:                     if (ref($changes{$item}) eq 'HASH') {
13967:                         %haschgs = %{$changes{$item}};
13968:                     }
13969:                     if (ref($confhash{$item}) eq 'HASH') {
13970:                         %inconf = %{$confhash{$item}};
13971:                     }
13972:                 }
13973:                 if (keys(%haschgs) > 0) {
13974:                     my $newacc = 
13975:                         &Apache::lonnet::usertools_access($env{'user.name'},
13976:                                                           $env{'user.domain'},
13977:                                                           $item,'reload',$context);
13978:                     if (($context eq 'requestcourses') ||
13979:                         ($context eq 'requestauthor')) {
13980:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
13981:                             $newenv{'environment.canrequest.'.$item} = $newacc;
13982:                         }
13983:                     } else {
13984:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
13985:                             $newenv{'environment.availabletools.'.$item} = $newacc;
13986:                         }
13987:                     }
13988:                     unless ($context eq 'requestauthor') {
13989:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
13990:                     }
13991:                     foreach my $type (@{$types},'default','_LC_adv') {
13992:                         if ($haschgs{$type}) {
13993:                             my $typetitle = $usertypes->{$type};
13994:                             if ($type eq 'default') {
13995:                                 $typetitle = $othertitle;
13996:                             } elsif ($type eq '_LC_adv') {
13997:                                 $typetitle = 'LON-CAPA Advanced Users'; 
13998:                             }
13999:                             if ($inconf{$type}) {
14000:                                 if ($context eq 'requestcourses') {
14001:                                     my $cond;
14002:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
14003:                                         if ($1 eq '') {
14004:                                             $cond = &mt('(Automatic processing of any request).');
14005:                                         } else {
14006:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
14007:                                         }
14008:                                     } else { 
14009:                                         $cond = $conditions{$inconf{$type}};
14010:                                     }
14011:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
14012:                                 } elsif ($context eq 'requestauthor') {
14013:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
14014:                                                              $titles{$inconf{$type}},$typetitle);
14015: 
14016:                                 } else {
14017:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
14018:                                 }
14019:                             } else {
14020:                                 if ($type eq '_LC_adv') {
14021:                                     if ($inconf{$type} eq '0') {
14022:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14023:                                     } else { 
14024:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
14025:                                     }
14026:                                 } else {
14027:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
14028:                                 }
14029:                             }
14030:                         }
14031:                     }
14032:                     unless ($context eq 'requestauthor') {
14033:                         $resulttext .= '</ul></li>';
14034:                     }
14035:                 }
14036:             }
14037:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
14038:                 if (ref($changes{'notify'}) eq 'HASH') {
14039:                     if ($changes{'notify'}{'approval'}) {
14040:                         if (ref($confhash{'notify'}) eq 'HASH') {
14041:                             if ($confhash{'notify'}{'approval'}) {
14042:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
14043:                             } else {
14044:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
14045:                             }
14046:                         }
14047:                     }
14048:                 }
14049:             }
14050:             if ($action eq 'requestcourses') {
14051:                 my @offon = ('off','on');
14052:                 if ($changes{'uniquecode'}) {
14053:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
14054:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
14055:                         $resulttext .= '<li>'.
14056:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
14057:                                        '</li>';
14058:                     } else {
14059:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
14060:                                        '</li>';
14061:                     }
14062:                 }
14063:                 foreach my $type ('textbooks','templates') {
14064:                     if (ref($changes{$type}) eq 'HASH') {
14065:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
14066:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
14067:                             my %coursehash = &Apache::lonnet::coursedescription($key);
14068:                             my $coursetitle = $coursehash{'description'};
14069:                             my $position = $confhash{$type}{$key}{'order'} + 1;
14070:                             $resulttext .= '<li>';
14071:                             foreach my $item ('subject','title','publisher','author') {
14072:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
14073:                                          ($type eq 'templates'));
14074:                                 my $name = $item.':';
14075:                                 $name =~ s/^(\w)/\U$1/;
14076:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
14077:                             }
14078:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
14079:                             if ($type eq 'textbooks') {
14080:                                 if ($confhash{$type}{$key}{'image'}) {
14081:                                     $resulttext .= ' '.&mt('Image: [_1]',
14082:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
14083:                                                    ' alt="Textbook cover" />').'<br />';
14084:                                 }
14085:                             }
14086:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
14087:                         }
14088:                         $resulttext .= '</ul></li>';
14089:                     }
14090:                 }
14091:                 if (ref($changes{'validation'}) eq 'HASH') {
14092:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
14093:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
14094:                         foreach my $item (@{$validationitemsref}) {
14095:                             if (exists($changes{'validation'}{$item})) {
14096:                                 if ($item eq 'markup') {
14097:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14098:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
14099:                                 } else {
14100:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
14101:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
14102:                                 }
14103:                             }
14104:                         }
14105:                         if (exists($changes{'validation'}{'dc'})) {
14106:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
14107:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
14108:                         }
14109:                     }
14110:                 }
14111:             }
14112:             $resulttext .= '</ul>';
14113:             if (keys(%newenv)) {
14114:                 &Apache::lonnet::appenv(\%newenv);
14115:             }
14116:         } else {
14117:             if ($context eq 'requestcourses') {
14118:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
14119:             } elsif ($context eq 'requestauthor') {
14120:                 $resulttext = &mt('No changes made to rights to request author space.');
14121:             } else {
14122:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
14123:             }
14124:         }
14125:     } else {
14126:         $resulttext = '<span class="LC_error">'.
14127: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14128:     }
14129:     if ($errors) {
14130:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
14131:                        '<ul>'.$errors.'</ul></p>';
14132:     }
14133:     return $resulttext;
14134: }
14135: 
14136: sub process_textbook_image {
14137:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
14138:     my $filename = $env{'form.'.$caller.'.filename'};
14139:     my ($error,$url);
14140:     my ($width,$height) = (50,50);
14141:     if ($configuserok eq 'ok') {
14142:         if ($switchserver) {
14143:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
14144:                          $switchserver);
14145:         } elsif ($author_ok eq 'ok') {
14146:             my $modified = [];
14147:             my ($result,$imageurl) =
14148:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14149:                                                         "$type/$cdom/$cnum/cover",$width,$height,
14150:                                                         '',$modified);
14151:             if ($result eq 'ok') {
14152:                 $url = $imageurl;
14153:                 &update_modify_urls($r,$modified);
14154:             } else {
14155:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
14156:             }
14157:         } else {
14158:             $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);
14159:         }
14160:     } else {
14161:         $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);
14162:     }
14163:     return ($url,$error);
14164: }
14165: 
14166: sub modify_ltitools {
14167:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14168:     my (%currtoolsec,%secchanges,%newtoolsec,%newkeyset);
14169:     &fetch_secrets($dom,'toolsec',\%domconfig,\%currtoolsec,\%secchanges,\%newtoolsec,\%newkeyset);
14170: 
14171:     my $confname = $dom.'-domainconfig';
14172:     my $servadm = $r->dir_config('lonAdmEMail');
14173:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14174: 
14175:     my ($resulttext,$ltitoolsoutput,$is_home,$errors,%ltitoolschg,%newtoolsenc,%newltitools);
14176:     my $toolserror =
14177:         &Apache::courseprefs::process_ltitools($r,$dom,$confname,$domconfig{'ltitools'},\%ltitoolschg,'domain',
14178:                                                $lastactref,$configuserok,$switchserver,$author_ok);
14179: 
14180:     my $home = &Apache::lonnet::domain($dom,'primary');
14181:     unless (($home eq 'no_host') || ($home eq '')) {
14182:         my @ids=&Apache::lonnet::current_machine_ids();
14183:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; last; } }
14184:     }
14185: 
14186:     if (keys(%ltitoolschg)) {
14187:         foreach my $id (keys(%ltitoolschg)) {
14188:             if (ref($ltitoolschg{$id}) eq 'HASH') {
14189:                 foreach my $inner (keys(%{$ltitoolschg{$id}})) {
14190:                     if (($inner eq 'secret') || ($inner eq 'key')) {
14191:                         if ($is_home) {
14192:                             $newtoolsenc{$id}{$inner} = $ltitoolschg{$id}{$inner};
14193:                         }
14194:                     }
14195:                 }
14196:             }
14197:         }
14198:         $ltitoolsoutput = &Apache::courseprefs::store_ltitools($dom,'','domain',\%ltitoolschg,$domconfig{'ltitools'});
14199:         if (keys(%ltitoolschg)) {
14200:             %newltitools = %ltitoolschg;
14201:         }
14202:     }
14203:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
14204:         foreach my $id (%{$domconfig{'ltitools'}}) {
14205:             next if ($id !~ /^\d+$/);
14206:             unless (exists($ltitoolschg{$id})) {
14207:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
14208:                     foreach my $inner (keys(%{$domconfig{'ltitools'}{$id}})) {
14209:                         if (($inner eq 'secret') || ($inner eq 'key')) {
14210:                             if ($is_home) {
14211:                                 $newtoolsenc{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14212:                             }
14213:                         } else {
14214:                             $newltitools{$id}{$inner} = $domconfig{'ltitools'}{$id}{$inner};
14215:                         }
14216:                     }
14217:                 } else {
14218:                     $newltitools{$id} = $domconfig{'ltitools'}{$id};
14219:                 }
14220:             }
14221:         }
14222:     }
14223:     if ($toolserror) {
14224:         $errors = '<li>'.$toolserror.'</li>';
14225:     }
14226:     if ((keys(%ltitoolschg) == 0) && (keys(%secchanges) == 0)) {
14227:         $resulttext = &mt('No changes made.');
14228:         if ($errors) {
14229:             $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
14230:                                  $errors.'</ul>';
14231:         }
14232:         return $resulttext;
14233:     }
14234:     my %ltitoolshash = (
14235:                           $action => { %newltitools }
14236:                        );
14237:     if (keys(%secchanges)) {
14238:         $ltitoolshash{'toolsec'} = \%newtoolsec;
14239:     }
14240:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,$dom);
14241:     if ($putresult eq 'ok') {
14242:         my %keystore;
14243:         if ($is_home) {
14244:             my %toolsenchash = (
14245:                                    $action => { %newtoolsenc }
14246:                                );
14247:             &Apache::lonnet::put_dom('encconfig',\%toolsenchash,$dom,undef,1);
14248:             &store_security($dom,'ltitools',\%secchanges,\%newkeyset,\%keystore,$lastactref);
14249:         }
14250:         $resulttext = &mt('Changes made:').'<ul>';
14251:         if (keys(%secchanges) > 0) {
14252:             $resulttext .= &lti_security_results('ltitools',\%secchanges,\%newtoolsec,\%newkeyset,\%keystore);
14253:         }
14254:         if (keys(%ltitoolschg) > 0) {
14255:             $resulttext .= $ltitoolsoutput;
14256:         }
14257:     } else {
14258:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14259:     }
14260:     if ($errors) {
14261:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
14262:                        $errors.'</ul></p>';
14263:     }
14264:     return $resulttext;
14265: }
14266: 
14267: sub fetch_secrets {
14268:     my ($dom,$context,$domconfig,$currsec,$secchanges,$newsec,$newkeyset) = @_;
14269:     my %keyset;
14270:     %{$currsec} = ();
14271:     $newsec->{'private'}{'keys'} = [];
14272:     $newsec->{'encrypt'} = {};
14273:     $newsec->{'rules'} = {};
14274:     if ($context eq 'ltisec') {
14275:         $newsec->{'linkprot'} = {};
14276:     }
14277:     if (ref($domconfig->{$context}) eq 'HASH') {
14278:         %{$currsec} = %{$domconfig->{$context}};
14279:         if ($context eq 'ltisec') {
14280:             if (ref($currsec->{'linkprot'}) eq 'HASH') {
14281:                 foreach my $id (keys(%{$currsec->{'linkprot'}})) {
14282:                     unless ($id =~ /^\d+$/) {
14283:                         delete($currsec->{'linkprot'}{$id});
14284:                     }
14285:                 }
14286:             }
14287:         }
14288:         if (ref($currsec->{'private'}) eq 'HASH') {
14289:             if (ref($currsec->{'private'}{'keys'}) eq 'ARRAY') {
14290:                 $newsec->{'private'}{'keys'} = $currsec->{'private'}{'keys'};
14291:                 map { $keyset{$_} = 1; } @{$currsec->{'private'}{'keys'}};
14292:             }
14293:         }
14294:     }
14295:     my @items= ('crs','dom');
14296:     if ($context eq 'ltisec') {
14297:         push(@items,'consumers');
14298:     }
14299:     foreach my $item (@items) {
14300:         my $formelement;
14301:         if (($context eq 'toolsec') || ($item eq 'consumers')) {
14302:             $formelement = 'form.'.$context.'_'.$item;
14303:         } else {
14304:             $formelement = 'form.'.$context.'_'.$item.'linkprot';
14305:         }
14306:         if ($env{$formelement}) {
14307:             $newsec->{'encrypt'}{$item} = 1;
14308:             if (ref($currsec->{'encrypt'}) eq 'HASH') {
14309:                 unless ($currsec->{'encrypt'}{$item}) {
14310:                     $secchanges->{'encrypt'} = 1;
14311:                 }
14312:             } else {
14313:                 $secchanges->{'encrypt'} = 1;
14314:             }
14315:         } elsif (ref($currsec->{'encrypt'}) eq 'HASH') {
14316:             if ($currsec->{'encrypt'}{$item}) {
14317:                 $secchanges->{'encrypt'} = 1;
14318:             }
14319:         }
14320:     }
14321:     my $secrets;
14322:     if ($context eq 'ltisec') {
14323:         $secrets = 'ltisecrets';
14324:     } else {
14325:         $secrets = 'toolsecrets';
14326:     }
14327:     unless (exists($currsec->{'rules'})) {
14328:         $currsec->{'rules'} = {};
14329:     }
14330:     &password_rule_changes($secrets,$newsec->{'rules'},$currsec->{'rules'},$secchanges);
14331: 
14332:     my @ids=&Apache::lonnet::current_machine_ids();
14333:     my %servers = &Apache::lonnet::get_servers($dom,'library');
14334: 
14335:     foreach my $hostid (keys(%servers)) {
14336:         if (($hostid ne '') && (grep(/^\Q$hostid\E$/,@ids))) {
14337:             my $newkey;
14338:             my $keyitem = 'form.'.$context.'_privkey_'.$hostid;
14339:             if (exists($env{$keyitem})) {
14340:                 $env{$keyitem} =~ s/(`)/'/g;
14341:                 if ($keyset{$hostid}) {
14342:                     if ($env{'form.'.$context.'_changeprivkey_'.$hostid}) {
14343:                         if ($env{$keyitem} ne '') {
14344:                             $secchanges->{'private'} = 1;
14345:                             $newkeyset->{$hostid} = $env{$keyitem};
14346:                         }
14347:                     }
14348:                 } elsif ($env{$keyitem} ne '') {
14349:                     unless (grep(/^\Q$hostid\E$/,@{$newsec->{'private'}{'keys'}})) {
14350:                         push(@{$newsec->{'private'}{'keys'}},$hostid);
14351:                     }
14352:                     $secchanges->{'private'} = 1;
14353:                     $newkeyset->{$hostid} = $env{$keyitem};
14354:                 }
14355:             }
14356:         }
14357:     }
14358: }
14359: 
14360: sub store_security {
14361:     my ($dom,$context,$secchanges,$newkeyset,$keystore,$lastactref) = @_;
14362:     return unless ((ref($secchanges) eq 'HASH') && (ref($newkeyset) eq 'HASH') &&
14363:                    (ref($keystore) eq 'HASH'));
14364:     if (keys(%{$secchanges})) {
14365:         if ($secchanges->{'private'}) {
14366:             my $who = &escape($env{'user.name'}.':'.$env{'user.domain'});
14367:             foreach my $hostid (keys(%{$newkeyset})) {
14368:                 my $storehash = {
14369:                                    key => $newkeyset->{$hostid},
14370:                                    who => $env{'user.name'}.':'.$env{'user.domain'},
14371:                                 };
14372:                 $keystore->{$hostid} = &Apache::lonnet::store_dom($storehash,$context,'private',
14373:                                                                   $dom,$hostid);
14374:             }
14375:         }
14376:         if (ref($lastactref) eq 'HASH') {
14377:             if (($secchanges->{'encrypt'}) || ($secchanges->{'private'})) {
14378:                 $lastactref->{'domdefaults'} = 1;
14379:             }
14380:         }
14381:     }
14382: }
14383: 
14384: sub lti_security_results {
14385:     my ($context,$secchanges,$newsec,$newkeyset,$keystore) = @_;
14386:     my $output;
14387:     foreach my $item (keys(%{$secchanges})) {
14388:         if ($item eq 'encrypt') {
14389:             my %encrypted;
14390:             if ($context eq 'lti') {
14391:                 %encrypted = (
14392:                               crs  => {
14393:                                         on => &mt('Encryption of stored link protection secrets defined in courses enabled'),
14394:                                         off => &mt('Encryption of stored link protection secrets defined in courses disabled'),
14395:                                       },
14396:                               dom => {
14397:                                        on => &mt('Encryption of stored link protection secrets defined in domain enabled'),
14398:                                        off => &mt('Encryption of stored link protection secrets defined in domain disabled'),
14399:                                      },
14400:                               consumers => {
14401:                                              on => &mt('Encryption of stored consumer secrets defined in domain enabled'),
14402:                                              off => &mt('Encryption of stored consumer secrets defined in domain disabled'),
14403:                                            },
14404:                              );
14405:             } else {
14406:                 %encrypted = (
14407:                               crs  => {
14408:                                         on => &mt('Encryption of stored external tool secrets defined in courses enabled'),
14409:                                         off => &mt('Encryption of stored external tool secrets defined in courses disabled'),
14410:                                       },
14411:                               dom => {
14412:                                        on => &mt('Encryption of stored external tool secrets defined in domain enabled'),
14413:                                        off => &mt('Encryption of stored external tool secrets defined in domain disabled'),
14414:                                      },
14415:                              );
14416: 
14417:             }
14418:             my @types= ('crs','dom');
14419:             if ($context eq 'lti') {
14420:                 push(@types,'consumers');
14421:             }
14422:             foreach my $type (@types) {
14423:                 my $shown = $encrypted{$type}{'off'};
14424:                 if (ref($newsec->{$item}) eq 'HASH') {
14425:                     if ($newsec->{$item}{$type}) {
14426:                         $shown = $encrypted{$type}{'on'};
14427:                     }
14428:                 }
14429:                 $output .= '<li>'.$shown.'</li>';
14430:             }
14431:         } elsif ($item eq 'rules') {
14432:             my %titles = &Apache::lonlocal::texthash(
14433:                                       min   => 'Minimum password length',
14434:                                       max   => 'Maximum password length',
14435:                                       chars => 'Required characters',
14436:                          );
14437:             foreach my $rule ('min','max') {
14438:                 if ($newsec->{rules}{$rule} eq '') {
14439:                     if ($rule eq 'min') {
14440:                         $output .= '<li>'.&mt('[_1] not set.',$titles{$rule});
14441:                                    ' '.&mt('Default of [_1] will be used',
14442:                                            $Apache::lonnet::passwdmin).'</li>';
14443:                     } else {
14444:                         $output .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
14445:                     }
14446:                 } else {
14447:                     $output .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$newsec->{rules}{$rule}).'</li>';
14448:                 }
14449:             }
14450:             if (ref($newsec->{'rules'}{'chars'}) eq 'ARRAY') {
14451:                 if (@{$newsec->{'rules'}{'chars'}} > 0) {
14452:                     my %rulenames = &Apache::lonlocal::texthash(
14453:                                              uc => 'At least one upper case letter',
14454:                                              lc => 'At least one lower case letter',
14455:                                              num => 'At least one number',
14456:                                              spec => 'At least one non-alphanumeric',
14457:                                     );
14458:                     my $needed = '<ul><li>'.
14459:                                  join('</li><li>',map {$rulenames{$_} } @{$newsec->{'rules'}{'chars'}}).
14460:                                  '</li></ul>';
14461:                     $output .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
14462:                 } else {
14463:                     $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
14464:                 }
14465:             } else {
14466:                 $output .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
14467:             }
14468:         } elsif ($item eq 'private') {
14469:             if (keys(%{$newkeyset})) {
14470:                 foreach my $hostid (sort(keys(%{$newkeyset}))) {
14471:                     if ($keystore->{$hostid} eq 'ok') {
14472:                         $output .= '<li>'.&mt('Encryption key for storage of shared secrets saved for [_1]',$hostid).'</li>';
14473:                     }
14474:                 }
14475:             }
14476:         } elsif ($item eq 'linkprot') {
14477:             next;
14478:         }
14479:     }
14480:     return $output;
14481: }
14482: 
14483: sub modify_proctoring {
14484:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
14485:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14486:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
14487:     my $confname = $dom.'-domainconfig';
14488:     my $servadm = $r->dir_config('lonAdmEMail');
14489:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
14490:     my %providernames = &proctoring_providernames();
14491:     my $maxnum = scalar(keys(%providernames));
14492: 
14493:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
14494:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
14495:     if (ref($requref) eq 'HASH') {
14496:         %requserfields = %{$requref};
14497:     }
14498:     if (ref($opturef) eq 'HASH') {
14499:         %optuserfields = %{$opturef};
14500:     }
14501:     if (ref($defref) eq 'HASH') {
14502:         %defaults = %{$defref};
14503:     }
14504:     if (ref($extref) eq 'HASH') {
14505:         %extended = %{$extref};
14506:     }
14507:     if (ref($crsref) eq 'HASH') {
14508:         %crsconf = %{$crsref};
14509:     }
14510:     if (ref($rolesref) eq 'ARRAY') {
14511:         @courseroles = @{$rolesref};
14512:     }
14513:     if (ref($ltiref) eq 'ARRAY') {
14514:         @ltiroles = @{$ltiref};
14515:     }
14516: 
14517:     if (ref($domconfig{$action}) eq 'HASH') {
14518:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
14519:         if (@todeleteimages) {
14520:             map { $imgdeletions{$_} = 1; } @todeleteimages;
14521:         }
14522:     }
14523:     my %customadds;
14524:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
14525:     if (@newcustom) {
14526:         map { $customadds{$_} = 1; } @newcustom;
14527:     }
14528:     foreach my $provider (sort(keys(%providernames))) {
14529:         $confhash{$provider} = {};
14530:         my $pos = $env{'form.proctoring_pos_'.$provider};
14531:         $pos =~ s/\D+//g;
14532:         $allpos[$pos] = $provider;
14533:         my (%current,%currentenc);
14534:         my $showroles = 0;
14535:         if (ref($domconfig{$action}) eq 'HASH') {
14536:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14537:                 %current = %{$domconfig{$action}{$provider}};
14538:                 foreach my $item ('key','secret') { 
14539:                     $currentenc{$item} = $current{$item};
14540:                     delete($current{$item});
14541:                 }
14542:             }
14543:         }
14544:         if ($env{'form.proctoring_available_'.$provider}) {
14545:             $confhash{$provider}{'available'} = 1;
14546:             unless ($current{'available'}) {
14547:                 $changes{$provider} = 1;
14548:             }
14549:         } else {
14550:             %{$confhash{$provider}} = %current;
14551:             %{$encconfhash{$provider}} = %currentenc;
14552:             $confhash{$provider}{'available'} = 0;
14553:             if ($current{'available'}) {
14554:                 $changes{$provider} = 1;
14555:             }
14556:         }
14557:         if ($confhash{$provider}{'available'}) {
14558:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
14559:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
14560:                 if ($field eq 'lifetime') {
14561:                     if ($possval =~ /^\d+$/) {
14562:                         $confhash{$provider}{$field} = $possval;
14563:                     }
14564:                 } elsif ($field eq 'version') {
14565:                     if ($possval =~ /^\d+\.\d+$/) {
14566:                         $confhash{$provider}{$field} = $possval;
14567:                     }
14568:                 } elsif ($field eq 'sigmethod') {
14569:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
14570:                         $confhash{$provider}{$field} = $possval;
14571:                     }
14572:                 } elsif ($field eq 'url') {
14573:                     $confhash{$provider}{$field} = $possval;
14574:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
14575:                     $encconfhash{$provider}{$field} = $possval;
14576:                     unless ($currentenc{$field} eq $possval) {
14577:                         $changes{$provider} = 1;
14578:                     }
14579:                 }
14580:                 unless (($field eq 'key') || ($field eq 'secret')) {
14581:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
14582:                         $changes{$provider} = 1;
14583:                     }
14584:                 }
14585:             }
14586:             if ($imgdeletions{$provider}) {
14587:                 $changes{$provider} = 1;
14588:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
14589:                 my ($imageurl,$error) =
14590:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
14591:                                               $configuserok,$switchserver,$author_ok);
14592:                 if ($imageurl) {
14593:                     $confhash{$provider}{'image'} = $imageurl;
14594:                     $changes{$provider} = 1; 
14595:                 }
14596:                 if ($error) {
14597:                     &Apache::lonnet::logthis($error);
14598:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
14599:                 }
14600:             } elsif (exists($current{'image'})) {
14601:                 $confhash{$provider}{'image'} = $current{'image'};
14602:             }
14603:             if (ref($requserfields{$provider}) eq 'ARRAY') {
14604:                 if (@{$requserfields{$provider}} > 0) {
14605:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
14606:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
14607:                             $confhash{$provider}{'incdom'} = 1;
14608:                         }
14609:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
14610:                             $changes{$provider} = 1;
14611:                         }
14612:                     }
14613:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
14614:                         $showroles = 1;
14615:                     }
14616:                 }
14617:             }
14618:             $confhash{$provider}{'fields'} = [];
14619:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
14620:                 if (@{$optuserfields{$provider}} > 0) {
14621:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
14622:                     foreach my $field (@{$optuserfields{$provider}}) {
14623:                         if (grep(/^\Q$field\E$/,@optfields)) {
14624:                             push(@{$confhash{$provider}{'fields'}},$field);
14625:                         }
14626:                     }
14627:                 }
14628:                 if (ref($current{'fields'}) eq 'ARRAY') {
14629:                     unless ($changes{$provider}) {
14630:                         my @new = sort(@{$confhash{$provider}{'fields'}});
14631:                         my @old = sort(@{$current{'fields'}}); 
14632:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14633:                         if (@diffs) {
14634:                             $changes{$provider} = 1;
14635:                         }
14636:                     }
14637:                 } elsif (@{$confhash{$provider}{'fields'}}) {
14638:                     $changes{$provider} = 1;
14639:                 }
14640:             }
14641:             if (ref($defaults{$provider}) eq 'ARRAY') {  
14642:                 if (@{$defaults{$provider}} > 0) {
14643:                     my %options;
14644:                     if (ref($extended{$provider}) eq 'HASH') {
14645:                         %options = %{$extended{$provider}};
14646:                     }
14647:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
14648:                     foreach my $field (@{$defaults{$provider}}) {
14649:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
14650:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
14651:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
14652:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
14653:                             }
14654:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
14655:                             foreach my $inner (keys(%{$options{$field}})) {
14656:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
14657:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
14658:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
14659:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
14660:                                     }
14661:                                 } else {
14662:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
14663:                                 }
14664:                             }
14665:                         } else {
14666:                             if (grep(/^\Q$field\E$/,@checked)) {
14667:                                 push(@{$confhash{$provider}{'defaults'}},$field);
14668:                             }
14669:                         }
14670:                     }
14671:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14672:                         if (ref($current{'defaults'}) eq 'ARRAY') {
14673:                             unless ($changes{$provider}) {
14674:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
14675:                                 my @old = sort(@{$current{'defaults'}});
14676:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14677:                                 if (@diffs) {
14678:                                     $changes{$provider} = 1; 
14679:                                 }
14680:                             }
14681:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
14682:                             if (@{$current{'defaults'}}) {
14683:                                 $changes{$provider} = 1;
14684:                             }
14685:                         }
14686:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14687:                         if (ref($current{'defaults'}) eq 'HASH') {
14688:                             unless ($changes{$provider}) {
14689:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
14690:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
14691:                                         $changes{$provider} = 1;
14692:                                         last;
14693:                                     }
14694:                                 }
14695:                             }
14696:                             unless ($changes{$provider}) {
14697:                                 foreach my $key (keys(%{$current{'defaults'}})) {
14698:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
14699:                                         $changes{$provider} = 1;
14700:                                         last;
14701:                                     }
14702:                                 }
14703:                             }
14704:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
14705:                             $changes{$provider} = 1;
14706:                         }
14707:                     }
14708:                 }
14709:             }
14710:             if (ref($crsconf{$provider}) eq 'ARRAY') {
14711:                 if (@{$crsconf{$provider}} > 0) {
14712:                     $confhash{$provider}{'crsconf'} = [];
14713:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
14714:                     foreach my $crsfield (@{$crsconf{$provider}}) {
14715:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
14716:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
14717:                         }
14718:                     }
14719:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
14720:                         unless ($changes{$provider}) {  
14721:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
14722:                             my @old = sort(@{$current{'crsconf'}});
14723:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
14724:                             if (@diffs) {
14725:                                 $changes{$provider} = 1;
14726:                             }
14727:                         }
14728:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
14729:                         $changes{$provider} = 1;
14730:                     }
14731:                 }
14732:             }
14733:             if ($showroles) {
14734:                 $confhash{$provider}{'roles'} = {};
14735:                 foreach my $role (@courseroles) {
14736:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
14737:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
14738:                         $confhash{$provider}{'roles'}{$role} = $poss;
14739:                     }
14740:                 }
14741:                 unless ($changes{$provider}) {
14742:                     if (ref($current{'roles'}) eq 'HASH') {
14743:                         foreach my $role (keys(%{$current{'roles'}})) {
14744:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
14745:                                 $changes{$provider} = 1;
14746:                                 last
14747:                             }
14748:                         }
14749:                         unless ($changes{$provider}) {
14750:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
14751:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
14752:                                     $changes{$provider} = 1;
14753:                                     last;
14754:                                 }
14755:                             }
14756:                         }
14757:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
14758:                         $changes{$provider} = 1;
14759:                     }
14760:                 }
14761:             }
14762:             if (ref($current{'custom'}) eq 'HASH') {
14763:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
14764:                 foreach my $key (keys(%{$current{'custom'}})) {
14765:                     if (grep(/^\Q$key\E$/,@customdels)) {
14766:                         $changes{$provider} = 1;
14767:                     } else {
14768:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
14769:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
14770:                             $changes{$provider} = 1;
14771:                         }
14772:                     }
14773:                 }
14774:             }
14775:             if ($customadds{$provider}) {
14776:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
14777:                 $name =~ s/(`)/'/g;
14778:                 $name =~ s/^\s+//;
14779:                 $name =~ s/\s+$//;
14780:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
14781:                 $value =~ s/(`)/'/g;
14782:                 $value =~ s/^\s+//;
14783:                 $value =~ s/\s+$//;
14784:                 if ($name ne '') {
14785:                     $confhash{$provider}{'custom'}{$name} = $value;
14786:                     $changes{$provider} = 1;
14787:                 }
14788:             }
14789:         }
14790:     }
14791:     if (@allpos > 0) {
14792:         my $idx = 0;
14793:         foreach my $provider (@allpos) {
14794:             if ($provider ne '') {
14795:                 $confhash{$provider}{'order'} = $idx;
14796:                 unless ($changes{$provider}) {
14797:                     if (ref($domconfig{$action}) eq 'HASH') {
14798:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
14799:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
14800:                                 $changes{$provider} = 1;
14801:                             }
14802:                         }
14803:                     }
14804:                 }
14805:                 $idx ++;
14806:             }
14807:         }
14808:     }
14809:     my %proc_hash = (
14810:                           $action => { %confhash }
14811:                        );
14812:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
14813:                                              $dom);
14814:     if ($putresult eq 'ok') {
14815:         my %proc_enchash = (
14816:                              $action => { %encconfhash }
14817:                          );
14818:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom,undef,1);
14819:         if (keys(%changes) > 0) {
14820:             my $cachetime = 24*60*60;
14821:             my %procall = %confhash;
14822:             foreach my $provider (keys(%procall)) {
14823:                 if (ref($encconfhash{$provider}) eq 'HASH') {
14824:                     foreach my $key ('key','secret') {
14825:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
14826:                     }
14827:                 }
14828:             }
14829:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
14830:             if (ref($lastactref) eq 'HASH') {
14831:                 $lastactref->{'proctoring'} = 1;
14832:             }
14833:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
14834:             my %bynum;
14835:             foreach my $provider (sort(keys(%changes))) {
14836:                 my $position = $confhash{$provider}{'order'};
14837:                 $bynum{$position} = $provider;
14838:             }
14839:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14840:                 my $provider = $bynum{$pos};
14841:                 my %lt = &proctoring_titles($provider);
14842:                 my %fieldtitles = &proctoring_fieldtitles($provider);
14843:                 if (!$confhash{$provider}{'available'}) {
14844:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
14845:                 } else {
14846:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
14847:                     if ($confhash{$provider}{'image'}) {
14848:                         $resulttext .= '&nbsp;'.
14849:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
14850:                                        ' alt="'.&mt('Proctoring icon').'" />';
14851:                     }
14852:                     $resulttext .= '<ul>';
14853:                     my $position = $pos + 1;
14854:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14855:                     foreach my $key ('version','sigmethod','url','lifetime') {
14856:                         if ($confhash{$provider}{$key} ne '') {
14857:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
14858:                         }
14859:                     }
14860:                     if ($encconfhash{$provider}{'key'} ne '') {
14861:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
14862:                     }
14863:                     if ($encconfhash{$provider}{'secret'} ne '') {
14864:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14865:                         my $num = length($encconfhash{$provider}{'secret'});
14866:                         $resulttext .= ('*'x$num).'</li>';
14867:                     }
14868:                     my (@fields,$showroles);
14869:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14870:                         push(@fields,@{$requserfields{$provider}});
14871:                     }
14872:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
14873:                         push(@fields,@{$confhash{$provider}{'fields'}});
14874:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
14875:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
14876:                     }
14877:                     if (@fields) {
14878:                         if (grep(/^roles$/,@fields)) {
14879:                             $showroles = 1;
14880:                         }
14881:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
14882:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
14883:                     }
14884:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
14885:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
14886:                             if ($confhash{$provider}{'incdom'}) {
14887:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
14888:                             } else { 
14889:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
14890:                             }
14891:                         }
14892:                     }
14893:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
14894:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
14895:                             $resulttext .= '<li>'.$lt{'defa'};
14896:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
14897:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
14898:                             }
14899:                             $resulttext =~ s/,$//;
14900:                             $resulttext .= '</li>';
14901:                         }
14902:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
14903:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
14904:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
14905:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
14906:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
14907:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
14908:                                 }
14909:                             }
14910:                             $resulttext .= '</ul></li>';
14911:                         }
14912:                     }
14913:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
14914:                         if (@{$crsconf{$provider}} > 0) {
14915:                             $resulttext .= '<li>'.&mt('Configurable in course:');
14916:                             my $numconfig = 0;
14917:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
14918:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
14919:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
14920:                                         $numconfig ++;
14921:                                         if ($provider eq 'examity') {
14922:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
14923:                                         } else {
14924:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
14925:                                         }
14926:                                     }
14927:                                     $resulttext =~ s/,$//;
14928:                                 }
14929:                             }
14930:                             if (!$numconfig) {
14931:                                 $resulttext .= '&nbsp;'.&mt('None');
14932:                             }
14933:                             $resulttext .= '</li>';
14934:                         }
14935:                     }
14936:                     if ($showroles) {
14937:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
14938:                             my $rolemaps;
14939:                             foreach my $role (@courseroles) {
14940:                                 if ($confhash{$provider}{'roles'}{$role}) {
14941:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
14942:                                                  $confhash{$provider}{'roles'}{$role}.',';
14943:                                 }
14944:                             }
14945:                             if ($rolemaps) {
14946:                                 $rolemaps =~ s/,$//;
14947:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14948:                             }
14949:                         }
14950:                     }
14951:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
14952:                         my $customlist;
14953:                         if (keys(%{$confhash{$provider}{'custom'}})) {
14954:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
14955:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
14956:                             }
14957:                             $customlist =~ s/,$//;
14958:                         }
14959:                         if ($customlist) {
14960:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
14961:                         }
14962:                     } 
14963:                     $resulttext .= '</ul></li>';
14964:                 }
14965:             }
14966:             $resulttext .= '</ul>';
14967:         } else {
14968:             $resulttext = &mt('No changes made.');
14969:         }
14970:     } else {
14971:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14972:     }
14973:     if ($errors) {
14974:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14975:                        $errors.'</ul>';
14976:     }
14977:     return $resulttext;
14978: }
14979: 
14980: sub process_proctoring_image {
14981:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
14982:     my $filename = $env{'form.'.$caller.'.filename'};
14983:     my ($error,$url);
14984:     my ($width,$height) = (21,21);
14985:     if ($configuserok eq 'ok') {
14986:         if ($switchserver) {
14987:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
14988:                          $switchserver);
14989:         } elsif ($author_ok eq 'ok') {
14990:             my $modified = [];
14991:             my ($result,$imageurl,$madethumb) =
14992:                 &Apache::lonconfigsettings::publishlogo($r,'upload',$caller,$dom,$confname,
14993:                                                         "proctoring/$provider/icon",$width,$height,
14994:                                                         '',$modified);
14995:             if ($result eq 'ok') {
14996:                 if ($madethumb) {
14997:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
14998:                     my $imagethumb = "$path/tn-".$imagefile;
14999:                     $url = $imagethumb;
15000:                 } else {
15001:                     $url = $imageurl;
15002:                 }
15003:                 &update_modify_urls($r,$modified);
15004:             } else {
15005:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
15006:             }
15007:         } else {
15008:             $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);
15009:         }
15010:     } else {
15011:         $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);
15012:     }
15013:     return ($url,$error);
15014: }
15015: 
15016: sub modify_lti {
15017:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
15018:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15019:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
15020:     my (%posslti,%posslticrs,%posscrstype);
15021:     my @courseroles = ('cc','in','ta','ep','st');
15022:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
15023:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
15024:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
15025:     my %coursetypetitles = &Apache::lonlocal::texthash (
15026:                                official   => 'Official',
15027:                                unofficial => 'Unofficial',
15028:                                community  => 'Community',
15029:                                textbook   => 'Textbook',
15030:                                placement  => 'Placement Test',
15031:                                lti        => 'LTI Provider',
15032:     );
15033:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
15034:     my %lt = &lti_names();
15035:     map { $posslti{$_} = 1; } @ltiroles;
15036:     map { $posslticrs{$_} = 1; } @lticourseroles;
15037:     map { $posscrstype{$_} = 1; } @coursetypes;
15038: 
15039:     my %menutitles = &ltimenu_titles();
15040:     my (%currltisec,%secchanges,%newltisec,%newltienc,%newkeyset);
15041: 
15042:     &fetch_secrets($dom,'ltisec',\%domconfig,\%currltisec,\%secchanges,\%newltisec,\%newkeyset);
15043: 
15044:     my (%linkprotchg,$linkprotoutput,$is_home);
15045:     my $proterror = &Apache::courseprefs::process_linkprot($dom,'',$currltisec{'linkprot'},
15046:                                                            \%linkprotchg,'domain');
15047:     my $home = &Apache::lonnet::domain($dom,'primary');
15048:     unless (($home eq 'no_host') || ($home eq '')) {
15049:         my @ids=&Apache::lonnet::current_machine_ids();
15050:         foreach my $id (@ids) { if ($id eq $home) { $is_home=1; } }
15051:     }
15052: 
15053:     if (keys(%linkprotchg)) {
15054:         $secchanges{'linkprot'} = 1;
15055:         my %oldlinkprot;
15056:         if (ref($currltisec{'linkprot'}) eq 'HASH') {
15057:             %oldlinkprot = %{$currltisec{'linkprot'}};
15058:         }
15059:         foreach my $id (keys(%linkprotchg)) {
15060:             if (ref($linkprotchg{$id}) eq 'HASH') {
15061:                 foreach my $inner (keys(%{$linkprotchg{$id}})) {
15062:                     if (($inner eq 'secret') || ($inner eq 'key')) {
15063:                         if ($is_home) {
15064:                             $newltienc{$id}{$inner} = $linkprotchg{$id}{$inner};
15065:                         }
15066:                     }
15067:                 }
15068:             } else {
15069:                 $newltisec{'linkprot'}{$id} = $linkprotchg{$id};
15070:             }
15071:         }
15072:         $linkprotoutput = &Apache::courseprefs::store_linkprot($dom,'','domain',\%linkprotchg,\%oldlinkprot);
15073:         if (keys(%linkprotchg)) {
15074:             %{$newltisec{'linkprot'}} = %linkprotchg;
15075:         }
15076:     }
15077:     if (ref($currltisec{'linkprot'}) eq 'HASH') {
15078:         foreach my $id (%{$currltisec{'linkprot'}}) {
15079:             next if ($id !~ /^\d+$/);
15080:             unless (exists($linkprotchg{$id})) {
15081:                 if (ref($currltisec{'linkprot'}{$id}) eq 'HASH') {
15082:                     foreach my $inner (keys(%{$currltisec{'linkprot'}{$id}})) {
15083:                         if (($inner eq 'secret') || ($inner eq 'key')) {
15084:                             if ($is_home) {
15085:                                 $newltienc{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15086:                             }
15087:                         } else {
15088:                             $newltisec{'linkprot'}{$id}{$inner} = $currltisec{'linkprot'}{$id}{$inner};
15089:                         }
15090:                     }
15091:                 } else {
15092:                     $newltisec{'linkprot'}{$id} = $currltisec{'linkprot'}{$id};
15093:                 }
15094:             }
15095:         }
15096:     }
15097:     if ($proterror) {
15098:         $errors .= '<li>'.$proterror.'</li>';
15099:     }
15100:     my (@items,%deletions,%itemids);
15101:     if ($env{'form.lti_add'}) {
15102:         my $consumer = $env{'form.lti_consumer_add'};
15103:         $consumer =~ s/(`)/'/g;
15104:         ($newid,my $error) = &get_lti_id($dom,$consumer);
15105:         if ($newid) {
15106:             $itemids{'add'} = $newid;
15107:             push(@items,'add');
15108:             $changes{$newid} = 1;
15109:         } else {
15110:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
15111:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
15112:         }
15113:     }
15114:     if (ref($domconfig{$action}) eq 'HASH') {
15115:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
15116:         if (@todelete) {
15117:             map { $deletions{$_} = 1; } @todelete;
15118:         }
15119:         my $maxnum = $env{'form.lti_maxnum'};
15120:         for (my $i=0; $i<$maxnum; $i++) {
15121:             my $itemid = $env{'form.lti_id_'.$i};
15122:             $itemid =~ s/\D+//g;
15123:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15124:                 if ($deletions{$itemid}) {
15125:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
15126:                 } else {
15127:                     push(@items,$i);
15128:                     $itemids{$i} = $itemid;
15129:                 }
15130:             }
15131:         }
15132:     }
15133:     foreach my $idx (@items) {
15134:         my $itemid = $itemids{$idx};
15135:         next unless ($itemid);
15136:         my $position = $env{'form.lti_pos_'.$itemid};
15137:         $position =~ s/\D+//g;
15138:         if ($position ne '') {
15139:             $allpos[$position] = $itemid;
15140:         }
15141:         foreach my $item ('consumer','key','secret','lifetime','requser','crsinc') {
15142:             my $formitem = 'form.lti_'.$item.'_'.$idx;
15143:             $env{$formitem} =~ s/(`)/'/g;
15144:             if ($item eq 'lifetime') {
15145:                 $env{$formitem} =~ s/[^\d.]//g;
15146:             }
15147:             if ($env{$formitem} ne '') {
15148:                 if (($item eq 'key') || ($item eq 'secret')) {
15149:                     $encconfig{$itemid}{$item} = $env{$formitem};
15150:                 } else {
15151:                     $confhash{$itemid}{$item} = $env{$formitem};
15152:                     unless (($idx eq 'add') || ($changes{$itemid})) {
15153:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
15154:                             $changes{$itemid} = 1;
15155:                         }
15156:                     }
15157:                 }
15158:             }
15159:         }
15160:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
15161:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
15162:         }
15163:         if ($confhash{$itemid}{'requser'}) {
15164:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
15165:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid';
15166:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
15167:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
15168:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
15169:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
15170:                 $mapuser =~ s/(`)/'/g;
15171:                 $mapuser =~ s/^\s+|\s+$//g;
15172:                 $confhash{$itemid}{'mapuser'} = $mapuser;
15173:             }
15174:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
15175:             my @makeuser;
15176:             foreach my $ltirole (sort(@possmakeuser)) {
15177:                 if ($posslti{$ltirole}) {
15178:                     push(@makeuser,$ltirole);
15179:                 }
15180:             }
15181:             $confhash{$itemid}{'makeuser'} = \@makeuser;
15182:             if (@makeuser) {
15183:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
15184:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
15185:                     $confhash{$itemid}{'lcauth'} = $lcauth;
15186:                     if ($lcauth ne 'internal') {
15187:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
15188:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
15189:                         $lcauthparm =~ s/`//g;
15190:                         if ($lcauthparm ne '') {
15191:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
15192:                         }
15193:                     }
15194:                 } else {
15195:                     $confhash{$itemid}{'lcauth'} = 'lti';
15196:                 }
15197:             }
15198:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
15199:             if (@possinstdata) {
15200:                 foreach my $field (@possinstdata) {
15201:                     if (exists($fieldtitles{$field})) {
15202:                         push(@{$confhash{$itemid}{'instdata'}});
15203:                     }
15204:                 }
15205:             }
15206:             if ($env{'form.lti_callback_'.$idx}) {
15207:                 if ($env{'form.lti_callbackparam_'.$idx}) {
15208:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
15209:                     $callback =~ s/^\s+|\s+$//g;
15210:                     $confhash{$itemid}{'callback'} = $callback;
15211:                 }
15212:             }
15213:             foreach my $field ('topmenu','inlinemenu') {
15214:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
15215:                     $confhash{$itemid}{$field} = 1;
15216:                 }
15217:             }
15218:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
15219:                 $confhash{$itemid}{lcmenu} = [];
15220:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
15221:                 foreach my $field (@possmenu) {
15222:                     if (exists($menutitles{$field})) {
15223:                         if ($field eq 'grades') {
15224:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
15225:                         }
15226:                         push(@{$confhash{$itemid}{lcmenu}},$field);
15227:                     }
15228:                 }
15229:             }
15230:             if ($confhash{$itemid}{'crsinc'}) {
15231:                 if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
15232:                     ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
15233:                     $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
15234:                 } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
15235:                     my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
15236:                     $mapcrs =~ s/(`)/'/g;
15237:                     $mapcrs =~ s/^\s+|\s+$//g;
15238:                     $confhash{$itemid}{'mapcrs'} = $mapcrs;
15239:                 }
15240:                 my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
15241:                 my @crstypes;
15242:                 foreach my $type (sort(@posstypes)) {
15243:                     if ($posscrstype{$type}) {
15244:                         push(@crstypes,$type);
15245:                     }
15246:                 }
15247:                 $confhash{$itemid}{'mapcrstype'} = \@crstypes;
15248:                 if ($env{'form.lti_storecrs_'.$idx}) {
15249:                     $confhash{$itemid}{'storecrs'} = 1;
15250:                 }       
15251:                 if ($env{'form.lti_makecrs_'.$idx}) {
15252:                     $confhash{$itemid}{'makecrs'} = 1;
15253:                 }
15254:                 foreach my $ltirole (@lticourseroles) {
15255:                     my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
15256:                     if (grep(/^\Q$possrole\E$/,@courseroles)) {
15257:                         $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
15258:                     }
15259:                 }
15260:                 my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
15261:                 my @selfenroll;
15262:                 foreach my $type (sort(@possenroll)) {
15263:                     if ($posslticrs{$type}) {
15264:                         push(@selfenroll,$type);
15265:                     }
15266:                 }
15267:                 $confhash{$itemid}{'selfenroll'} = \@selfenroll;
15268:                 if ($env{'form.lti_crssec_'.$idx}) {
15269:                     if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
15270:                         $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
15271:                     } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
15272:                         my $section = $env{'form.lti_customsection_'.$idx};
15273:                         $section =~ s/(`)/'/g;
15274:                         $section =~ s/^\s+|\s+$//g;
15275:                         if ($section ne '') {
15276:                             $confhash{$itemid}{'section'} = $section;
15277:                         }
15278:                     }
15279:                 }
15280:                 foreach my $field ('passback','roster') {
15281:                     if ($env{'form.lti_'.$field.'_'.$idx}) {
15282:                         $confhash{$itemid}{$field} = 1;
15283:                     }
15284:                 }
15285:                 if ($env{'form.lti_passback_'.$idx}) {
15286:                     if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
15287:                         $confhash{$itemid}{'passbackformat'} = '1.0';
15288:                     } else {
15289:                         $confhash{$itemid}{'passbackformat'} = '1.1';
15290:                     }
15291:                 }
15292:             }
15293:             unless (($idx eq 'add') || ($changes{$itemid})) {
15294:                 if ($confhash{$itemid}{'crsinc'}) {
15295:                     foreach my $field ('mapcrs','storecrs','makecrs','section','passback','roster') {
15296:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15297:                             $changes{$itemid} = 1;
15298:                         }
15299:                     }
15300:                     unless ($changes{$itemid}) {
15301:                         if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
15302:                             if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
15303:                                 $changes{$itemid} = 1;
15304:                             }
15305:                         }
15306:                     }
15307:                     foreach my $field ('mapcrstype','selfenroll') {
15308:                         unless ($changes{$itemid}) {
15309:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15310:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15311:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15312:                                                                                    $confhash{$itemid}{$field});
15313:                                     if (@diffs) {
15314:                                         $changes{$itemid} = 1;
15315:                                     }
15316:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15317:                                     $changes{$itemid} = 1;
15318:                                 }
15319:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15320:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15321:                                     $changes{$itemid} = 1;
15322:                                 }
15323:                             }
15324:                         }
15325:                     }
15326:                     unless ($changes{$itemid}) {
15327:                         if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
15328:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15329:                                 foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
15330:                                     if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
15331:                                         $confhash{$itemid}{'maproles'}{$ltirole}) {
15332:                                         $changes{$itemid} = 1;
15333:                                         last;
15334:                                     }
15335:                                 }
15336:                                 unless ($changes{$itemid}) {
15337:                                     foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
15338:                                         if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
15339:                                             $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
15340:                                             $changes{$itemid} = 1;
15341:                                             last;
15342:                                         }
15343:                                     }
15344:                                 }
15345:                             } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
15346:                                 $changes{$itemid} = 1;
15347:                             }
15348:                         } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15349:                             unless ($changes{$itemid}) {
15350:                                 if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
15351:                                     $changes{$itemid} = 1;
15352:                                 }
15353:                             }
15354:                         }
15355:                     }
15356:                 }
15357:                 unless ($changes{$itemid}) {
15358:                     foreach my $field ('mapuser','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
15359:                         if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
15360:                             $changes{$itemid} = 1;
15361:                         }
15362:                     }
15363:                     unless ($changes{$itemid}) {
15364:                         foreach my $field ('makeuser','lcmenu') {
15365:                             if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
15366:                                 if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15367:                                     my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
15368:                                                                                    $confhash{$itemid}{$field});
15369:                                     if (@diffs) {
15370:                                         $changes{$itemid} = 1;
15371:                                     }
15372:                                 } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
15373:                                     $changes{$itemid} = 1;
15374:                                 }
15375:                             } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
15376:                                 if (@{$confhash{$itemid}{$field}} > 0) {
15377:                                     $changes{$itemid} = 1;
15378:                                 }
15379:                             }
15380:                         }
15381:                     }
15382:                 }
15383:             }
15384:         }
15385:     }
15386:     if (@allpos > 0) {
15387:         my $idx = 0;
15388:         foreach my $itemid (@allpos) {
15389:             if ($itemid ne '') {
15390:                 $confhash{$itemid}{'order'} = $idx;
15391:                 if (ref($domconfig{$action}) eq 'HASH') {
15392:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
15393:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
15394:                             $changes{$itemid} = 1;
15395:                         }
15396:                     }
15397:                 }
15398:                 $idx ++;
15399:             }
15400:         }
15401:     }
15402:     my %ltihash = (
15403:                       $action => { %confhash }
15404:                   );
15405:     my %ltienchash = (
15406:                          $action => { %encconfig }
15407:                      );
15408:     if (keys(%secchanges)) {
15409:         $ltihash{'ltisec'} = \%newltisec;
15410:         if ($secchanges{'linkprot'}) {
15411:             if ($is_home) {
15412:                 $ltienchash{'linkprot'} = \%newltienc;
15413:             }
15414:         }
15415:     }
15416:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,$dom);
15417:     if ($putresult eq 'ok') {
15418:         my %keystore;
15419:         &store_security($dom,'lti',\%secchanges,\%newkeyset,\%keystore,$lastactref);
15420:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom,undef,1);
15421:         if ((keys(%changes) == 0) && (keys(%secchanges) == 0)) {
15422:             return &mt('No changes made.');
15423:         }
15424:         $resulttext = &mt('Changes made:').'<ul>';
15425:         if (keys(%secchanges) > 0) {
15426:             $resulttext .= &lti_security_results('lti',\%secchanges,\%newltisec,\%newkeyset,\%keystore);
15427:             if (exists($secchanges{'linkprot'})) {
15428:                 $resulttext .= $linkprotoutput;
15429:             }
15430:         }
15431:         if (keys(%changes) > 0) {
15432:             my $cachetime = 24*60*60;
15433:             my %ltiall = %confhash;
15434:             foreach my $id (keys(%ltiall)) {
15435:                 if (ref($encconfig{$id}) eq 'HASH') {
15436:                     foreach my $item ('key','secret') {
15437:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
15438:                     }
15439:                 }
15440:             }
15441:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
15442:             if (ref($lastactref) eq 'HASH') {
15443:                 $lastactref->{'lti'} = 1;
15444:             }
15445:             my %bynum;
15446:             foreach my $itemid (sort(keys(%changes))) {
15447:                 my $position = $confhash{$itemid}{'order'};
15448:                 $bynum{$position} = $itemid;
15449:             }
15450:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
15451:                 my $itemid = $bynum{$pos};
15452:                 if (ref($confhash{$itemid}) ne 'HASH') {
15453:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
15454:                 } else {
15455:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b><ul>';
15456:                     my $position = $pos + 1;
15457:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
15458:                     foreach my $item ('version','lifetime') {
15459:                         if ($confhash{$itemid}{$item} ne '') {
15460:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
15461:                         }
15462:                     }
15463:                     if ($encconfig{$itemid}{'key'} ne '') {
15464:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
15465:                     }
15466:                     if ($encconfig{$itemid}{'secret'} ne '') {
15467:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
15468:                         my $num = length($encconfig{$itemid}{'secret'});
15469:                         $resulttext .= ('*'x$num).'</li>';
15470:                     }
15471:                     if ($confhash{$itemid}{'requser'}) {
15472:                         if ($confhash{$itemid}{'callback'}) {
15473:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
15474:                         } else {
15475:                             $resulttext .= '<li>'.&mt('Callback to logout LON-CAPA on log out from Consumer').'</li>';
15476:                         }
15477:                         if ($confhash{$itemid}{'mapuser'}) {
15478:                             my $shownmapuser;
15479:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
15480:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
15481:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
15482:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
15483:                             } else {
15484:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
15485:                             }
15486:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
15487:                         }
15488:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
15489:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
15490:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
15491:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
15492:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
15493:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
15494:                                 } else {
15495:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
15496:                                                        $confhash{$itemid}{'lcauth'});
15497:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
15498:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
15499:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
15500:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
15501:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
15502:                                         }
15503:                                     } else {
15504:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
15505:                                     }
15506:                                 }
15507:                                 $resulttext .= '</li>';
15508:                             } else {
15509:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
15510:                             }
15511:                         }
15512:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
15513:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
15514:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
15515:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
15516:                             } else {
15517:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
15518:                             }
15519:                         }
15520:                         foreach my $item ('topmenu','inlinemenu') {
15521:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15522:                             if ($confhash{$itemid}{$item}) {
15523:                                 $resulttext .= &mt('Yes');
15524:                             } else {
15525:                                 $resulttext .= &mt('No');
15526:                             }
15527:                             $resulttext .= '</li>';
15528:                         }
15529:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15530:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15531:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
15532:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>';
15533:                             } else {
15534:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>';
15535:                             }
15536:                         }
15537:                         if ($confhash{$itemid}{'crsinc'}) {
15538:                             if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
15539:                                 my $rolemaps;
15540:                                 foreach my $role (@ltiroles) {
15541:                                     if ($confhash{$itemid}{'maproles'}{$role}) {
15542:                                         $rolemaps .= ('&nbsp;'x2).$role.'='.
15543:                                                      &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
15544:                                                                                 'Course').',';
15545:                                     }
15546:                                 }
15547:                                 if ($rolemaps) {
15548:                                     $rolemaps =~ s/,$//;
15549:                                     $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
15550:                                 }
15551:                             }
15552:                             if ($confhash{$itemid}{'mapcrs'}) {
15553:                                 $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
15554:                             }
15555:                             if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
15556:                                 if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
15557:                                     $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
15558:                                                    join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
15559:                                                    '</li>';
15560:                                 } else {
15561:                                     $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
15562:                                 }
15563:                             }
15564:                             if ($confhash{$itemid}{'storecrs'}) {
15565:                                 $resulttext .= '<li>'.&mt('Store mapping of course identifier to LON-CAPA CourseID').': '.$confhash{$itemid}{'storecrs'}.'</li>';
15566:                             }
15567:                             if ($confhash{$itemid}{'makecrs'}) {
15568:                                 $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
15569:                             } else {
15570:                                 $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
15571:                             }
15572:                             if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
15573:                                 if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
15574:                                     $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
15575:                                                               join(', ',@{$confhash{$itemid}{'selfenroll'}})).
15576:                                                    '</li>';
15577:                                 } else {
15578:                                     $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
15579:                                 }
15580:                             }
15581:                             if ($confhash{$itemid}{'section'}) {
15582:                                 if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
15583:                                     $resulttext .= '<li>'.&mt('User section from standard field:').
15584:                                                          ' (course_section_sourcedid)'.'</li>';  
15585:                                 } else {
15586:                                     $resulttext .= '<li>'.&mt('User section from:').' '.
15587:                                                           $confhash{$itemid}{'section'}.'</li>';
15588:                                 }
15589:                             } else {
15590:                                 $resulttext .= '<li>'.&mt('No section assignment').'</li>';
15591:                             }
15592:                             foreach my $item ('passback','roster','topmenu','inlinemenu') {
15593:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
15594:                                 if ($confhash{$itemid}{$item}) {
15595:                                     $resulttext .= &mt('Yes');
15596:                                     if ($item eq 'passback') {
15597:                                         if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
15598:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
15599:                                         } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
15600:                                             $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
15601:                                         }
15602:                                     }
15603:                                 } else {
15604:                                     $resulttext .= &mt('No');
15605:                                 }
15606:                                 $resulttext .= '</li>';
15607:                             }
15608:                             if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
15609:                                 if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
15610:                                     $resulttext .= '<li>'.&mt('Menu items:').' '.
15611:                                                    join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
15612:                                 } else {
15613:                                     $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
15614:                                 }
15615:                             }
15616:                         }
15617:                     }
15618:                     $resulttext .= '</ul></li>';
15619:                 }
15620:             }
15621:         }
15622:         $resulttext .= '</ul>';
15623:     } else {
15624:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
15625:     }
15626:     if ($errors) {
15627:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
15628:                        $errors.'</ul>';
15629:     }
15630:     return $resulttext;
15631: }
15632: 
15633: sub get_lti_id {
15634:     my ($domain,$consumer) = @_;
15635:     # get lock on lti db
15636:     my $lockhash = {
15637:                       lock => $env{'user.name'}.
15638:                               ':'.$env{'user.domain'},
15639:                    };
15640:     my $tries = 0;
15641:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15642:     my ($id,$error);
15643: 
15644:     while (($gotlock ne 'ok') && ($tries<10)) {
15645:         $tries ++;
15646:         sleep (0.1);
15647:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
15648:     }
15649:     if ($gotlock eq 'ok') {
15650:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
15651:         if ($currids{'lock'}) {
15652:             delete($currids{'lock'});
15653:             if (keys(%currids)) {
15654:                 my @curr = sort { $a <=> $b } keys(%currids);
15655:                 if ($curr[-1] =~ /^\d+$/) {
15656:                     $id = 1 + $curr[-1];
15657:                 }
15658:             } else {
15659:                 $id = 1;
15660:             }
15661:             if ($id) {
15662:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
15663:                     $error = 'nostore';
15664:                 }
15665:             } else {
15666:                 $error = 'nonumber';
15667:             }
15668:         }
15669:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
15670:     } else {
15671:         $error = 'nolock';
15672:     }
15673:     return ($id,$error);
15674: }
15675: 
15676: sub modify_autoenroll {
15677:     my ($dom,$lastactref,%domconfig) = @_;
15678:     my ($resulttext,%changes);
15679:     my %currautoenroll;
15680:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
15681:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
15682:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
15683:         }
15684:     }
15685:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
15686:     my %title = ( run => 'Auto-enrollment active',
15687:                   sender => 'Sender for notification messages',
15688:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
15689:                   autofailsafe => 'Failsafe for no drops if institutional data missing for a section');
15690:     my @offon = ('off','on');
15691:     my $sender_uname = $env{'form.sender_uname'};
15692:     my $sender_domain = $env{'form.sender_domain'};
15693:     if ($sender_domain eq '') {
15694:         $sender_uname = '';
15695:     } elsif ($sender_uname eq '') {
15696:         $sender_domain = '';
15697:     }
15698:     my $coowners = $env{'form.autoassign_coowners'};
15699:     my $autofailsafe = $env{'form.autoenroll_autofailsafe'};
15700:     $autofailsafe =~ s{^\s+|\s+$}{}g;
15701:     if ($autofailsafe =~ /\D/) {
15702:         undef($autofailsafe);
15703:     }
15704:     my $failsafe = $env{'form.autoenroll_failsafe'};
15705:     unless (($failsafe eq 'zero') || ($failsafe eq 'any')) {
15706:         $failsafe = 'off';
15707:         undef($autofailsafe);
15708:     }
15709:     my %autoenrollhash =  (
15710:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
15711:                                        'sender_uname' => $sender_uname,
15712:                                        'sender_domain' => $sender_domain,
15713:                                        'co-owners' => $coowners,
15714:                                        'autofailsafe' => $autofailsafe,
15715:                                        'failsafe' => $failsafe,
15716:                                 }
15717:                      );
15718:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
15719:                                              $dom);
15720:     if ($putresult eq 'ok') {
15721:         if (exists($currautoenroll{'run'})) {
15722:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
15723:                  $changes{'run'} = 1;
15724:              }
15725:         } elsif ($autorun) {
15726:             if ($env{'form.autoenroll_run'} ne '1') {
15727:                  $changes{'run'} = 1;
15728:             }
15729:         }
15730:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
15731:             $changes{'sender'} = 1;
15732:         }
15733:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
15734:             $changes{'sender'} = 1;
15735:         }
15736:         if ($currautoenroll{'co-owners'} ne '') {
15737:             if ($currautoenroll{'co-owners'} ne $coowners) {
15738:                 $changes{'coowners'} = 1;
15739:             }
15740:         } elsif ($coowners) {
15741:             $changes{'coowners'} = 1;
15742:         }
15743:         if ($currautoenroll{'autofailsafe'} ne $autofailsafe) {
15744:             $changes{'autofailsafe'} = 1;
15745:         }
15746:         if ($currautoenroll{'failsafe'} ne $failsafe) {
15747:             $changes{'failsafe'} = 1;
15748:         }
15749:         if (keys(%changes) > 0) {
15750:             $resulttext = &mt('Changes made:').'<ul>';
15751:             if ($changes{'run'}) {
15752:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
15753:             }
15754:             if ($changes{'sender'}) {
15755:                 if ($sender_uname eq '' || $sender_domain eq '') {
15756:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
15757:                 } else {
15758:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
15759:                 }
15760:             }
15761:             if ($changes{'coowners'}) {
15762:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
15763:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
15764:                 if (ref($lastactref) eq 'HASH') {
15765:                     $lastactref->{'domainconfig'} = 1;
15766:                 }
15767:             }
15768:             if ($changes{'autofailsafe'}) {
15769:                 if ($autofailsafe ne '') {
15770:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$autofailsafe).'</li>';
15771:                 } else {
15772:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
15773:                 }
15774:             }
15775:             if ($changes{'failsafe'}) {
15776:                 if ($failsafe eq 'off') {
15777:                     unless ($changes{'autofailsafe'}) {
15778:                         $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section not in use').'</li>';
15779:                     }
15780:                 } elsif ($failsafe eq 'zero') {
15781:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero').'</li>';
15782:                 } else {
15783:                     $resulttext .= '<li>'.&mt('Failsafe applies if retrieved section enrollment is zero or greater').'</li>';
15784:                 }
15785:             }
15786:             if (($changes{'autofailsafe'}) || ($changes{'failsafe'})) {
15787:                 &Apache::lonnet::get_domain_defaults($dom,1);
15788:                 if (ref($lastactref) eq 'HASH') {
15789:                     $lastactref->{'domdefaults'} = 1;
15790:                 }
15791:             }
15792:             $resulttext .= '</ul>';
15793:         } else {
15794:             $resulttext = &mt('No changes made to auto-enrollment settings');
15795:         }
15796:     } else {
15797:         $resulttext = '<span class="LC_error">'.
15798: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
15799:     }
15800:     return $resulttext;
15801: }
15802: 
15803: sub modify_autoupdate {
15804:     my ($dom,%domconfig) = @_;
15805:     my ($resulttext,%currautoupdate,%fields,%changes);
15806:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
15807:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
15808:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
15809:         }
15810:     }
15811:     my @offon = ('off','on');
15812:     my %title = &Apache::lonlocal::texthash (
15813:                     run        => 'Auto-update:',
15814:                     classlists => 'Updates to user information in classlists?',
15815:                     unexpired  => 'Skip updates for users without active or future roles?',
15816:                     lastactive => 'Skip updates for inactive users?',
15817:                 );
15818:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15819:     my %fieldtitles = &Apache::lonlocal::texthash (
15820:                         id => 'Student/Employee ID',
15821:                         permanentemail => 'E-mail address',
15822:                         lastname => 'Last Name',
15823:                         firstname => 'First Name',
15824:                         middlename => 'Middle Name',
15825:                         generation => 'Generation',
15826:                       );
15827:     $othertitle = &mt('All users');
15828:     if (keys(%{$usertypes}) >  0) {
15829:         $othertitle = &mt('Other users');
15830:     }
15831:     foreach my $key (keys(%env)) {
15832:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
15833:             my ($usertype,$item) = ($1,$2);
15834:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
15835:                 if ($usertype eq 'default') {   
15836:                     push(@{$fields{$1}},$2);
15837:                 } elsif (ref($types) eq 'ARRAY') {
15838:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
15839:                         push(@{$fields{$1}},$2);
15840:                     }
15841:                 }
15842:             }
15843:         }
15844:     }
15845:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
15846:     @lockablenames = sort(@lockablenames);
15847:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
15848:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15849:         if (@changed) {
15850:             $changes{'lockablenames'} = 1;
15851:         }
15852:     } else {
15853:         if (@lockablenames) {
15854:             $changes{'lockablenames'} = 1;
15855:         }
15856:     }
15857:     my %updatehash = (
15858:                       autoupdate => { run => $env{'form.autoupdate_run'},
15859:                                       classlists => $env{'form.classlists'},
15860:                                       unexpired  => $env{'form.unexpired'},
15861:                                       fields => {%fields},
15862:                                       lockablenames => \@lockablenames,
15863:                                     }
15864:                      );
15865:     my $lastactivedays;
15866:     if ($env{'form.lastactive'}) {
15867:         $lastactivedays = $env{'form.lastactivedays'};
15868:         $lastactivedays =~ s/^\s+|\s+$//g;
15869:         unless ($lastactivedays =~ /^\d+$/) {
15870:             undef($lastactivedays);
15871:             $env{'form.lastactive'} = 0;
15872:         }
15873:     }
15874:     $updatehash{'autoupdate'}{'lastactive'} = $lastactivedays;
15875:     foreach my $key (keys(%currautoupdate)) {
15876:         if (($key eq 'run') || ($key eq 'classlists') || ($key eq 'unexpired') || ($key eq 'lastactive')) {
15877:             if (exists($updatehash{autoupdate}{$key})) {
15878:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
15879:                     $changes{$key} = 1;
15880:                 }
15881:             }
15882:         } elsif ($key eq 'fields') {
15883:             if (ref($currautoupdate{$key}) eq 'HASH') {
15884:                 foreach my $item (@{$types},'default') {
15885:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
15886:                         my $change = 0;
15887:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
15888:                             if (!exists($fields{$item})) {
15889:                                 $change = 1;
15890:                                 last;
15891:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
15892:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
15893:                                     $change = 1;
15894:                                     last;
15895:                                 }
15896:                             }
15897:                         }
15898:                         if ($change) {
15899:                             push(@{$changes{$key}},$item);
15900:                         }
15901:                     } 
15902:                 }
15903:             }
15904:         } elsif ($key eq 'lockablenames') {
15905:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
15906:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
15907:                 if (@changed) {
15908:                     $changes{'lockablenames'} = 1;
15909:                 }
15910:             } else {
15911:                 if (@lockablenames) {
15912:                     $changes{'lockablenames'} = 1;
15913:                 }
15914:             }
15915:         }
15916:     }
15917:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
15918:         if (@lockablenames) {
15919:             $changes{'lockablenames'} = 1;
15920:         }
15921:     }
15922:     unless (grep(/^unexpired$/,keys(%currautoupdate))) {
15923:         if ($updatehash{'autoupdate'}{'unexpired'}) {
15924:             $changes{'unexpired'} = 1;
15925:         }
15926:     }
15927:     unless (grep(/^lastactive$/,keys(%currautoupdate))) {
15928:         if ($updatehash{'autoupdate'}{'lastactive'} ne '') {
15929:             $changes{'lastactive'} = 1;
15930:         }
15931:     }
15932:     foreach my $item (@{$types},'default') {
15933:         if (defined($fields{$item})) {
15934:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
15935:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
15936:                     my $change = 0;
15937:                     if (ref($fields{$item}) eq 'ARRAY') {
15938:                         foreach my $type (@{$fields{$item}}) {
15939:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
15940:                                 $change = 1;
15941:                                 last;
15942:                             }
15943:                         }
15944:                     }
15945:                     if ($change) {
15946:                         push(@{$changes{'fields'}},$item);
15947:                     }
15948:                 } else {
15949:                     push(@{$changes{'fields'}},$item);
15950:                 }
15951:             } else {
15952:                 push(@{$changes{'fields'}},$item);
15953:             }
15954:         }
15955:     }
15956:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
15957:                                              $dom);
15958:     if ($putresult eq 'ok') {
15959:         if (keys(%changes) > 0) {
15960:             $resulttext = &mt('Changes made:').'<ul>';
15961:             foreach my $key (sort(keys(%changes))) {
15962:                 if ($key eq 'lockablenames') {
15963:                     $resulttext .= '<li>';
15964:                     if (@lockablenames) {
15965:                         $usertypes->{'default'} = $othertitle;
15966:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
15967:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
15968:                     } else {
15969:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
15970:                     }
15971:                     $resulttext .= '</li>';
15972:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
15973:                     foreach my $item (@{$changes{$key}}) {
15974:                         my @newvalues;
15975:                         foreach my $type (@{$fields{$item}}) {
15976:                             push(@newvalues,$fieldtitles{$type});
15977:                         }
15978:                         my $newvaluestr;
15979:                         if (@newvalues > 0) {
15980:                             $newvaluestr = join(', ',@newvalues);
15981:                         } else {
15982:                             $newvaluestr = &mt('none');
15983:                         }
15984:                         if ($item eq 'default') {
15985:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
15986:                         } else {
15987:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
15988:                         }
15989:                     }
15990:                 } else {
15991:                     my $newvalue;
15992:                     if ($key eq 'run') {
15993:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
15994:                     } elsif ($key eq 'lastactive') {
15995:                         $newvalue = $offon[$env{'form.lastactive'}];
15996:                         unless ($lastactivedays eq '') {
15997:                             $newvalue .= '; '.&mt('inactive = no activity in last [quant,_1,day]',$lastactivedays);
15998:                         }
15999:                     } else {
16000:                         $newvalue = $offon[$env{'form.'.$key}];
16001:                     }
16002:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
16003:                 }
16004:             }
16005:             $resulttext .= '</ul>';
16006:         } else {
16007:             $resulttext = &mt('No changes made to autoupdates');
16008:         }
16009:     } else {
16010:         $resulttext = '<span class="LC_error">'.
16011: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
16012:     }
16013:     return $resulttext;
16014: }
16015: 
16016: sub modify_autocreate {
16017:     my ($dom,%domconfig) = @_;
16018:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
16019:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
16020:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
16021:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
16022:         }
16023:     }
16024:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
16025:                  req => 'Auto-creation of validated requests for official courses',
16026:                  xmldc => 'Identity of course creator of courses from XML files',
16027:                );
16028:     my @types = ('xml','req');
16029:     foreach my $item (@types) {
16030:         $newvals{$item} = $env{'form.autocreate_'.$item};
16031:         $newvals{$item} =~ s/\D//g;
16032:         $newvals{$item} = 0 if ($newvals{$item} eq '');
16033:     }
16034:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
16035:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
16036:     unless (exists($domcoords{$newvals{'xmldc'}})) {
16037:         $newvals{'xmldc'} = '';
16038:     } 
16039:     %autocreatehash =  (
16040:                         autocreate => { xml => $newvals{'xml'},
16041:                                         req => $newvals{'req'},
16042:                                       }
16043:                        );
16044:     if ($newvals{'xmldc'} ne '') {
16045:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
16046:     }
16047:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
16048:                                              $dom);
16049:     if ($putresult eq 'ok') {
16050:         my @items = @types;
16051:         if ($newvals{'xml'}) {
16052:             push(@items,'xmldc');
16053:         }
16054:         foreach my $item (@items) {
16055:             if (exists($currautocreate{$item})) {
16056:                 if ($currautocreate{$item} ne $newvals{$item}) {
16057:                     $changes{$item} = 1;
16058:                 }
16059:             } elsif ($newvals{$item}) {
16060:                 $changes{$item} = 1;
16061:             }
16062:         }
16063:         if (keys(%changes) > 0) {
16064:             my @offon = ('off','on'); 
16065:             $resulttext = &mt('Changes made:').'<ul>';
16066:             foreach my $item (@types) {
16067:                 if ($changes{$item}) {
16068:                     my $newtxt = $offon[$newvals{$item}];
16069:                     $resulttext .= '<li>'.
16070:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
16071:                                        '<b>','</b>').
16072:                                    '</li>';
16073:                 }
16074:             }
16075:             if ($changes{'xmldc'}) {
16076:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
16077:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
16078:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
16079:             }
16080:             $resulttext .= '</ul>';
16081:         } else {
16082:             $resulttext = &mt('No changes made to auto-creation settings');
16083:         }
16084:     } else {
16085:         $resulttext = '<span class="LC_error">'.
16086:             &mt('An error occurred: [_1]',$putresult).'</span>';
16087:     }
16088:     return $resulttext;
16089: }
16090: 
16091: sub modify_directorysrch {
16092:     my ($dom,$lastactref,%domconfig) = @_;
16093:     my ($resulttext,%changes);
16094:     my %currdirsrch;
16095:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
16096:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
16097:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
16098:         }
16099:     }
16100:     my %title = ( available => 'Institutional directory search available',
16101:                   localonly => 'Other domains can search institution',
16102:                   lcavailable => 'LON-CAPA directory search available',
16103:                   lclocalonly => 'Other domains can search LON-CAPA domain',
16104:                   searchby => 'Search types',
16105:                   searchtypes => 'Search latitude');
16106:     my @offon = ('off','on');
16107:     my @otherdoms = ('Yes','No');
16108: 
16109:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
16110:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
16111:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
16112: 
16113:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16114:     if (keys(%{$usertypes}) == 0) {
16115:         @cansearch = ('default');
16116:     } else {
16117:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
16118:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
16119:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
16120:                     push(@{$changes{'cansearch'}},$type);
16121:                 }
16122:             }
16123:             foreach my $type (@cansearch) {
16124:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
16125:                     push(@{$changes{'cansearch'}},$type);
16126:                 }
16127:             }
16128:         } else {
16129:             push(@{$changes{'cansearch'}},@cansearch);
16130:         }
16131:     }
16132: 
16133:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
16134:         foreach my $by (@{$currdirsrch{'searchby'}}) {
16135:             if (!grep(/^\Q$by\E$/,@searchby)) {
16136:                 push(@{$changes{'searchby'}},$by);
16137:             }
16138:         }
16139:         foreach my $by (@searchby) {
16140:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
16141:                 push(@{$changes{'searchby'}},$by);
16142:             }
16143:         }
16144:     } else {
16145:         push(@{$changes{'searchby'}},@searchby);
16146:     }
16147: 
16148:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
16149:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
16150:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
16151:                 push(@{$changes{'searchtypes'}},$type);
16152:             }
16153:         }
16154:         foreach my $type (@searchtypes) {
16155:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
16156:                 push(@{$changes{'searchtypes'}},$type);
16157:             }
16158:         }
16159:     } else {
16160:         if (exists($currdirsrch{'searchtypes'})) {
16161:             foreach my $type (@searchtypes) {  
16162:                 if ($type ne $currdirsrch{'searchtypes'}) { 
16163:                     push(@{$changes{'searchtypes'}},$type);
16164:                 }
16165:             }
16166:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
16167:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
16168:             }   
16169:         } else {
16170:             push(@{$changes{'searchtypes'}},@searchtypes); 
16171:         }
16172:     }
16173: 
16174:     my %dirsrch_hash =  (
16175:             directorysrch => { available => $env{'form.dirsrch_available'},
16176:                                cansearch => \@cansearch,
16177:                                localonly => $env{'form.dirsrch_instlocalonly'},
16178:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
16179:                                lcavailable => $env{'form.dirsrch_domavailable'},
16180:                                searchby => \@searchby,
16181:                                searchtypes => \@searchtypes,
16182:                              }
16183:             );
16184:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
16185:                                              $dom);
16186:     if ($putresult eq 'ok') {
16187:         if (exists($currdirsrch{'available'})) {
16188:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
16189:                  $changes{'available'} = 1;
16190:              }
16191:         } else {
16192:             if ($env{'form.dirsrch_available'} eq '1') {
16193:                 $changes{'available'} = 1;
16194:             }
16195:         }
16196:         if (exists($currdirsrch{'lcavailable'})) {
16197:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
16198:                 $changes{'lcavailable'} = 1;
16199:             }
16200:         } else {
16201:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
16202:                 $changes{'lcavailable'} = 1;
16203:             }
16204:         }
16205:         if (exists($currdirsrch{'localonly'})) {
16206:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
16207:                 $changes{'localonly'} = 1;
16208:             }
16209:         } else {
16210:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
16211:                 $changes{'localonly'} = 1;
16212:             }
16213:         }
16214:         if (exists($currdirsrch{'lclocalonly'})) {
16215:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
16216:                 $changes{'lclocalonly'} = 1;
16217:             }
16218:         } else {
16219:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
16220:                 $changes{'lclocalonly'} = 1;
16221:             }
16222:         }
16223:         if (keys(%changes) > 0) {
16224:             $resulttext = &mt('Changes made:').'<ul>';
16225:             if ($changes{'available'}) {
16226:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
16227:             }
16228:             if ($changes{'lcavailable'}) {
16229:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
16230:             }
16231:             if ($changes{'localonly'}) {
16232:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
16233:             }
16234:             if ($changes{'lclocalonly'}) {
16235:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
16236:             }
16237:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
16238:                 my $chgtext;
16239:                 if (ref($usertypes) eq 'HASH') {
16240:                     if (keys(%{$usertypes}) > 0) {
16241:                         foreach my $type (@{$types}) {
16242:                             if (grep(/^\Q$type\E$/,@cansearch)) {
16243:                                 $chgtext .= $usertypes->{$type}.'; ';
16244:                             }
16245:                         }
16246:                         if (grep(/^default$/,@cansearch)) {
16247:                             $chgtext .= $othertitle;
16248:                         } else {
16249:                             $chgtext =~ s/\; $//;
16250:                         }
16251:                         $resulttext .=
16252:                             '<li>'.
16253:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
16254:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
16255:                             '</li>';
16256:                     }
16257:                 }
16258:             }
16259:             if (ref($changes{'searchby'}) eq 'ARRAY') {
16260:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
16261:                 my $chgtext;
16262:                 foreach my $type (@{$titleorder}) {
16263:                     if (grep(/^\Q$type\E$/,@searchby)) {
16264:                         if (defined($searchtitles->{$type})) {
16265:                             $chgtext .= $searchtitles->{$type}.'; ';
16266:                         }
16267:                     }
16268:                 }
16269:                 $chgtext =~ s/\; $//;
16270:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
16271:             }
16272:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
16273:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
16274:                 my $chgtext;
16275:                 foreach my $type (@{$srchtypeorder}) {
16276:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
16277:                         if (defined($srchtypes_desc->{$type})) {
16278:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
16279:                         }
16280:                     }
16281:                 }
16282:                 $chgtext =~ s/\; $//;
16283:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
16284:             }
16285:             $resulttext .= '</ul>';
16286:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
16287:             if (ref($lastactref) eq 'HASH') {
16288:                 $lastactref->{'directorysrch'} = 1;
16289:             }
16290:         } else {
16291:             $resulttext = &mt('No changes made to directory search settings');
16292:         }
16293:     } else {
16294:         $resulttext = '<span class="LC_error">'.
16295:                       &mt('An error occurred: [_1]',$putresult).'</span>';
16296:     }
16297:     return $resulttext;
16298: }
16299: 
16300: sub modify_contacts {
16301:     my ($dom,$lastactref,%domconfig) = @_;
16302:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
16303:     if (ref($domconfig{'contacts'}) eq 'HASH') {
16304:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
16305:             $currsetting{$key} = $domconfig{'contacts'}{$key};
16306:         }
16307:     }
16308:     my (%others,%to,%bcc,%includestr,%includeloc);
16309:     my @contacts = ('supportemail','adminemail');
16310:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
16311:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
16312:     my @toggles = ('reporterrors','reportupdates','reportstatus');
16313:     my @lonstatus = ('threshold','sysmail','weights','excluded');
16314:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
16315:     foreach my $type (@mailings) {
16316:         @{$newsetting{$type}} = 
16317:             &Apache::loncommon::get_env_multiple('form.'.$type);
16318:         foreach my $item (@contacts) {
16319:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
16320:                 $contacts_hash{contacts}{$type}{$item} = 1;
16321:             } else {
16322:                 $contacts_hash{contacts}{$type}{$item} = 0;
16323:             }
16324:         }
16325:         $others{$type} = $env{'form.'.$type.'_others'};
16326:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
16327:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16328:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
16329:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
16330:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
16331:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
16332:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
16333:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16334:             }
16335:         }
16336:     }
16337:     foreach my $item (@contacts) {
16338:         $to{$item} = $env{'form.'.$item};
16339:         $contacts_hash{'contacts'}{$item} = $to{$item};
16340:     }
16341:     foreach my $item (@toggles) {
16342:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
16343:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
16344:         }
16345:     }
16346:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
16347:     foreach my $item (@lonstatus) {
16348:         if ($item eq 'excluded') {
16349:             my (%serverhomes,@excluded);
16350:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
16351:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
16352:             if (@possexcluded) {
16353:                 foreach my $id (sort(@possexcluded)) {
16354:                     if ($serverhomes{$id}) {
16355:                         push(@excluded,$id);
16356:                     }
16357:                 }
16358:             }
16359:             if (@excluded) {
16360:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
16361:             }
16362:         } elsif ($item eq 'weights') {
16363:             foreach my $type ('E','W','N','U') {
16364:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
16365:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
16366:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
16367:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
16368:                             $env{'form.error'.$item.'_'.$type};
16369:                     }
16370:                 }
16371:             }
16372:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
16373:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
16374:             if ($env{'form.error'.$item} =~ /^\d+$/) {
16375:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
16376:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
16377:                 }
16378:             }
16379:         }
16380:     }
16381:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
16382:         foreach my $field (@{$fields}) {
16383:             if (ref($possoptions->{$field}) eq 'ARRAY') {
16384:                 my $value = $env{'form.helpform_'.$field};
16385:                 $value =~ s/^\s+|\s+$//g;
16386:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
16387:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
16388:                     if ($field eq 'screenshot') {
16389:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
16390:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
16391:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
16392:                         }
16393:                     }
16394:                 }
16395:             }
16396:         }
16397:     }
16398:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
16399:     my (@statuses,%usertypeshash,@overrides);
16400:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
16401:         @statuses = @{$types};
16402:         if (ref($usertypes) eq 'HASH') {
16403:             %usertypeshash = %{$usertypes};
16404:         }
16405:     }
16406:     if (@statuses) {
16407:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
16408:         foreach my $type (@possoverrides) {
16409:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
16410:                 push(@overrides,$type);
16411:             }
16412:         }
16413:         if (@overrides) {
16414:             foreach my $type (@overrides) {
16415:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
16416:                 foreach my $item (@contacts) {
16417:                     if (grep(/^\Q$item\E$/,@standard)) {
16418:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
16419:                         $newsetting{'override_'.$type}{$item} = 1;
16420:                     } else {
16421:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
16422:                         $newsetting{'override_'.$type}{$item} = 0;
16423:                     }
16424:                 }
16425:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
16426:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16427:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
16428:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
16429:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
16430:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
16431:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
16432:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
16433:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
16434:                 }
16435:             }     
16436:         }
16437:     }
16438:     if (keys(%currsetting) > 0) {
16439:         foreach my $item (@contacts) {
16440:             if ($to{$item} ne $currsetting{$item}) {
16441:                 $changes{$item} = 1;
16442:             }
16443:         }
16444:         foreach my $type (@mailings) {
16445:             foreach my $item (@contacts) {
16446:                 if (ref($currsetting{$type}) eq 'HASH') {
16447:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
16448:                         push(@{$changes{$type}},$item);
16449:                     }
16450:                 } else {
16451:                     push(@{$changes{$type}},@{$newsetting{$type}});
16452:                 }
16453:             }
16454:             if ($others{$type} ne $currsetting{$type}{'others'}) {
16455:                 push(@{$changes{$type}},'others');
16456:             }
16457:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16458:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
16459:                     push(@{$changes{$type}},'bcc'); 
16460:                 }
16461:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
16462:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
16463:                     push(@{$changes{$type}},'include');
16464:                 }
16465:             }
16466:         }
16467:         if (ref($fields) eq 'ARRAY') {
16468:             if (ref($currsetting{'helpform'}) eq 'HASH') {
16469:                 foreach my $field (@{$fields}) {
16470:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
16471:                         push(@{$changes{'helpform'}},$field);
16472:                     }
16473:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16474:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
16475:                             push(@{$changes{'helpform'}},'maxsize');
16476:                         }
16477:                     }
16478:                 }
16479:             } else {
16480:                 foreach my $field (@{$fields}) {
16481:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16482:                         push(@{$changes{'helpform'}},$field);
16483:                     }
16484:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16485:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16486:                             push(@{$changes{'helpform'}},'maxsize');
16487:                         }
16488:                     }
16489:                 }
16490:             }
16491:         }
16492:         if (@statuses) {
16493:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
16494:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
16495:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
16496:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
16497:                             foreach my $item (@contacts,'bcc','others','include') {
16498:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
16499:                                     push(@{$changes{'overrides'}},$key);
16500:                                     last;
16501:                                 }
16502:                             }
16503:                         } else {
16504:                             push(@{$changes{'overrides'}},$key);
16505:                         }
16506:                     }
16507:                 }
16508:                 foreach my $key (@overrides) {
16509:                     unless (exists($currsetting{'overrides'}{$key})) {
16510:                         push(@{$changes{'overrides'}},$key);
16511:                     }
16512:                 }
16513:             } else {
16514:                 foreach my $key (@overrides) {
16515:                     push(@{$changes{'overrides'}},$key); 
16516:                 }
16517:             }
16518:         }
16519:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
16520:             foreach my $key ('excluded','weights','threshold','sysmail') {
16521:                 if ($key eq 'excluded') {
16522:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16523:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
16524:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16525:                             (@{$currsetting{'lonstatus'}{$key}})) {
16526:                             my @diffs =
16527:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
16528:                                                                    $currsetting{'lonstatus'}{$key});
16529:                             if (@diffs) {
16530:                                 push(@{$changes{'lonstatus'}},$key);
16531:                             }
16532:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
16533:                             push(@{$changes{'lonstatus'}},$key);
16534:                         }
16535:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
16536:                              (@{$currsetting{'lonstatus'}{$key}})) {
16537:                         push(@{$changes{'lonstatus'}},$key);
16538:                     }
16539:                 } elsif ($key eq 'weights') {
16540:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
16541:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
16542:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16543:                             foreach my $type ('E','W','N','U') {
16544:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
16545:                                         $currsetting{'lonstatus'}{$key}{$type}) {
16546:                                     push(@{$changes{'lonstatus'}},$key);
16547:                                     last;
16548:                                 }
16549:                             }
16550:                         } else {
16551:                             foreach my $type ('E','W','N','U') {
16552:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
16553:                                     push(@{$changes{'lonstatus'}},$key);
16554:                                     last;
16555:                                 }
16556:                             }
16557:                         }
16558:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
16559:                         foreach my $type ('E','W','N','U') {
16560:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
16561:                                 push(@{$changes{'lonstatus'}},$key);
16562:                                 last;
16563:                             }
16564:                         }
16565:                     }
16566:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
16567:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16568:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16569:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
16570:                                 push(@{$changes{'lonstatus'}},$key);
16571:                             }
16572:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
16573:                             push(@{$changes{'lonstatus'}},$key);
16574:                         }
16575:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
16576:                         push(@{$changes{'lonstatus'}},$key);
16577:                     }
16578:                 }
16579:             }
16580:         } else {
16581:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16582:                 foreach my $key ('excluded','weights','threshold','sysmail') {
16583:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16584:                         push(@{$changes{'lonstatus'}},$key);
16585:                     }
16586:                 }
16587:             }
16588:         }
16589:     } else {
16590:         my %default;
16591:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
16592:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
16593:         $default{'errormail'} = 'adminemail';
16594:         $default{'packagesmail'} = 'adminemail';
16595:         $default{'helpdeskmail'} = 'supportemail';
16596:         $default{'otherdomsmail'} = 'supportemail';
16597:         $default{'lonstatusmail'} = 'adminemail';
16598:         $default{'requestsmail'} = 'adminemail';
16599:         $default{'updatesmail'} = 'adminemail';
16600:         $default{'hostipmail'} = 'adminemail';
16601:         foreach my $item (@contacts) {
16602:            if ($to{$item} ne $default{$item}) {
16603:                $changes{$item} = 1;
16604:            }
16605:         }
16606:         foreach my $type (@mailings) {
16607:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
16608:                 push(@{$changes{$type}},@{$newsetting{$type}});
16609:             }
16610:             if ($others{$type} ne '') {
16611:                 push(@{$changes{$type}},'others');
16612:             }
16613:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16614:                 if ($bcc{$type} ne '') {
16615:                     push(@{$changes{$type}},'bcc');
16616:                 }
16617:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
16618:                     push(@{$changes{$type}},'include');
16619:                 }
16620:             }
16621:         }
16622:         if (ref($fields) eq 'ARRAY') {
16623:             foreach my $field (@{$fields}) {
16624:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
16625:                     push(@{$changes{'helpform'}},$field);
16626:                 }
16627:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
16628:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
16629:                         push(@{$changes{'helpform'}},'maxsize');
16630:                     }
16631:                 }
16632:             }
16633:         }
16634:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
16635:             foreach my $key ('excluded','weights','threshold','sysmail') {
16636:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
16637:                     push(@{$changes{'lonstatus'}},$key);
16638:                 }
16639:             }
16640:         }
16641:     }
16642:     foreach my $item (@toggles) {
16643:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
16644:             $changes{$item} = 1;
16645:         } elsif ((!$env{'form.'.$item}) &&
16646:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
16647:             $changes{$item} = 1;
16648:         }
16649:     }
16650:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
16651:                                              $dom);
16652:     if ($putresult eq 'ok') {
16653:         if (keys(%changes) > 0) {
16654:             &Apache::loncommon::devalidate_domconfig_cache($dom);
16655:             if (ref($lastactref) eq 'HASH') {
16656:                 $lastactref->{'domainconfig'} = 1;
16657:             }
16658:             my ($titles,$short_titles)  = &contact_titles();
16659:             $resulttext = &mt('Changes made:').'<ul>';
16660:             foreach my $item (@contacts) {
16661:                 if ($changes{$item}) {
16662:                     $resulttext .= '<li>'.$titles->{$item}.
16663:                                     &mt(' set to: ').
16664:                                     '<span class="LC_cusr_emph">'.
16665:                                     $to{$item}.'</span></li>';
16666:                 }
16667:             }
16668:             foreach my $type (@mailings) {
16669:                 if (ref($changes{$type}) eq 'ARRAY') {
16670:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16671:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
16672:                     } else {
16673:                         $resulttext .= '<li>'.$titles->{$type}.': ';
16674:                     }
16675:                     my @text;
16676:                     foreach my $item (@{$newsetting{$type}}) {
16677:                         push(@text,$short_titles->{$item});
16678:                     }
16679:                     if ($others{$type} ne '') {
16680:                         push(@text,$others{$type});
16681:                     }
16682:                     if (@text) {
16683:                         $resulttext .= '<span class="LC_cusr_emph">'.
16684:                                        join(', ',@text).'</span>';
16685:                     }
16686:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
16687:                         if ($bcc{$type} ne '') {
16688:                             my $bcctext;
16689:                             if (@text) {
16690:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
16691:                             } else {
16692:                                 $bcctext = '(Bcc)';
16693:                             }
16694:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
16695:                         } elsif (!@text) {
16696:                             $resulttext .= &mt('No one');
16697:                         }   
16698:                         if ($includestr{$type} ne '') {
16699:                             if ($includeloc{$type} eq 'b') {
16700:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
16701:                             } elsif ($includeloc{$type} eq 's') {
16702:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
16703:                             }
16704:                         }
16705:                     } elsif (!@text) {
16706:                         $resulttext .= &mt('No recipients');
16707:                     }
16708:                     $resulttext .= '</li>';
16709:                 }
16710:             }
16711:             if (ref($changes{'overrides'}) eq 'ARRAY') {
16712:                 my @deletions;
16713:                 foreach my $type (@{$changes{'overrides'}}) {
16714:                     if ($usertypeshash{$type}) {
16715:                         if (grep(/^\Q$type\E/,@overrides)) {
16716:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
16717:                                                       $usertypeshash{$type}).'<ul><li>';
16718:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
16719:                                 my @text;
16720:                                 foreach my $item (@contacts) {
16721:                                     if ($newsetting{'override_'.$type}{$item}) { 
16722:                                         push(@text,$short_titles->{$item});
16723:                                     }
16724:                                 }
16725:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
16726:                                     push(@text,$newsetting{'override_'.$type}{'others'});
16727:                                 }
16728:   
16729:                                 if (@text) {
16730:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
16731:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
16732:                                 }
16733:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
16734:                                     my $bcctext;
16735:                                     if (@text) {
16736:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
16737:                                     } else {
16738:                                         $bcctext = '(Bcc)';
16739:                                     }
16740:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
16741:                                 } elsif (!@text) {
16742:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
16743:                                 }
16744:                                 $resulttext .= '</li>';
16745:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
16746:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
16747:                                     if ($loc eq 'b') {
16748:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
16749:                                     } elsif ($loc eq 's') {
16750:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
16751:                                     }
16752:                                 }
16753:                             }
16754:                             $resulttext .= '</li></ul></li>';
16755:                         } else {
16756:                             push(@deletions,$usertypeshash{$type});
16757:                         }
16758:                     }
16759:                 }
16760:                 if (@deletions) {
16761:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
16762:                                               join(', ',@deletions)).'</li>';
16763:                 }
16764:             }
16765:             my @offon = ('off','on');
16766:             my $corelink = &core_link_msu();
16767:             if ($changes{'reporterrors'}) {
16768:                 $resulttext .= '<li>'.
16769:                                &mt('E-mail error reports to [_1] set to "'.
16770:                                    $offon[$env{'form.reporterrors'}].'".',
16771:                                    $corelink).
16772:                                '</li>';
16773:             }
16774:             if ($changes{'reportupdates'}) {
16775:                 $resulttext .= '<li>'.
16776:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
16777:                                     $offon[$env{'form.reportupdates'}].'".',
16778:                                     $corelink).
16779:                                 '</li>';
16780:             }
16781:             if ($changes{'reportstatus'}) {
16782:                 $resulttext .= '<li>'.
16783:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
16784:                                     $offon[$env{'form.reportstatus'}].'".',
16785:                                     $corelink).
16786:                                 '</li>';
16787:             }
16788:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
16789:                 $resulttext .= '<li>'.
16790:                                &mt('Nightly status check e-mail settings').':<ul>';
16791:                 my (%defval,%use_def,%shown);
16792:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
16793:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
16794:                 $defval{'weights'} =
16795:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
16796:                 $defval{'excluded'} = &mt('None');
16797:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
16798:                     foreach my $item ('threshold','sysmail','weights','excluded') {
16799:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
16800:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
16801:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
16802:                             } elsif ($item eq 'weights') {
16803:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
16804:                                     foreach my $type ('E','W','N','U') {
16805:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
16806:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
16807:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
16808:                                         } else {
16809:                                             $shown{$item} .= $lonstatus_defs->{$type};
16810:                                         }
16811:                                         $shown{$item} .= ', ';
16812:                                     }
16813:                                     $shown{$item} =~ s/, $//;
16814:                                 } else {
16815:                                     $shown{$item} = $defval{$item};
16816:                                 }
16817:                             } elsif ($item eq 'excluded') {
16818:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
16819:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
16820:                                 } else {
16821:                                     $shown{$item} = $defval{$item};
16822:                                 }
16823:                             }
16824:                         } else {
16825:                             $shown{$item} = $defval{$item};
16826:                         }
16827:                     }
16828:                 } else {
16829:                     foreach my $item ('threshold','weights','excluded','sysmail') {
16830:                         $shown{$item} = $defval{$item};
16831:                     }
16832:                 }
16833:                 foreach my $item ('threshold','weights','excluded','sysmail') {
16834:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
16835:                                           $shown{$item}).'</li>';
16836:                 }
16837:                 $resulttext .= '</ul></li>';
16838:             }
16839:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
16840:                 my (@optional,@required,@unused,$maxsizechg);
16841:                 foreach my $field (@{$changes{'helpform'}}) {
16842:                     if ($field eq 'maxsize') {
16843:                         $maxsizechg = 1;
16844:                         next;
16845:                     }
16846:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
16847:                         push(@optional,$field);
16848:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
16849:                         push(@unused,$field);
16850:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
16851:                         push(@required,$field);
16852:                     }
16853:                 }
16854:                 if (@optional) {
16855:                     $resulttext .= '<li>'.
16856:                                    &mt('Help form fields changed to "Optional": [_1].',
16857:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
16858:                                    '</li>';
16859:                 }
16860:                 if (@required) {
16861:                     $resulttext .= '<li>'.
16862:                                    &mt('Help form fields changed to "Required": [_1].',
16863:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
16864:                                    '</li>';
16865:                 }
16866:                 if (@unused) {
16867:                     $resulttext .= '<li>'.
16868:                                    &mt('Help form fields changed to "Not shown": [_1].',
16869:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
16870:                                    '</li>';
16871:                 }
16872:                 if ($maxsizechg) {
16873:                     $resulttext .= '<li>'.
16874:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
16875:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
16876:                                    '</li>';
16877:                 }
16878:             }
16879:             $resulttext .= '</ul>';
16880:         } else {
16881:             $resulttext = &mt('No changes made to contacts and form settings');
16882:         }
16883:     } else {
16884:         $resulttext = '<span class="LC_error">'.
16885:             &mt('An error occurred: [_1].',$putresult).'</span>';
16886:     }
16887:     return $resulttext;
16888: }
16889: 
16890: sub modify_privacy {
16891:     my ($dom,%domconfig) = @_;
16892:     my ($resulttext,%current,%changes);
16893:     if (ref($domconfig{'privacy'}) eq 'HASH') {
16894:         %current = %{$domconfig{'privacy'}};
16895:     }
16896:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
16897:     my @items = ('domain','author','course','community');
16898:     my %names = &Apache::lonlocal::texthash (
16899:                    domain => 'Assigned domain role(s)',
16900:                    author => 'Assigned co-author role(s)',
16901:                    course => 'Assigned course role(s)',
16902:                    community => 'Assigned community role(s)',
16903:                 );
16904:     my %roles = &Apache::lonlocal::texthash (
16905:                    domain => 'Domain role',
16906:                    author => 'Co-author role',
16907:                    course => 'Course role',
16908:                    community => 'Community role',
16909:                 );
16910:     my %titles = &Apache::lonlocal::texthash (
16911:                   approval => 'Approval for role in different domain',
16912:                   othdom   => 'User information available in other domain',
16913:                   priv     => 'Information viewable by privileged user in same domain',
16914:                   unpriv   => 'Information viewable by unprivileged user in same domain',
16915:                   instdom  => 'Other domain shares institution/provider',
16916:                   extdom   => 'Other domain has different institution/provider',
16917:                   none     => 'Not allowed',
16918:                   user     => 'User authorizes',
16919:                   domain   => 'Domain Coordinator authorizes',
16920:                   auto     => 'Unrestricted',
16921:                   notify   => 'Notify when role needs authorization',
16922:     );
16923:     my %fieldnames = &Apache::lonlocal::texthash (
16924:                         id => 'Student/Employee ID',
16925:                         permanentemail => 'E-mail address',
16926:                         lastname => 'Last Name',
16927:                         firstname => 'First Name',
16928:                         middlename => 'Middle Name',
16929:                         generation => 'Generation',
16930:     );
16931:     my ($othertitle,$usertypes,$types) =
16932:         &Apache::loncommon::sorted_inst_types($dom);
16933:     my (%by_ip,%by_location,@intdoms,@instdoms);
16934:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
16935: 
16936:     my %privacyhash = (
16937:                        'approval' => {
16938:                                        instdom => {},
16939:                                        extdom  => {},
16940:                                      },
16941:                        'othdom'   => {},
16942:                        'priv'     => {},
16943:                        'unpriv'   => {},
16944:                       );
16945:     foreach my $item (@items) {
16946:         if (@instdoms > 1) {
16947:             if ($env{'form.privacy_approval_instdom_'.$item} =~ /^(none|user|domain|auto)$/) {
16948:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
16949:             }
16950:             if (ref($current{'approval'}) eq 'HASH') {
16951:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
16952:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
16953:                         $changes{'approval'} = 1;
16954:                     }
16955:                 }
16956:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
16957:                 $changes{'approval'} = 1;
16958:             }
16959:         }
16960:         if (keys(%by_location) > 0) {
16961:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
16962:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
16963:             }
16964:             if (ref($current{'approval'}) eq 'HASH') {
16965:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
16966:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
16967:                         $changes{'approval'} = 1;
16968:                     }
16969:                 }
16970:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
16971:                 $changes{'approval'} = 1;
16972:             }
16973:         }
16974:         foreach my $status ('priv','unpriv') {
16975:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
16976:             my @newvalues;
16977:             foreach my $field (@possibles) {
16978:                 if (grep(/^\Q$field\E$/,@fields)) {
16979:                     $privacyhash{$status}{$item}{$field} = 1;
16980:                     push(@newvalues,$field);
16981:                 }
16982:             }
16983:             @newvalues = sort(@newvalues);
16984:             if (ref($current{$status}) eq 'HASH') {
16985:                 if (ref($current{$status}{$item}) eq 'HASH') {
16986:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
16987:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
16988:                     if (@diffs > 0) {
16989:                         $changes{$status} = 1;
16990:                     }
16991:                 }
16992:             } else {
16993:                 my @stdfields;
16994:                 foreach my $field (@fields) {
16995:                     if ($field eq 'id') {
16996:                         next if ($status eq 'unpriv');
16997:                         next if (($status eq 'priv') && ($item eq 'community'));
16998:                     }
16999:                     push(@stdfields,$field);
17000:                 }
17001:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17002:                 if (@diffs > 0) {
17003:                     $changes{$status} = 1;
17004:                 }
17005:             }
17006:         }
17007:     }
17008:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
17009:         my @statuses;
17010:         if (ref($types) eq 'ARRAY') {
17011:             @statuses = @{$types};
17012:         }
17013:         foreach my $type (@statuses,'default') {
17014:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
17015:             my @newvalues;
17016:             foreach my $field (sort(@possfields)) {
17017:                 if (grep(/^\Q$field\E$/,@fields)) {
17018:                     $privacyhash{'othdom'}{$type}{$field} = 1;
17019:                     push(@newvalues,$field);
17020:                 }
17021:             }
17022:             @newvalues = sort(@newvalues);
17023:             if (ref($current{'othdom'}) eq 'HASH') {
17024:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
17025:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
17026:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
17027:                     if (@diffs > 0) {
17028:                         $changes{'othdom'} = 1;
17029:                     }
17030:                 }
17031:             } else {
17032:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
17033:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
17034:                 if (@diffs > 0) {
17035:                     $changes{'othdom'} = 1;
17036:                 }
17037:             }
17038:         }
17039:         my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
17040:         my %notify;
17041:         foreach my $possdc (&Apache::loncommon::get_env_multiple('form.privacy_notify')) {
17042:             if (exists($domcoords{$possdc})) {
17043:                 $notify{$possdc} = 1;
17044:             }
17045:         }
17046:         my $notify = join(',',sort(keys(%notify)));
17047:         if ($current{'notify'} ne $notify) {
17048:             $changes{'notify'} = 1;
17049:         }
17050:         $privacyhash{'notify'} = $notify;
17051:     }
17052:     my %confighash = (
17053:                         privacy => \%privacyhash,
17054:                      );
17055:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17056:     if ($putresult eq 'ok') {
17057:         if (keys(%changes) > 0) {
17058:             $resulttext = &mt('Changes made: ').'<ul>';
17059:             foreach my $key ('approval','notify','othdom','priv','unpriv') {
17060:                 if ($changes{$key}) {
17061:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17062:                     if ($key eq 'approval') {
17063:                         if (keys(%{$privacyhash{$key}{instdom}})) {
17064:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
17065:                             foreach my $item (@items) {
17066:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
17067:                             }
17068:                             $resulttext .= '</ul></li>';
17069:                         }
17070:                         if (keys(%{$privacyhash{$key}{extdom}})) {
17071:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
17072:                             foreach my $item (@items) {
17073:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
17074:                             }
17075:                             $resulttext .= '</ul></li>';
17076:                         }
17077:                     } elsif ($key eq 'notify') {
17078:                         if ($privacyhash{$key}) {
17079:                             foreach my $dc (split(/,/,$privacyhash{$key})) {
17080:                                 my ($dcname,$dcdom) = split(/:/,$dc);
17081:                                 $resulttext .= '<li>'.&Apache::loncommon::plainname($dcname,$dcdom).'</li>';
17082:                             }
17083:                         } else {
17084:                             $resulttext .= '<li>'.&mt('No DCs to notify').'</li>';
17085:                         }
17086:                     } elsif ($key eq 'othdom') {
17087:                         my @statuses;
17088:                         if (ref($types) eq 'ARRAY') {
17089:                             @statuses = @{$types};
17090:                         }
17091:                         if (ref($privacyhash{$key}) eq 'HASH') {
17092:                             foreach my $status (@statuses,'default') {
17093:                                 if ($status eq 'default') {
17094:                                     $resulttext .= '<li>'.$othertitle.': ';
17095:                                 } elsif (ref($usertypes) eq 'HASH') {
17096:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
17097:                                 } else {
17098:                                     next;
17099:                                 }
17100:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
17101:                                     if (keys(%{$privacyhash{$key}{$status}})) {
17102:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
17103:                                     } else {
17104:                                         $resulttext .= &mt('none');
17105:                                     }
17106:                                 }
17107:                                 $resulttext .= '</li>';
17108:                             }
17109:                         }
17110:                     } else {
17111:                         foreach my $item (@items) {
17112:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
17113:                                 $resulttext .= '<li>'.$names{$item}.': ';
17114:                                 if (keys(%{$privacyhash{$key}{$item}})) {
17115:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
17116:                                 } else {
17117:                                     $resulttext .= &mt('none');
17118:                                 }
17119:                                 $resulttext .= '</li>';
17120:                             }
17121:                         }
17122:                     }
17123:                     $resulttext .= '</ul></li>';
17124:                 }
17125:             }
17126:             $resulttext .= '</ul>';
17127:         } else {
17128:             $resulttext = &mt('No changes made to user information settings');
17129:         }
17130:     } else {
17131:         $resulttext = '<span class="LC_error">'.
17132:             &mt('An error occurred: [_1]',$putresult).'</span>';
17133:     }
17134:     return $resulttext;
17135: }
17136: 
17137: sub modify_passwords {
17138:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17139:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
17140:         $updatedefaults,$updateconf);
17141:     my $customfn = 'resetpw.html';
17142:     if (ref($domconfig{'passwords'}) eq 'HASH') {
17143:         %current = %{$domconfig{'passwords'}};
17144:     }
17145:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17146:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17147:     if (ref($types) eq 'ARRAY') {
17148:         @oktypes = @{$types};
17149:     }
17150:     push(@oktypes,'default');
17151: 
17152:     my %titles = &Apache::lonlocal::texthash (
17153:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
17154:         intauth_check  => 'Check bcrypt cost if authenticated',
17155:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
17156:         permanent      => 'Permanent e-mail address',
17157:         critical       => 'Critical notification address',
17158:         notify         => 'Notification address',
17159:         min            => 'Minimum password length',
17160:         max            => 'Maximum password length',
17161:         chars          => 'Required characters',
17162:         expire         => 'Password expiration (days)',
17163:         numsaved       => 'Number of previous passwords to save',
17164:         reset          => 'Resetting Forgotten Password',
17165:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
17166:         rules          => 'Rules for LON-CAPA Passwords',
17167:         crsownerchg    => 'Course Owner Changing Student Passwords',
17168:         username       => 'Username',
17169:         email          => 'E-mail address',
17170:     );
17171: 
17172: #
17173: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
17174: #
17175:     my (%curr_defaults,%save_defaults);
17176:     if (ref($domconfig{'defaults'}) eq 'HASH') {
17177:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
17178:             if ($key =~ /^intauth_(cost|check|switch)$/) {
17179:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
17180:             } else {
17181:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
17182:             }
17183:         }
17184:     }
17185:     my %staticdefaults = (
17186:         'resetlink'      => 2,
17187:         'resetcase'      => \@oktypes,
17188:         'resetprelink'   => 'both',
17189:         'resetemail'     => ['critical','notify','permanent'],
17190:         'intauth_cost'   => 10,
17191:         'intauth_check'  => 0,
17192:         'intauth_switch' => 0,
17193:     );
17194:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17195:     foreach my $type (@oktypes) {
17196:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
17197:     }
17198:     my $linklife = $env{'form.passwords_link'};
17199:     $linklife =~ s/^\s+|\s+$//g;
17200:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
17201:         $newvalues{'resetlink'} = $linklife;
17202:         if ($current{'resetlink'}) {
17203:             if ($current{'resetlink'} ne $linklife) {
17204:                 $changes{'reset'} = 1;
17205:             }
17206:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17207:             if ($staticdefaults{'resetlink'} ne $linklife) {
17208:                 $changes{'reset'} = 1;
17209:             }
17210:         }
17211:     } elsif ($current{'resetlink'}) {
17212:         $changes{'reset'} = 1;
17213:     }
17214:     my @casesens;
17215:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
17216:     foreach my $case (sort(@posscase)) {
17217:         if (grep(/^\Q$case\E$/,@oktypes)) {
17218:             push(@casesens,$case);
17219:         }
17220:     }
17221:     $newvalues{'resetcase'} = \@casesens;
17222:     if (ref($current{'resetcase'}) eq 'ARRAY') {
17223:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
17224:         if (@diffs > 0) {
17225:             $changes{'reset'} = 1;
17226:         }
17227:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17228:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
17229:         if (@diffs > 0) {
17230:             $changes{'reset'} = 1;
17231:         }
17232:     }
17233:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
17234:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
17235:         if (exists($current{'resetprelink'})) {
17236:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
17237:                 $changes{'reset'} = 1;
17238:             }
17239:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17240:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
17241:                 $changes{'reset'} = 1;
17242:             }
17243:         }
17244:     } elsif ($current{'resetprelink'}) {
17245:         $changes{'reset'} = 1;
17246:     }
17247:     foreach my $type (@oktypes) {
17248:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
17249:         my @postlink;
17250:         foreach my $item (sort(@possplink)) {
17251:             if ($item =~ /^(email|username)$/) {
17252:                 push(@postlink,$item);
17253:             }
17254:         }
17255:         $newvalues{'resetpostlink'}{$type} = \@postlink;
17256:         unless ($changes{'reset'}) {
17257:             if (ref($current{'resetpostlink'}) eq 'HASH') {
17258:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
17259:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
17260:                     if (@diffs > 0) {
17261:                         $changes{'reset'} = 1;
17262:                     }
17263:                 } else {
17264:                     $changes{'reset'} = 1;
17265:                 }
17266:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17267:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
17268:                 if (@diffs > 0) {
17269:                     $changes{'reset'} = 1;
17270:                 }
17271:             }
17272:         }
17273:     }
17274:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
17275:     my @resetemail;
17276:     foreach my $item (sort(@possemailsrc)) {
17277:         if ($item =~ /^(permanent|critical|notify)$/) {
17278:             push(@resetemail,$item);
17279:         }
17280:     }
17281:     $newvalues{'resetemail'} = \@resetemail;
17282:     unless ($changes{'reset'}) {
17283:         if (ref($current{'resetemail'}) eq 'ARRAY') {
17284:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
17285:             if (@diffs > 0) {
17286:                 $changes{'reset'} = 1;
17287:             }
17288:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
17289:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
17290:             if (@diffs > 0) {
17291:                 $changes{'reset'} = 1;
17292:             }
17293:         }
17294:     }
17295:     if ($env{'form.passwords_stdtext'} == 0) {
17296:         $newvalues{'resetremove'} = 1;
17297:         unless ($current{'resetremove'}) {
17298:             $changes{'reset'} = 1;
17299:         }
17300:     } elsif ($current{'resetremove'}) {
17301:         $changes{'reset'} = 1;
17302:     }
17303:     if ($env{'form.passwords_customfile.filename'} ne '') {
17304:         my $servadm = $r->dir_config('lonAdmEMail');
17305:         my ($configuserok,$author_ok,$switchserver) =
17306:             &config_check($dom,$confname,$servadm);
17307:         my $error;
17308:         if ($configuserok eq 'ok') {
17309:             if ($switchserver) {
17310:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
17311:             } else {
17312:                 if ($author_ok eq 'ok') {
17313:                     my $modified = [];
17314:                     my ($result,$customurl) =
17315:                         &Apache::lonconfigsettings::publishlogo($r,'upload','passwords_customfile',$dom,
17316:                                                                 $confname,'customtext/resetpw','','',$customfn,
17317:                                                                 $modified);
17318:                     if ($result eq 'ok') {
17319:                         $newvalues{'resetcustom'} = $customurl;
17320:                         $changes{'reset'} = 1;
17321:                         &update_modify_urls($r,$modified);
17322:                     } else {
17323:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
17324:                     }
17325:                 } else {
17326:                     $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);
17327:                 }
17328:             }
17329:         } else {
17330:             $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);
17331:         }
17332:         if ($error) {
17333:             &Apache::lonnet::logthis($error);
17334:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17335:         }
17336:     } elsif ($current{'resetcustom'}) {
17337:         if ($env{'form.passwords_custom_del'}) {
17338:             $changes{'reset'} = 1;
17339:         } else {
17340:             $newvalues{'resetcustom'} = $current{'resetcustom'};
17341:         }
17342:     }
17343:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
17344:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
17345:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
17346:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
17347:             $changes{'intauth'} = 1;
17348:         }
17349:     } else {
17350:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
17351:     }
17352:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
17353:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
17354:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
17355:             $changes{'intauth'} = 1;
17356:         }
17357:     } else {
17358:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17359:     }
17360:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
17361:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
17362:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
17363:             $changes{'intauth'} = 1;
17364:         }
17365:     } else {
17366:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
17367:     }
17368:     foreach my $item ('cost','check','switch') {
17369:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
17370:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
17371:             $updatedefaults = 1;
17372:         }
17373:     }
17374:     &password_rule_changes('passwords',\%newvalues,\%current,\%changes);
17375:     my %crsownerchg = (
17376:                         by => [],
17377:                         for => [],
17378:                       );
17379:     foreach my $item ('by','for') {
17380:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
17381:         foreach my $type (sort(@posstypes)) {
17382:             if (grep(/^\Q$type\E$/,@oktypes)) {
17383:                 push(@{$crsownerchg{$item}},$type);
17384:             }
17385:         }
17386:     }
17387:     $newvalues{'crsownerchg'} = \%crsownerchg;
17388:     if (ref($current{'crsownerchg'}) eq 'HASH') {
17389:         foreach my $item ('by','for') {
17390:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
17391:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
17392:                 if (@diffs > 0) {
17393:                     $changes{'crsownerchg'} = 1;
17394:                     last;
17395:                 }
17396:             }
17397:         }
17398:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
17399:         foreach my $item ('by','for') {
17400:             if (@{$crsownerchg{$item}} > 0) {
17401:                 $changes{'crsownerchg'} = 1;
17402:                 last;
17403:             }
17404:         }
17405:     }
17406: 
17407:     my %confighash = (
17408:                         defaults  => \%save_defaults,
17409:                         passwords => \%newvalues,
17410:                      );
17411:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
17412: 
17413:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
17414:     if ($putresult eq 'ok') {
17415:         if (keys(%changes) > 0) {
17416:             $resulttext = &mt('Changes made: ').'<ul>';
17417:             foreach my $key ('reset','intauth','rules','crsownerchg') {
17418:                 if ($changes{$key}) {
17419:                     unless ($key eq 'intauth') {
17420:                         $updateconf = 1;
17421:                     }
17422:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
17423:                     if ($key eq 'reset') {
17424:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
17425:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
17426:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
17427:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
17428:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
17429:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
17430:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
17431:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
17432:                             }
17433:                         } else {
17434:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
17435:                         }
17436:                         if ($confighash{'passwords'}{'resetlink'}) {
17437:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
17438:                         } else {
17439:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
17440:                                                   &mt('Will default to 2 hours').'</li>';
17441:                         }
17442:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
17443:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
17444:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
17445:                             } else {
17446:                                 my $casesens;
17447:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
17448:                                     if ($type eq 'default') {
17449:                                         $casesens .= $othertitle.', ';
17450:                                     } elsif ($usertypes->{$type} ne '') {
17451:                                         $casesens .= $usertypes->{$type}.', ';
17452:                                     }
17453:                                 }
17454:                                 $casesens =~ s/\Q, \E$//;
17455:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
17456:                             }
17457:                         } else {
17458:                             $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>';
17459:                         }
17460:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
17461:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
17462:                         } else {
17463:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
17464:                         }
17465:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
17466:                             my $output;
17467:                             if (ref($types) eq 'ARRAY') {
17468:                                 foreach my $type (@{$types}) {
17469:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
17470:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
17471:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
17472:                                         } else {
17473:                                             $output .= $usertypes->{$type}.' -- '.
17474:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
17475:                                         }
17476:                                     }
17477:                                 }
17478:                             }
17479:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
17480:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
17481:                                     $output .= $othertitle.' -- '.&mt('none');
17482:                                 } else {
17483:                                     $output .= $othertitle.' -- '.
17484:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
17485:                                 }
17486:                             }
17487:                             if ($output) {
17488:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
17489:                             } else {
17490:                                 $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>';
17491:                             }
17492:                         } else {
17493:                             $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>';
17494:                         }
17495:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
17496:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
17497:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
17498:                             } else {
17499:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17500:                             }
17501:                         } else {
17502:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
17503:                         }
17504:                         if ($confighash{'passwords'}{'resetremove'}) {
17505:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
17506:                         } else {
17507:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
17508:                         }
17509:                         if ($confighash{'passwords'}{'resetcustom'}) {
17510:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
17511:                                                                             &mt('custom text'),600,500,undef,undef,
17512:                                                                             undef,undef,'background-color:#ffffff');
17513:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
17514:                         } else {
17515:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
17516:                         }
17517:                     } elsif ($key eq 'intauth') {
17518:                         foreach my $item ('cost','switch','check') {
17519:                             my $value = $save_defaults{$key.'_'.$item};
17520:                             if ($item eq 'switch') {
17521:                                 my %optiondesc = &Apache::lonlocal::texthash (
17522:                                                      0 => 'No',
17523:                                                      1 => 'Yes',
17524:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
17525:                                                  );
17526:                                 if ($value =~ /^(0|1|2)$/) {
17527:                                     $value = $optiondesc{$value};
17528:                                 } else {
17529:                                     $value = &mt('none -- defaults to No');
17530:                                 }
17531:                             } elsif ($item eq 'check') {
17532:                                 my %optiondesc = &Apache::lonlocal::texthash (
17533:                                                      0 => 'No',
17534:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
17535:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
17536:                                                  );
17537:                                 if ($value =~ /^(0|1|2)$/) {
17538:                                     $value = $optiondesc{$value};
17539:                                 } else {
17540:                                     $value = &mt('none -- defaults to No');
17541:                                 }
17542:                             }
17543:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
17544:                         }
17545:                     } elsif ($key eq 'rules') {
17546:                         foreach my $rule ('min','max','expire','numsaved') {
17547:                             if ($confighash{'passwords'}{$rule} eq '') {
17548:                                 if ($rule eq 'min') {
17549:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
17550:                                                    ' '.&mt('Default of [_1] will be used',
17551:                                                            $Apache::lonnet::passwdmin).'</li>';
17552:                                 } else {
17553:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
17554:                                 }
17555:                             } else {
17556:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
17557:                             }
17558:                         }
17559:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
17560:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
17561:                                 my %rulenames = &Apache::lonlocal::texthash(
17562:                                                      uc => 'At least one upper case letter',
17563:                                                      lc => 'At least one lower case letter',
17564:                                                      num => 'At least one number',
17565:                                                      spec => 'At least one non-alphanumeric',
17566:                                                    );
17567:                                 my $needed = '<ul><li>'.
17568:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
17569:                                              '</li></ul>'; 
17570:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
17571:                             } else {
17572:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17573:                             }
17574:                         } else {
17575:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
17576:                         }
17577:                     } elsif ($key eq 'crsownerchg') {
17578:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
17579:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
17580:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
17581:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17582:                             } else {
17583:                                 my %crsownerstr;
17584:                                 foreach my $item ('by','for') {
17585:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
17586:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
17587:                                             if ($type eq 'default') {
17588:                                                 $crsownerstr{$item} .= $othertitle.', ';
17589:                                             } elsif ($usertypes->{$type} ne '') {
17590:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
17591:                                             }
17592:                                         }
17593:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
17594:                                     }
17595:                                 }
17596:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
17597:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
17598:                             }
17599:                         } else {
17600:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
17601:                         }
17602:                     }
17603:                     $resulttext .= '</ul></li>';
17604:                 }
17605:             }
17606:             $resulttext .= '</ul>';
17607:         } else {
17608:             $resulttext = &mt('No changes made to password settings');
17609:         }
17610:         my $cachetime = 24*60*60;
17611:         if ($updatedefaults) {
17612:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17613:             if (ref($lastactref) eq 'HASH') {
17614:                 $lastactref->{'domdefaults'} = 1;
17615:             }
17616:         }
17617:         if ($updateconf) {
17618:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
17619:             if (ref($lastactref) eq 'HASH') {
17620:                 $lastactref->{'passwdconf'} = 1;
17621:             }
17622:         }
17623:     } else {
17624:         $resulttext = '<span class="LC_error">'.
17625:             &mt('An error occurred: [_1]',$putresult).'</span>';
17626:     }
17627:     if ($errors) {
17628:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
17629:                        $errors.'</ul></p>';
17630:     }
17631:     return $resulttext;
17632: }
17633: 
17634: sub password_rule_changes {
17635:     my ($prefix,$newvalues,$current,$changes) = @_;
17636:     return unless ((ref($newvalues) eq 'HASH') &&
17637:                    (ref($current) eq 'HASH') &&
17638:                    (ref($changes) eq 'HASH'));
17639:     my (@rules,%staticdefaults);
17640:     if ($prefix eq 'passwords') {
17641:         @rules = ('min','max','expire','numsaved');
17642:     } elsif (($prefix eq 'ltisecrets') || ($prefix eq 'toolsecrets')) {
17643:         @rules = ('min','max');
17644:     }
17645:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
17646:     foreach my $rule (@rules) {
17647:         $env{'form.'.$prefix.'_'.$rule} =~ s/^\s+|\s+$//g;
17648:         my $ruleok;
17649:         if ($rule eq 'expire') {
17650:             if (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+(|\.\d*)$/) &&
17651:                 ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
17652:                 $ruleok = 1;
17653:             }
17654:         } elsif ($rule eq 'min') {
17655:             if ($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) {
17656:                 if ($env{'form.'.$prefix.'_'.$rule} >= $staticdefaults{$rule}) {
17657:                     $ruleok = 1;
17658:                 }
17659:             }
17660:         } elsif (($env{'form.'.$prefix.'_'.$rule} =~ /^\d+$/) &&
17661:                  ($env{'form.'.$prefix.'_'.$rule} ne '0')) {
17662:             $ruleok = 1;
17663:         }
17664:         if ($ruleok) {
17665:             $newvalues->{$rule} = $env{'form.'.$prefix.'_'.$rule};
17666:             if (exists($current->{$rule})) {
17667:                 if ($newvalues->{$rule} ne $current->{$rule}) {
17668:                     $changes->{'rules'} = 1;
17669:                 }
17670:             } elsif ($rule eq 'min') {
17671:                 if ($staticdefaults{$rule} ne $newvalues->{$rule}) {
17672:                     $changes->{'rules'} = 1;
17673:                 }
17674:             } else {
17675:                 $changes->{'rules'} = 1;
17676:             }
17677:         } elsif (exists($current->{$rule})) {
17678:             $changes->{'rules'} = 1;
17679:         }
17680:     }
17681:     my @posschars = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_chars');
17682:     my @chars;
17683:     foreach my $item (sort(@posschars)) {
17684:         if ($item =~ /^(uc|lc|num|spec)$/) {
17685:             push(@chars,$item);
17686:         }
17687:     }
17688:     $newvalues->{'chars'} = \@chars;
17689:     unless ($changes->{'rules'}) {
17690:         if (ref($current->{'chars'}) eq 'ARRAY') {
17691:             my @diffs = &Apache::loncommon::compare_arrays($current->{'chars'},\@chars);
17692:             if (@diffs > 0) {
17693:                 $changes->{'rules'} = 1;
17694:             }
17695:         } else {
17696:             if (@chars > 0) {
17697:                 $changes->{'rules'} = 1;
17698:             }
17699:         }
17700:     }
17701:     return;
17702: }
17703: 
17704: sub modify_usercreation {
17705:     my ($dom,%domconfig) = @_;
17706:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
17707:     my $warningmsg;
17708:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17709:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17710:             if ($key eq 'cancreate') {
17711:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17712:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17713:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
17714:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17715:                         } else {
17716:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17717:                         }
17718:                     }
17719:                 }
17720:             } elsif ($key eq 'email_rule') {
17721:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17722:             } else {
17723:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17724:             }
17725:         }
17726:     }
17727:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
17728:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
17729:     my @contexts = ('author','course','requestcrs');
17730:     foreach my $item(@contexts) {
17731:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
17732:     }
17733:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
17734:         foreach my $item (@contexts) {
17735:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
17736:                 push(@{$changes{'cancreate'}},$item);
17737:             }
17738:         }
17739:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
17740:         foreach my $item (@contexts) {
17741:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
17742:                 if ($cancreate{$item} ne 'any') {
17743:                     push(@{$changes{'cancreate'}},$item);
17744:                 }
17745:             } else {
17746:                 if ($cancreate{$item} ne 'none') {
17747:                     push(@{$changes{'cancreate'}},$item);
17748:                 }
17749:             }
17750:         }
17751:     } else {
17752:         foreach my $item (@contexts)  {
17753:             push(@{$changes{'cancreate'}},$item);
17754:         }
17755:     }
17756: 
17757:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
17758:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
17759:             if (!grep(/^\Q$type\E$/,@username_rule)) {
17760:                 push(@{$changes{'username_rule'}},$type);
17761:             }
17762:         }
17763:         foreach my $type (@username_rule) {
17764:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
17765:                 push(@{$changes{'username_rule'}},$type);
17766:             }
17767:         }
17768:     } else {
17769:         push(@{$changes{'username_rule'}},@username_rule);
17770:     }
17771: 
17772:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
17773:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
17774:             if (!grep(/^\Q$type\E$/,@id_rule)) {
17775:                 push(@{$changes{'id_rule'}},$type);
17776:             }
17777:         }
17778:         foreach my $type (@id_rule) {
17779:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
17780:                 push(@{$changes{'id_rule'}},$type);
17781:             }
17782:         }
17783:     } else {
17784:         push(@{$changes{'id_rule'}},@id_rule);
17785:     }
17786: 
17787:     my @authen_contexts = ('author','course','domain');
17788:     my @authtypes = ('int','krb4','krb5','loc','lti');
17789:     my %authhash;
17790:     foreach my $item (@authen_contexts) {
17791:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
17792:         foreach my $auth (@authtypes) {
17793:             if (grep(/^\Q$auth\E$/,@authallowed)) {
17794:                 $authhash{$item}{$auth} = 1;
17795:             } else {
17796:                 $authhash{$item}{$auth} = 0;
17797:             }
17798:         }
17799:     }
17800:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
17801:         foreach my $item (@authen_contexts) {
17802:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
17803:                 foreach my $auth (@authtypes) {
17804:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
17805:                         push(@{$changes{'authtypes'}},$item);
17806:                         last;
17807:                     }
17808:                 }
17809:             }
17810:         }
17811:     } else {
17812:         foreach my $item (@authen_contexts) {
17813:             push(@{$changes{'authtypes'}},$item);
17814:         }
17815:     }
17816: 
17817:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
17818:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
17819:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
17820:     $save_usercreate{'id_rule'} = \@id_rule;
17821:     $save_usercreate{'username_rule'} = \@username_rule,
17822:     $save_usercreate{'authtypes'} = \%authhash;
17823: 
17824:     my %usercreation_hash =  (
17825:         usercreation     => \%save_usercreate,
17826:     );
17827: 
17828:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
17829:                                              $dom);
17830: 
17831:     if ($putresult eq 'ok') {
17832:         if (keys(%changes) > 0) {
17833:             $resulttext = &mt('Changes made:').'<ul>';
17834:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17835:                 my %lt = &usercreation_types();
17836:                 foreach my $type (@{$changes{'cancreate'}}) {
17837:                     my $chgtext = $lt{$type}.', ';
17838:                     if ($cancreate{$type} eq 'none') {
17839:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
17840:                     } elsif ($cancreate{$type} eq 'any') {
17841:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
17842:                     } elsif ($cancreate{$type} eq 'official') {
17843:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
17844:                     } elsif ($cancreate{$type} eq 'unofficial') {
17845:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
17846:                     }
17847:                     $resulttext .= '<li>'.$chgtext.'</li>';
17848:                 }
17849:             }
17850:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
17851:                 my ($rules,$ruleorder) = 
17852:                     &Apache::lonnet::inst_userrules($dom,'username');
17853:                 my $chgtext = '<ul>';
17854:                 foreach my $type (@username_rule) {
17855:                     if (ref($rules->{$type}) eq 'HASH') {
17856:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
17857:                     }
17858:                 }
17859:                 $chgtext .= '</ul>';
17860:                 if (@username_rule > 0) {
17861:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
17862:                 } else {
17863:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
17864:                 }
17865:             }
17866:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
17867:                 my ($idrules,$idruleorder) = 
17868:                     &Apache::lonnet::inst_userrules($dom,'id');
17869:                 my $chgtext = '<ul>';
17870:                 foreach my $type (@id_rule) {
17871:                     if (ref($idrules->{$type}) eq 'HASH') {
17872:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
17873:                     }
17874:                 }
17875:                 $chgtext .= '</ul>';
17876:                 if (@id_rule > 0) {
17877:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
17878:                 } else {
17879:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
17880:                 }
17881:             }
17882:             my %authname = &authtype_names();
17883:             my %context_title = &context_names();
17884:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
17885:                 my $chgtext = '<ul>';
17886:                 foreach my $type (@{$changes{'authtypes'}}) {
17887:                     my @allowed;
17888:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
17889:                     foreach my $auth (@authtypes) {
17890:                         if ($authhash{$type}{$auth}) {
17891:                             push(@allowed,$authname{$auth});
17892:                         }
17893:                     }
17894:                     if (@allowed > 0) {
17895:                         $chgtext .= join(', ',@allowed).'</li>';
17896:                     } else {
17897:                         $chgtext .= &mt('none').'</li>';
17898:                     }
17899:                 }
17900:                 $chgtext .= '</ul>';
17901:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
17902:                 $resulttext .= '</li>';
17903:             }
17904:             $resulttext .= '</ul>';
17905:         } else {
17906:             $resulttext = &mt('No changes made to user creation settings');
17907:         }
17908:     } else {
17909:         $resulttext = '<span class="LC_error">'.
17910:             &mt('An error occurred: [_1]',$putresult).'</span>';
17911:     }
17912:     if ($warningmsg ne '') {
17913:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17914:     }
17915:     return $resulttext;
17916: }
17917: 
17918: sub modify_selfcreation {
17919:     my ($dom,$lastactref,%domconfig) = @_;
17920:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
17921:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
17922:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17923:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
17924:     if (ref($typesref) eq 'ARRAY') {
17925:         @types = @{$typesref};
17926:     }
17927:     if (ref($usertypesref) eq 'HASH') {
17928:         %usertypes = %{$usertypesref};
17929:     }
17930:     $usertypes{'default'} = $othertitle;
17931: #
17932: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
17933: #
17934:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
17935:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
17936:             if ($key eq 'cancreate') {
17937:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
17938:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
17939:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
17940:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
17941:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
17942:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
17943:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
17944:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
17945:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17946:                         } else {
17947:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
17948:                         }
17949:                     }
17950:                 }
17951:             } elsif ($key eq 'email_rule') {
17952:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
17953:             } else {
17954:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
17955:             }
17956:         }
17957:     }
17958: #
17959: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
17960: #
17961:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17962:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17963:             if ($key eq 'selfcreate') {
17964:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
17965:             } else {
17966:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
17967:             }
17968:         }
17969:     }
17970: #
17971: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
17972: #
17973:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17974:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
17975:             if ($key eq 'inststatusguest') {
17976:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
17977:             } else {
17978:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
17979:             }
17980:         }
17981:     }
17982: 
17983:     my @contexts = ('selfcreate');
17984:     @{$cancreate{'selfcreate'}} = ();
17985:     %{$cancreate{'emailusername'}} = ();
17986:     if (@types) {
17987:         @{$cancreate{'statustocreate'}} = ();
17988:     }
17989:     %{$cancreate{'selfcreateprocessing'}} = ();
17990:     %{$cancreate{'shibenv'}} = ();
17991:     %{$cancreate{'emailverified'}} = ();
17992:     %{$cancreate{'emailoptions'}} = ();
17993:     %{$cancreate{'emaildomain'}} = ();
17994:     my %selfcreatetypes = (
17995:                              sso   => 'users authenticated by institutional single sign on',
17996:                              login => 'users authenticated by institutional log-in',
17997:                              email => 'users verified by e-mail',
17998:                           );
17999: #
18000: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
18001: # is permitted.
18002: #
18003:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
18004: 
18005:     my (@statuses,%email_rule);
18006:     foreach my $item ('login','sso','email') {
18007:         if ($item eq 'email') {
18008:             if ($env{'form.cancreate_email'}) {
18009:                 if (@types) {
18010:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
18011:                     foreach my $status (@poss_statuses) {
18012:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
18013:                             push(@statuses,$status);
18014:                         }
18015:                     }
18016:                     $save_inststatus{'inststatusguest'} = \@statuses;
18017:                 } else {
18018:                     push(@statuses,'default');
18019:                 }
18020:                 if (@statuses) {
18021:                     my %curr_rule;
18022:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
18023:                         foreach my $type (@statuses) {
18024:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
18025:                         }
18026:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
18027:                         foreach my $type (@statuses) {
18028:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
18029:                         }
18030:                     }
18031:                     push(@{$cancreate{'selfcreate'}},'email');
18032:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
18033:                     my %curremaildom;
18034:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
18035:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
18036:                     }
18037:                     foreach my $type (@statuses) {
18038:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
18039:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
18040:                         }
18041:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
18042:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
18043:                         }
18044:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
18045: #
18046: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
18047: #
18048:                             my $chosen = $1;
18049:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
18050:                                 my $emaildom;
18051:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
18052:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
18053:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
18054:                                     if (ref($curremaildom{$type}) eq 'HASH') {
18055:                                         if (exists($curremaildom{$type}{$chosen})) {
18056:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
18057:                                                 push(@{$changes{'cancreate'}},'emaildomain');
18058:                                             }
18059:                                         } elsif ($emaildom ne '') {
18060:                                             push(@{$changes{'cancreate'}},'emaildomain');
18061:                                         }
18062:                                     } elsif ($emaildom ne '') {
18063:                                         push(@{$changes{'cancreate'}},'emaildomain');
18064:                                     } 
18065:                                 }
18066:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18067:                             } elsif ($chosen eq 'custom') {
18068:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
18069:                                 $email_rule{$type} = [];
18070:                                 if (ref($emailrules) eq 'HASH') {
18071:                                     foreach my $rule (@possemail_rules) {
18072:                                         if (exists($emailrules->{$rule})) {
18073:                                             push(@{$email_rule{$type}},$rule);
18074:                                         }
18075:                                     }
18076:                                 }
18077:                                 if (@{$email_rule{$type}}) {
18078:                                     $cancreate{'emailoptions'}{$type} = 'custom';
18079:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
18080:                                         if (@{$curr_rule{$type}} > 0) {
18081:                                             foreach my $rule (@{$curr_rule{$type}}) {
18082:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
18083:                                                     push(@{$changes{'email_rule'}},$type);
18084:                                                 }
18085:                                             }
18086:                                         }
18087:                                         foreach my $type (@{$email_rule{$type}}) {
18088:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
18089:                                                 push(@{$changes{'email_rule'}},$type);
18090:                                             }
18091:                                         }
18092:                                     } else {
18093:                                         push(@{$changes{'email_rule'}},$type);
18094:                                     }
18095:                                 }
18096:                             } else {
18097:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
18098:                             }
18099:                         }
18100:                     }
18101:                     if (@types) {
18102:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18103:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
18104:                             if (@changed) {
18105:                                 push(@{$changes{'inststatus'}},'inststatusguest');
18106:                             }
18107:                         } else {
18108:                             push(@{$changes{'inststatus'}},'inststatusguest');
18109:                         }
18110:                     }
18111:                 } else {
18112:                     delete($env{'form.cancreate_email'});
18113:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18114:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18115:                             push(@{$changes{'inststatus'}},'inststatusguest');
18116:                         }
18117:                     }
18118:                 }
18119:             } else {
18120:                 $save_inststatus{'inststatusguest'} = [];
18121:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
18122:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
18123:                         push(@{$changes{'inststatus'}},'inststatusguest');
18124:                     }
18125:                 }
18126:             }
18127:         } else {
18128:             if ($env{'form.cancreate_'.$item}) {
18129:                 push(@{$cancreate{'selfcreate'}},$item);
18130:             }
18131:         }
18132:     }
18133:     my (%userinfo,%savecaptcha);
18134:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
18135: #
18136: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
18137: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
18138: #
18139: 
18140:     if ($env{'form.cancreate_email'}) {
18141:         push(@contexts,'emailusername');
18142:         if (@statuses) {
18143:             foreach my $type (@statuses) {
18144:                 if (ref($infofields) eq 'ARRAY') {
18145:                     foreach my $field (@{$infofields}) {
18146:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
18147:                             $cancreate{'emailusername'}{$type}{$field} = $1;
18148:                         }
18149:                     }
18150:                 }
18151:             }
18152:         }
18153: #
18154: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
18155: # queued requests for self-creation of account verified by e-mail.
18156: #
18157: 
18158:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
18159:         @approvalnotify = sort(@approvalnotify);
18160:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
18161:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18162:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
18163:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
18164:                     push(@{$changes{'cancreate'}},'notify');
18165:                 }
18166:             } else {
18167:                 if ($cancreate{'notify'}{'approval'}) {
18168:                     push(@{$changes{'cancreate'}},'notify');
18169:                 }
18170:             }
18171:         } elsif ($cancreate{'notify'}{'approval'}) {
18172:             push(@{$changes{'cancreate'}},'notify');
18173:         }
18174: 
18175:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
18176:     }
18177: #  
18178: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
18179: # institutional log-in.
18180: #
18181:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
18182:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
18183:                ($domdefaults{'auth_def'} eq 'localauth'))) {
18184:             $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.').' '.
18185:                           &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.');
18186:         }
18187:     }
18188:     my @fields = ('lastname','firstname','middlename','generation',
18189:                   'permanentemail','id');
18190:     my @shibfields = (@fields,'inststatus');
18191:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18192: #
18193: # Where usernames may created for institutional log-in and/or institutional single sign on:
18194: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
18195: # may self-create accounts 
18196: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
18197: # which the user may supply, if institutional data is unavailable.
18198: #
18199:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
18200:         if (@types) {
18201:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
18202:             push(@contexts,'statustocreate');
18203:             foreach my $type (@types) {
18204:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
18205:                 foreach my $field (@fields) {
18206:                     if (grep(/^\Q$field\E$/,@modifiable)) {
18207:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
18208:                     } else {
18209:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
18210:                     }
18211:                 }
18212:             }
18213:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
18214:                 foreach my $type (@types) {
18215:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
18216:                         foreach my $field (@fields) {
18217:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
18218:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
18219:                                 push(@{$changes{'selfcreate'}},$type);
18220:                                 last;
18221:                             }
18222:                         }
18223:                     }
18224:                 }
18225:             } else {
18226:                 foreach my $type (@types) {
18227:                     push(@{$changes{'selfcreate'}},$type);
18228:                 }
18229:             }
18230:         }
18231:         foreach my $field (@shibfields) {
18232:             if ($env{'form.shibenv_'.$field} ne '') {
18233:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
18234:             }
18235:         }
18236:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
18237:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
18238:                 foreach my $field (@shibfields) {
18239:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
18240:                         push(@{$changes{'cancreate'}},'shibenv');
18241:                     }
18242:                 }
18243:             } else {
18244:                 foreach my $field (@shibfields) {
18245:                     if ($env{'form.shibenv_'.$field}) {
18246:                         push(@{$changes{'cancreate'}},'shibenv');
18247:                         last;
18248:                     }
18249:                 }
18250:             }
18251:         }
18252:     }
18253:     foreach my $item (@contexts) {
18254:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
18255:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
18256:                 if (ref($cancreate{$item}) eq 'ARRAY') {
18257:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
18258:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18259:                             push(@{$changes{'cancreate'}},$item);
18260:                         }
18261:                     }
18262:                 }
18263:             }
18264:             if (ref($cancreate{$item}) eq 'ARRAY') {
18265:                 foreach my $type (@{$cancreate{$item}}) {
18266:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
18267:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18268:                             push(@{$changes{'cancreate'}},$item);
18269:                         }
18270:                     }
18271:                 }
18272:             }
18273:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
18274:             if (ref($cancreate{$item}) eq 'HASH') {
18275:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
18276:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18277:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
18278:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18279:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18280:                                     push(@{$changes{'cancreate'}},$item);
18281:                                 }
18282:                             }
18283:                         }
18284:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18285:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18286:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18287:                                 push(@{$changes{'cancreate'}},$item);
18288:                             }
18289:                         }
18290:                     }
18291:                 }
18292:                 foreach my $type (keys(%{$cancreate{$item}})) {
18293:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18294:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18295:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
18296:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
18297:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18298:                                         push(@{$changes{'cancreate'}},$item);
18299:                                     }
18300:                                 }
18301:                             } else {
18302:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18303:                                     push(@{$changes{'cancreate'}},$item);
18304:                                 }
18305:                             }
18306:                         }
18307:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18308:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
18309:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18310:                                 push(@{$changes{'cancreate'}},$item);
18311:                             }
18312:                         }
18313:                     }
18314:                 }
18315:             }
18316:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
18317:             if (ref($cancreate{$item}) eq 'ARRAY') {
18318:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
18319:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18320:                         push(@{$changes{'cancreate'}},$item);
18321:                     }
18322:                 }
18323:             }
18324:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
18325:             if (ref($cancreate{$item}) eq 'HASH') {
18326:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18327:                     push(@{$changes{'cancreate'}},$item);
18328:                 }
18329:             }
18330:         } elsif ($item eq 'emailusername') {
18331:             if (ref($cancreate{$item}) eq 'HASH') {
18332:                 foreach my $type (keys(%{$cancreate{$item}})) {
18333:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
18334:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
18335:                             if ($cancreate{$item}{$type}{$field}) {
18336:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
18337:                                     push(@{$changes{'cancreate'}},$item);
18338:                                 }
18339:                                 last;
18340:                             }
18341:                         }
18342:                     }
18343:                 }
18344:             }
18345:         }
18346:     }
18347: #
18348: # Populate %save_usercreate hash with updates to self-creation configuration.
18349: #
18350:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
18351:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
18352:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
18353:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
18354:     if (ref($cancreate{'notify'}) eq 'HASH') {
18355:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
18356:     }
18357:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
18358:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
18359:     }
18360:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
18361:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
18362:     }
18363:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
18364:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
18365:     }
18366:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
18367:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
18368:     }
18369:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18370:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
18371:     }
18372:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
18373:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
18374:     }
18375:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
18376:     $save_usercreate{'email_rule'} = \%email_rule;
18377: 
18378:     my %userconfig_hash = (
18379:             usercreation     => \%save_usercreate,
18380:             usermodification => \%save_usermodify,
18381:             inststatus       => \%save_inststatus,
18382:     );
18383: 
18384:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
18385:                                              $dom);
18386: #
18387: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
18388: #
18389:     if ($putresult eq 'ok') {
18390:         if (keys(%changes) > 0) {
18391:             $resulttext = &mt('Changes made:').'<ul>';
18392:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
18393:                 my %lt = &selfcreation_types();
18394:                 foreach my $type (@{$changes{'cancreate'}}) {
18395:                     my $chgtext = '';
18396:                     if ($type eq 'selfcreate') {
18397:                         if (@{$cancreate{$type}} == 0) {
18398:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
18399:                         } else {
18400:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
18401:                                         '<ul>';
18402:                             foreach my $case (@{$cancreate{$type}}) {
18403:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
18404:                             }
18405:                             $chgtext .= '</ul>';
18406:                             if (ref($cancreate{$type}) eq 'ARRAY') {
18407:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
18408:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
18409:                                         if (@{$cancreate{'statustocreate'}} == 0) {
18410:                                             $chgtext .= '<span class="LC_warning">'.
18411:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
18412:                                                         '</span><br />';
18413:                                         }
18414:                                     }
18415:                                 }
18416:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
18417:                                     if (!@statuses) {
18418:                                         $chgtext .= '<span class="LC_warning">'.
18419:                                                     &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.").
18420:                                                     '</span><br />';
18421: 
18422:                                     }
18423:                                 }
18424:                             }
18425:                         }
18426:                     } elsif ($type eq 'shibenv') {
18427:                         if (keys(%{$cancreate{$type}}) == 0) {
18428:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
18429:                         } else {
18430:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
18431:                                         '<ul>';
18432:                             foreach my $field (@shibfields) {
18433:                                 next if ($cancreate{$type}{$field} eq '');
18434:                                 if ($field eq 'inststatus') {
18435:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
18436:                                 } else {
18437:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
18438:                                 }
18439:                             }
18440:                             $chgtext .= '</ul>';
18441:                         }
18442:                     } elsif ($type eq 'statustocreate') {
18443:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
18444:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
18445:                             if (@{$cancreate{'selfcreate'}} > 0) {
18446:                                 if (@{$cancreate{'statustocreate'}} == 0) {
18447:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
18448:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
18449:                                         $chgtext .= '<br />'.
18450:                                                     '<span class="LC_warning">'.
18451:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
18452:                                                     '</span>';
18453:                                     }
18454:                                 } elsif (keys(%usertypes) > 0) {
18455:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18456:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
18457:                                     } else {
18458:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
18459:                                     }
18460:                                     $chgtext .= '<ul>';
18461:                                     foreach my $case (@{$cancreate{$type}}) {
18462:                                         if ($case eq 'default') {
18463:                                             $chgtext .= '<li>'.$othertitle.'</li>';
18464:                                         } else {
18465:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
18466:                                         }
18467:                                     }
18468:                                     $chgtext .= '</ul>';
18469:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
18470:                                         $chgtext .= '<span class="LC_warning">'.
18471:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
18472:                                                     '</span>';
18473:                                     }
18474:                                 }
18475:                             } else {
18476:                                 if (@{$cancreate{$type}} == 0) {
18477:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
18478:                                 } else {
18479:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
18480:                                 }
18481:                             }
18482:                             $chgtext .= '<br />';
18483:                         }
18484:                     } elsif ($type eq 'selfcreateprocessing') {
18485:                         my %choices = &Apache::lonlocal::texthash (
18486:                                                                     automatic => 'Automatic approval',
18487:                                                                     approval  => 'Queued for approval',
18488:                                                                   );
18489:                         if (@types) {
18490:                             if (@statuses) {
18491:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
18492:                                             '<ul>';
18493:                                 foreach my $status (@statuses) {
18494:                                     if ($status eq 'default') {
18495:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18496:                                     } else {
18497:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
18498:                                     }
18499:                                 }
18500:                                 $chgtext .= '</ul>';
18501:                             }
18502:                         } else {
18503:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
18504:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
18505:                         }
18506:                     } elsif ($type eq 'emailverified') {
18507:                         my %options = &Apache::lonlocal::texthash (
18508:                                                                     all   => 'Same as e-mail',
18509:                                                                     first => 'Omit @domain',
18510:                                                                     free  => 'Free to choose',
18511:                                                                   );
18512:                         if (@types) {
18513:                             if (@statuses) {
18514:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
18515:                                             '<ul>';
18516:                                 foreach my $status (@statuses) {
18517:                                     if ($status eq 'default') {
18518:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18519:                                     } else {
18520:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
18521:                                     }
18522:                                 }
18523:                                 $chgtext .= '</ul>';
18524:                             }
18525:                         } else {
18526:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
18527:                                             $options{$cancreate{'emailverified'}{'default'}});
18528:                         }
18529:                     } elsif ($type eq 'emailoptions') {
18530:                         my %options = &Apache::lonlocal::texthash (
18531:                                                                     any     => 'Any e-mail',
18532:                                                                     inst    => 'Institutional only',
18533:                                                                     noninst => 'Non-institutional only',
18534:                                                                     custom  => 'Custom restrictions',
18535:                                                                   );
18536:                         if (@types) {
18537:                             if (@statuses) {
18538:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
18539:                                             '<ul>';
18540:                                 foreach my $status (@statuses) {
18541:                                     if ($type eq 'default') {
18542:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18543:                                     } else {
18544:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
18545:                                     }
18546:                                 }
18547:                                 $chgtext .= '</ul>';
18548:                             }
18549:                         } else {
18550:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
18551:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
18552:                             } else {
18553:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
18554:                                                 $options{$cancreate{'emailoptions'}{'default'}});
18555:                             }
18556:                         }
18557:                     } elsif ($type eq 'emaildomain') {
18558:                         my $output;
18559:                         if (@statuses) {
18560:                             foreach my $type (@statuses) {
18561:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
18562:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
18563:                                         if ($type eq 'default') {
18564:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18565:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18566:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18567:                                             } else {
18568:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
18569:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18570:                                             }
18571:                                         } else {
18572:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18573:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
18574:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18575:                                             } else {
18576:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
18577:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
18578:                                             }
18579:                                         }
18580:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
18581:                                         if ($type eq 'default') {
18582:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18583:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18584:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
18585:                                             } else {
18586:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
18587:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18588:                                             }
18589:                                         } else {
18590:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
18591:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
18592:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
18593:                                             } else {
18594:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
18595:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
18596:                                             }
18597:                                         }
18598:                                     }
18599:                                 }
18600:                             }
18601:                         }
18602:                         if ($output ne '') {
18603:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
18604:                                         '<ul>'.$output.'</ul>';
18605:                         }
18606:                     } elsif ($type eq 'captcha') {
18607:                         if ($savecaptcha{$type} eq 'notused') {
18608:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
18609:                         } else {
18610:                             my %captchas = &captcha_phrases();
18611:                             if ($captchas{$savecaptcha{$type}}) {
18612:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
18613:                             } else {
18614:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
18615:                             }
18616:                         }
18617:                     } elsif ($type eq 'recaptchakeys') {
18618:                         my ($privkey,$pubkey);
18619:                         if (ref($savecaptcha{$type}) eq 'HASH') {
18620:                             $pubkey = $savecaptcha{$type}{'public'};
18621:                             $privkey = $savecaptcha{$type}{'private'};
18622:                         }
18623:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
18624:                         if (!$pubkey) {
18625:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
18626:                         } else {
18627:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
18628:                         }
18629:                         if (!$privkey) {
18630:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
18631:                         } else {
18632:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
18633:                         }
18634:                         $chgtext .= '</ul>';
18635:                     } elsif ($type eq 'recaptchaversion') {
18636:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
18637:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
18638:                         }
18639:                     } elsif ($type eq 'emailusername') {
18640:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
18641:                             if (@statuses) {
18642:                                 foreach my $type (@statuses) {
18643:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
18644:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
18645:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
18646:                                                     '<ul>';
18647:                                             foreach my $field (@{$infofields}) {
18648:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
18649:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
18650:                                                 }
18651:                                             }
18652:                                             $chgtext .= '</ul>';
18653:                                         } else {
18654:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18655:                                         }
18656:                                     } else {
18657:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
18658:                                     }
18659:                                 }
18660:                             }
18661:                         }
18662:                     } elsif ($type eq 'notify') {
18663:                         my $numapprove = 0;
18664:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
18665:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
18666:                                 if ($cancreate{'notify'}{'approval'}) {
18667:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
18668:                                     $numapprove ++;
18669:                                 }
18670:                             }
18671:                         }
18672:                         unless ($numapprove) {
18673:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
18674:                         }
18675:                     }
18676:                     if ($chgtext) {
18677:                         $resulttext .= '<li>'.$chgtext.'</li>';
18678:                     }
18679:                 }
18680:             }
18681:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
18682:                 my ($emailrules,$emailruleorder) =
18683:                     &Apache::lonnet::inst_userrules($dom,'email');
18684:                 foreach my $type (@{$changes{'email_rule'}}) {
18685:                     if (ref($email_rule{$type}) eq 'ARRAY') {
18686:                         my $chgtext = '<ul>';
18687:                         foreach my $rule (@{$email_rule{$type}}) {
18688:                             if (ref($emailrules->{$rule}) eq 'HASH') {
18689:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
18690:                             }
18691:                         }
18692:                         $chgtext .= '</ul>';
18693:                         my $typename;
18694:                         if (@types) {
18695:                             if ($type eq 'default') {
18696:                                 $typename = $othertitle;
18697:                             } else {
18698:                                 $typename = $usertypes{$type};
18699:                             } 
18700:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
18701:                         }
18702:                         if (@{$email_rule{$type}} > 0) {
18703:                             $resulttext .= '<li>'.
18704:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
18705:                                                $usertypes{$type}).
18706:                                            $chgtext.
18707:                                            '</li>';
18708:                         } else {
18709:                             $resulttext .= '<li>'.
18710:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
18711:                                            '</li>'.
18712:                                            &mt('(Affiliation: [_1])',$typename);
18713:                         }
18714:                     }
18715:                 }
18716:             }
18717:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
18718:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
18719:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
18720:                         my $chgtext = '<ul>';
18721:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
18722:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
18723:                         }
18724:                         $chgtext .= '</ul>';
18725:                         $resulttext .= '<li>'.
18726:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
18727:                                           $chgtext.
18728:                                        '</li>';
18729:                     } else {
18730:                         $resulttext .= '<li>'.
18731:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
18732:                                        '</li>';
18733:                     }
18734:                 }
18735:             }
18736:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
18737:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
18738:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18739:                 foreach my $type (@{$changes{'selfcreate'}}) {
18740:                     my $typename = $type;
18741:                     if (keys(%usertypes) > 0) {
18742:                         if ($usertypes{$type} ne '') {
18743:                             $typename = $usertypes{$type};
18744:                         }
18745:                     }
18746:                     my @modifiable;
18747:                     $resulttext .= '<li>'.
18748:                                     &mt('Self-creation of account by users with status: [_1]',
18749:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
18750:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
18751:                     foreach my $field (@fields) {
18752:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
18753:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
18754:                         }
18755:                     }
18756:                     if (@modifiable > 0) {
18757:                         $resulttext .= join(', ',@modifiable);
18758:                     } else {
18759:                         $resulttext .= &mt('none');
18760:                     }
18761:                     $resulttext .= '</li>';
18762:                 }
18763:                 $resulttext .= '</ul></li>';
18764:             }
18765:             $resulttext .= '</ul>';
18766:             my $cachetime = 24*60*60;
18767:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
18768:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18769:             if (ref($lastactref) eq 'HASH') {
18770:                 $lastactref->{'domdefaults'} = 1;
18771:             }
18772:         } else {
18773:             $resulttext = &mt('No changes made to self-creation settings');
18774:         }
18775:     } else {
18776:         $resulttext = '<span class="LC_error">'.
18777:             &mt('An error occurred: [_1]',$putresult).'</span>';
18778:     }
18779:     if ($warningmsg ne '') {
18780:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
18781:     }
18782:     return $resulttext;
18783: }
18784: 
18785: sub process_captcha {
18786:     my ($container,$changes,$newsettings,$currsettings) = @_;
18787:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
18788:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
18789:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
18790:         $newsettings->{'captcha'} = 'original';
18791:     }
18792:     my %current;
18793:     if (ref($currsettings) eq 'HASH') {
18794:         %current = %{$currsettings};
18795:     }
18796:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
18797:         if ($container eq 'cancreate') {
18798:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18799:                 push(@{$changes->{'cancreate'}},'captcha');
18800:             } elsif (!defined($changes->{'cancreate'})) {
18801:                 $changes->{'cancreate'} = ['captcha'];
18802:             }
18803:         } elsif ($container eq 'passwords') {
18804:             $changes->{'reset'} = 1;
18805:         } else {
18806:             $changes->{'captcha'} = 1;
18807:         }
18808:     }
18809:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
18810:     if ($newsettings->{'captcha'} eq 'recaptcha') {
18811:         $newpub = $env{'form.'.$container.'_recaptchapub'};
18812:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
18813:         $newpub =~ s/[^\w\-]//g;
18814:         $newpriv =~ s/[^\w\-]//g;
18815:         $newsettings->{'recaptchakeys'} = {
18816:                                              public  => $newpub,
18817:                                              private => $newpriv,
18818:                                           };
18819:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
18820:         $newversion =~ s/\D//g;
18821:         if ($newversion ne '2') {
18822:             $newversion = 1;
18823:         }
18824:         $newsettings->{'recaptchaversion'} = $newversion;
18825:     }
18826:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
18827:         $currpub = $current{'recaptchakeys'}{'public'};
18828:         $currpriv = $current{'recaptchakeys'}{'private'};
18829:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
18830:             $newsettings->{'recaptchakeys'} = {
18831:                                                  public  => '',
18832:                                                  private => '',
18833:                                               }
18834:         }
18835:     }
18836:     if ($current{'captcha'} eq 'recaptcha') {
18837:         $currversion = $current{'recaptchaversion'};
18838:         if ($currversion ne '2') {
18839:             $currversion = 1;
18840:         }
18841:     }
18842:     if ($currversion ne $newversion) {
18843:         if ($container eq 'cancreate') {
18844:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18845:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
18846:             } elsif (!defined($changes->{'cancreate'})) {
18847:                 $changes->{'cancreate'} = ['recaptchaversion'];
18848:             }
18849:         } elsif ($container eq 'passwords') {
18850:             $changes->{'reset'} = 1;
18851:         } else {
18852:             $changes->{'recaptchaversion'} = 1;
18853:         }
18854:     }
18855:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
18856:         if ($container eq 'cancreate') {
18857:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
18858:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
18859:             } elsif (!defined($changes->{'cancreate'})) {
18860:                 $changes->{'cancreate'} = ['recaptchakeys'];
18861:             }
18862:         } elsif ($container eq 'passwords') {
18863:             $changes->{'reset'} = 1;
18864:         } else {
18865:             $changes->{'recaptchakeys'} = 1;
18866:         }
18867:     }
18868:     return;
18869: }
18870: 
18871: sub modify_usermodification {
18872:     my ($dom,%domconfig) = @_;
18873:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
18874:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
18875:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
18876:             if ($key eq 'selfcreate') {
18877:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
18878:             } else {  
18879:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
18880:             }
18881:         }
18882:     }
18883:     my @contexts = ('author','course');
18884:     my %context_title = (
18885:                            author => 'In author context',
18886:                            course => 'In course context',
18887:                         );
18888:     my @fields = ('lastname','firstname','middlename','generation',
18889:                   'permanentemail','id');
18890:     my %roles = (
18891:                   author => ['ca','aa'],
18892:                   course => ['st','ep','ta','in','cr'],
18893:                 );
18894:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
18895:     foreach my $context (@contexts) {
18896:         foreach my $role (@{$roles{$context}}) {
18897:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
18898:             foreach my $item (@fields) {
18899:                 if (grep(/^\Q$item\E$/,@modifiable)) {
18900:                     $modifyhash{$context}{$role}{$item} = 1;
18901:                 } else {
18902:                     $modifyhash{$context}{$role}{$item} = 0;
18903:                 }
18904:             }
18905:         }
18906:         if (ref($curr_usermodification{$context}) eq 'HASH') {
18907:             foreach my $role (@{$roles{$context}}) {
18908:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
18909:                     foreach my $field (@fields) {
18910:                         if ($modifyhash{$context}{$role}{$field} ne 
18911:                                 $curr_usermodification{$context}{$role}{$field}) {
18912:                             push(@{$changes{$context}},$role);
18913:                             last;
18914:                         }
18915:                     }
18916:                 }
18917:             }
18918:         } else {
18919:             foreach my $context (@contexts) {
18920:                 foreach my $role (@{$roles{$context}}) {
18921:                     push(@{$changes{$context}},$role);
18922:                 }
18923:             }
18924:         }
18925:     }
18926:     my %usermodification_hash =  (
18927:                                    usermodification => \%modifyhash,
18928:                                  );
18929:     my $putresult = &Apache::lonnet::put_dom('configuration',
18930:                                              \%usermodification_hash,$dom);
18931:     if ($putresult eq 'ok') {
18932:         if (keys(%changes) > 0) {
18933:             $resulttext = &mt('Changes made: ').'<ul>';
18934:             foreach my $context (@contexts) {
18935:                 if (ref($changes{$context}) eq 'ARRAY') {
18936:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
18937:                     if (ref($changes{$context}) eq 'ARRAY') {
18938:                         foreach my $role (@{$changes{$context}}) {
18939:                             my $rolename;
18940:                             if ($role eq 'cr') {
18941:                                 $rolename = &mt('Custom');
18942:                             } else {
18943:                                 $rolename = &Apache::lonnet::plaintext($role);
18944:                             }
18945:                             my @modifiable;
18946:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
18947:                             foreach my $field (@fields) {
18948:                                 if ($modifyhash{$context}{$role}{$field}) {
18949:                                     push(@modifiable,$fieldtitles{$field});
18950:                                 }
18951:                             }
18952:                             if (@modifiable > 0) {
18953:                                 $resulttext .= join(', ',@modifiable);
18954:                             } else {
18955:                                 $resulttext .= &mt('none'); 
18956:                             }
18957:                             $resulttext .= '</li>';
18958:                         }
18959:                         $resulttext .= '</ul></li>';
18960:                     }
18961:                 }
18962:             }
18963:             $resulttext .= '</ul>';
18964:         } else {
18965:             $resulttext = &mt('No changes made to user modification settings');
18966:         }
18967:     } else {
18968:         $resulttext = '<span class="LC_error">'.
18969:             &mt('An error occurred: [_1]',$putresult).'</span>';
18970:     }
18971:     return $resulttext;
18972: }
18973: 
18974: sub modify_defaults {
18975:     my ($dom,$lastactref,%domconfig) = @_;
18976:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
18977:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18978:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
18979:                  'portal_def');
18980:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
18981:     foreach my $item (@items) {
18982:         $newvalues{$item} = $env{'form.'.$item};
18983:         if ($item eq 'auth_def') {
18984:             if ($newvalues{$item} ne '') {
18985:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
18986:                     push(@errors,$item);
18987:                 }
18988:             }
18989:         } elsif ($item eq 'lang_def') {
18990:             if ($newvalues{$item} ne '') {
18991:                 if ($newvalues{$item} =~ /^(\w+)/) {
18992:                     my $langcode = $1;
18993:                     if ($langcode ne 'x_chef') {
18994:                         if (code2language($langcode) eq '') {
18995:                             push(@errors,$item);
18996:                         }
18997:                     }
18998:                 } else {
18999:                     push(@errors,$item);
19000:                 }
19001:             }
19002:         } elsif ($item eq 'timezone_def') {
19003:             if ($newvalues{$item} ne '') {
19004:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
19005:                     push(@errors,$item);   
19006:                 }
19007:             }
19008:         } elsif ($item eq 'datelocale_def') {
19009:             if ($newvalues{$item} ne '') {
19010:                 my @datelocale_ids = DateTime::Locale->ids();
19011:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
19012:                     push(@errors,$item);
19013:                 }
19014:             }
19015:         } elsif ($item eq 'portal_def') {
19016:             if ($newvalues{$item} ne '') {
19017:                 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])\/?$/) {
19018:                     foreach my $field ('email','web') {
19019:                         if ($env{'form.'.$item.'_'.$field}) {
19020:                             $newvalues{$item.'_'.$field} = $env{'form.'.$item.'_'.$field};
19021:                         }
19022:                     }
19023:                 } else {
19024:                     push(@errors,$item);
19025:                 }
19026:             }
19027:         }
19028:         if (grep(/^\Q$item\E$/,@errors)) {
19029:             $newvalues{$item} = $domdefaults{$item};
19030:             if ($item eq 'portal_def') {
19031:                 if ($domdefaults{$item}) {
19032:                     foreach my $field ('email','web') {
19033:                         if (exists($domdefaults{$item.'_'.$field})) {
19034:                             $newvalues{$item.'_'.$field} = $domdefaults{$item.'_'.$field};
19035:                         }
19036:                     }
19037:                 }
19038:             }
19039:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
19040:             $changes{$item} = 1;
19041:         }
19042:         if ($item eq 'portal_def') {
19043:             unless (grep(/^\Q$item\E$/,@errors)) {
19044:                 if ($newvalues{$item} eq '') { 
19045:                     foreach my $field ('email','web') {
19046:                         if (exists($domdefaults{$item.'_'.$field})) {
19047:                             delete($domdefaults{$item.'_'.$field});
19048:                         }
19049:                     }
19050:                 } else {
19051:                     unless ($changes{$item}) {
19052:                         foreach my $field ('email','web') {
19053:                             if ($domdefaults{$item.'_'.$field} ne $newvalues{$item.'_'.$field}) {
19054:                                 $changes{$item} = 1;
19055:                                 last;
19056:                             }
19057:                         }
19058:                     }
19059:                     foreach my $field ('email','web') {
19060:                         if ($newvalues{$item.'_'.$field}) {
19061:                             $domdefaults{$item.'_'.$field} = $newvalues{$item.'_'.$field};
19062:                         } elsif (exists($domdefaults{$item.'_'.$field})) {
19063:                             delete($domdefaults{$item.'_'.$field});
19064:                         }
19065:                     }
19066:                 }
19067:             }
19068:         }
19069:         $domdefaults{$item} = $newvalues{$item};
19070:     }
19071:     my %staticdefaults = (
19072:                            'intauth_cost'   => 10,
19073:                            'intauth_check'  => 0,
19074:                            'intauth_switch' => 0,
19075:                          );
19076:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
19077:         if (exists($domdefaults{$item})) {
19078:             $newvalues{$item} = $domdefaults{$item};
19079:         } else {
19080:             $newvalues{$item} = $staticdefaults{$item};
19081:         }
19082:     }
19083:     my ($unamemaprules,$ruleorder);
19084:     my @possunamemaprules = &Apache::loncommon::get_env_multiple('form.unamemap_rule');
19085:     if (@possunamemaprules) {
19086:         ($unamemaprules,$ruleorder) =
19087:             &Apache::lonnet::inst_userrules($dom,'unamemap');
19088:         if ((ref($unamemaprules) eq 'HASH') && (ref($ruleorder) eq 'ARRAY')) {
19089:             if (@{$ruleorder} > 0) {
19090:                 my %possrules;
19091:                 map { $possrules{$_} = 1; } @possunamemaprules;
19092:                 foreach my $rule (@{$ruleorder}) {
19093:                     if ($possrules{$rule}) {
19094:                         push(@{$newvalues{'unamemap_rule'}},$rule);
19095:                     }
19096:                 }
19097:             }
19098:         }
19099:     }
19100:     if (ref($domdefaults{'unamemap_rule'}) eq 'ARRAY') {
19101:         if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19102:             my @rulediffs = &Apache::loncommon::compare_arrays($domdefaults{'unamemap_rule'},
19103:                                                                $newvalues{'unamemap_rule'});
19104:             if (@rulediffs) {
19105:                 $changes{'unamemap_rule'} = 1;
19106:                 $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19107:             }
19108:         } elsif (@{$domdefaults{'unamemap_rule'}} > 0) {
19109:             $changes{'unamemap_rule'} = 1;
19110:             delete($domdefaults{'unamemap_rule'});
19111:         }
19112:     } elsif (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19113:         if (@{$newvalues{'unamemap_rule'}} > 0) {
19114:             $changes{'unamemap_rule'} = 1;
19115:             $domdefaults{'unamemap_rule'} = $newvalues{'unamemap_rule'};
19116:         }
19117:     }
19118:     my %defaults_hash = (
19119:                          defaults => \%newvalues,
19120:                         );
19121:     my $title = &defaults_titles();
19122: 
19123:     my $currinststatus;
19124:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
19125:         $currinststatus = $domconfig{'inststatus'};
19126:     } else {
19127:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19128:         $currinststatus = {
19129:                              inststatustypes => $usertypes,
19130:                              inststatusorder => $types,
19131:                              inststatusguest => [],
19132:                           };
19133:     }
19134:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
19135:     my @allpos;
19136:     my %alltypes;
19137:     my @inststatusguest;
19138:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
19139:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
19140:             unless (grep(/^\Q$type\E$/,@todelete)) {
19141:                 push(@inststatusguest,$type);
19142:             }
19143:         }
19144:     }
19145:     my ($currtitles,$currorder);
19146:     if (ref($currinststatus) eq 'HASH') {
19147:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
19148:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
19149:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
19150:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
19151:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
19152:                     }
19153:                 }
19154:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
19155:                     my $position = $env{'form.inststatus_pos_'.$type};
19156:                     $position =~ s/\D+//g;
19157:                     $allpos[$position] = $type;
19158:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
19159:                     $alltypes{$type} =~ s/`//g;
19160:                 }
19161:             }
19162:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
19163:             $currtitles =~ s/,$//;
19164:         }
19165:     }
19166:     if ($env{'form.addinststatus'}) {
19167:         my $newtype = $env{'form.addinststatus'};
19168:         $newtype =~ s/\W//g;
19169:         unless (exists($alltypes{$newtype})) {
19170:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
19171:             $alltypes{$newtype} =~ s/`//g; 
19172:             my $position = $env{'form.addinststatus_pos'};
19173:             $position =~ s/\D+//g;
19174:             if ($position ne '') {
19175:                 $allpos[$position] = $newtype;
19176:             }
19177:         }
19178:     }
19179:     my @orderedstatus;
19180:     foreach my $type (@allpos) {
19181:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
19182:             push(@orderedstatus,$type);
19183:         }
19184:     }
19185:     foreach my $type (keys(%alltypes)) {
19186:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
19187:             delete($alltypes{$type});
19188:         }
19189:     }
19190:     $defaults_hash{'inststatus'} = {
19191:                                      inststatustypes => \%alltypes,
19192:                                      inststatusorder => \@orderedstatus,
19193:                                      inststatusguest => \@inststatusguest,
19194:                                    };
19195:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
19196:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
19197:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
19198:         }
19199:     }
19200:     if ($currorder ne join(',',@orderedstatus)) {
19201:         $changes{'inststatus'}{'inststatusorder'} = 1;
19202:     }
19203:     my $newtitles;
19204:     foreach my $item (@orderedstatus) {
19205:         $newtitles .= $alltypes{$item}.',';
19206:     }
19207:     $newtitles =~ s/,$//;
19208:     if ($currtitles ne $newtitles) {
19209:         $changes{'inststatus'}{'inststatustypes'} = 1;
19210:     }
19211:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
19212:                                              $dom);
19213:     if ($putresult eq 'ok') {
19214:         if (keys(%changes) > 0) {
19215:             $resulttext = &mt('Changes made:').'<ul>';
19216:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
19217:             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";
19218:             foreach my $item (sort(keys(%changes))) {
19219:                 if ($item eq 'inststatus') {
19220:                     if (ref($changes{'inststatus'}) eq 'HASH') {
19221:                         if (@orderedstatus) {
19222:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
19223:                             foreach my $type (@orderedstatus) { 
19224:                                 $resulttext .= $alltypes{$type}.', ';
19225:                             }
19226:                             $resulttext =~ s/, $//;
19227:                             $resulttext .= '</li>';
19228:                         } else {
19229:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
19230:                         }
19231:                     }
19232:                 } elsif ($item eq 'unamemap_rule') {
19233:                     if (ref($newvalues{'unamemap_rule'}) eq 'ARRAY') {
19234:                         my @rulenames;
19235:                         if (ref($unamemaprules) eq 'HASH') {
19236:                             foreach my $rule (@{$newvalues{'unamemap_rule'}}) {
19237:                                 if (ref($unamemaprules->{$rule}) eq 'HASH') {
19238:                                     push(@rulenames,$unamemaprules->{$rule}->{'name'});
19239:                                 }
19240:                             }
19241:                         }
19242:                         if (@rulenames) {
19243:                             $resulttext .= '<li>'.&mt('Mapping for missing usernames includes: [_1]',
19244:                                                      '<ul><li>'.join('</li><li>',@rulenames).'</li></ul>').
19245:                                            '</li>';
19246:                         } else {
19247:                             $resulttext .= '<li>'.&mt('No mapping for missing usernames via standard log-in').'</li>';
19248:                         }
19249:                     } else {
19250:                         $resulttext .= '<li>'.&mt('Mapping for missing usernames via standard log-in deleted').'</li>';
19251:                     }
19252:                 } else {
19253:                     my $value = $env{'form.'.$item};
19254:                     if ($value eq '') {
19255:                         $value = &mt('none');
19256:                     } elsif ($item eq 'auth_def') {
19257:                         my %authnames = &authtype_names();
19258:                         my %shortauth = (
19259:                                           internal   => 'int',
19260:                                           krb4       => 'krb4',
19261:                                           krb5       => 'krb5',
19262:                                           localauth  => 'loc',
19263:                                           lti        => 'lti',
19264:                         );
19265:                         $value = $authnames{$shortauth{$value}};
19266:                     }
19267:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
19268:                     $mailmsgtext .= "$title->{$item} set to $value\n";
19269:                     if ($item eq 'portal_def') {
19270:                         if ($env{'form.'.$item} ne '') {
19271:                             foreach my $field ('email','web') {
19272:                                 $value = $env{'form.'.$item.'_'.$field};
19273:                                 if ($value) {
19274:                                     $value = &mt('Yes');
19275:                                 } else {
19276:                                     $value = &mt('No');
19277:                                 }
19278:                                 $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$field},$value).'</li>';
19279:                             }
19280:                         }
19281:                     }
19282:                 }
19283:             }
19284:             $resulttext .= '</ul>';
19285:             $mailmsgtext .= "\n";
19286:             my $cachetime = 24*60*60;
19287:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19288:             if (ref($lastactref) eq 'HASH') {
19289:                 $lastactref->{'domdefaults'} = 1;
19290:             }
19291:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
19292:                 my $notify = 1;
19293:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
19294:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
19295:                         $notify = 0;
19296:                     }
19297:                 }
19298:                 if ($notify) {
19299:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
19300:                                                "LON-CAPA Domain Settings Change - $dom",
19301:                                                $mailmsgtext);
19302:                 }
19303:             }
19304:         } else {
19305:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
19306:         }
19307:     } else {
19308:         $resulttext = '<span class="LC_error">'.
19309:             &mt('An error occurred: [_1]',$putresult).'</span>';
19310:     }
19311:     if (@errors > 0) {
19312:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
19313:         foreach my $item (@errors) {
19314:             $resulttext .= ' "'.$title->{$item}.'",';
19315:         }
19316:         $resulttext =~ s/,$//;
19317:     }
19318:     return $resulttext;
19319: }
19320: 
19321: sub modify_scantron {
19322:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19323:     my ($resulttext,%confhash,%changes,$errors);
19324:     my $custom = 'custom.tab';
19325:     my $default = 'default.tab';
19326:     my $servadm = $r->dir_config('lonAdmEMail');
19327:     my ($configuserok,$author_ok,$switchserver) =
19328:         &config_check($dom,$confname,$servadm);
19329:     if ($env{'form.scantronformat.filename'} ne '') {
19330:         my $error;
19331:         if ($configuserok eq 'ok') {
19332:             if ($switchserver) {
19333:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
19334:             } else {
19335:                 if ($author_ok eq 'ok') {
19336:                     my $modified = [];
19337:                     my ($result,$scantronurl) =
19338:                         &Apache::lonconfigsettings::publishlogo($r,'upload','scantronformat',$dom,
19339:                                                                 $confname,'scantron','','',$custom,
19340:                                                                 $modified);
19341:                     if ($result eq 'ok') {
19342:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
19343:                         $changes{'scantronformat'} = 1;
19344:                         &update_modify_urls($r,$modified);
19345:                     } else {
19346:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
19347:                     }
19348:                 } else {
19349:                     $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);
19350:                 }
19351:             }
19352:         } else {
19353:             $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);
19354:         }
19355:         if ($error) {
19356:             &Apache::lonnet::logthis($error);
19357:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
19358:         }
19359:     }
19360:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19361:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
19362:             if ($env{'form.scantronformat_del'}) {
19363:                 $confhash{'scantron'}{'scantronformat'} = '';
19364:                 $changes{'scantronformat'} = 1;
19365:             } else {
19366:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
19367:             }
19368:         }
19369:     }
19370:     my @options = ('hdr','pad','rem');
19371:     my @fields = &scantroncsv_fields();
19372:     my %titles = &scantronconfig_titles();
19373:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
19374:     my ($newdat,$currdat,%newcol,%currcol);
19375:     if (grep(/^dat$/,@formats)) {
19376:         $confhash{'scantron'}{config}{dat} = 1;
19377:         $newdat = 1;
19378:     } else {
19379:         $newdat = 0;
19380:     }
19381:     if (grep(/^csv$/,@formats)) {
19382:         my %bynum;
19383:         foreach my $field (@fields) {
19384:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
19385:                 my $posscol = $1;
19386:                 if (($posscol < 20) && (!$bynum{$posscol})) {
19387:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
19388:                     $bynum{$posscol} = $field;
19389:                     $newcol{$field} = $posscol;
19390:                 }
19391:             }
19392:         }
19393:         if (keys(%newcol)) {
19394:             foreach my $option (@options) {
19395:                 if ($env{'form.scantroncsv_'.$option}) {
19396:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
19397:                 }
19398:             }
19399:         }
19400:     }
19401:     $currdat = 1;
19402:     if (ref($domconfig{'scantron'}) eq 'HASH') {
19403:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
19404:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
19405:                 $currdat = 0;
19406:             }
19407:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19408:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19409:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
19410:                 }
19411:             }
19412:         }
19413:     }
19414:     if ($currdat != $newdat) {
19415:         $changes{'config'} = 1;
19416:     } else {
19417:         foreach my $field (@fields) {
19418:             if ($currcol{$field} ne '') {
19419:                 if ($currcol{$field} ne $newcol{$field}) {
19420:                     $changes{'config'} = 1;
19421:                     last;
19422:                 }
19423:             } elsif ($newcol{$field} ne '') {
19424:                 $changes{'config'} = 1;
19425:                 last;
19426:             }
19427:         }
19428:     }
19429:     if (keys(%confhash) > 0) {
19430:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
19431:                                                  $dom);
19432:         if ($putresult eq 'ok') {
19433:             if (keys(%changes) > 0) {
19434:                 if (ref($confhash{'scantron'}) eq 'HASH') {
19435:                     $resulttext = &mt('Changes made:').'<ul>';
19436:                     if ($changes{'scantronformat'}) {
19437:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
19438:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
19439:                         } else {
19440:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
19441:                         }
19442:                     }
19443:                     if ($changes{'config'}) {
19444:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
19445:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
19446:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
19447:                             }
19448:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
19449:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
19450:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
19451:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
19452:                                         foreach my $field (@fields) {
19453:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
19454:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
19455:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
19456:                                             }
19457:                                         }
19458:                                         $resulttext .= '</ul></li>';
19459:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
19460:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
19461:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
19462:                                                 foreach my $option (@options) {
19463:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
19464:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
19465:                                                     }
19466:                                                 }
19467:                                                 $resulttext .= '</ul></li>';
19468:                                             }
19469:                                         }
19470:                                     }
19471:                                 }
19472:                             }
19473:                         } else {
19474:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
19475:                         }
19476:                     }
19477:                     $resulttext .= '</ul>';
19478:                 } else {
19479:                     $resulttext = &mt('Changes made to bubblesheet format file.');
19480:                 }
19481:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
19482:                 if (ref($lastactref) eq 'HASH') {
19483:                     $lastactref->{'domainconfig'} = 1;
19484:                 }
19485:             } else {
19486:                 $resulttext = &mt('No changes made to bubblesheet format settings');
19487:             }
19488:         } else {
19489:             $resulttext = '<span class="LC_error">'.
19490:                 &mt('An error occurred: [_1]',$putresult).'</span>';
19491:         }
19492:     } else {
19493:         $resulttext = &mt('No changes made to bubblesheet format file'); 
19494:     }
19495:     if ($errors) {
19496:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
19497:                        $errors.'</ul></p>';
19498:     }
19499:     return $resulttext;
19500: }
19501: 
19502: sub modify_coursecategories {
19503:     my ($dom,$lastactref,%domconfig) = @_;
19504:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
19505:         $cathash);
19506:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
19507:     my @catitems = ('unauth','auth');
19508:     my @cattypes = ('std','domonly','codesrch','none');
19509:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19510:         $cathash = $domconfig{'coursecategories'}{'cats'};
19511:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
19512:             $changes{'togglecats'} = 1;
19513:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
19514:         }
19515:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
19516:             $changes{'categorize'} = 1;
19517:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
19518:         }
19519:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
19520:             $changes{'togglecatscomm'} = 1;
19521:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
19522:         }
19523:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
19524:             $changes{'categorizecomm'} = 1;
19525:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
19526: 
19527:         }
19528:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
19529:             $changes{'togglecatsplace'} = 1;
19530:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
19531:         }
19532:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
19533:             $changes{'categorizeplace'} = 1;
19534:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
19535:         }
19536:         foreach my $item (@catitems) {
19537:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19538:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
19539:                     $changes{$item} = 1;
19540:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19541:                 }
19542:             }
19543:         }
19544:     } else {
19545:         $changes{'togglecats'} = 1;
19546:         $changes{'categorize'} = 1;
19547:         $changes{'togglecatscomm'} = 1;
19548:         $changes{'categorizecomm'} = 1;
19549:         $changes{'togglecatsplace'} = 1;
19550:         $changes{'categorizeplace'} = 1;
19551:         $domconfig{'coursecategories'} = {
19552:                                              togglecats => $env{'form.togglecats'},
19553:                                              categorize => $env{'form.categorize'},
19554:                                              togglecatscomm => $env{'form.togglecatscomm'},
19555:                                              categorizecomm => $env{'form.categorizecomm'},
19556:                                              togglecatsplace => $env{'form.togglecatsplace'},
19557:                                              categorizeplace => $env{'form.categorizeplace'},
19558:                                          };
19559:         foreach my $item (@catitems) {
19560:             if ($env{'form.coursecat_'.$item} ne 'std') {
19561:                 $changes{$item} = 1;
19562:             }
19563:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
19564:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
19565:             }
19566:         }
19567:     }
19568:     if (ref($cathash) eq 'HASH') {
19569:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
19570:             push (@deletecategory,'instcode::0');
19571:         }
19572:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
19573:             push(@deletecategory,'communities::0');
19574:         }
19575:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
19576:             push(@deletecategory,'placement::0');
19577:         }
19578:     }
19579:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
19580:     if (ref($cathash) eq 'HASH') {
19581:         if (@deletecategory > 0) {
19582:             #FIXME Need to remove category from all courses using a deleted category 
19583:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
19584:             foreach my $item (@deletecategory) {
19585:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
19586:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
19587:                     $deletions{$item} = 1;
19588:                     &recurse_cat_deletes($item,$cathash,\%deletions);
19589:                 }
19590:             }
19591:         }
19592:         foreach my $item (keys(%{$cathash})) {
19593:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
19594:             if ($cathash->{$item} ne $env{'form.'.$item}) {
19595:                 $reorderings{$item} = 1;
19596:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
19597:             }
19598:             if ($env{'form.addcategory_name_'.$item} ne '') {
19599:                 my $newcat = $env{'form.addcategory_name_'.$item};
19600:                 my $newdepth = $depth+1;
19601:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19602:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
19603:                 $adds{$newitem} = 1; 
19604:             }
19605:             if ($env{'form.subcat_'.$item} ne '') {
19606:                 my $newcat = $env{'form.subcat_'.$item};
19607:                 my $newdepth = $depth+1;
19608:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
19609:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
19610:                 $adds{$newitem} = 1;
19611:             }
19612:         }
19613:     }
19614:     if ($env{'form.instcode'} eq '1') {
19615:         if (ref($cathash) eq 'HASH') {
19616:             my $newitem = 'instcode::0';
19617:             if ($cathash->{$newitem} eq '') {  
19618:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19619:                 $adds{$newitem} = 1;
19620:             }
19621:         } else {
19622:             my $newitem = 'instcode::0';
19623:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
19624:             $adds{$newitem} = 1;
19625:         }
19626:     }
19627:     if ($env{'form.communities'} eq '1') {
19628:         if (ref($cathash) eq 'HASH') {
19629:             my $newitem = 'communities::0';
19630:             if ($cathash->{$newitem} eq '') {
19631:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19632:                 $adds{$newitem} = 1;
19633:             }
19634:         } else {
19635:             my $newitem = 'communities::0';
19636:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
19637:             $adds{$newitem} = 1;
19638:         }
19639:     }
19640:     if ($env{'form.placement'} eq '1') {
19641:         if (ref($cathash) eq 'HASH') {
19642:             my $newitem = 'placement::0';
19643:             if ($cathash->{$newitem} eq '') {
19644:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19645:                 $adds{$newitem} = 1;
19646:             }
19647:         } else {
19648:             my $newitem = 'placement::0';
19649:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
19650:             $adds{$newitem} = 1;
19651:         }
19652:     }
19653:     if ($env{'form.addcategory_name'} ne '') {
19654:         if (($env{'form.addcategory_name'} ne 'instcode') &&
19655:             ($env{'form.addcategory_name'} ne 'communities') &&
19656:             ($env{'form.addcategory_name'} ne 'placement')) {
19657:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
19658:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
19659:             $adds{$newitem} = 1;
19660:         }
19661:     }
19662:     my $putresult;
19663:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19664:         if (keys(%deletions) > 0) {
19665:             foreach my $key (keys(%deletions)) {
19666:                 if ($predelallitems{$key} ne '') {
19667:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
19668:                 }
19669:             }
19670:         }
19671:         my (@chkcats,@chktrails,%chkallitems);
19672:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
19673:         if (ref($chkcats[0]) eq 'ARRAY') {
19674:             my $depth = 0;
19675:             my $chg = 0;
19676:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
19677:                 my $name = $chkcats[0][$i];
19678:                 my $item;
19679:                 if ($name eq '') {
19680:                     $chg ++;
19681:                 } else {
19682:                     $item = &escape($name).'::0';
19683:                     if ($chg) {
19684:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
19685:                     }
19686:                     $depth ++; 
19687:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
19688:                     $depth --;
19689:                 }
19690:             }
19691:         }
19692:     }
19693:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19694:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
19695:         if ($putresult eq 'ok') {
19696:             my %title = (
19697:                          togglecats     => 'Show/Hide a course in catalog',
19698:                          categorize     => 'Assign a category to a course',
19699:                          togglecatscomm => 'Show/Hide a community in catalog',
19700:                          categorizecomm => 'Assign a category to a community',
19701:                         );
19702:             my %level = (
19703:                          dom  => 'set in Domain ("Modify Course/Community")',
19704:                          crs  => 'set in Course ("Course Configuration")',
19705:                          comm => 'set in Community ("Community Configuration")',
19706:                          none     => 'No catalog',
19707:                          std      => 'Standard catalog',
19708:                          domonly  => 'Domain-only catalog',
19709:                          codesrch => 'Code search form',
19710:                         );
19711:             $resulttext = &mt('Changes made:').'<ul>';
19712:             if ($changes{'togglecats'}) {
19713:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
19714:             }
19715:             if ($changes{'categorize'}) {
19716:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
19717:             }
19718:             if ($changes{'togglecatscomm'}) {
19719:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
19720:             }
19721:             if ($changes{'categorizecomm'}) {
19722:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
19723:             }
19724:             if ($changes{'unauth'}) {
19725:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
19726:             }
19727:             if ($changes{'auth'}) {
19728:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
19729:             }
19730:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
19731:                 my $cathash;
19732:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
19733:                     $cathash = $domconfig{'coursecategories'}{'cats'};
19734:                 } else {
19735:                     $cathash = {};
19736:                 } 
19737:                 my (@cats,@trails,%allitems);
19738:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
19739:                 if (keys(%deletions) > 0) {
19740:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
19741:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
19742:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
19743:                     }
19744:                     $resulttext .= '</ul></li>';
19745:                 }
19746:                 if (keys(%reorderings) > 0) {
19747:                     my %sort_by_trail;
19748:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
19749:                     foreach my $key (keys(%reorderings)) {
19750:                         if ($allitems{$key} ne '') {
19751:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19752:                         }
19753:                     }
19754:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19755:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19756:                     }
19757:                     $resulttext .= '</ul></li>';
19758:                 }
19759:                 if (keys(%adds) > 0) {
19760:                     my %sort_by_trail;
19761:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
19762:                     foreach my $key (keys(%adds)) {
19763:                         if ($allitems{$key} ne '') {
19764:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
19765:                         }
19766:                     }
19767:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
19768:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
19769:                     }
19770:                     $resulttext .= '</ul></li>';
19771:                 }
19772:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
19773:                 if (ref($lastactref) eq 'HASH') {
19774:                     $lastactref->{'cats'} = 1;
19775:                 }
19776:             }
19777:             $resulttext .= '</ul>';
19778:             if ($changes{'unauth'} || $changes{'auth'}) {
19779:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
19780:                 if ($changes{'auth'}) {
19781:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
19782:                 }
19783:                 if ($changes{'unauth'}) {
19784:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
19785:                 }
19786:                 my $cachetime = 24*60*60;
19787:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19788:                 if (ref($lastactref) eq 'HASH') {
19789:                     $lastactref->{'domdefaults'} = 1;
19790:                 }
19791:             }
19792:         } else {
19793:             $resulttext = '<span class="LC_error">'.
19794:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19795:         }
19796:     } else {
19797:         $resulttext = &mt('No changes made to course and community categories');
19798:     }
19799:     return $resulttext;
19800: }
19801: 
19802: sub modify_serverstatuses {
19803:     my ($dom,%domconfig) = @_;
19804:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
19805:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
19806:         %currserverstatus = %{$domconfig{'serverstatuses'}};
19807:     }
19808:     my @pages = &serverstatus_pages();
19809:     foreach my $type (@pages) {
19810:         $newserverstatus{$type}{'namedusers'} = '';
19811:         $newserverstatus{$type}{'machines'} = '';
19812:         if (defined($env{'form.'.$type.'_namedusers'})) {
19813:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
19814:             my @okusers;
19815:             foreach my $user (@users) {
19816:                 my ($uname,$udom) = split(/:/,$user);
19817:                 if (($udom =~ /^$match_domain$/) &&   
19818:                     (&Apache::lonnet::domain($udom)) &&
19819:                     ($uname =~ /^$match_username$/)) {
19820:                     if (!grep(/^\Q$user\E/,@okusers)) {
19821:                         push(@okusers,$user);
19822:                     }
19823:                 }
19824:             }
19825:             if (@okusers > 0) {
19826:                  @okusers = sort(@okusers);
19827:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
19828:             }
19829:         }
19830:         if (defined($env{'form.'.$type.'_machines'})) {
19831:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
19832:             my @okmachines;
19833:             foreach my $ip (@machines) {
19834:                 my @parts = split(/\./,$ip);
19835:                 next if (@parts < 4);
19836:                 my $badip = 0;
19837:                 for (my $i=0; $i<4; $i++) {
19838:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
19839:                         $badip = 1;
19840:                         last;
19841:                     }
19842:                 }
19843:                 if (!$badip) {
19844:                     push(@okmachines,$ip);     
19845:                 }
19846:             }
19847:             @okmachines = sort(@okmachines);
19848:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
19849:         }
19850:     }
19851:     my %serverstatushash =  (
19852:                                 serverstatuses => \%newserverstatus,
19853:                             );
19854:     foreach my $type (@pages) {
19855:         foreach my $setting ('namedusers','machines') {
19856:             my (@current,@new);
19857:             if (ref($currserverstatus{$type}) eq 'HASH') {
19858:                 if ($currserverstatus{$type}{$setting} ne '') { 
19859:                     @current = split(/,/,$currserverstatus{$type}{$setting});
19860:                 }
19861:             }
19862:             if ($newserverstatus{$type}{$setting} ne '') {
19863:                 @new = split(/,/,$newserverstatus{$type}{$setting});
19864:             }
19865:             if (@current > 0) {
19866:                 if (@new > 0) {
19867:                     foreach my $item (@current) {
19868:                         if (!grep(/^\Q$item\E$/,@new)) {
19869:                             $changes{$type}{$setting} = 1;
19870:                             last;
19871:                         }
19872:                     }
19873:                     foreach my $item (@new) {
19874:                         if (!grep(/^\Q$item\E$/,@current)) {
19875:                             $changes{$type}{$setting} = 1;
19876:                             last;
19877:                         }
19878:                     }
19879:                 } else {
19880:                     $changes{$type}{$setting} = 1;
19881:                 }
19882:             } elsif (@new > 0) {
19883:                 $changes{$type}{$setting} = 1;
19884:             }
19885:         }
19886:     }
19887:     if (keys(%changes) > 0) {
19888:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
19889:         my $putresult = &Apache::lonnet::put_dom('configuration',
19890:                                                  \%serverstatushash,$dom);
19891:         if ($putresult eq 'ok') {
19892:             $resulttext .= &mt('Changes made:').'<ul>';
19893:             foreach my $type (@pages) {
19894:                 if (ref($changes{$type}) eq 'HASH') {
19895:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
19896:                     if ($changes{$type}{'namedusers'}) {
19897:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
19898:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
19899:                         } else {
19900:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
19901:                         }
19902:                     }
19903:                     if ($changes{$type}{'machines'}) {
19904:                         if ($newserverstatus{$type}{'machines'} eq '') {
19905:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
19906:                         } else {
19907:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
19908:                         }
19909: 
19910:                     }
19911:                     $resulttext .= '</ul></li>';
19912:                 }
19913:             }
19914:             $resulttext .= '</ul>';
19915:         } else {
19916:             $resulttext = '<span class="LC_error">'.
19917:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
19918: 
19919:         }
19920:     } else {
19921:         $resulttext = &mt('No changes made to access to server status pages');
19922:     }
19923:     return $resulttext;
19924: }
19925: 
19926: sub modify_helpsettings {
19927:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
19928:     my ($resulttext,$errors,%changes,%helphash);
19929:     my %defaultchecked = ('submitbugs' => 'on');
19930:     my @offon = ('off','on');
19931:     my @toggles = ('submitbugs');
19932:     my %current = ('submitbugs' => '',
19933:                    'adhoc'      => {},
19934:                   );
19935:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
19936:         %current = %{$domconfig{'helpsettings'}};
19937:     }
19938:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19939:     foreach my $item (@toggles) {
19940:         if ($defaultchecked{$item} eq 'on') { 
19941:             if ($current{$item} eq '') {
19942:                 if ($env{'form.'.$item} eq '0') {
19943:                     $changes{$item} = 1;
19944:                 }
19945:             } elsif ($current{$item} ne $env{'form.'.$item}) {
19946:                 $changes{$item} = 1;
19947:             }
19948:         } elsif ($defaultchecked{$item} eq 'off') {
19949:             if ($current{$item} eq '') {
19950:                 if ($env{'form.'.$item} eq '1') {
19951:                     $changes{$item} = 1;
19952:                 }
19953:             } elsif ($current{$item} ne $env{'form.'.$item}) {
19954:                 $changes{$item} = 1;
19955:             }
19956:         }
19957:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
19958:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
19959:         }
19960:     }
19961:     my $maxnum = $env{'form.helproles_maxnum'};
19962:     my $confname = $dom.'-domainconfig';
19963:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
19964:     my (@allpos,%newsettings,%changedprivs,$newrole);
19965:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
19966:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
19967:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
19968:     my %lt = &Apache::lonlocal::texthash(
19969:                     s      => 'system',
19970:                     d      => 'domain',
19971:                     order  => 'Display order',
19972:                     access => 'Role usage',
19973:                     all    => 'All with domain helpdesk or helpdesk assistant role',
19974:                     dh     => 'All with domain helpdesk role',
19975:                     da     => 'All with domain helpdesk assistant role',
19976:                     none   => 'None',
19977:                     status => 'Determined based on institutional status',
19978:                     inc    => 'Include all, but exclude specific personnel',
19979:                     exc    => 'Exclude all, but include specific personnel',
19980:     );
19981:     for (my $num=0; $num<=$maxnum; $num++) {
19982:         my ($prefix,$identifier,$rolename,%curr);
19983:         if ($num == $maxnum) {
19984:             next unless ($env{'form.newcusthelp'} == $maxnum);
19985:             $identifier = 'custhelp'.$num;
19986:             $prefix = 'helproles_'.$num;
19987:             $rolename = $env{'form.custhelpname'.$num};
19988:             $rolename=~s/[^A-Za-z0-9]//gs;
19989:             next if ($rolename eq '');
19990:             next if (exists($existing{'rolesdef_'.$rolename}));
19991:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
19992:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
19993:                                                      $newprivs{'c'},$confname,$dom);
19994:             if ($result ne 'ok') {
19995:                 $errors .= '<li><span class="LC_error">'.
19996:                            &mt('An error occurred storing the new custom role: [_1]',
19997:                            $result).'</span></li>';
19998:                 next;
19999:             } else {
20000:                 $changedprivs{$rolename} = \%newprivs;
20001:                 $newrole = $rolename;
20002:             }
20003:         } else {
20004:             $prefix = 'helproles_'.$num;
20005:             $rolename = $env{'form.'.$prefix};
20006:             next if ($rolename eq '');
20007:             next unless (exists($existing{'rolesdef_'.$rolename}));
20008:             $identifier = 'custhelp'.$num;
20009:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
20010:             my %currprivs;
20011:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
20012:                 split(/\_/,$existing{'rolesdef_'.$rolename});
20013:             foreach my $level ('c','d','s') {
20014:                 if ($newprivs{$level} ne $currprivs{$level}) {
20015:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
20016:                                                              $newprivs{'c'},$confname,$dom);
20017:                     if ($result ne 'ok') {
20018:                         $errors .= '<li><span class="LC_error">'.
20019:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
20020:                                        $rolename,$result).'</span></li>';
20021:                     } else {
20022:                         $changedprivs{$rolename} = \%newprivs;
20023:                     }
20024:                     last;
20025:                 }
20026:             }
20027:             if (ref($current{'adhoc'}) eq 'HASH') {
20028:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20029:                     %curr = %{$current{'adhoc'}{$rolename}};
20030:                 }
20031:             }
20032:         }
20033:         my $newpos = $env{'form.'.$prefix.'_pos'};
20034:         $newpos =~ s/\D+//g;
20035:         $allpos[$newpos] = $rolename;
20036:         my $newdesc = $env{'form.'.$prefix.'_desc'};
20037:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
20038:         if ($curr{'desc'}) {
20039:             if ($curr{'desc'} ne $newdesc) {
20040:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
20041:                 $newsettings{$rolename}{'desc'} = $newdesc;
20042:             }
20043:         } elsif ($newdesc ne '') {
20044:             $changes{'customrole'}{$rolename}{'desc'} = 1;
20045:             $newsettings{$rolename}{'desc'} = $newdesc;
20046:         }
20047:         my $access = $env{'form.'.$prefix.'_access'};
20048:         if (grep(/^\Q$access\E$/,@accesstypes)) {
20049:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
20050:             if ($access eq 'status') {
20051:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
20052:                 if (scalar(@statuses) == 0) {
20053:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
20054:                 } else {
20055:                     my (@shownstatus,$numtypes);
20056:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20057:                     if (ref($types) eq 'ARRAY') {
20058:                         $numtypes = scalar(@{$types});
20059:                         foreach my $type (sort(@statuses)) {
20060:                             if ($type eq 'default') {
20061:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20062:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
20063:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
20064:                                 push(@shownstatus,$usertypes->{$type});
20065:                             }
20066:                         }
20067:                     }
20068:                     if (grep(/^default$/,@statuses)) {
20069:                         push(@shownstatus,$othertitle);
20070:                     }
20071:                     if (scalar(@shownstatus) == 1+$numtypes) {
20072:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
20073:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
20074:                     } else {
20075:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
20076:                         if (ref($curr{'status'}) eq 'ARRAY') {
20077:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20078:                             if (@diffs) {
20079:                                 $changes{'customrole'}{$rolename}{$access} = 1;
20080:                             }
20081:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20082:                             $changes{'customrole'}{$rolename}{$access} = 1;
20083:                         }
20084:                     }
20085:                 }
20086:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
20087:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
20088:                 my @newspecstaff;
20089:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
20090:                 foreach my $person (sort(@personnel)) {
20091:                     if ($domhelpdesk{$person}) {
20092:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
20093:                     }
20094:                 }
20095:                 if (ref($curr{$access}) eq 'ARRAY') {
20096:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
20097:                     if (@diffs) {
20098:                         $changes{'customrole'}{$rolename}{$access} = 1;
20099:                     }
20100:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20101:                     $changes{'customrole'}{$rolename}{$access} = 1;
20102:                 }
20103:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
20104:                     my ($uname,$udom) = split(/:/,$person);
20105:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
20106:                 }
20107:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
20108:             }
20109:         } else {
20110:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
20111:         }
20112:         unless ($curr{'access'} eq $access) {
20113:             $changes{'customrole'}{$rolename}{'access'} = 1;
20114:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
20115:         }
20116:     }
20117:     if (@allpos > 0) {
20118:         my $idx = 0;
20119:         foreach my $rolename (@allpos) {
20120:             if ($rolename ne '') {
20121:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
20122:                 if (ref($current{'adhoc'}) eq 'HASH') {
20123:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
20124:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
20125:                             $changes{'customrole'}{$rolename}{'order'} = 1;
20126:                             $newsettings{$rolename}{'order'} = $idx+1;
20127:                         }
20128:                     }
20129:                 }
20130:                 $idx ++;
20131:             }
20132:         }
20133:     }
20134:     my $putresult;
20135:     if (keys(%changes) > 0) {
20136:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
20137:         if ($putresult eq 'ok') {
20138:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
20139:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
20140:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
20141:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
20142:                 }
20143:             }
20144:             my $cachetime = 24*60*60;
20145:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20146:             if (ref($lastactref) eq 'HASH') {
20147:                 $lastactref->{'domdefaults'} = 1;
20148:             }
20149:         } else {
20150:             $errors .= '<li><span class="LC_error">'.
20151:                        &mt('An error occurred storing the settings: [_1]',
20152:                            $putresult).'</span></li>';
20153:         }
20154:     }
20155:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
20156:         $resulttext = &mt('Changes made:').'<ul>';
20157:         my (%shownprivs,@levelorder);
20158:         @levelorder = ('c','d','s');
20159:         if ((keys(%changes)) && ($putresult eq 'ok')) {
20160:             foreach my $item (sort(keys(%changes))) {
20161:                 if ($item eq 'submitbugs') {
20162:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
20163:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
20164:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
20165:                 } elsif ($item eq 'customrole') {
20166:                     if (ref($changes{'customrole'}) eq 'HASH') {
20167:                         my @keyorder = ('order','desc','access','status','exc','inc');
20168:                         my %keytext = &Apache::lonlocal::texthash(
20169:                                                                    order  => 'Order',
20170:                                                                    desc   => 'Role description',
20171:                                                                    access => 'Role usage',
20172:                                                                    status => 'Allowed institutional types',
20173:                                                                    exc    => 'Allowed personnel',
20174:                                                                    inc    => 'Disallowed personnel',
20175:                         );
20176:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
20177:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
20178:                                 if ($role eq $newrole) {
20179:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
20180:                                                               $role).'<ul>';
20181:                                 } else {
20182:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20183:                                                               $role).'<ul>';
20184:                                 }
20185:                                 foreach my $key (@keyorder) {
20186:                                     if ($changes{'customrole'}{$role}{$key}) {
20187:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
20188:                                                                   $keytext{$key},$newsettings{$role}{$key}).
20189:                                                        '</li>';
20190:                                     }
20191:                                 }
20192:                                 if (ref($changedprivs{$role}) eq 'HASH') {
20193:                                     $shownprivs{$role} = 1;
20194:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
20195:                                     foreach my $level (@levelorder) {
20196:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20197:                                             next if ($item eq '');
20198:                                             my ($priv) = split(/\&/,$item,2);
20199:                                             if (&Apache::lonnet::plaintext($priv)) {
20200:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20201:                                                 unless ($level eq 'c') {
20202:                                                     $resulttext .= ' ('.$lt{$level}.')';
20203:                                                 }
20204:                                                 $resulttext .= '</li>';
20205:                                             }
20206:                                         }
20207:                                     }
20208:                                     $resulttext .= '</ul>';
20209:                                 }
20210:                                 $resulttext .= '</ul></li>';
20211:                             }
20212:                         }
20213:                     }
20214:                 }
20215:             }
20216:         }
20217:         if (keys(%changedprivs)) {
20218:             foreach my $role (sort(keys(%changedprivs))) {
20219:                 unless ($shownprivs{$role}) {
20220:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
20221:                                               $role).'<ul>'.
20222:                                    '<li>'.&mt('Privileges set to :').'<ul>';
20223:                     foreach my $level (@levelorder) {
20224:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
20225:                             next if ($item eq '');
20226:                             my ($priv) = split(/\&/,$item,2);
20227:                             if (&Apache::lonnet::plaintext($priv)) {
20228:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
20229:                                 unless ($level eq 'c') {
20230:                                     $resulttext .= ' ('.$lt{$level}.')';
20231:                                 }
20232:                                 $resulttext .= '</li>';
20233:                             }
20234:                         }
20235:                     }
20236:                     $resulttext .= '</ul></li></ul></li>';
20237:                 }
20238:             }
20239:         }
20240:         $resulttext .= '</ul>';
20241:     } else {
20242:         $resulttext = &mt('No changes made to help settings');
20243:     }
20244:     if ($errors) {
20245:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
20246:                                     $errors.'</ul>';
20247:     }
20248:     return $resulttext;
20249: }
20250: 
20251: sub modify_coursedefaults {
20252:     my ($dom,$lastactref,%domconfig) = @_;
20253:     my ($resulttext,$errors,%changes,%defaultshash);
20254:     my %defaultchecked = (
20255:                            'canuse_pdfforms' => 'off',
20256:                            'uselcmath'       => 'on',
20257:                            'usejsme'         => 'on',
20258:                            'inline_chem'     => 'on',
20259:                            'ltiauth'         => 'off',
20260:                          );
20261:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme','inline_chem','ltiauth');
20262:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
20263:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
20264:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
20265:                    'mysqltables_textbook','mysqltables_placement');
20266:     my @types = ('official','unofficial','community','textbook','placement');
20267:     my %staticdefaults = (
20268:                            anonsurvey_threshold => 10,
20269:                            uploadquota          => 500,
20270:                            postsubmit           => 60,
20271:                            mysqltables          => 172800,
20272:                            domexttool           => 1,
20273:                          );
20274:     my %texoptions = (
20275:                         MathJax  => 'MathJax',
20276:                         mimetex  => &mt('Convert to Images'),
20277:                         tth      => &mt('TeX to HTML'),
20278:                      );
20279:     $defaultshash{'coursedefaults'} = {};
20280: 
20281:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
20282:         if ($domconfig{'coursedefaults'} eq '') {
20283:             $domconfig{'coursedefaults'} = {};
20284:         }
20285:     }
20286: 
20287:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
20288:         foreach my $item (@toggles) {
20289:             if ($defaultchecked{$item} eq 'on') {
20290:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20291:                     ($env{'form.'.$item} eq '0')) {
20292:                     $changes{$item} = 1;
20293:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20294:                     $changes{$item} = 1;
20295:                 }
20296:             } elsif ($defaultchecked{$item} eq 'off') {
20297:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
20298:                     ($env{'form.'.$item} eq '1')) {
20299:                     $changes{$item} = 1;
20300:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
20301:                     $changes{$item} = 1;
20302:                 }
20303:             }
20304:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
20305:         }
20306:         foreach my $item (@numbers) {
20307:             my ($currdef,$newdef);
20308:             $newdef = $env{'form.'.$item};
20309:             if ($item eq 'anonsurvey_threshold') {
20310:                 $currdef = $domconfig{'coursedefaults'}{$item};
20311:                 $newdef =~ s/\D//g;
20312:                 if ($newdef eq '' || $newdef < 1) {
20313:                     $newdef = 1;
20314:                 }
20315:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
20316:             } else {
20317:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
20318:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
20319:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
20320:                 }
20321:                 $newdef =~ s/[^\w.\-]//g;
20322:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
20323:             }
20324:             if ($currdef ne $newdef) {
20325:                 if ($item eq 'anonsurvey_threshold') {
20326:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
20327:                         $changes{$item} = 1;
20328:                     }
20329:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
20330:                     my $setting = $1;
20331:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
20332:                         $changes{$setting} = 1;
20333:                     }
20334:                 }
20335:             }
20336:         }
20337:         my $texengine;
20338:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
20339:             $texengine = $env{'form.texengine'};
20340:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
20341:             if ($currdef eq '') {
20342:                 unless ($texengine eq $Apache::lonnet::deftex) {
20343:                     $changes{'texengine'} = 1;
20344:                 }
20345:             } elsif ($currdef ne $texengine) {
20346:                 $changes{'texengine'} = 1;
20347:             }
20348:         }
20349:         if ($texengine ne '') {
20350:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
20351:         }
20352:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
20353:         my @currclonecode;
20354:         if (ref($currclone) eq 'HASH') {
20355:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
20356:                 @currclonecode = @{$currclone->{'instcode'}};
20357:             }
20358:         }
20359:         my $newclone;
20360:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
20361:             $newclone = $env{'form.canclone'};
20362:         }
20363:         if ($newclone eq 'instcode') {
20364:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
20365:             my (%codedefaults,@code_order,@clonecode);
20366:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
20367:                                                     \@code_order);
20368:             foreach my $item (@code_order) {
20369:                 if (grep(/^\Q$item\E$/,@newcodes)) {
20370:                     push(@clonecode,$item);
20371:                 }
20372:             }
20373:             if (@clonecode) {
20374:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
20375:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
20376:                 if (@diffs) {
20377:                     $changes{'canclone'} = 1;
20378:                 }
20379:             } else {
20380:                 $newclone eq '';
20381:             }
20382:         } elsif ($newclone ne '') {
20383:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
20384:         }
20385:         if ($newclone ne $currclone) {
20386:             $changes{'canclone'} = 1;
20387:         }
20388:         my %credits;
20389:         foreach my $type (@types) {
20390:             unless ($type eq 'community') {
20391:                 $credits{$type} = $env{'form.'.$type.'_credits'};
20392:                 $credits{$type} =~ s/[^\d.]+//g;
20393:             }
20394:         }
20395:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
20396:             ($env{'form.coursecredits'} eq '1')) {
20397:             $changes{'coursecredits'} = 1;
20398:             foreach my $type (keys(%credits)) {
20399:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20400:             }
20401:         } else {
20402:             if ($env{'form.coursecredits'} eq '1') {
20403:                 foreach my $type (@types) {
20404:                     unless ($type eq 'community') {
20405:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
20406:                             $changes{'coursecredits'} = 1;
20407:                         }
20408:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
20409:                     }
20410:                 }
20411:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20412:                 foreach my $type (@types) {
20413:                     unless ($type eq 'community') {
20414:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
20415:                             $changes{'coursecredits'} = 1;
20416:                             last;
20417:                         }
20418:                     }
20419:                 }
20420:             }
20421:         }
20422:         if ($env{'form.postsubmit'} eq '1') {
20423:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
20424:             my %currtimeout;
20425:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20426:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
20427:                     $changes{'postsubmit'} = 1;
20428:                 }
20429:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20430:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
20431:                 }
20432:             } else {
20433:                 $changes{'postsubmit'} = 1;
20434:             }
20435:             foreach my $type (@types) {
20436:                 my $timeout = $env{'form.'.$type.'_timeout'};
20437:                 $timeout =~ s/\D//g;
20438:                 if ($timeout == $staticdefaults{'postsubmit'}) {
20439:                     $timeout = '';
20440:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
20441:                     $timeout = '0';
20442:                 }
20443:                 unless ($timeout eq '') {
20444:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
20445:                 }
20446:                 if (exists($currtimeout{$type})) {
20447:                     if ($timeout ne $currtimeout{$type}) {
20448:                         $changes{'postsubmit'} = 1;
20449:                     }
20450:                 } elsif ($timeout ne '') {
20451:                     $changes{'postsubmit'} = 1;
20452:                 }
20453:             }
20454:         } else {
20455:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
20456:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20457:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
20458:                     $changes{'postsubmit'} = 1;
20459:                 }
20460:             } else {
20461:                 $changes{'postsubmit'} = 1;
20462:             }
20463:         }
20464:         my (%newdomexttool,%newexttool,%olddomexttool,%oldexttool);
20465:         map { $newdomexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.domexttool');
20466:         map { $newexttool{$_} = 1; } &Apache::loncommon::get_env_multiple('form.exttool'); 
20467:         if (ref($domconfig{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20468:             %olddomexttool = %{$domconfig{'coursedefaults'}{'domexttool'}};
20469:         } else {
20470:            foreach my $type (@types) {
20471:                if ($staticdefaults{'domexttool'}) {
20472:                    $olddomexttool{$type} = 1;
20473:                } else {
20474:                    $olddomexttool{$type} = 0;
20475:                }
20476:             }
20477:         }
20478:         if (ref($domconfig{'coursedefaults'}{'exttool'}) eq 'HASH') {
20479:             %oldexttool = %{$domconfig{'coursedefaults'}{'exttool'}};     
20480:         } else {
20481:             foreach my $type (@types) {
20482:                if ($staticdefaults{'exttool'}) {
20483:                    $oldexttool{$type} = 1;
20484:                } else {
20485:                    $oldexttool{$type} = 0;
20486:                }
20487:             }
20488:         }
20489:         foreach my $type (@types) {
20490:             unless ($newdomexttool{$type}) {
20491:                 $newdomexttool{$type} = 0;
20492:             }
20493:             unless ($newexttool{$type}) {
20494:                 $newexttool{$type} = 0;
20495:             }
20496:             if ($newdomexttool{$type} != $olddomexttool{$type}) {
20497:                 $changes{'domexttool'} = 1;
20498:             }
20499:             if ($newexttool{$type} != $oldexttool{$type}) {
20500:                 $changes{'exttool'} = 1;
20501:             }
20502:         }
20503:         $defaultshash{'coursedefaults'}{'domexttool'} = \%newdomexttool;
20504:         $defaultshash{'coursedefaults'}{'exttool'} = \%newexttool;
20505:     }
20506:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20507:                                              $dom);
20508:     if ($putresult eq 'ok') {
20509:         if (keys(%changes) > 0) {
20510:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20511:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
20512:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
20513:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'}) ||
20514:                 ($changes{'inline_chem'}) || ($changes{'ltiauth'}) || ($changes{'domexttool'}) ||
20515:                 ($changes{'exttool'}) ) {
20516:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','inline_chem','texengine',
20517:                                   'ltiauth') {
20518:                     if ($changes{$item}) {
20519:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
20520:                     }
20521:                 }
20522:                 if ($changes{'coursecredits'}) {
20523:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20524:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
20525:                             $domdefaults{$type.'credits'} =
20526:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
20527:                         }
20528:                     }
20529:                 }
20530:                 if ($changes{'postsubmit'}) {
20531:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20532:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
20533:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20534:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
20535:                                 $domdefaults{$type.'postsubtimeout'} =
20536:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20537:                             }
20538:                         }
20539:                     }
20540:                 }
20541:                 if ($changes{'uploadquota'}) {
20542:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20543:                         foreach my $type (@types) {
20544:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
20545:                         }
20546:                     }
20547:                 }
20548:                 if ($changes{'canclone'}) {
20549:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20550:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20551:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
20552:                             if (@clonecodes) {
20553:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
20554:                             }
20555:                         }
20556:                     } else {
20557:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
20558:                     }
20559:                 }
20560:                 if ($changes{'domexttool'}) {
20561:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20562:                         foreach my $type (@types) {
20563:                             $domdefaults{$type.'domexttool'}=$defaultshash{'coursedefaults'}{'domexttool'}{$type};
20564:                         }
20565:                     }
20566:                 }
20567:                 if ($changes{'exttool'}) {
20568:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
20569:                         foreach my $type (@types) {
20570:                             $domdefaults{$type.'exttool'}=$defaultshash{'coursedefaults'}{'exttool'}{$type};
20571:                         }
20572:                     }
20573:                 }
20574:                 my $cachetime = 24*60*60;
20575:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20576:                 if (ref($lastactref) eq 'HASH') {
20577:                     $lastactref->{'domdefaults'} = 1;
20578:                 }
20579:             }
20580:             $resulttext = &mt('Changes made:').'<ul>';
20581:             foreach my $item (sort(keys(%changes))) {
20582:                 if ($item eq 'canuse_pdfforms') {
20583:                     if ($env{'form.'.$item} eq '1') {
20584:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
20585:                     } else {
20586:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
20587:                     }
20588:                 } elsif ($item eq 'uselcmath') {
20589:                     if ($env{'form.'.$item} eq '1') {
20590:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
20591:                     } else {
20592:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
20593:                     }
20594:                 } elsif ($item eq 'usejsme') {
20595:                     if ($env{'form.'.$item} eq '1') {
20596:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
20597:                     } else {
20598:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
20599:                     }
20600:                 } elsif ($item eq 'inline_chem') {
20601:                     if ($env{'form.'.$item} eq '1') {
20602:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses inline previewer').'</li>';
20603:                     } else {
20604:                         $resulttext .= '<li>'.&mt('Chemical Reaction Response uses pop-up previewer').'</li>';
20605:                     }
20606:                 } elsif ($item eq 'texengine') {
20607:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
20608:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
20609:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
20610:                     }
20611:                 } elsif ($item eq 'anonsurvey_threshold') {
20612:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
20613:                 } elsif ($item eq 'uploadquota') {
20614:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
20615:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
20616:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
20617:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
20618:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
20619:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
20620:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
20621:                                        '</ul>'.
20622:                                        '</li>';
20623:                     } else {
20624:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
20625:                     }
20626:                 } elsif ($item eq 'mysqltables') {
20627:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
20628:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
20629:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
20630:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
20631:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
20632:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
20633:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
20634:                                        '</ul>'.
20635:                                        '</li>';
20636:                     } else {
20637:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
20638:                     }
20639:                 } elsif ($item eq 'postsubmit') {
20640:                     if ($domdefaults{'postsubmit'} eq 'off') {
20641:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
20642:                     } else {
20643:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
20644:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
20645:                             $resulttext .= &mt('durations:').'<ul>';
20646:                             foreach my $type (@types) {
20647:                                 $resulttext .= '<li>';
20648:                                 my $timeout;
20649:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
20650:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
20651:                                 }
20652:                                 my $display;
20653:                                 if ($timeout eq '0') {
20654:                                     $display = &mt('unlimited');
20655:                                 } elsif ($timeout eq '') {
20656:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
20657:                                 } else {
20658:                                     $display = &mt('[quant,_1,second]',$timeout);
20659:                                 }
20660:                                 if ($type eq 'community') {
20661:                                     $resulttext .= &mt('Communities');
20662:                                 } elsif ($type eq 'official') {
20663:                                     $resulttext .= &mt('Official courses');
20664:                                 } elsif ($type eq 'unofficial') {
20665:                                     $resulttext .= &mt('Unofficial courses');
20666:                                 } elsif ($type eq 'textbook') {
20667:                                     $resulttext .= &mt('Textbook courses');
20668:                                 } elsif ($type eq 'placement') {
20669:                                     $resulttext .= &mt('Placement tests');
20670:                                 }
20671:                                 $resulttext .= ' -- '.$display.'</li>';
20672:                             }
20673:                             $resulttext .= '</ul>';
20674:                         }
20675:                         $resulttext .= '</li>';
20676:                     }
20677:                 } elsif ($item eq 'coursecredits') {
20678:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
20679:                         if (($domdefaults{'officialcredits'} eq '') &&
20680:                             ($domdefaults{'unofficialcredits'} eq '') &&
20681:                             ($domdefaults{'textbookcredits'} eq '')) {
20682:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20683:                         } else {
20684:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
20685:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
20686:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
20687:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
20688:                                            '</ul>'.
20689:                                            '</li>';
20690:                         }
20691:                     } else {
20692:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
20693:                     }
20694:                 } elsif ($item eq 'canclone') {
20695:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
20696:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
20697:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
20698:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
20699:                         }
20700:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
20701:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
20702:                     } else {
20703:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
20704:                     }
20705:                 } elsif ($item eq 'ltiauth') {
20706:                     if ($env{'form.'.$item} eq '1') {
20707:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL need not require re-authentication').'</li>';
20708:                     } else {
20709:                         $resulttext .= '<li>'.&mt('LTI launch of deep-linked URL will require re-authentication').'</li>';
20710:                     }
20711:                 } elsif ($item eq 'domexttool') {
20712:                     my @noyes = (&mt('no'),&mt('yes'));
20713:                     if (ref($defaultshash{'coursedefaults'}{'domexttool'}) eq 'HASH') {
20714:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used as follows:').'<ul>'.
20715:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'official'}].'</b>').'</li>'.
20716:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'unofficial'}].'</b>').'</li>'.
20717:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'textbook'}].'</b>').'</li>'.
20718:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'placement'}].'</b>').'</li>'.
20719:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'domexttool'}{'community'}].'</b>').'</li>'.
20720:                                        '</ul>'.
20721:                                        '</li>';
20722:                     } else {
20723:                         $resulttext .= '<li>'.&mt('External Tools defined in the domain may be used in all course types, by default').'</li>';
20724:                     }
20725:                 } elsif ($item eq 'exttool') {
20726:                     my @noyes = (&mt('no'),&mt('yes'));
20727:                     if (ref($defaultshash{'coursedefaults'}{'exttool'}) eq 'HASH') {
20728:                         $resulttext .= '<li>'.&mt('External Tools can be defined and configured in course containers as follows:').'<ul>'.
20729:                                        '<li>'.&mt('Official courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'official'}].'</b>').'</li>'.
20730:                                        '<li>'.&mt('Unofficial courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'unofficial'}].'</b>').'</li>'.
20731:                                        '<li>'.&mt('Textbook courses: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'textbook'}].'</b>').'</li>'.
20732:                                        '<li>'.&mt('Placement tests: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'placement'}].'</b>').'</li>'.
20733:                                        '<li>'.&mt('Communities: [_1]','<b>'.$noyes[$defaultshash{'coursedefaults'}{'exttool'}{'community'}].'</b>').'</li>'.
20734:                                        '</ul>'.
20735:                                        '</li>';
20736:                     } else {
20737:                         $resulttext .= '<li>'.&mt('External Tools can not be defined in any course types, by default').'</li>';
20738:                     }
20739:                 }
20740:             }
20741:             $resulttext .= '</ul>';
20742:         } else {
20743:             $resulttext = &mt('No changes made to course defaults');
20744:         }
20745:     } else {
20746:         $resulttext = '<span class="LC_error">'.
20747:             &mt('An error occurred: [_1]',$putresult).'</span>';
20748:     }
20749:     return $resulttext;
20750: }
20751: 
20752: sub modify_selfenrollment {
20753:     my ($dom,$lastactref,%domconfig) = @_;
20754:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
20755:     my @types = ('official','unofficial','community','textbook','placement');
20756:     my %titles = &tool_titles();
20757:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
20758:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
20759:     $ordered{'default'} = ['types','registered','approval','limit'];
20760: 
20761:     my (%roles,%shown,%toplevel);
20762:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
20763: 
20764:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
20765:         if ($domconfig{'selfenrollment'} eq '') {
20766:             $domconfig{'selfenrollment'} = {};
20767:         }
20768:     }
20769:     %toplevel = (
20770:                   admin      => 'Configuration Rights',
20771:                   default    => 'Default settings',
20772:                   validation => 'Validation of self-enrollment requests',
20773:                 );
20774:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
20775: 
20776:     if (ref($ordered{'admin'}) eq 'ARRAY') {
20777:         foreach my $item (@{$ordered{'admin'}}) {
20778:             foreach my $type (@types) {
20779:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
20780:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
20781:                 } else {
20782:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
20783:                 }
20784:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
20785:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
20786:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
20787:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
20788:                             push(@{$changes{'admin'}{$type}},$item);
20789:                         }
20790:                     } else {
20791:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
20792:                             push(@{$changes{'admin'}{$type}},$item);
20793:                         }
20794:                     }
20795:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
20796:                     push(@{$changes{'admin'}{$type}},$item);
20797:                 }
20798:             }
20799:         }
20800:     }
20801: 
20802:     foreach my $item (@{$ordered{'default'}}) {
20803:         foreach my $type (@types) {
20804:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
20805:             if ($item eq 'types') {
20806:                 unless (($value eq 'all') || ($value eq 'dom')) {
20807:                     $value = '';
20808:                 }
20809:             } elsif ($item eq 'registered') {
20810:                 unless ($value eq '1') {
20811:                     $value = 0;
20812:                 }
20813:             } elsif ($item eq 'approval') {
20814:                 unless ($value =~ /^[012]$/) {
20815:                     $value = 0;
20816:                 }
20817:             } else {
20818:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20819:                     $value = 'none';
20820:                 }
20821:             }
20822:             $selfenrollhash{'default'}{$type}{$item} = $value;
20823:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
20824:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20825:                     if ($selfenrollhash{'default'}{$type}{$item} ne
20826:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
20827:                          push(@{$changes{'default'}{$type}},$item);
20828:                     }
20829:                 } else {
20830:                     push(@{$changes{'default'}{$type}},$item);
20831:                 }
20832:             } else {
20833:                 push(@{$changes{'default'}{$type}},$item);
20834:             }
20835:             if ($item eq 'limit') {
20836:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
20837:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
20838:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
20839:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
20840:                     }
20841:                 } else {
20842:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
20843:                 }
20844:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
20845:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
20846:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
20847:                          push(@{$changes{'default'}{$type}},'cap');
20848:                     }
20849:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
20850:                     push(@{$changes{'default'}{$type}},'cap');
20851:                 }
20852:             }
20853:         }
20854:     }
20855: 
20856:     foreach my $item (@{$itemsref}) {
20857:         if ($item eq 'fields') {
20858:             my @changed;
20859:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
20860:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
20861:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
20862:             }
20863:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20864:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
20865:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
20866:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
20867:                 } else {
20868:                     @changed = @{$selfenrollhash{'validation'}{$item}};
20869:                 }
20870:             } else {
20871:                 @changed = @{$selfenrollhash{'validation'}{$item}};
20872:             }
20873:             if (@changed) {
20874:                 if ($selfenrollhash{'validation'}{$item}) { 
20875:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
20876:                 } else {
20877:                     $changes{'validation'}{$item} = &mt('None');
20878:                 }
20879:             }
20880:         } else {
20881:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
20882:             if ($item eq 'markup') {
20883:                if ($env{'form.selfenroll_validation_'.$item}) {
20884:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
20885:                }
20886:             }
20887:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
20888:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
20889:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
20890:                 }
20891:             }
20892:         }
20893:     }
20894: 
20895:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
20896:                                              $dom);
20897:     if ($putresult eq 'ok') {
20898:         if (keys(%changes) > 0) {
20899:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20900:             $resulttext = &mt('Changes made:').'<ul>';
20901:             foreach my $key ('admin','default','validation') {
20902:                 if (ref($changes{$key}) eq 'HASH') {
20903:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
20904:                     if ($key eq 'validation') {
20905:                         foreach my $item (@{$itemsref}) {
20906:                             if (exists($changes{$key}{$item})) {
20907:                                 if ($item eq 'markup') {
20908:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20909:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
20910:                                 } else {  
20911:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
20912:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
20913:                                 }
20914:                             }
20915:                         }
20916:                     } else {
20917:                         foreach my $type (@types) {
20918:                             if ($type eq 'community') {
20919:                                 $roles{'1'} = &mt('Community personnel');
20920:                             } else {
20921:                                 $roles{'1'} = &mt('Course personnel');
20922:                             }
20923:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
20924:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
20925:                                     if ($key eq 'admin') {
20926:                                         my @mgrdc = ();
20927:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20928:                                             foreach my $item (@{$ordered{'admin'}}) {
20929:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
20930:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
20931:                                                         push(@mgrdc,$item);
20932:                                                     }
20933:                                                 }
20934:                                             }
20935:                                             if (@mgrdc) {
20936:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
20937:                                             } else {
20938:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
20939:                                             }
20940:                                         }
20941:                                     } else {
20942:                                         if (ref($ordered{$key}) eq 'ARRAY') {
20943:                                             foreach my $item (@{$ordered{$key}}) {
20944:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20945:                                                     $domdefaults{$type.'selfenroll'.$item} =
20946:                                                         $selfenrollhash{$key}{$type}{$item};
20947:                                                 }
20948:                                             }
20949:                                         }
20950:                                     }
20951:                                 }
20952:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
20953:                                 foreach my $item (@{$ordered{$key}}) {
20954:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
20955:                                         $resulttext .= '<li>';
20956:                                         if ($key eq 'admin') {
20957:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
20958:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
20959:                                         } else {
20960:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
20961:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
20962:                                         }
20963:                                         $resulttext .= '</li>';
20964:                                     }
20965:                                 }
20966:                                 $resulttext .= '</ul></li>';
20967:                             }
20968:                         }
20969:                         $resulttext .= '</ul></li>'; 
20970:                     }
20971:                 }
20972:             }
20973:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
20974:                 my $cachetime = 24*60*60;
20975:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20976:                 if (ref($lastactref) eq 'HASH') {
20977:                     $lastactref->{'domdefaults'} = 1;
20978:                 }
20979:             }
20980:             $resulttext .= '</ul>';
20981:         } else {
20982:             $resulttext = &mt('No changes made to self-enrollment settings');
20983:         }
20984:     } else {
20985:         $resulttext = '<span class="LC_error">'.
20986:             &mt('An error occurred: [_1]',$putresult).'</span>';
20987:     }
20988:     return $resulttext;
20989: }
20990: 
20991: sub modify_wafproxy {
20992:     my ($dom,$action,$lastactref,%domconfig) = @_;
20993:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20994:     my (%othercontrol,%canset,%values,%curralias,%currsaml,%currvalue,@warnings,
20995:         %wafproxy,%changes,%expirecache,%expiresaml);
20996:     foreach my $server (sort(keys(%servers))) {
20997:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20998:         if ($serverhome eq $server) {
20999:             my $serverdom = &Apache::lonnet::host_domain($server);
21000:             if ($serverdom eq $dom) {
21001:                 $canset{$server} = 1;
21002:             }
21003:         }
21004:     }
21005:     if (ref($domconfig{'wafproxy'}) eq 'HASH') {
21006:         %{$values{$dom}} = ();
21007:         if (ref($domconfig{'wafproxy'}{'alias'}) eq 'HASH') {
21008:             %curralias = %{$domconfig{'wafproxy'}{'alias'}};
21009:         }
21010:         if (ref($domconfig{'wafproxy'}{'saml'}) eq 'HASH') {
21011:             %currsaml = %{$domconfig{'wafproxy'}{'saml'}};
21012:         }
21013:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21014:             $currvalue{$item} = $domconfig{'wafproxy'}{$item};
21015:         }
21016:     }
21017:     my $output;
21018:     if (keys(%canset)) {
21019:         %{$wafproxy{'alias'}} = ();
21020:         %{$wafproxy{'saml'}} = ();
21021:         foreach my $key (sort(keys(%canset))) {
21022:             if ($env{'form.wafproxy_'.$dom}) {
21023:                 $wafproxy{'alias'}{$key} = $env{'form.wafproxy_alias_'.$key};
21024:                 $wafproxy{'alias'}{$key} =~ s/^\s+|\s+$//g;
21025:                 if ($wafproxy{'alias'}{$key} ne $curralias{$key}) {
21026:                     $changes{'alias'} = 1;
21027:                 }
21028:                 if ($env{'form.wafproxy_alias_saml_'.$key}) {
21029:                     $wafproxy{'saml'}{$key} = 1;
21030:                 }
21031:                 if ($wafproxy{'saml'}{$key} ne $currsaml{$key}) {
21032:                     $changes{'saml'} = 1;
21033:                 }
21034:             } else {
21035:                 $wafproxy{'alias'}{$key} = '';
21036:                 $wafproxy{'saml'}{$key} = '';
21037:                 if ($curralias{$key}) {
21038:                     $changes{'alias'} = 1;
21039:                 }
21040:                 if ($currsaml{$key}) {
21041:                     $changes{'saml'} = 1;
21042:                 }
21043:             }
21044:             if ($wafproxy{'alias'}{$key} eq '') {
21045:                 if ($curralias{$key}) {
21046:                     $expirecache{$key} = 1;
21047:                 }
21048:                 delete($wafproxy{'alias'}{$key});
21049:             }
21050:             if ($wafproxy{'saml'}{$key} eq '') {
21051:                 if ($currsaml{$key}) {
21052:                     $expiresaml{$key} = 1;
21053:                 }
21054:                 delete($wafproxy{'saml'}{$key});
21055:             }
21056:         }
21057:         unless (keys(%{$wafproxy{'alias'}})) {
21058:             delete($wafproxy{'alias'});
21059:         }
21060:         unless (keys(%{$wafproxy{'saml'}})) {
21061:             delete($wafproxy{'saml'});
21062:         }
21063:         # Localization for values in %warn occurs in &mt() calls separately.
21064:         my %warn = (
21065:                      trusted => 'trusted IP range(s)',
21066:                      vpnint => 'internal IP range(s) for VPN sessions(s)',
21067:                      vpnext => 'IP range(s) for backend WAF connections',
21068:                    );
21069:         foreach my $item ('remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21070:             my $possible = $env{'form.wafproxy_'.$item};
21071:             $possible =~ s/^\s+|\s+$//g;
21072:             if ($possible ne '') {
21073:                 if ($item eq 'remoteip') {
21074:                     if ($possible =~ /^[mhn]$/) {
21075:                         $wafproxy{$item} = $possible;
21076:                     }
21077:                 } elsif ($item eq 'ipheader') {
21078:                     if ($wafproxy{'remoteip'} eq 'h') {
21079:                         $wafproxy{$item} = $possible;
21080:                     }
21081:                 } elsif ($item eq 'sslopt') {
21082:                     if ($possible =~ /^0|1$/) {
21083:                         $wafproxy{$item} = $possible;
21084:                     }
21085:                 } else {
21086:                     my (@ok,$count);
21087:                     if (($item eq 'vpnint') || ($item eq 'vpnext')) {
21088:                         unless ($env{'form.wafproxy_vpnaccess'}) {
21089:                             $possible = '';
21090:                         }
21091:                     } elsif ($item eq 'trusted') {
21092:                         unless ($wafproxy{'remoteip'} eq 'h') {
21093:                             $possible = '';
21094:                         }
21095:                     }
21096:                     unless ($possible eq '') {
21097:                         $possible =~ s/[\r\n]+/\s/g;
21098:                         $possible =~ s/\s*-\s*/-/g;
21099:                         $possible =~ s/\s+/,/g;
21100:                         $possible =~ s/,+/,/g;
21101:                     }
21102:                     $count = 0;
21103:                     if ($possible ne '') {
21104:                         foreach my $poss (split(/\,/,$possible)) {
21105:                             $count ++;
21106:                             $poss = &validate_ip_pattern($poss);
21107:                             if ($poss ne '') {
21108:                                 push(@ok,$poss);
21109:                             }
21110:                         }
21111:                         my $diff = $count - scalar(@ok);
21112:                         if ($diff) {
21113:                             push(@warnings,'<li>'.
21114:                                  &mt('[quant,_1,IP] invalid and excluded from saved value for [_2]',
21115:                                      $diff,$warn{$item}).
21116:                                  '</li>');
21117:                         }
21118:                         if (@ok) {
21119:                             my @cidr_list;
21120:                             foreach my $item (@ok) {
21121:                                 @cidr_list = &Net::CIDR::cidradd($item,@cidr_list);
21122:                             }
21123:                             $wafproxy{$item} = join(',',@cidr_list);
21124:                         }
21125:                     }
21126:                 }
21127:                 if ($wafproxy{$item} ne $currvalue{$item}) {
21128:                     $changes{$item} = 1;
21129:                 }
21130:             } elsif ($currvalue{$item}) {
21131:                 $changes{$item} = 1;
21132:             } 
21133:         }
21134:     } else {
21135:         if (keys(%curralias)) {
21136:             $changes{'alias'} = 1;
21137:         }
21138:         if (keys(%currsaml)) {
21139:             $changes{'saml'} = 1;
21140:         } 
21141:         if (keys(%currvalue)) {
21142:             foreach my $key (keys(%currvalue)) {
21143:                 $changes{$key} = 1;
21144:             }
21145:         }
21146:     }
21147:     if (keys(%changes)) {
21148:         my %defaultshash = (
21149:                               wafproxy => \%wafproxy,
21150:                            ); 
21151:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21152:                                                  $dom);
21153:         if ($putresult eq 'ok') {
21154:             my $cachetime = 24*60*60;
21155:             my (%domdefaults,$updatedomdefs);
21156:             foreach my $item ('ipheader','trusted','vpnint','vpnext','sslopt') {
21157:                 if ($changes{$item}) {
21158:                     unless ($updatedomdefs) {
21159:                         %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
21160:                         $updatedomdefs = 1;
21161:                     }
21162:                     if ($wafproxy{$item}) {
21163:                         $domdefaults{'waf_'.$item} = $wafproxy{$item};
21164:                     } elsif (exists($domdefaults{'waf_'.$item})) {
21165:                         delete($domdefaults{'waf_'.$item});
21166:                     } 
21167:                 }
21168:             }
21169:             if ($updatedomdefs) {
21170:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21171:                 if (ref($lastactref) eq 'HASH') {
21172:                     $lastactref->{'domdefaults'} = 1;
21173:                 }
21174:             }
21175:             if ((exists($wafproxy{'alias'})) || (keys(%expirecache))) {
21176:                 my %updates = %expirecache;
21177:                 foreach my $key (keys(%expirecache)) {
21178:                     &Apache::lonnet::devalidate_cache_new('proxyalias',$key);
21179:                 }
21180:                 if (ref($wafproxy{'alias'}) eq 'HASH') {
21181:                     my $cachetime = 24*60*60;
21182:                     foreach my $key (keys(%{$wafproxy{'alias'}})) {
21183:                         $updates{$key} = 1;
21184:                         &Apache::lonnet::do_cache_new('proxyalias',$key,$wafproxy{'alias'}{$key},
21185:                                                       $cachetime);
21186:                     }
21187:                 }
21188:                 if (ref($lastactref) eq 'HASH') {
21189:                     $lastactref->{'proxyalias'} = \%updates;
21190:                 }
21191:             }
21192:             if ((exists($wafproxy{'saml'})) || (keys(%expiresaml))) {
21193:                 my %samlupdates = %expiresaml;
21194:                 foreach my $key (keys(%expiresaml)) {
21195:                     &Apache::lonnet::devalidate_cache_new('proxysaml',$key);
21196:                 }
21197:                 if (ref($wafproxy{'saml'}) eq 'HASH') {
21198:                     my $cachetime = 24*60*60;
21199:                     foreach my $key (keys(%{$wafproxy{'saml'}})) {
21200:                         $samlupdates{$key} = 1;
21201:                         &Apache::lonnet::do_cache_new('proxysaml',$key,$wafproxy{'saml'}{$key},
21202:                                                       $cachetime);
21203:                     }
21204:                 }
21205:                 if (ref($lastactref) eq 'HASH') {
21206:                     $lastactref->{'proxysaml'} = \%samlupdates;
21207:                 }
21208:             }
21209:             $output = &mt('Changes were made to Web Application Firewall/Reverse Proxy').'<ul>';
21210:             foreach my $item ('alias','saml','remoteip','ipheader','trusted','vpnint','vpnext','sslopt') {
21211:                 if ($changes{$item}) {
21212:                     if ($item eq 'alias') {
21213:                         my $numaliased = 0;
21214:                         if (ref($wafproxy{'alias'}) eq 'HASH') {
21215:                             my $shown;
21216:                             if (keys(%{$wafproxy{'alias'}})) {
21217:                                 foreach my $server (sort(keys(%{$wafproxy{'alias'}}))) {
21218:                                     $shown .= '<li>'.&mt('[_1] aliased by [_2]',
21219:                                                          &Apache::lonnet::hostname($server),
21220:                                                          $wafproxy{'alias'}{$server}).'</li>';
21221:                                     $numaliased ++;
21222:                                 }
21223:                                 if ($numaliased) {
21224:                                     $output .= '<li>'.&mt('Aliases for hostnames set to: [_1]',
21225:                                                           '<ul>'.$shown.'</ul>').'</li>';
21226:                                 }
21227:                             }
21228:                         }
21229:                         unless ($numaliased) {
21230:                             $output .= '<li>'.&mt('Aliases deleted for hostnames').'</li>';
21231:                         }
21232:                     } elsif ($item eq 'saml') {
21233:                         my $shown; 
21234:                         if (ref($wafproxy{'saml'}) eq 'HASH') {
21235:                             if (keys(%{$wafproxy{'saml'}})) {
21236:                                 $shown = join(', ',sort(keys(%{$wafproxy{'saml'}})));
21237:                             }
21238:                         }
21239:                         if ($shown) {
21240:                             $output .= '<li>'.&mt('Alias used by SSO Auth for: [_1]',
21241:                                                   $shown).'</li>';
21242:                         } else {
21243:                             $output .= '<li>'.&mt('No alias used for SSO Auth').'</li>';
21244:                         }
21245:                     } else {
21246:                         if ($item eq 'remoteip') {
21247:                             my %ip_methods = &remoteip_methods();
21248:                             if ($wafproxy{$item} =~ /^[mh]$/) {
21249:                                 $output .= '<li>'.&mt("Method for determining user's IP set to: [_1]",
21250:                                                       $ip_methods{$wafproxy{$item}}).'</li>';
21251:                             } else {
21252:                                 if (($env{'form.wafproxy_'.$dom}) && (ref($wafproxy{'alias'}) eq 'HASH')) {
21253:                                     $output .= '<li>'.&mt("No method in use to get user's real IP (will report IP used by WAF).").
21254:                                                '</li>';
21255:                                 } else {
21256:                                     $output .= '<li>'.&mt('WAF/Reverse Proxy not in use').'</li>';
21257:                                 }
21258:                             }
21259:                         } elsif ($item eq 'ipheader') {
21260:                             if ($wafproxy{$item}) {
21261:                                 $output .= '<li>'.&mt('Request header with remote IP set to: [_1]',
21262:                                                       $wafproxy{$item}).'</li>';
21263:                             } else {
21264:                                 $output .= '<li>'.&mt('Request header with remote IP deleted').'</li>';
21265:                             }
21266:                         } elsif ($item eq 'trusted') {
21267:                             if ($wafproxy{$item}) {
21268:                                 $output .= '<li>'.&mt('Trusted IP range(s) set to: [_1]',
21269:                                                       $wafproxy{$item}).'</li>';
21270:                             } else {
21271:                                 $output .= '<li>'.&mt('Trusted IP range(s) deleted').'</li>';
21272:                             }
21273:                         } elsif ($item eq 'vpnint') {
21274:                             if ($wafproxy{$item}) {
21275:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions set to: [_1]',
21276:                                                        $wafproxy{$item}).'</li>';
21277:                             } else {
21278:                                 $output .= '<li>'.&mt('Internal IP Range(s) for VPN sessions deleted').'</li>';
21279:                             }
21280:                         } elsif ($item eq 'vpnext') {
21281:                             if ($wafproxy{$item}) {
21282:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections set to: [_1]',
21283:                                                        $wafproxy{$item}).'</li>';
21284:                             } else {
21285:                                 $output .= '<li>'.&mt('IP Range(s) for backend WAF connections deleted').'</li>';
21286:                             }
21287:                         } elsif ($item eq 'sslopt') {
21288:                             if ($wafproxy{$item}) {
21289:                                 $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>';
21290:                             } else {
21291:                                 $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>';
21292:                             }
21293:                         }
21294:                     }
21295:                 }
21296:             }
21297:             $output .= '</ul>';
21298:         } else {
21299:             $output = '<span class="LC_error">'.
21300:                       &mt('An error occurred: [_1]',$putresult).'</span>';
21301:         }
21302:     } elsif (keys(%canset)) {
21303:         $output = &mt('No changes made to Web Application Firewall/Reverse Proxy settings');
21304:     }
21305:     if (@warnings) {
21306:         $output .= '<br />'.&mt('Warnings:').'<ul>'.
21307:                        join("\n",@warnings).'</ul>';
21308:     }
21309:     return $output;
21310: }
21311: 
21312: sub validate_ip_pattern {
21313:     my ($pattern) = @_;
21314:     if ($pattern =~ /^([^-]+)\-([^-]+)$/) {
21315:         my ($start,$end) = ($1,$2);
21316:         if ((&Net::CIDR::cidrvalidate($start)) && (&Net::CIDR::cidrvalidate($end))) {
21317:             if (($start !~ m{/}) && ($end !~ m{/})) {
21318:                 return $start.'-'.$end;
21319:             }
21320:         }
21321:     } elsif ($pattern ne '') {
21322:         $pattern = &Net::CIDR::cidrvalidate($pattern);
21323:         if ($pattern ne '') {
21324:             return $pattern;
21325:         }
21326:     }
21327:     return;
21328: }
21329: 
21330: sub modify_usersessions {
21331:     my ($dom,$lastactref,%domconfig) = @_;
21332:     my @hostingtypes = ('version','excludedomain','includedomain');
21333:     my @offloadtypes = ('primary','default');
21334:     my %types = (
21335:                   remote => \@hostingtypes,
21336:                   hosted => \@hostingtypes,
21337:                   spares => \@offloadtypes,
21338:                 );
21339:     my @prefixes = ('remote','hosted','spares');
21340:     my @lcversions = &Apache::lonnet::all_loncaparevs();
21341:     my (%by_ip,%by_location,@intdoms,@instdoms);
21342:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21343:     my @locations = sort(keys(%by_location));
21344:     my (%defaultshash,%changes);
21345:     foreach my $prefix (@prefixes) {
21346:         $defaultshash{'usersessions'}{$prefix} = {};
21347:     }
21348:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21349:     my $resulttext;
21350:     my %iphost = &Apache::lonnet::get_iphost();
21351:     foreach my $prefix (@prefixes) {
21352:         next if ($prefix eq 'spares');
21353:         foreach my $type (@{$types{$prefix}}) {
21354:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21355:             if ($type eq 'version') {
21356:                 my $value = $env{'form.'.$prefix.'_'.$type};
21357:                 my $okvalue;
21358:                 if ($value ne '') {
21359:                     if (grep(/^\Q$value\E$/,@lcversions)) {
21360:                         $okvalue = $value;
21361:                     }
21362:                 }
21363:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21364:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21365:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
21366:                             if ($inuse == 0) {
21367:                                 $changes{$prefix}{$type} = 1;
21368:                             } else {
21369:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
21370:                                     $changes{$prefix}{$type} = 1;
21371:                                 }
21372:                                 if ($okvalue ne '') {
21373:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21374:                                 } 
21375:                             }
21376:                         } else {
21377:                             if (($inuse == 1) && ($okvalue ne '')) {
21378:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21379:                                 $changes{$prefix}{$type} = 1;
21380:                             }
21381:                         }
21382:                     } else {
21383:                         if (($inuse == 1) && ($okvalue ne '')) {
21384:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21385:                             $changes{$prefix}{$type} = 1;
21386:                         }
21387:                     }
21388:                 } else {
21389:                     if (($inuse == 1) && ($okvalue ne '')) {
21390:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
21391:                         $changes{$prefix}{$type} = 1;
21392:                     }
21393:                 }
21394:             } else {
21395:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21396:                 my @okvals;
21397:                 foreach my $val (@vals) {
21398:                     if ($val =~ /:/) {
21399:                         my @items = split(/:/,$val);
21400:                         foreach my $item (@items) {
21401:                             if (ref($by_location{$item}) eq 'ARRAY') {
21402:                                 push(@okvals,$item);
21403:                             }
21404:                         }
21405:                     } else {
21406:                         if (ref($by_location{$val}) eq 'ARRAY') {
21407:                             push(@okvals,$val);
21408:                         }
21409:                     }
21410:                 }
21411:                 @okvals = sort(@okvals);
21412:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
21413:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
21414:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21415:                             if ($inuse == 0) {
21416:                                 $changes{$prefix}{$type} = 1; 
21417:                             } else {
21418:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21419:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
21420:                                 if (@changed > 0) {
21421:                                     $changes{$prefix}{$type} = 1;
21422:                                 }
21423:                             }
21424:                         } else {
21425:                             if ($inuse == 1) {
21426:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21427:                                 $changes{$prefix}{$type} = 1;
21428:                             }
21429:                         } 
21430:                     } else {
21431:                         if ($inuse == 1) {
21432:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21433:                             $changes{$prefix}{$type} = 1;
21434:                         }
21435:                     }
21436:                 } else {
21437:                     if ($inuse == 1) {
21438:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
21439:                         $changes{$prefix}{$type} = 1;
21440:                     }
21441:                 }
21442:             }
21443:         }
21444:     }
21445: 
21446:     my @alldoms = &Apache::lonnet::all_domains();
21447:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21448:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
21449:     my $savespares;
21450: 
21451:     foreach my $lonhost (sort(keys(%servers))) {
21452:         my $serverhomeID =
21453:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
21454:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
21455:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
21456:         my %spareschg;
21457:         foreach my $type (@{$types{'spares'}}) {
21458:             my @okspares;
21459:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
21460:             foreach my $server (@checked) {
21461:                 if (&Apache::lonnet::hostname($server) ne '') {
21462:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
21463:                         unless (grep(/^\Q$server\E$/,@okspares)) {
21464:                             push(@okspares,$server);
21465:                         }
21466:                     }
21467:                 }
21468:             }
21469:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
21470:             my $newspare;
21471:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
21472:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
21473:                     $newspare = $new;
21474:                 }
21475:             }
21476:             my @spares;
21477:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
21478:                 @spares = sort(@okspares,$newspare);
21479:             } else {
21480:                 @spares = sort(@okspares);
21481:             }
21482:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
21483:             if (ref($spareid{$lonhost}) eq 'HASH') {
21484:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
21485:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
21486:                     if (@diffs > 0) {
21487:                         $spareschg{$type} = 1;
21488:                     }
21489:                 }
21490:             }
21491:         }
21492:         if (keys(%spareschg) > 0) {
21493:             $changes{'spares'}{$lonhost} = \%spareschg;
21494:         }
21495:     }
21496:     $defaultshash{'usersessions'}{'offloadnow'} = {};
21497:     $defaultshash{'usersessions'}{'offloadoth'} = {};
21498:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
21499:     my @okoffload;
21500:     if (@offloadnow) {
21501:         foreach my $server (@offloadnow) {
21502:             if (&Apache::lonnet::hostname($server) ne '') {
21503:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
21504:                     push(@okoffload,$server);
21505:                 }
21506:             }
21507:         }
21508:         if (@okoffload) {
21509:             foreach my $lonhost (@okoffload) {
21510:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
21511:             }
21512:         }
21513:     }
21514:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
21515:     my @okoffloadoth;
21516:     if (@offloadoth) {
21517:         foreach my $server (@offloadoth) {
21518:             if (&Apache::lonnet::hostname($server) ne '') {
21519:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
21520:                     push(@okoffloadoth,$server);
21521:                 }
21522:             }
21523:         }
21524:         if (@okoffloadoth) {
21525:             foreach my $lonhost (@okoffloadoth) {
21526:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
21527:             }
21528:         }
21529:     }
21530:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
21531:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
21532:             if (ref($changes{'spares'}) eq 'HASH') {
21533:                 if (keys(%{$changes{'spares'}}) > 0) {
21534:                     $savespares = 1;
21535:                 }
21536:             }
21537:         } else {
21538:             $savespares = 1;
21539:         }
21540:         foreach my $offload ('offloadnow','offloadoth') {
21541:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
21542:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
21543:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
21544:                         $changes{$offload} = 1;
21545:                         last;
21546:                     }
21547:                 }
21548:                 unless ($changes{$offload}) {
21549:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
21550:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
21551:                             $changes{$offload} = 1;
21552:                             last;
21553:                         }
21554:                     }
21555:                 }
21556:             } else {
21557:                 if (($offload eq 'offloadnow') && (@okoffload)) {
21558:                      $changes{'offloadnow'} = 1;
21559:                 }
21560:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
21561:                     $changes{'offloadoth'} = 1;
21562:                 }
21563:             } 
21564:         }
21565:     } else {
21566:         if (@okoffload) {
21567:             $changes{'offloadnow'} = 1;
21568:         }
21569:         if (@okoffloadoth) {
21570:             $changes{'offloadoth'} = 1;
21571:         }
21572:     }
21573:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
21574:     if ((keys(%changes) > 0) || ($savespares)) {
21575:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21576:                                                  $dom);
21577:         if ($putresult eq 'ok') {
21578:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21579:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
21580:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
21581:                 }
21582:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
21583:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
21584:                 }
21585:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21586:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
21587:                 }
21588:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21589:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
21590:                 }
21591:             }
21592:             my $cachetime = 24*60*60;
21593:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21594:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
21595:             if (ref($lastactref) eq 'HASH') {
21596:                 $lastactref->{'domdefaults'} = 1;
21597:                 $lastactref->{'usersessions'} = 1;
21598:             }
21599:             if (keys(%changes) > 0) {
21600:                 my %lt = &usersession_titles();
21601:                 $resulttext = &mt('Changes made:').'<ul>';
21602:                 foreach my $prefix (@prefixes) {
21603:                     if (ref($changes{$prefix}) eq 'HASH') {
21604:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21605:                         if ($prefix eq 'spares') {
21606:                             if (ref($changes{$prefix}) eq 'HASH') {
21607:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
21608:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
21609:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
21610:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
21611:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
21612:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
21613:                                         foreach my $type (@{$types{$prefix}}) {
21614:                                             if ($changes{$prefix}{$lonhost}{$type}) {
21615:                                                 my $offloadto = &mt('None');
21616:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
21617:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
21618:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
21619:                                                     }
21620:                                                 }
21621:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
21622:                                             }
21623:                                         }
21624:                                     }
21625:                                     $resulttext .= '</li>';
21626:                                 }
21627:                             }
21628:                         } else {
21629:                             foreach my $type (@{$types{$prefix}}) {
21630:                                 if (defined($changes{$prefix}{$type})) {
21631:                                     my ($newvalue,$notinuse);
21632:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
21633:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
21634:                                             if ($type eq 'version') {
21635:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
21636:                                             } else {
21637:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
21638:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
21639:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
21640:                                                     }
21641:                                                 } else {
21642:                                                     $notinuse = 1;
21643:                                                 }
21644:                                             }
21645:                                         }
21646:                                     }
21647:                                     if ($newvalue eq '') {
21648:                                         if ($type eq 'version') {
21649:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
21650:                                         } elsif ($notinuse) {
21651:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
21652:                                         } else {
21653:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
21654:                                         }
21655:                                     } else {
21656:                                         if ($type eq 'version') {
21657:                                             $newvalue .= ' '.&mt('(or later)');
21658:                                         }
21659:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
21660:                                     }
21661:                                 }
21662:                             }
21663:                         }
21664:                         $resulttext .= '</ul>';
21665:                     }
21666:                 }
21667:                 if ($changes{'offloadnow'}) {
21668:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
21669:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
21670:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
21671:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
21672:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21673:                             }
21674:                             $resulttext .= '</ul>';
21675:                         } else {
21676:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
21677:                         }
21678:                     } else {
21679:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
21680:                     }
21681:                 }
21682:                 if ($changes{'offloadoth'}) {
21683:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
21684:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
21685:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
21686:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
21687:                                 $resulttext .= '<li>'.$lonhost.'</li>';
21688:                             }
21689:                             $resulttext .= '</ul>';
21690:                         } else {
21691:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
21692:                         }
21693:                     } else {
21694:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
21695:                     }
21696:                 }
21697:                 $resulttext .= '</ul>';
21698:             } else {
21699:                 $resulttext = $nochgmsg;
21700:             }
21701:         } else {
21702:             $resulttext = '<span class="LC_error">'.
21703:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21704:         }
21705:     } else {
21706:         $resulttext = $nochgmsg;
21707:     }
21708:     return $resulttext;
21709: }
21710: 
21711: sub modify_ssl {
21712:     my ($dom,$lastactref,%domconfig) = @_;
21713:     my (%by_ip,%by_location,@intdoms,@instdoms);
21714:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21715:     my @locations = sort(keys(%by_location));
21716:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21717:     my (%defaultshash,%changes);
21718:     my $action = 'ssl';
21719:     my @prefixes = ('connto','connfrom','replication');
21720:     foreach my $prefix (@prefixes) {
21721:         $defaultshash{$action}{$prefix} = {};
21722:     }
21723:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21724:     my $resulttext;
21725:     my %iphost = &Apache::lonnet::get_iphost();
21726:     my @reptypes = ('certreq','nocertreq');
21727:     my @connecttypes = ('dom','intdom','other');
21728:     my %types = (
21729:                   connto      => \@connecttypes,
21730:                   connfrom    => \@connecttypes,
21731:                   replication => \@reptypes,
21732:                 );
21733:     foreach my $prefix (sort(keys(%types))) {
21734:         foreach my $type (@{$types{$prefix}}) {
21735:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21736:                 my $value = 'yes';
21737:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
21738:                     $value = $env{'form.'.$prefix.'_'.$type};
21739:                 }
21740:                 if (ref($domconfig{$action}) eq 'HASH') {
21741:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21742:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
21743:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
21744:                                 $changes{$prefix}{$type} = 1;
21745:                             }
21746:                             $defaultshash{$action}{$prefix}{$type} = $value;
21747:                         } else {
21748:                             $defaultshash{$action}{$prefix}{$type} = $value;
21749:                             $changes{$prefix}{$type} = 1;
21750:                         }
21751:                     } else {
21752:                         $defaultshash{$action}{$prefix}{$type} = $value;
21753:                         $changes{$prefix}{$type} = 1;
21754:                     }
21755:                 } else {
21756:                     $defaultshash{$action}{$prefix}{$type} = $value;
21757:                     $changes{$prefix}{$type} = 1;
21758:                 }
21759:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
21760:                     delete($changes{$prefix}{$type});
21761:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
21762:                     delete($changes{$prefix}{$type});
21763:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
21764:                     delete($changes{$prefix}{$type});
21765:                 }
21766:             } elsif ($prefix eq 'replication') {
21767:                 if (@locations > 0) {
21768:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21769:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21770:                     my @okvals;
21771:                     foreach my $val (@vals) {
21772:                         if ($val =~ /:/) {
21773:                             my @items = split(/:/,$val);
21774:                             foreach my $item (@items) {
21775:                                 if (ref($by_location{$item}) eq 'ARRAY') {
21776:                                     push(@okvals,$item);
21777:                                 }
21778:                             }
21779:                         } else {
21780:                             if (ref($by_location{$val}) eq 'ARRAY') {
21781:                                 push(@okvals,$val);
21782:                             }
21783:                         }
21784:                     }
21785:                     @okvals = sort(@okvals);
21786:                     if (ref($domconfig{$action}) eq 'HASH') {
21787:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
21788:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
21789:                                 if ($inuse == 0) {
21790:                                     $changes{$prefix}{$type} = 1;
21791:                                 } else {
21792:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21793:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
21794:                                     if (@changed > 0) {
21795:                                         $changes{$prefix}{$type} = 1;
21796:                                     }
21797:                                 }
21798:                             } else {
21799:                                 if ($inuse == 1) {
21800:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
21801:                                     $changes{$prefix}{$type} = 1;
21802:                                 }
21803:                             }
21804:                         } else {
21805:                             if ($inuse == 1) {
21806:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
21807:                                 $changes{$prefix}{$type} = 1;
21808:                             }
21809:                         }
21810:                     } else {
21811:                         if ($inuse == 1) {
21812:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
21813:                             $changes{$prefix}{$type} = 1;
21814:                         }
21815:                     }
21816:                 }
21817:             }
21818:         }
21819:     }
21820:     if (keys(%changes)) {
21821:         foreach my $prefix (keys(%changes)) {
21822:             if (ref($changes{$prefix}) eq 'HASH') {
21823:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
21824:                     delete($changes{$prefix});
21825:                 }
21826:             } else {
21827:                 delete($changes{$prefix});
21828:             }
21829:         }
21830:     }
21831:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
21832:     if (keys(%changes) > 0) {
21833:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21834:                                                  $dom);
21835:         if ($putresult eq 'ok') {
21836:             if (ref($defaultshash{$action}) eq 'HASH') {
21837:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
21838:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
21839:                 }
21840:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
21841:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
21842:                 }
21843:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
21844:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
21845:                 }
21846:             }
21847:             my $cachetime = 24*60*60;
21848:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21849:             if (ref($lastactref) eq 'HASH') {
21850:                 $lastactref->{'domdefaults'} = 1;
21851:             }
21852:             if (keys(%changes) > 0) {
21853:                 my %titles = &ssl_titles();
21854:                 $resulttext = &mt('Changes made:').'<ul>';
21855:                 foreach my $prefix (@prefixes) {
21856:                     if (ref($changes{$prefix}) eq 'HASH') {
21857:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
21858:                         foreach my $type (@{$types{$prefix}}) {
21859:                             if (defined($changes{$prefix}{$type})) {
21860:                                 my ($newvalue,$notinuse);
21861:                                 if (ref($defaultshash{$action}) eq 'HASH') {
21862:                                     if (ref($defaultshash{$action}{$prefix})) {
21863:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
21864:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
21865:                                         } else {
21866:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
21867:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
21868:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
21869:                                                 }
21870:                                             } else {
21871:                                                 $notinuse = 1;
21872:                                             }
21873:                                         }
21874:                                     }
21875:                                     if ($notinuse) {
21876:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
21877:                                     } elsif ($newvalue eq '') {
21878:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
21879:                                     } else {
21880:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
21881:                                     }
21882:                                 }
21883:                             }
21884:                         }
21885:                         $resulttext .= '</ul>';
21886:                     }
21887:                 }
21888:             } else {
21889:                 $resulttext = $nochgmsg;
21890:             }
21891:         } else {
21892:             $resulttext = '<span class="LC_error">'.
21893:                           &mt('An error occurred: [_1]',$putresult).'</span>';
21894:         }
21895:     } else {
21896:         $resulttext = $nochgmsg;
21897:     }
21898:     return $resulttext;
21899: }
21900: 
21901: sub modify_trust {
21902:     my ($dom,$lastactref,%domconfig) = @_;
21903:     my (%by_ip,%by_location,@intdoms,@instdoms);
21904:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
21905:     my @locations = sort(keys(%by_location));
21906:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
21907:     my @types = ('exc','inc');
21908:     my (%defaultshash,%changes);
21909:     foreach my $prefix (@prefixes) {
21910:         $defaultshash{'trust'}{$prefix} = {};
21911:     }
21912:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
21913:     my $resulttext;
21914:     foreach my $prefix (@prefixes) {
21915:         foreach my $type (@types) {
21916:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
21917:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
21918:             my @okvals;
21919:             foreach my $val (@vals) {
21920:                 if ($val =~ /:/) {
21921:                     my @items = split(/:/,$val);
21922:                     foreach my $item (@items) {
21923:                         if (ref($by_location{$item}) eq 'ARRAY') {
21924:                             push(@okvals,$item);
21925:                         }
21926:                     }
21927:                 } else {
21928:                     if (ref($by_location{$val}) eq 'ARRAY') {
21929:                         push(@okvals,$val);
21930:                     }
21931:                 }
21932:             }
21933:             @okvals = sort(@okvals);
21934:             if (ref($domconfig{'trust'}) eq 'HASH') {
21935:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
21936:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21937:                         if ($inuse == 0) {
21938:                             $changes{$prefix}{$type} = 1;
21939:                         } else {
21940:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21941:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
21942:                             if (@changed > 0) {
21943:                                 $changes{$prefix}{$type} = 1;
21944:                             }
21945:                         }
21946:                     } else {
21947:                         if ($inuse == 1) {
21948:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21949:                             $changes{$prefix}{$type} = 1;
21950:                         }
21951:                     }
21952:                 } else {
21953:                     if ($inuse == 1) {
21954:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21955:                         $changes{$prefix}{$type} = 1;
21956:                     }
21957:                 }
21958:             } else {
21959:                 if ($inuse == 1) {
21960:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
21961:                     $changes{$prefix}{$type} = 1;
21962:                 }
21963:             }
21964:         }
21965:     }
21966:     my $nochgmsg = &mt('No changes made to trust settings.');
21967:     if (keys(%changes) > 0) {
21968:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
21969:                                                  $dom);
21970:         if ($putresult eq 'ok') {
21971:             if (ref($defaultshash{'trust'}) eq 'HASH') {
21972:                 foreach my $prefix (@prefixes) {
21973:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
21974:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
21975:                     }
21976:                 }
21977:             }
21978:             my $cachetime = 24*60*60;
21979:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
21980:             &Apache::lonnet::do_cache_new('trust',$dom,$defaultshash{'trust'},3600);
21981:             if (ref($lastactref) eq 'HASH') {
21982:                 $lastactref->{'domdefaults'} = 1;
21983:                 $lastactref->{'trust'} = 1;
21984:             }
21985:             if (keys(%changes) > 0) {
21986:                 my %lt = &trust_titles();
21987:                 $resulttext = &mt('Changes made:').'<ul>';
21988:                 foreach my $prefix (@prefixes) {
21989:                     if (ref($changes{$prefix}) eq 'HASH') {
21990:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
21991:                         foreach my $type (@types) {
21992:                             if (defined($changes{$prefix}{$type})) {
21993:                                 my ($newvalue,$notinuse);
21994:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
21995:                                     if (ref($defaultshash{'trust'}{$prefix})) {
21996:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
21997:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
21998:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
21999:                                             }
22000:                                         } else {
22001:                                             $notinuse = 1;
22002:                                         }
22003:                                     }
22004:                                 }
22005:                                 if ($notinuse) {
22006:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
22007:                                 } elsif ($newvalue eq '') {
22008:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
22009:                                 } else {
22010:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
22011:                                 }
22012:                             }
22013:                         }
22014:                         $resulttext .= '</ul>';
22015:                     }
22016:                 }
22017:                 $resulttext .= '</ul>';
22018:             } else {
22019:                 $resulttext = $nochgmsg;
22020:             }
22021:         } else {
22022:             $resulttext = '<span class="LC_error">'.
22023:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22024:         }
22025:     } else {
22026:         $resulttext = $nochgmsg;
22027:     }
22028:     return $resulttext;
22029: }
22030: 
22031: sub modify_loadbalancing {
22032:     my ($dom,%domconfig) = @_;
22033:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
22034:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
22035:     my ($othertitle,$usertypes,$types) =
22036:         &Apache::loncommon::sorted_inst_types($dom);
22037:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
22038:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
22039:     my @sparestypes = ('primary','default');
22040:     my %typetitles = &sparestype_titles();
22041:     my $resulttext;
22042:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22043:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22044:         %existing = %{$domconfig{'loadbalancing'}};
22045:     }
22046:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
22047:                               \%currtargets,\%currrules,\%currcookies);
22048:     my ($saveloadbalancing,%defaultshash,%changes);
22049:     my ($alltypes,$othertypes,$titles) =
22050:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
22051:     my %ruletitles = &offloadtype_text();
22052:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
22053:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
22054:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
22055:         if ($balancer eq '') {
22056:             next;
22057:         }
22058:         if (!exists($servers{$balancer})) {
22059:             if (exists($currbalancer{$balancer})) {
22060:                 push(@{$changes{'delete'}},$balancer);
22061:             }
22062:             next;
22063:         }
22064:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
22065:             push(@{$changes{'delete'}},$balancer);
22066:             next;
22067:         }
22068:         if (!exists($currbalancer{$balancer})) {
22069:             push(@{$changes{'add'}},$balancer);
22070:         }
22071:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
22072:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
22073:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
22074:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
22075:             $saveloadbalancing = 1;
22076:         }
22077:         foreach my $sparetype (@sparestypes) {
22078:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
22079:             my @offloadto;
22080:             foreach my $target (@targets) {
22081:                 if (($servers{$target}) && ($target ne $balancer)) {
22082:                     if ($sparetype eq 'default') {
22083:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
22084:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
22085:                         }
22086:                     }
22087:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
22088:                         push(@offloadto,$target);
22089:                     }
22090:                 }
22091:             }
22092:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
22093:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
22094:                     push(@offloadto,$balancer);
22095:                 }
22096:             }
22097:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
22098:         }
22099:         if ($env{'form.loadbalancing_cookie_'.$i}) {
22100:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
22101:             if (exists($currbalancer{$balancer})) { 
22102:                 unless ($currcookies{$balancer}) {
22103:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
22104:                 }
22105:             }
22106:         } elsif (exists($currbalancer{$balancer})) {
22107:             if ($currcookies{$balancer}) {
22108:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
22109:             }
22110:         }
22111:         if (ref($currtargets{$balancer}) eq 'HASH') {
22112:             foreach my $sparetype (@sparestypes) {
22113:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
22114:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
22115:                     if (@targetdiffs > 0) {
22116:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22117:                     }
22118:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22119:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22120:                         $changes{'curr'}{$balancer}{'targets'} = 1;
22121:                     }
22122:                 }
22123:             }
22124:         } else {
22125:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
22126:                 foreach my $sparetype (@sparestypes) {
22127:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22128:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22129:                             $changes{'curr'}{$balancer}{'targets'} = 1;
22130:                         }
22131:                     }
22132:                 }
22133:             }
22134:         }
22135:         my $ishomedom;
22136:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
22137:             $ishomedom = 1;
22138:         }
22139:         if (ref($alltypes) eq 'ARRAY') {
22140:             foreach my $type (@{$alltypes}) {
22141:                 my $rule;
22142:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
22143:                          (!$ishomedom)) {
22144:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
22145:                 }
22146:                 if ($rule eq 'specific') {
22147:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
22148:                     if (exists($servers{$specifiedhost})) {
22149:                         $rule = $specifiedhost;
22150:                     }
22151:                 }
22152:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
22153:                 if (ref($currrules{$balancer}) eq 'HASH') {
22154:                     if ($rule ne $currrules{$balancer}{$type}) {
22155:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22156:                     }
22157:                 } elsif ($rule ne '') {
22158:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
22159:                 }
22160:             }
22161:         }
22162:     }
22163:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
22164:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
22165:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
22166:             $defaultshash{'loadbalancing'} = {};
22167:         }
22168:         my $putresult = &Apache::lonnet::put_dom('configuration',
22169:                                                  \%defaultshash,$dom);
22170:         if ($putresult eq 'ok') {
22171:             if (keys(%changes) > 0) {
22172:                 my %toupdate;
22173:                 if (ref($changes{'delete'}) eq 'ARRAY') {
22174:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
22175:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
22176:                         $toupdate{$balancer} = 1;
22177:                     }
22178:                 }
22179:                 if (ref($changes{'add'}) eq 'ARRAY') {
22180:                     foreach my $balancer (sort(@{$changes{'add'}})) {
22181:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
22182:                         $toupdate{$balancer} = 1;
22183:                     }
22184:                 }
22185:                 if (ref($changes{'curr'}) eq 'HASH') {
22186:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
22187:                         $toupdate{$balancer} = 1;
22188:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
22189:                             if ($changes{'curr'}{$balancer}{'targets'}) {
22190:                                 my %offloadstr;
22191:                                 foreach my $sparetype (@sparestypes) {
22192:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22193:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
22194:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22195:                                         }
22196:                                     }
22197:                                 }
22198:                                 if (keys(%offloadstr) == 0) {
22199:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
22200:                                 } else {
22201:                                     my $showoffload;
22202:                                     foreach my $sparetype (@sparestypes) {
22203:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
22204:                                         if (defined($offloadstr{$sparetype})) {
22205:                                             $showoffload .= $offloadstr{$sparetype};
22206:                                         } else {
22207:                                             $showoffload .= &mt('None');
22208:                                         }
22209:                                         $showoffload .= ('&nbsp;'x3);
22210:                                     }
22211:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
22212:                                 }
22213:                             }
22214:                         }
22215:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
22216:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
22217:                                 foreach my $type (@{$alltypes}) {
22218:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
22219:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22220:                                         my $balancetext;
22221:                                         if ($rule eq '') {
22222:                                             $balancetext =  $ruletitles{'default'};
22223:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
22224:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22225:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
22226:                                                 foreach my $sparetype (@sparestypes) {
22227:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
22228:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
22229:                                                     }
22230:                                                 }
22231:                                                 foreach my $item (@{$alltypes}) {
22232:                                                     next if ($item =~  /^_LC_ipchange/);
22233:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
22234:                                                     if ($hasrule eq 'homeserver') {
22235:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
22236:                                                     } else {
22237:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
22238:                                                             if ($servers{$hasrule}) {
22239:                                                                 $toupdate{$hasrule} = 1;
22240:                                                             }
22241:                                                         }
22242:                                                     }
22243:                                                 }
22244:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
22245:                                                     $balancetext =  $ruletitles{$rule};
22246:                                                 } else {
22247:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
22248:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
22249:                                                     if ($receiver) {
22250:                                                         $toupdate{$receiver};
22251:                                                     }
22252:                                                 }
22253:                                             } else {
22254:                                                 $balancetext =  $ruletitles{$rule};
22255:                                             }
22256:                                         } else {
22257:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
22258:                                         }
22259:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
22260:                                     }
22261:                                 }
22262:                             }
22263:                         }
22264:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
22265:                             if ($currcookies{$balancer}) {
22266:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use disabled',
22267:                                                           $balancer).'</li>';
22268:                             } else {
22269:                                 $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
22270:                                                           $balancer).'</li>';
22271:                             }
22272:                         }
22273:                     }
22274:                 }
22275:                 if (keys(%toupdate)) {
22276:                     my %thismachine;
22277:                     my $updatedhere;
22278:                     my $cachetime = 60*60*24;
22279:                     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
22280:                     foreach my $lonhost (keys(%toupdate)) {
22281:                         if ($thismachine{$lonhost}) {
22282:                             unless ($updatedhere) {
22283:                                 &Apache::lonnet::do_cache_new('loadbalancing',$dom,
22284:                                                               $defaultshash{'loadbalancing'},
22285:                                                               $cachetime);
22286:                                 $updatedhere = 1;
22287:                             }
22288:                         } else {
22289:                             my $cachekey = &escape('loadbalancing').':'.&escape($dom);
22290:                             &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
22291:                         }
22292:                     }
22293:                 }
22294:                 if ($resulttext ne '') {
22295:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
22296:                 } else {
22297:                     $resulttext = $nochgmsg;
22298:                 }
22299:             } else {
22300:                 $resulttext = $nochgmsg;
22301:             }
22302:         } else {
22303:             $resulttext = '<span class="LC_error">'.
22304:                           &mt('An error occurred: [_1]',$putresult).'</span>';
22305:         }
22306:     } else {
22307:         $resulttext = $nochgmsg;
22308:     }
22309:     return $resulttext;
22310: }
22311: 
22312: sub recurse_check {
22313:     my ($chkcats,$categories,$depth,$name) = @_;
22314:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
22315:         my $chg = 0;
22316:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
22317:             my $category = $chkcats->[$depth]{$name}[$j];
22318:             my $item;
22319:             if ($category eq '') {
22320:                 $chg ++;
22321:             } else {
22322:                 my $deeper = $depth + 1;
22323:                 $item = &escape($category).':'.&escape($name).':'.$depth;
22324:                 if ($chg) {
22325:                     $categories->{$item} -= $chg;
22326:                 }
22327:                 &recurse_check($chkcats,$categories,$deeper,$category);
22328:                 $deeper --;
22329:             }
22330:         }
22331:     }
22332:     return;
22333: }
22334: 
22335: sub recurse_cat_deletes {
22336:     my ($item,$coursecategories,$deletions) = @_;
22337:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
22338:     my $subdepth = $depth + 1;
22339:     if (ref($coursecategories) eq 'HASH') {
22340:         foreach my $subitem (keys(%{$coursecategories})) {
22341:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
22342:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
22343:                 delete($coursecategories->{$subitem});
22344:                 $deletions->{$subitem} = 1;
22345:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
22346:             }
22347:         }
22348:     }
22349:     return;
22350: }
22351: 
22352: sub active_dc_picker {
22353:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
22354:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
22355:     my @domcoord = keys(%domcoords);
22356:     if (keys(%currhash)) {
22357:         foreach my $dc (keys(%currhash)) {
22358:             unless (exists($domcoords{$dc})) {
22359:                 push(@domcoord,$dc);
22360:             }
22361:         }
22362:     }
22363:     @domcoord = sort(@domcoord);
22364:     my $numdcs = scalar(@domcoord);
22365:     my $rows = 0;
22366:     my $table;
22367:     if ($numdcs > 1) {
22368:         $table = '<table>';
22369:         for (my $i=0; $i<@domcoord; $i++) {
22370:             my $rem = $i%($numinrow);
22371:             if ($rem == 0) {
22372:                 if ($i > 0) {
22373:                     $table .= '</tr>';
22374:                 }
22375:                 $table .= '<tr>';
22376:                 $rows ++;
22377:             }
22378:             my $check = '';
22379:             if ($inputtype eq 'radio') {
22380:                 if (keys(%currhash) == 0) {
22381:                     if (!$i) {
22382:                         $check = ' checked="checked"';
22383:                     }
22384:                 } elsif (exists($currhash{$domcoord[$i]})) {
22385:                     $check = ' checked="checked"';
22386:                 }
22387:             } else {
22388:                 if (exists($currhash{$domcoord[$i]})) {
22389:                     $check = ' checked="checked"';
22390:                 }
22391:             }
22392:             if ($i == @domcoord - 1) {
22393:                 my $colsleft = $numinrow - $rem;
22394:                 if ($colsleft > 1) {
22395:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
22396:                 } else {
22397:                     $table .= '<td class="LC_left_item">';
22398:                 }
22399:             } else {
22400:                 $table .= '<td class="LC_left_item">';
22401:             }
22402:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
22403:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22404:             $table .= '<span class="LC_nobreak"><label>'.
22405:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
22406:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
22407:             if ($user ne $dcname.':'.$dcdom) {
22408:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22409:             }
22410:             $table .= '</label></span></td>';
22411:         }
22412:         $table .= '</tr></table>';
22413:     } elsif ($numdcs == 1) {
22414:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
22415:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
22416:         if ($inputtype eq 'radio') {
22417:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
22418:             if ($user ne $dcname.':'.$dcdom) {
22419:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22420:             }
22421:         } else {
22422:             my $check;
22423:             if (exists($currhash{$domcoord[0]})) {
22424:                 $check = ' checked="checked"';
22425:             }
22426:             $table = '<span class="LC_nobreak"><label>'.
22427:                      '<input type="checkbox" name="'.$name.'" '.
22428:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
22429:             if ($user ne $dcname.':'.$dcdom) {
22430:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
22431:             }
22432:             $table .= '</label></span>';
22433:             $rows ++;
22434:         }
22435:     }
22436:     return ($numdcs,$table,$rows);
22437: }
22438: 
22439: sub usersession_titles {
22440:     return &Apache::lonlocal::texthash(
22441:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
22442:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
22443:                spares => 'Servers offloaded to, when busy',
22444:                version => 'LON-CAPA version requirement',
22445:                excludedomain => 'Allow all, but exclude specific domains',
22446:                includedomain => 'Deny all, but include specific domains',
22447:                primary => 'Primary (checked first)',
22448:                default => 'Default',
22449:            );
22450: }
22451: 
22452: sub id_for_thisdom {
22453:     my (%servers) = @_;
22454:     my %altids;
22455:     foreach my $server (keys(%servers)) {
22456:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
22457:         if ($serverhome ne $server) {
22458:             $altids{$serverhome} = $server;
22459:         }
22460:     }
22461:     return %altids;
22462: }
22463: 
22464: sub count_servers {
22465:     my ($currbalancer,%servers) = @_;
22466:     my (@spares,$numspares);
22467:     foreach my $lonhost (sort(keys(%servers))) {
22468:         next if ($currbalancer eq $lonhost);
22469:         push(@spares,$lonhost);
22470:     }
22471:     if ($currbalancer) {
22472:         $numspares = scalar(@spares);
22473:     } else {
22474:         $numspares = scalar(@spares) - 1;
22475:     }
22476:     return ($numspares,@spares);
22477: }
22478: 
22479: sub lonbalance_targets_js {
22480:     my ($dom,$types,$servers,$settings) = @_;
22481:     my $select = &mt('Select');
22482:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
22483:     if (ref($servers) eq 'HASH') {
22484:         $alltargets = join("','",sort(keys(%{$servers})));
22485:         my @homedoms;
22486:         foreach my $server (sort(keys(%{$servers}))) {
22487:             if (&Apache::lonnet::host_domain($server) eq $dom) {
22488:                 push(@homedoms,'1');
22489:             } else {
22490:                 push(@homedoms,'0');
22491:             }
22492:         }
22493:         $allishome = join("','",@homedoms);
22494:     }
22495:     if (ref($types) eq 'ARRAY') {
22496:         if (@{$types} > 0) {
22497:             @alltypes = @{$types};
22498:         }
22499:     }
22500:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
22501:     $allinsttypes = join("','",@alltypes);
22502:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
22503:     if (ref($settings) eq 'HASH') {
22504:         %existing = %{$settings};
22505:     }
22506:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
22507:                               \%currtargets,\%currrules,\%currcookies);
22508:     my $balancers = join("','",sort(keys(%currbalancer)));
22509:     return <<"END";
22510: 
22511: <script type="text/javascript">
22512: // <![CDATA[
22513: 
22514: currBalancers = new Array('$balancers');
22515: 
22516: function toggleTargets(balnum) {
22517:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22518:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
22519:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
22520:     var prevbalancer = prevhostitem.value;
22521:     var baltotal = document.getElementById('loadbalancing_total').value;
22522:     prevhostitem.value = balancer;
22523:     if (prevbalancer != '') {
22524:         var prevIdx = currBalancers.indexOf(prevbalancer);
22525:         if (prevIdx != -1) {
22526:             currBalancers.splice(prevIdx,1);
22527:         }
22528:     }
22529:     if (balancer == '') {
22530:         hideSpares(balnum);
22531:     } else {
22532:         var currIdx = currBalancers.indexOf(balancer);
22533:         if (currIdx == -1) {
22534:             currBalancers.push(balancer);
22535:         }
22536:         var homedoms = new Array('$allishome');
22537:         var ishomedom = homedoms[lonhostitem.selectedIndex];
22538:         showSpares(balancer,ishomedom,balnum);
22539:     }
22540:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
22541:     return;
22542: }
22543: 
22544: function showSpares(balancer,ishomedom,balnum) {
22545:     var alltargets = new Array('$alltargets');
22546:     var insttypes = new Array('$allinsttypes');
22547:     var offloadtypes = new Array('primary','default');
22548: 
22549:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
22550:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
22551:  
22552:     for (var i=0; i<offloadtypes.length; i++) {
22553:         var count = 0;
22554:         for (var j=0; j<alltargets.length; j++) {
22555:             if (alltargets[j] != balancer) {
22556:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
22557:                 item.value = alltargets[j];
22558:                 item.style.textAlign='left';
22559:                 item.style.textFace='normal';
22560:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
22561:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
22562:                     item.disabled = '';
22563:                 } else {
22564:                     item.disabled = 'disabled';
22565:                     item.checked = false;
22566:                 }
22567:                 count ++;
22568:             }
22569:         }
22570:     }
22571:     for (var k=0; k<insttypes.length; k++) {
22572:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
22573:             if (ishomedom == 1) {
22574:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22575:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22576:             } else {
22577:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22578:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22579:             }
22580:         } else {
22581:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
22582:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
22583:         }
22584:         if ((insttypes[k] != '_LC_external') && 
22585:             ((insttypes[k] != '_LC_internetdom') ||
22586:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
22587:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
22588:             item.options.length = 0;
22589:             item.options[0] = new Option("","",true,true);
22590:             var idx = 0;
22591:             for (var m=0; m<alltargets.length; m++) {
22592:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
22593:                     idx ++;
22594:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22595:                 }
22596:             }
22597:         }
22598:     }
22599:     return;
22600: }
22601: 
22602: function hideSpares(balnum) {
22603:     var alltargets = new Array('$alltargets');
22604:     var insttypes = new Array('$allinsttypes');
22605:     var offloadtypes = new Array('primary','default');
22606: 
22607:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
22608:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
22609: 
22610:     var total = alltargets.length - 1;
22611:     for (var i=0; i<offloadtypes; i++) {
22612:         for (var j=0; j<total; j++) {
22613:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
22614:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
22615:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
22616:         }
22617:     }
22618:     for (var k=0; k<insttypes.length; k++) {
22619:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
22620:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
22621:         if (insttypes[k] != '_LC_external') {
22622:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
22623:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
22624:         }
22625:     }
22626:     return;
22627: }
22628: 
22629: function checkOffloads(item,balnum,type) {
22630:     var alltargets = new Array('$alltargets');
22631:     var offloadtypes = new Array('primary','default');
22632:     if (item.checked) {
22633:         var total = alltargets.length - 1;
22634:         var other;
22635:         if (type == offloadtypes[0]) {
22636:             other = offloadtypes[1];
22637:         } else {
22638:             other = offloadtypes[0];
22639:         }
22640:         for (var i=0; i<total; i++) {
22641:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
22642:             if (server == item.value) {
22643:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
22644:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
22645:                 }
22646:             }
22647:         }
22648:     }
22649:     return;
22650: }
22651: 
22652: function singleServerToggle(balnum,type) {
22653:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
22654:     if (offloadtoSelIdx == 0) {
22655:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
22656:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22657: 
22658:     } else {
22659:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
22660:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22661:     }
22662:     return;
22663: }
22664: 
22665: function balanceruleChange(formname,balnum,type) {
22666:     if (type == '_LC_external') {
22667:         return;
22668:     }
22669:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
22670:     for (var i=0; i<typesRules.length; i++) {
22671:         if (formname.elements[typesRules[i]].checked) {
22672:             if (formname.elements[typesRules[i]].value != 'specific') {
22673:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
22674:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
22675:             } else {
22676:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
22677:             }
22678:         }
22679:     }
22680:     return;
22681: }
22682: 
22683: function balancerDeleteChange(balnum) {
22684:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
22685:     var baltotal = document.getElementById('loadbalancing_total').value;
22686:     var addtarget;
22687:     var removetarget;
22688:     var action = 'delete';
22689:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
22690:         var lonhost = hostitem.value;
22691:         var currIdx = currBalancers.indexOf(lonhost);
22692:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
22693:             if (currIdx != -1) {
22694:                 currBalancers.splice(currIdx,1);
22695:             }
22696:             addtarget = lonhost;
22697:         } else {
22698:             if (currIdx == -1) {
22699:                 currBalancers.push(lonhost);
22700:             }
22701:             removetarget = lonhost;
22702:             action = 'undelete';
22703:         }
22704:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
22705:     }
22706:     return;
22707: }
22708: 
22709: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
22710:     if (baltotal > 1) {
22711:         var offloadtypes = new Array('primary','default');
22712:         var alltargets = new Array('$alltargets');
22713:         var insttypes = new Array('$allinsttypes');
22714:         for (var i=0; i<baltotal; i++) {
22715:             if (i != balnum) {
22716:                 for (var j=0; j<offloadtypes.length; j++) {
22717:                     var total = alltargets.length - 1;
22718:                     for (var k=0; k<total; k++) {
22719:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
22720:                         var server = serveritem.value;
22721:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
22722:                             if (server == addtarget) {
22723:                                 serveritem.disabled = '';
22724:                             }
22725:                         }
22726:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22727:                             if (server == removetarget) {
22728:                                 serveritem.disabled = 'disabled';
22729:                                 serveritem.checked = false;
22730:                             }
22731:                         }
22732:                     }
22733:                 }
22734:                 for (var j=0; j<insttypes.length; j++) {
22735:                     if (insttypes[j] != '_LC_external') {
22736:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
22737:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
22738:                             var currSel = singleserver.selectedIndex;
22739:                             var currVal = singleserver.options[currSel].value;
22740:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
22741:                                 var numoptions = singleserver.options.length;
22742:                                 var needsnew = 1;
22743:                                 for (var k=0; k<numoptions; k++) {
22744:                                     if (singleserver.options[k] == addtarget) {
22745:                                         needsnew = 0;
22746:                                         break;
22747:                                     }
22748:                                 }
22749:                                 if (needsnew == 1) {
22750:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
22751:                                 }
22752:                             }
22753:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
22754:                                 singleserver.options.length = 0;
22755:                                 if ((currVal) && (currVal != removetarget)) {
22756:                                     singleserver.options[0] = new Option("","",false,false);
22757:                                 } else {
22758:                                     singleserver.options[0] = new Option("","",true,true);
22759:                                 }
22760:                                 var idx = 0;
22761:                                 for (var m=0; m<alltargets.length; m++) {
22762:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
22763:                                         idx ++;
22764:                                         if (currVal == alltargets[m]) {
22765:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
22766:                                         } else {
22767:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
22768:                                         }
22769:                                     }
22770:                                 }
22771:                             }
22772:                         }
22773:                     }
22774:                 }
22775:             }
22776:         }
22777:     }
22778:     return;
22779: }
22780: 
22781: // ]]>
22782: </script>
22783: 
22784: END
22785: }
22786: 
22787: 
22788: sub new_spares_js {
22789:     my @sparestypes = ('primary','default');
22790:     my $types = join("','",@sparestypes);
22791:     my $select = &mt('Select');
22792:     return <<"END";
22793: 
22794: <script type="text/javascript">
22795: // <![CDATA[
22796: 
22797: function updateNewSpares(formname,lonhost) {
22798:     var types = new Array('$types');
22799:     var include = new Array();
22800:     var exclude = new Array();
22801:     for (var i=0; i<types.length; i++) {
22802:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
22803:         for (var j=0; j<spareboxes.length; j++) {
22804:             if (formname.elements[spareboxes[j]].checked) {
22805:                 exclude.push(formname.elements[spareboxes[j]].value);
22806:             } else {
22807:                 include.push(formname.elements[spareboxes[j]].value);
22808:             }
22809:         }
22810:     }
22811:     for (var i=0; i<types.length; i++) {
22812:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
22813:         var selIdx = newSpare.selectedIndex;
22814:         var currnew = newSpare.options[selIdx].value;
22815:         var okSpares = new Array();
22816:         for (var j=0; j<newSpare.options.length; j++) {
22817:             var possible = newSpare.options[j].value;
22818:             if (possible != '') {
22819:                 if (exclude.indexOf(possible) == -1) {
22820:                     okSpares.push(possible);
22821:                 } else {
22822:                     if (currnew == possible) {
22823:                         selIdx = 0;
22824:                     }
22825:                 }
22826:             }
22827:         }
22828:         for (var k=0; k<include.length; k++) {
22829:             if (okSpares.indexOf(include[k]) == -1) {
22830:                 okSpares.push(include[k]);
22831:             }
22832:         }
22833:         okSpares.sort();
22834:         newSpare.options.length = 0;
22835:         if (selIdx == 0) {
22836:             newSpare.options[0] = new Option("$select","",true,true);
22837:         } else {
22838:             newSpare.options[0] = new Option("$select","",false,false);
22839:         }
22840:         for (var m=0; m<okSpares.length; m++) {
22841:             var idx = m+1;
22842:             var selThis = 0;
22843:             if (selIdx != 0) {
22844:                 if (okSpares[m] == currnew) {
22845:                     selThis = 1;
22846:                 }
22847:             }
22848:             if (selThis == 1) {
22849:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
22850:             } else {
22851:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
22852:             }
22853:         }
22854:     }
22855:     return;
22856: }
22857: 
22858: function checkNewSpares(lonhost,type) {
22859:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
22860:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
22861:     if (chosen != '') {
22862:         var othertype;
22863:         var othernewSpare;
22864:         if (type == 'primary') {
22865:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
22866:         }
22867:         if (type == 'default') {
22868:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
22869:         }
22870:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
22871:             othernewSpare.selectedIndex = 0;
22872:         }
22873:     }
22874:     return;
22875: }
22876: 
22877: // ]]>
22878: </script>
22879: 
22880: END
22881: 
22882: }
22883: 
22884: sub common_domprefs_js {
22885:     return <<"END";
22886: 
22887: <script type="text/javascript">
22888: // <![CDATA[
22889: 
22890: function getIndicesByName(formname,item) {
22891:     var group = new Array();
22892:     for (var i=0;i<formname.elements.length;i++) {
22893:         if (formname.elements[i].name == item) {
22894:             group.push(formname.elements[i].id);
22895:         }
22896:     }
22897:     return group;
22898: }
22899: 
22900: // ]]>
22901: </script>
22902: 
22903: END
22904: 
22905: }
22906: 
22907: sub recaptcha_js {
22908:     my %lt = &captcha_phrases();
22909:     return <<"END";
22910: 
22911: <script type="text/javascript">
22912: // <![CDATA[
22913: 
22914: function updateCaptcha(caller,context) {
22915:     var privitem;
22916:     var pubitem;
22917:     var privtext;
22918:     var pubtext;
22919:     var versionitem;
22920:     var versiontext;
22921:     if (document.getElementById(context+'_recaptchapub')) {
22922:         pubitem = document.getElementById(context+'_recaptchapub');
22923:     } else {
22924:         return;
22925:     }
22926:     if (document.getElementById(context+'_recaptchapriv')) {
22927:         privitem = document.getElementById(context+'_recaptchapriv');
22928:     } else {
22929:         return;
22930:     }
22931:     if (document.getElementById(context+'_recaptchapubtxt')) {
22932:         pubtext = document.getElementById(context+'_recaptchapubtxt');
22933:     } else {
22934:         return;
22935:     }
22936:     if (document.getElementById(context+'_recaptchaprivtxt')) {
22937:         privtext = document.getElementById(context+'_recaptchaprivtxt');
22938:     } else {
22939:         return;
22940:     }
22941:     if (document.getElementById(context+'_recaptchaversion')) {
22942:         versionitem = document.getElementById(context+'_recaptchaversion');
22943:     } else {
22944:         return;
22945:     }
22946:     if (document.getElementById(context+'_recaptchavertxt')) {
22947:         versiontext = document.getElementById(context+'_recaptchavertxt');
22948:     } else {
22949:         return;
22950:     }
22951:     if (caller.checked) {
22952:         if (caller.value == 'recaptcha') {
22953:             pubitem.type = 'text';
22954:             privitem.type = 'text';
22955:             pubitem.size = '40';
22956:             privitem.size = '40';
22957:             pubtext.innerHTML = "$lt{'pub'}";
22958:             privtext.innerHTML = "$lt{'priv'}";
22959:             versionitem.type = 'text';
22960:             versionitem.size = '3';
22961:             versiontext.innerHTML = "$lt{'ver'}";
22962:         } else {
22963:             pubitem.type = 'hidden';
22964:             privitem.type = 'hidden';
22965:             versionitem.type = 'hidden';
22966:             pubtext.innerHTML = '';
22967:             privtext.innerHTML = '';
22968:             versiontext.innerHTML = '';
22969:         }
22970:     }
22971:     return;
22972: }
22973: 
22974: // ]]>
22975: </script>
22976: 
22977: END
22978: 
22979: }
22980: 
22981: sub toggle_display_js {
22982:     return <<"END";
22983: 
22984: <script type="text/javascript">
22985: // <![CDATA[
22986: 
22987: function toggleDisplay(domForm,caller) {
22988:     if (document.getElementById(caller)) {
22989:         var divitem = document.getElementById(caller);
22990:         var optionsElement = domForm.coursecredits;
22991:         var checkval = 1;
22992:         var dispval = 'block';
22993:         var selfcreateRegExp = /^cancreate_emailverified/;
22994:         if (caller == 'emailoptions') {
22995:             optionsElement = domForm.cancreate_email;
22996:         }
22997:         if (caller == 'studentsubmission') {
22998:             optionsElement = domForm.postsubmit;
22999:         }
23000:         if (caller == 'cloneinstcode') {
23001:             optionsElement = domForm.canclone;
23002:             checkval = 'instcode';
23003:         }
23004:         if (selfcreateRegExp.test(caller)) {
23005:             optionsElement = domForm.elements[caller];
23006:             checkval = 'other';
23007:             dispval = 'inline'
23008:         }
23009:         if (optionsElement.length) {
23010:             var currval;
23011:             for (var i=0; i<optionsElement.length; i++) {
23012:                 if (optionsElement[i].checked) {
23013:                    currval = optionsElement[i].value;
23014:                 }
23015:             }
23016:             if (currval == checkval) {
23017:                 divitem.style.display = dispval;
23018:             } else {
23019:                 divitem.style.display = 'none';
23020:             }
23021:         }
23022:     }
23023:     return;
23024: }
23025: 
23026: // ]]>
23027: </script>
23028: 
23029: END
23030: 
23031: }
23032: 
23033: sub captcha_phrases {
23034:     return &Apache::lonlocal::texthash (
23035:                  priv => 'Private key',
23036:                  pub  => 'Public key',
23037:                  original  => 'original (CAPTCHA)',
23038:                  recaptcha => 'successor (ReCAPTCHA)',
23039:                  notused   => 'unused',
23040:                  ver => 'ReCAPTCHA version (1 or 2)',
23041:     );
23042: }
23043: 
23044: sub devalidate_remote_domconfs {
23045:     my ($dom,$cachekeys) = @_;
23046:     return unless (ref($cachekeys) eq 'HASH');
23047:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
23048:     my %thismachine;
23049:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
23050:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
23051:                       'directorysrch','passwdconf','cats','proxyalias','proxysaml',
23052:                       'ipaccess','trust');
23053:     my %cache_by_lonhost;
23054:     if (exists($cachekeys->{'samllanding'})) {
23055:         if (ref($cachekeys->{'samllanding'}) eq 'HASH') {
23056:             my %landing = %{$cachekeys->{'samllanding'}};
23057:             my %domservers = &Apache::lonnet::get_servers($dom);
23058:             if (keys(%domservers)) {
23059:                 foreach my $server (keys(%domservers)) {
23060:                     my @cached;
23061:                     next if ($thismachine{$server});
23062:                     if ($landing{$server}) {
23063:                         push(@cached,&escape('samllanding').':'.&escape($server));
23064:                     }
23065:                     if (@cached) {
23066:                         $cache_by_lonhost{$server} = \@cached;
23067:                     }
23068:                 }
23069:             }
23070:         }
23071:     }
23072:     if (keys(%servers)) {
23073:         foreach my $server (keys(%servers)) {
23074:             next if ($thismachine{$server});
23075:             my @cached;
23076:             foreach my $name (@posscached) {
23077:                 if ($cachekeys->{$name}) {
23078:                     if (($name eq 'proxyalias') || ($name eq 'proxysaml')) {
23079:                         if (ref($cachekeys->{$name}) eq 'HASH') {
23080:                             foreach my $key (keys(%{$cachekeys->{$name}})) {
23081:                                 push(@cached,&escape($name).':'.&escape($key));
23082:                             }
23083:                         }
23084:                     } else {
23085:                         push(@cached,&escape($name).':'.&escape($dom));
23086:                     }
23087:                 }
23088:             }
23089:             if ((exists($cache_by_lonhost{$server})) &&
23090:                 (ref($cache_by_lonhost{$server}) eq 'ARRAY')) {
23091:                 push(@cached,@{$cache_by_lonhost{$server}});
23092:             }
23093:             if (@cached) {
23094:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
23095:             }
23096:         }
23097:     }
23098:     return;
23099: }
23100: 
23101: 1;

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